class Dasht::List

Attributes

offset[RW]
values[RW]

Public Class Methods

new() click to toggle source
# File lib/dasht/list.rb, line 20
def initialize
  @offset = 0
  @values = []
end

Public Instance Methods

append(data) click to toggle source

Public: Add data to the list. Returns a pointer to the new data.

# 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) click to toggle source

Public: Return an enumerator that walks through the list, yielding data.

# 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) click to toggle source

Public: Get the value at a given pointer, or nil if the pointer is no longer valid.

# File lib/dasht/list.rb, line 41
def get(pointer)
  index = _pointer_to_index(pointer)
  return @values[index]
end
head_pointer() click to toggle source

Public: Get a pointer to the first value.

# File lib/dasht/list.rb, line 30
def head_pointer
  return offset
end
tail_pointer() click to toggle source

Public: Get a pointer to right after the last value.

# File lib/dasht/list.rb, line 35
def tail_pointer
  return offset + @values.length
end
to_s() click to toggle source
# File lib/dasht/list.rb, line 25
def to_s
  return @values.to_s
end
trim_to(pointer) click to toggle source

Public: Remove data up to (but not including) the specified pointer.

# 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) click to toggle source

Public: Walk through the list, removing links from the list while the block returns true. Stop when it returns false.

# 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

Private Instance Methods

_pointer_to_index(pointer) click to toggle source

Convert a pointer to an index in the list.

# File lib/dasht/list.rb, line 89
def _pointer_to_index(pointer)
  return [pointer - offset, 0].max
end