Class: IndexedBoundedStack
- Inherits:
-
Object
- Object
- IndexedBoundedStack
- Includes:
- Enumerable
- Defined in:
- lib/lrjew/indexed_bounded_stack.rb
Instance Method Summary collapse
- #delete(data) ⇒ Object
- #each(&block) ⇒ Object
- #include?(data) ⇒ Boolean
-
#initialize(bounded_stack) ⇒ IndexedBoundedStack
constructor
A new instance of IndexedBoundedStack.
- #length ⇒ Object (also: #size)
- #push(data) ⇒ Object
Constructor Details
#initialize(bounded_stack) ⇒ IndexedBoundedStack
Returns a new instance of IndexedBoundedStack.
4 5 6 7 |
# File 'lib/lrjew/indexed_bounded_stack.rb', line 4 def initialize(bounded_stack) @bounded_stack = bounded_stack @index = {} end |
Instance Method Details
#delete(data) ⇒ Object
22 23 24 25 26 27 |
# File 'lib/lrjew/indexed_bounded_stack.rb', line 22 def delete(data) if node = @index[data] @bounded_stack.delete(node) @index.delete(data) end end |
#each(&block) ⇒ Object
35 36 37 |
# File 'lib/lrjew/indexed_bounded_stack.rb', line 35 def each(&block) @bounded_stack.each(&block) end |
#include?(data) ⇒ Boolean
9 10 11 |
# File 'lib/lrjew/indexed_bounded_stack.rb', line 9 def include?(data) @index.has_key?(data) end |
#length ⇒ Object Also known as: size
29 30 31 |
# File 'lib/lrjew/indexed_bounded_stack.rb', line 29 def length @indices.length end |
#push(data) ⇒ Object
13 14 15 16 17 18 19 20 |
# File 'lib/lrjew/indexed_bounded_stack.rb', line 13 def push(data) node, shifted = @bounded_stack.push(data) @index[data] = node if shifted @index.delete(shifted) end [node, shifted] end |