Class: HashSet
- Inherits:
-
Object
- Object
- HashSet
- Defined in:
- lib/simms_structures/hash_set.rb
Instance Attribute Summary collapse
-
#count ⇒ Object
readonly
Returns the value of attribute count.
Instance Method Summary collapse
- #include?(key) ⇒ Boolean
-
#initialize(num_buckets = 8) ⇒ HashSet
constructor
A new instance of HashSet.
- #insert(key) ⇒ Object
- #remove(key) ⇒ Object
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
#count ⇒ Object (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
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 |