class HashSet
Attributes
count[R]
Public Class Methods
new(num_buckets = 8)
click to toggle source
# File lib/simms_structures/hash_set.rb, line 6 def initialize(num_buckets = 8) @store = Array.new(num_buckets) { Array.new } @count = 0 end
Public Instance Methods
include?(key)
click to toggle source
# File lib/simms_structures/hash_set.rb, line 17 def include?(key) self[key].include?(key) end
insert(key)
click to toggle source
# 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)
click to toggle source
# File lib/simms_structures/hash_set.rb, line 21 def remove(key) self[key].delete(key) @count -= 1 end
Private Instance Methods
[](num)
click to toggle source
# File lib/simms_structures/hash_set.rb, line 28 def [](num) @store[num.hash % num_buckets] end
num_buckets()
click to toggle source
# File lib/simms_structures/hash_set.rb, line 32 def num_buckets @store.length end
resize!()
click to toggle source
# File lib/simms_structures/hash_set.rb, line 36 def resize! old_store = @store new_num_buckets = num_buckets * 2 @store = Array.new(new_num_buckets) { Array.new } @count = 0 old_store.each do |bucket| bucket.each { |num| insert(num) } end end