Class: DS::OrderedSet

Inherits:
Object
  • Object
show all
Defined in:
lib/ds/sets/ordered_set.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeOrderedSet

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

#sizeObject (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_aObject



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