Class: Array64

Inherits:
Object show all
Defined in:
lib/rlang/lib/array/array64.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(count) ⇒ Array64

count: number of elements in Array Array elements are native types or pointers to objects Arrays are fixed size for now



19
20
21
22
23
24
25
26
27
28
# File 'lib/rlang/lib/array/array64.rb', line 19

def initialize(count)
  # Avoid allocating 0 bytes in memory
  if count == 0
    @ptr = 0
  else
    # Memory size is count * 8 bytes
    @ptr = Object.allocate(count << 3)
  end
  @count = count
end

Instance Attribute Details

#countObject (readonly)

Returns the value of attribute count.



13
14
15
# File 'lib/rlang/lib/array/array64.rb', line 13

def count
  @count
end

#ptrObject (readonly)

Returns the value of attribute ptr.



13
14
15
# File 'lib/rlang/lib/array/array64.rb', line 13

def ptr
  @ptr
end

Instance Method Details

#[](idx) ⇒ Object



34
35
36
37
38
39
# File 'lib/rlang/lib/array/array64.rb', line 34

def [](idx)
  result :I64
  raise "Index out of bound" if idx >= @count || idx < -@count
  # offset in memory for elt #idx is idx * 8
  Memory.load64(@ptr + (idx << 3))
end

#[]=(idx, value) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/rlang/lib/array/array64.rb', line 41

def []=(idx, value)
  arg value: :I64
  result :I64
  raise "Index out of bound" if idx >= @count || idx < -@count
  # offset in memory for elt #idx is idx * 8
  Memory.store64(@ptr + (idx << 3), value)
  value
end

#empty?Boolean

Returns:

  • (Boolean)


32
# File 'lib/rlang/lib/array/array64.rb', line 32

def empty?; self.size == 0; end

#freeObject



50
51
52
53
54
# File 'lib/rlang/lib/array/array64.rb', line 50

def free
  result :none
  Object.free(@ptr)
  Object.free(self)
end

#lengthObject



31
# File 'lib/rlang/lib/array/array64.rb', line 31

def length; @count; end

#sizeObject



30
# File 'lib/rlang/lib/array/array64.rb', line 30

def size; @count; end