Class: Array32
Instance Attribute Summary collapse
-
#count ⇒ Object
readonly
Returns the value of attribute count.
-
#ptr ⇒ Object
readonly
Returns the value of attribute ptr.
Instance Method Summary collapse
- #[](idx) ⇒ Object
- #[]=(idx, value) ⇒ Object
- #empty? ⇒ Boolean
- #free ⇒ Object
-
#initialize(count) ⇒ Array32
constructor
count: number of elements in Array Array elements are native types or pointers to objects Arrays are fixed size for now.
- #length ⇒ Object
- #size ⇒ Object
Constructor Details
#initialize(count) ⇒ Array32
count: number of elements in Array Array elements are native types or pointers to objects Arrays are fixed size for now
21 22 23 24 25 26 27 28 29 |
# File 'lib/rlang/lib/array/array32.rb', line 21 def initialize(count) if count == 0 @ptr = 0 else # Memory size is count * 4 bytes @ptr = Object.allocate(count << 2) end @count = count end |
Instance Attribute Details
#count ⇒ Object (readonly)
Returns the value of attribute count.
13 14 15 |
# File 'lib/rlang/lib/array/array32.rb', line 13 def count @count end |
#ptr ⇒ Object (readonly)
Returns the value of attribute ptr.
13 14 15 |
# File 'lib/rlang/lib/array/array32.rb', line 13 def ptr @ptr end |
Instance Method Details
#[](idx) ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/rlang/lib/array/array32.rb', line 35 def [](idx) result :I32 raise "Index out of bound" if idx >= @count || idx < -@count idx = @count + idx if idx <0 # offset in memory for elt #idx is idx * 4 Memory.load32(@ptr + (idx << 2)) end |
#[]=(idx, value) ⇒ Object
43 44 45 46 47 48 49 50 51 |
# File 'lib/rlang/lib/array/array32.rb', line 43 def []=(idx, value) arg value: :I32 result :I32 raise "Index out of bound" if idx >= @count || idx < -@count idx = @count + idx if idx <0 # offset in memory for elt #idx is idx * 4 Memory.store32(@ptr + (idx << 2), value) value end |
#empty? ⇒ Boolean
33 |
# File 'lib/rlang/lib/array/array32.rb', line 33 def empty?; @count == 0; end |
#free ⇒ Object
53 54 55 56 57 |
# File 'lib/rlang/lib/array/array32.rb', line 53 def free result :none Object.free(@ptr) Object.free(self) end |
#length ⇒ Object
32 |
# File 'lib/rlang/lib/array/array32.rb', line 32 def length; @count; end |
#size ⇒ Object
31 |
# File 'lib/rlang/lib/array/array32.rb', line 31 def size; @count; end |