Class: DS::IndexedSet

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

Overview

IndexedSet Class

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeIndexedSet

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

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

Returns:

  • (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_aObject



29
30
31
# File 'lib/ds/sets/indexed_set.rb', line 29

def to_a
  @cache
end