Sha256: 186fcd6033c650000e1147bd65ec6e41c1555ebce2ff06fceb1431d57fb497a6
Contents?: true
Size: 1.02 KB
Versions: 1
Compression:
Stored size: 1.02 KB
Contents
context :ImmutableQueue do role :front do end role :back do end def push(element) b = back || ImmutableStack::empty ImmutableQueue.new(front, b.push(element)) end def pop f, b = front, back if f == ImmutableStack::empty #reverse the back stack to be able to pop from the front in correct order until b == ImmutableStack::empty e, b = b.pop f = f.push(e) end end head, f = f.pop if f == b #can only happen if they are both ImmutableStack::empty [head, ImmutableQueue::empty] else [head, ImmutableQueue.new(f, b)] end end def self.empty @empty ||= ImmutableQueue.new(ImmutableStack::empty, ImmutableStack::empty) end def push_array(arr) q = self if arr arr.each do |i| q = q.push i end end q end private def initialize(front, back) @front = front || ImmutableStack::empty @back = back || ImmutableStack::empty self.freeze end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
maroon-0.8.0 | base/immutable_queue.rb |