struct Slice(T)
Overview
A Slice is a Pointer
with an associated size.
While a pointer is unsafe because no bound checks are performed when reading from and writing to it, reading from and writing to a slice involve bound checks. In this way, a slice is a safe alternative to Pointer.
Included Modules
Defined in:
slice.crClass Method Summary
-
.new(size : Int, &block)
Allocates
size * sizeof(T)
bytes of heap memory initialized to the value returned by the block (which is invoked once with each index in the range0...size
) and returns a slice pointing to that memory. -
.new(size : Int)
Allocates
size * sizeof(T)
bytes of heap memory initialized to zero and returns a slice pointing to that memory. -
.new(size : Int, value : T)
Allocates
size * sizeof(T)
bytes of heap memory initialized to value and returns a slice pointing to that memory. -
.new(pointer : Pointer(T), size : Int)
Creates a slice to the given pointer, bounded by the given size.
Instance Method Summary
-
#+(offset : Int)
Returns a new slice that i offset elements apart from this slice.
- #==(other : self)
-
#[](index : Int)
Returns the element at the given index.
-
#[](start, count)
Returns a new slice that starts at start elements from this slice's start, and of count size.
-
#[]=(index : Int, value : T)
Sets the given value at the given index.
- #at(index : Int, &block)
- #at(index : Int)
- #bytesize
- #copy_from(source : Pointer(T), count)
- #copy_to(target : Pointer(T), count)
-
#each(&block)
Pass each element of slice to block.
- #each
- #empty?
- #hexdump
- #hexstring
- #inspect(io)
- #pointer(size)
-
#reverse_each(&block)
Same as
#each
, but works in reverse. - #reverse_each
- #rindex(&block)
- #rindex(value)
-
#size
Returns the size of this slice.
- #to_a
- #to_s(io)
- #to_slice
-
#to_unsafe : Pointer(T)
Returns this slice's pointer.
Macro Summary
Instance methods inherited from module Iterable
cyclecycle(n) cycle, each each, each_cons(count : Int) each_cons, each_slice(count : Int) each_slice, each_with_index(offset = 0) each_with_index, each_with_object(obj) each_with_object
Instance methods inherited from module Enumerable(T)
all?all?(&block) all?, any?
any?(&block) any?, compact_map(&block) compact_map, count(item)
count(&block) count, cycle(&block)
cycle(n, &block) cycle, each(&block : T -> _) each, each_cons(count : Int, &block) each_cons, each_slice(count : Int, &block) each_slice, each_with_index(offset = 0, &block) each_with_index, each_with_object(obj, &block) each_with_object, find(if_none = nil, &block) find, first
first(count : Int) first, first? first?, flat_map(&block : T -> Array(U)) flat_map, grep(pattern) grep, group_by(&block : T -> U) group_by, in_groups_of(size : Int, filled_up_with : U = nil, &block)
in_groups_of(size : Int, filled_up_with : U = nil) in_groups_of, includes?(obj) includes?, index(obj)
index(&block) index, index_by(&block : T -> U) index_by, join(separator = "")
join(separator, io, &block)
join(separator = "", &block)
join(separator, io) join, map(&block : T -> U) map, map_with_index(&block : T, Int32 -> U) map_with_index, max max, max? max?, max_by(&block : T -> U) max_by, max_by?(&block : T -> U) max_by?, max_of(&block : T -> U) max_of, max_of?(&block : T -> U) max_of?, min min, min? min?, min_by(&block : T -> U) min_by, min_by?(&block : T -> U) min_by?, min_of(&block : T -> U) min_of, min_of?(&block : T -> U) min_of?, minmax minmax, minmax? minmax?, minmax_by(&block : T -> U) minmax_by, minmax_by?(&block : T -> U) minmax_by?, minmax_of(&block : T -> U) minmax_of, minmax_of?(&block : T -> U) minmax_of?, none?(&block)
none? none?, one?(&block) one?, partition(&block) partition, product(initial : Number, &block)
product
product(initial : Number)
product(&block) product, reduce(memo, &block)
reduce(&block) reduce, reject(&block : T -> ) reject, select(&block : T -> ) select, size size, skip(count : Int) skip, skip_while(&block) skip_while, sum
sum(initial)
sum(&block)
sum(initial, &block) sum, take_while(&block) take_while, to_a to_a, to_h to_h, to_set to_set
Instance methods inherited from struct Struct
==(other : self) : Bool
==,
hash : Int32
hash,
inspect(io : IO) : Nil
inspect,
to_s(io)
to_s
Instance methods inherited from struct Value
==(other)
==
Instance methods inherited from class Object
!=(other)
!=,
!~(other)
!~,
==(other)
==,
===(other)===(other : YAML::Any)
===(other : JSON::Any) ===, =~(other) =~, class class, clone clone, crystal_type_id crystal_type_id, dup dup, hash hash, inspect
inspect(io : IO) inspect, itself itself, not_nil! not_nil!, tap(&block) tap, to_json to_json, to_pretty_json(io : IO)
to_pretty_json to_pretty_json, to_s
to_s(io : IO) to_s, to_yaml(io : IO)
to_yaml to_yaml, try(&block) try
Class methods inherited from class Object
==(other : Class)
==,
===(other)
===,
cast(other) : self
cast,
from_json(string_or_io) : self
from_json,
from_yaml(string : String) : self
from_yaml,
hash
hash,
inspect(io)
inspect,
name : String
name,
to_s(io)
to_s,
|(other : U.class)
|
Class Method Detail
Allocates size * sizeof(T)
bytes of heap memory initialized to the value
returned by the block (which is invoked once with each index in the range 0...size
)
and returns a slice pointing to that memory.
The memory is allocated by the GC
, so when there are
no pointers to this memory, it will be automatically freed.
slice = Slice.new(3) { |i| i + 10 }
slice # => [10, 11, 12]
Allocates size * sizeof(T)
bytes of heap memory initialized to zero
and returns a slice pointing to that memory.
The memory is allocated by the GC
, so when there are
no pointers to this memory, it will be automatically freed.
slice = Slice(UInt8).new(3)
slice # => [0, 0, 0]
Allocates size * sizeof(T)
bytes of heap memory initialized to value
and returns a slice pointing to that memory.
The memory is allocated by the GC
, so when there are
no pointers to this memory, it will be automatically freed.
slice = Slice.new(3, 10)
slice # => [10, 10, 10]
Creates a slice to the given pointer, bounded by the given size. This method does not allocate heap memory.
ptr = Pointer.malloc(9) { |i| ('a'.ord + i).to_u8 }
slice = Slice.new(ptr, 3)
slice.size # => 3
slice # => [97, 98, 99]
String.new(slice) # => "abc"
Instance Method Detail
Returns a new slice that i offset elements apart from this slice.
slice = Slice.new(5) { |i| i + 10 }
slice # => [10, 11, 12, 13, 14]
slice2 = slice + 2
slice2 # => [12, 13, 14]
Returns the element at the given index.
Negative indices can be used to start counting from the end of the slice.
Raises IndexError
if trying to access an element outside the slice's range.
slice = Slice.new(5) { |i| i + 10 }
slice[0] # => 10
slice[4] # => 14
slice[-1] # => 14
slice[5] # => IndexError
Returns a new slice that starts at start elements from this slice's start, and of count size.
Raises IndexError
if the new slice falls outside this slice.
slice = Slice.new(5) { |i| i + 10 }
slice # => [10, 11, 12, 13, 14]
slice2 = slice[1, 3]
slice2 # => [11, 12, 13]
Sets the given value at the given index.
Negative indices can be used to start counting from the end of the slice.
Raises IndexError
if trying to set an element outside the slice's range.
slice = Slice.new(5) { |i| i + 10 }
slice[0] = 20
slice[-1] = 30
slice # => [20, 11, 12, 13, 30]
slice[4] = 1 # => IndexError
Returns a hexdump of this slice, assuming it's a Slice(UInt8)
.
This method is specially useful for debugging binary data and
incoming/outgoing data in protocols.
slice = UInt8.slice(97, 62, 63, 8, 255)
slice.hexdump # => "6162 6308 ff abc.."
Returns a hexstring representation of this slice, assuming it's
a Slice(UInt8)
.
slice = UInt8.slice(97, 62, 63, 8, 255)
slice.hexstring # => "61626308ff"
Returns this slice's pointer.
slice = Slice.new(3, 10)
slice.to_unsafe[0] # => 10
Macro Detail
Create a new Slice
with the given args. The type of the
slice will be the union of the type of the given args.
The slice is allocated on the heap.
slice = Slice[1, 'a']
slice[0] # => 1
slice[1] # => 'a'
slice.class # => Slice(Char | Int32)
See also: Number.slice
.