class DataStructures101::Hash::Bucket
Utility class to manipulate (key, pairs) that have same hash code. @see ChainedHashTable
@author Rene Hernandez @since 0.2
Attributes
table[R]
Public Class Methods
new()
click to toggle source
# File lib/data_structures_101/hash/bucket.rb, line 12 def initialize @table = [] end
Public Instance Methods
[](key)
click to toggle source
# File lib/data_structures_101/hash/bucket.rb, line 16 def [](key) find(key) end
[]=(key, value)
click to toggle source
# File lib/data_structures_101/hash/bucket.rb, line 20 def []=(key, value) insert(key, value) end
delete(key)
click to toggle source
# File lib/data_structures_101/hash/bucket.rb, line 45 def delete(key) idx = @table.find_index { |table_key, _| table_key == key } return nil if idx.nil? value = @table[idx].last @table[idx] = @table.last if idx != @table.size - 1 @table.pop value end
each() { |key, value| ... }
click to toggle source
# File lib/data_structures_101/hash/bucket.rb, line 55 def each return enum_for(:each) unless block_given? @table.each do |key, value| yield(key, value) end end
find(key)
click to toggle source
# File lib/data_structures_101/hash/bucket.rb, line 40 def find(key) pair = @table.find { |table_key, _| table_key == key } pair.nil? ? nil : pair.last end
insert(key, value)
click to toggle source
# File lib/data_structures_101/hash/bucket.rb, line 24 def insert(key, value) idx = @table.find_index { |table_key, _| table_key == key } if idx.nil? @table << [key, value] return nil else value, @table[idx][1] = @table[idx][1], value return value end end
size()
click to toggle source
# File lib/data_structures_101/hash/bucket.rb, line 36 def size @table.size end