class Hamster::Deque
@see en.wikipedia.org/wiki/Deque “Deque” on Wikipedia
deque = deque.shift # => Hamster::Deque[‘b’, ‘c’]
deque.last # => ‘c’
deque.first # => ‘a’
deque = deque.push(‘a’).push(‘b’).push(‘c’) # => Hamster::Deque[‘a’, ‘b’, ‘c’]
deque = Hamster::Deque.empty # => Hamster::Deque[]
@example
unchanged.
{#unshift}) all return a new collection and leave the existing one
operations that “modify” deques ({#push}, {#pop}, {#shift}, and
Like all Hamster collections, ‘Deque` is immutable. The four basic
Hamster::Deque.empty.push(’b’).push(‘c’).unshift(‘a’)
Or you can start with an empty deque and build it up:
Hamster::Deque[1, 2, 3, 4, 5]
Hamster::Deque.new([:first, :second, :third])
To create a new ‘Deque`:
than adding and removing from the ends of a {Vector}.
last element. But adding and removing from the ends of a `Deque` is faster
any element in the collection. `Deque`s only allow access to the first and
A `Deque` differs from a {Vector} in that vectors allow indexed access to
for use as an immutable queue or stack.
front and end of the sequence in constant time. This makes `Deque` perfect
objects, which allows elements to be retrieved, added and removed at the
A `Deque` (or double-ended queue) is an ordered, sequential collection of
def [](*items)
-
(Deque)
-
def [](*items) items.empty? ? empty : new(items) end
def alloc(front, rear)
- Private: -
Returns:
-
(Deque)
-
def alloc(front, rear) result = allocate result.instance_variable_set(:@front, front) result.instance_variable_set(:@rear, rear) result.freeze end
def clear
-
(Deque)
-
def clear self.class.empty end
def empty
-
(Deque)
-
def empty @empty ||= self.new end
def empty?
-
(Boolean)
-
def empty? @front.empty? && @rear.empty? end
def eql?(other)
-
(Boolean)
-
Parameters:
-
other
(Object
) -- The collection to compare with
def eql?(other) return true if other.equal?(self) instance_of?(other.class) && to_ary.eql?(other.to_ary) end
def first
-
(Object)
-
def first return @front.head unless @front.empty? @rear.last # memoize? end
def initialize(items=[])
def initialize(items=[]) @front = Hamster::List.from_enum(items) @rear = EmptyList end
def inspect
-
(String)
-
def inspect result = "#{self.class}[" i = 0 @front.each { |obj| result << ', ' if i > 0; result << obj.inspect; i += 1 } @rear.to_a.tap { |a| a.reverse! }.each { |obj| result << ', ' if i > 0; result << obj.inspect; i += 1 } result << "]" end
def last
-
(Object)
-
def last return @rear.head unless @rear.empty? @front.last # memoize? end
def marshal_dump
- Private: -
Returns:
-
(::Array)
-
def marshal_dump to_a end
def marshal_load(array)
- Private: -
def marshal_load(array) initialize(array) end
def pop
-
(Deque)
-
def pop front, rear = @front, @rear if rear.empty? return self.class.empty if front.empty? front, rear = EmptyList, front.reverse end self.class.alloc(front, rear.tail) end
def pretty_print(pp)
- Private: -
def pretty_print(pp) pp.group(1, "#{self.class}[", "]") do pp.breakable '' pp.seplist(self.to_a) { |obj| obj.pretty_print(pp) } end end
def push(item)
-
(Deque)
-
Parameters:
-
item
(Object
) -- The item to add
def push(item) self.class.alloc(@front, @rear.cons(item)) end
def shift
-
(Deque)
-
def shift front, rear = @front, @rear if front.empty? return self.class.empty if rear.empty? front, rear = rear.reverse, EmptyList end self.class.alloc(front.tail, rear) end
def size
-
(Integer)
-
def size @front.size + @rear.size end
def to_a
-
(Array)
-
def to_a @front.to_a.concat(@rear.to_a.tap { |a| a.reverse! }) end
def to_list
-
(Hamster::List)
-
def to_list @front.append(@rear.reverse) end
def unshift(item)
-
(Deque)
-
Parameters:
-
item
(Object
) -- The item to add
def unshift(item) self.class.alloc(@front.cons(item), @rear) end