Class: DS::OrderedSet
- Inherits:
-
Object
- Object
- DS::OrderedSet
- Defined in:
- lib/ds/sets/ordered_set.rb
Instance Attribute Summary collapse
-
#size ⇒ Object
readonly
Returns the value of attribute size.
Instance Method Summary collapse
-
#index(e) ⇒ Object
Returns element index.
-
#initialize ⇒ OrderedSet
constructor
Creates new Ordered Set.
-
#push(e) ⇒ Object
Adds new element to set.
- #to_a ⇒ Object
Constructor Details
#initialize ⇒ OrderedSet
Creates new Ordered Set.
6 7 8 9 |
# File 'lib/ds/sets/ordered_set.rb', line 6 def initialize @size = 0 @store = {} end |
Instance Attribute Details
#size ⇒ Object (readonly)
Returns the value of attribute size.
3 4 5 |
# File 'lib/ds/sets/ordered_set.rb', line 3 def size @size end |
Instance Method Details
#index(e) ⇒ Object
Returns element index.
22 23 24 |
# File 'lib/ds/sets/ordered_set.rb', line 22 def index e @store[e] end |
#push(e) ⇒ Object
Adds new element to set
12 13 14 15 16 17 18 19 |
# File 'lib/ds/sets/ordered_set.rb', line 12 def push e if index = @store[e] index else @size = @size+1 @store[e] = @size-1 end end |
#to_a ⇒ Object
26 27 28 |
# File 'lib/ds/sets/ordered_set.rb', line 26 def to_a @store.sort{|a,b| a[1]<=>b[1]}.map{|e| e.first} end |