Sha256: 2a50f487eb20a3eba4f294444c8bfab2aae3c86a3fad592531ef06ce54a61452
Contents?: true
Size: 1.02 KB
Versions: 2
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
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
maroon-0.7.1 | base/immutable_queue.rb |
maroon-0.7.0 | base/immutable_queue.rb |