Class: HashSet

Inherits:
Object
  • Object
show all
Defined in:
lib/simms_structures/hash_set.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(num_buckets = 8) ⇒ HashSet

Returns a new instance of HashSet.



6
7
8
9
# File 'lib/simms_structures/hash_set.rb', line 6

def initialize(num_buckets = 8)
  @store = Array.new(num_buckets) { Array.new }
  @count = 0
end

Instance Attribute Details

#countObject (readonly)

Returns the value of attribute count.



4
5
6
# File 'lib/simms_structures/hash_set.rb', line 4

def count
  @count
end

Instance Method Details

#include?(key) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/simms_structures/hash_set.rb', line 17

def include?(key)
  self[key].include?(key)
end

#insert(key) ⇒ Object



11
12
13
14
15
# File 'lib/simms_structures/hash_set.rb', line 11

def insert(key)
  resize! if @count == num_buckets
  self[key] << key unless include?(key)
  @count += 1
end

#remove(key) ⇒ Object



21
22
23
24
# File 'lib/simms_structures/hash_set.rb', line 21

def remove(key)
  self[key].delete(key)
  @count -= 1
end