Class: Klam::Absvector
- Inherits:
-
Object
- Object
- Klam::Absvector
- Includes:
- Enumerable
- Defined in:
- lib/klam/absvector.rb
Instance Attribute Summary collapse
-
#array ⇒ Object
readonly
Returns the value of attribute array.
-
#size ⇒ Object
readonly
Returns the value of attribute size.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #[](i) ⇒ Object
-
#each(&blk) ⇒ Object
In Shen, the data types that are implemented on top of absvectors use index 0 for auxilliary information.
- #hash ⇒ Object
-
#initialize(n_or_array, fill = nil) ⇒ Absvector
constructor
A new instance of Absvector.
- #store(i, x) ⇒ Object
- #to_a ⇒ Object
Constructor Details
#initialize(n_or_array, fill = nil) ⇒ Absvector
Returns a new instance of Absvector.
6 7 8 9 10 11 12 13 14 15 16 17 |
# 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 |
Instance Attribute Details
#array ⇒ Object (readonly)
Returns the value of attribute array.
4 5 6 |
# File 'lib/klam/absvector.rb', line 4 def array @array end |
#size ⇒ Object (readonly)
Returns the value of attribute size.
4 5 6 |
# File 'lib/klam/absvector.rb', line 4 def size @size end |
Instance Method Details
#==(other) ⇒ Object
45 46 47 48 |
# File 'lib/klam/absvector.rb', line 45 def ==(other) other.kind_of?(Klam::Absvector) && other.size == @size && other.array == @array end |
#[](i) ⇒ Object
19 20 21 22 23 24 |
# 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) ⇒ Object
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.
37 38 39 |
# File 'lib/klam/absvector.rb', line 37 def each(&blk) to_a.each(&blk) end |
#hash ⇒ Object
50 51 52 |
# File 'lib/klam/absvector.rb', line 50 def hash @array.hash end |
#store(i, x) ⇒ Object
26 27 28 29 30 31 32 |
# 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 ⇒ Object
41 42 43 |
# File 'lib/klam/absvector.rb', line 41 def to_a @array[1..-1] end |