Class: DS::IndexedSet
- Inherits:
-
Object
- Object
- DS::IndexedSet
- Defined in:
- lib/ds/sets/indexed_set.rb
Overview
IndexedSet Class
Instance Attribute Summary collapse
-
#size ⇒ Object
readonly
Returns the value of attribute size.
Instance Method Summary collapse
- #empty? ⇒ Boolean
-
#index(e) ⇒ Object
Returns element index.
-
#initialize ⇒ IndexedSet
constructor
Creates new Indexed Set.
-
#push(e) ⇒ Object
Adds new element to set.
- #to_a ⇒ Object
Constructor Details
#initialize ⇒ IndexedSet
Creates new Indexed Set.
7 8 9 10 11 |
# File 'lib/ds/sets/indexed_set.rb', line 7 def initialize @size = 0 @store = {} @cache = [] end |
Instance Attribute Details
#size ⇒ Object (readonly)
Returns the value of attribute size.
4 5 6 |
# File 'lib/ds/sets/indexed_set.rb', line 4 def size @size end |
Instance Method Details
#empty? ⇒ Boolean
33 34 35 |
# File 'lib/ds/sets/indexed_set.rb', line 33 def empty? @size == 0 end |
#index(e) ⇒ Object
Returns element index.
25 26 27 |
# File 'lib/ds/sets/indexed_set.rb', line 25 def index(e) @store[e] end |
#push(e) ⇒ Object
Adds new element to set
14 15 16 17 18 19 20 21 22 |
# File 'lib/ds/sets/indexed_set.rb', line 14 def push(e) unless @store[e] index = size @store[e] = index @size += 1 @cache[index] = e end @store[e] end |
#to_a ⇒ Object
29 30 31 |
# File 'lib/ds/sets/indexed_set.rb', line 29 def to_a @cache end |