class Klam::Absvector

Attributes

array[R]
size[R]

Public Class Methods

new(n_or_array, fill = nil) click to toggle source
# File lib/klam/absvector.rb, line 6
def initialize(n_or_array, fill = nil)
  if n_or_array.kind_of?(Array)
    # This is a convenience constructor for making Shen vectors from Ruby
    # Arrays. Shen vectors use 1-based indexing and store the size in
    # slot zero.
    @array = Array.new(n_or_array)
    @array.unshift(@array.size)
  else
    @array = Array.new(n_or_array, fill)
  end
    @size = @array.size
end

Public Instance Methods

==(other) click to toggle source
# File lib/klam/absvector.rb, line 45
def ==(other)
  other.kind_of?(Klam::Absvector) && other.size == @size &&
    other.array == @array
end
[](i) click to toggle source
# File lib/klam/absvector.rb, line 19
def [](i)
  if i < 0 || i >= @size
    raise Klam::Error, "index out of bounds: #{i}"
  end
  @array[i]
end
each(&blk) click to toggle source

In Shen, the data types that are implemented on top of absvectors use index 0 for auxilliary information. To ease interop scenarios, to_a and each are overridden to skip the first slot.

# File lib/klam/absvector.rb, line 37
def each(&blk)
  to_a.each(&blk)
end
hash() click to toggle source
# File lib/klam/absvector.rb, line 50
def hash
  @array.hash
end
store(i, x) click to toggle source
# File lib/klam/absvector.rb, line 26
def store(i, x)
  if i < 0 || i >= @size
    raise Klam::Error, "index out of bounds: #{i}"
  end
  @array[i] = x
  self
end
to_a() click to toggle source
# File lib/klam/absvector.rb, line 41
def to_a
  @array[1..-1]
end