Class: Dasht::List
- Inherits:
-
Object
- Object
- Dasht::List
- Defined in:
- lib/dasht/list.rb
Instance Attribute Summary collapse
-
#offset ⇒ Object
Returns the value of attribute offset.
-
#values ⇒ Object
Returns the value of attribute values.
Instance Method Summary collapse
-
#append(data) ⇒ Object
Public: Add data to the list.
-
#enum(start_pointer = nil, end_pointer = nil) ⇒ Object
Public: Return an enumerator that walks through the list, yielding data.
-
#get(pointer) ⇒ Object
Public: Get the value at a given pointer, or nil if the pointer is no longer valid.
-
#head_pointer ⇒ Object
Public: Get a pointer to the first value.
-
#initialize ⇒ List
constructor
A new instance of List.
-
#tail_pointer ⇒ Object
Public: Get a pointer to right after the last value.
- #to_s ⇒ Object
-
#trim_to(pointer) ⇒ Object
Public: Remove data up to (but not including) the specified pointer.
-
#trim_while(&block) ⇒ Object
Public: Walk through the list, removing links from the list while the block returns true.
Constructor Details
#initialize ⇒ List
Returns a new instance of List.
20 21 22 23 |
# File 'lib/dasht/list.rb', line 20 def initialize @offset = 0 @values = [] end |
Instance Attribute Details
#offset ⇒ Object
Returns the value of attribute offset.
18 19 20 |
# File 'lib/dasht/list.rb', line 18 def offset @offset end |
#values ⇒ Object
Returns the value of attribute values.
17 18 19 |
# File 'lib/dasht/list.rb', line 17 def values @values end |
Instance Method Details
#append(data) ⇒ Object
Public: Add data to the list. Returns a pointer to the new data.
61 62 63 64 65 |
# File 'lib/dasht/list.rb', line 61 def append(data) pointer = self.tail_pointer @values << data return pointer end |
#enum(start_pointer = nil, end_pointer = nil) ⇒ Object
Public: Return an enumerator that walks through the list, yielding data.
48 49 50 51 52 53 54 55 56 57 |
# File 'lib/dasht/list.rb', line 48 def enum(start_pointer = nil, end_pointer = nil) index = _pointer_to_index(start_pointer || head_pointer) end_index = _pointer_to_index(end_pointer || tail_pointer) return Enumerator.new do |yielder| while index < end_index yielder << @values[index] index += 1 end end end |
#get(pointer) ⇒ Object
Public: Get the value at a given pointer, or nil if the pointer is no longer valid.
41 42 43 44 |
# File 'lib/dasht/list.rb', line 41 def get(pointer) index = _pointer_to_index(pointer) return @values[index] end |
#head_pointer ⇒ Object
Public: Get a pointer to the first value.
30 31 32 |
# File 'lib/dasht/list.rb', line 30 def head_pointer return offset end |
#tail_pointer ⇒ Object
Public: Get a pointer to right after the last value.
35 36 37 |
# File 'lib/dasht/list.rb', line 35 def tail_pointer return offset + @values.length end |
#to_s ⇒ Object
25 26 27 |
# File 'lib/dasht/list.rb', line 25 def to_s return @values.to_s end |
#trim_to(pointer) ⇒ Object
Public: Remove data up to (but not including) the specified pointer.
68 69 70 71 72 73 74 |
# File 'lib/dasht/list.rb', line 68 def trim_to(pointer) return if pointer.nil? index = _pointer_to_index(pointer) @offset += index @values = @values.slice(index, @values.length) return end |
#trim_while(&block) ⇒ Object
Public: Walk through the list, removing links from the list while the block returns true. Stop when it returns false.
78 79 80 81 82 83 84 |
# File 'lib/dasht/list.rb', line 78 def trim_while(&block) while (@values.length > 0) && yield(@values.first) @values.shift @offset += 1 end return end |