Class: HashTable::StringTraits

Inherits:
Object
  • Object
show all
Defined in:
lib/hashtable/traits.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStringTraits

Returns a new instance of StringTraits.



22
23
24
25
# File 'lib/hashtable/traits.rb', line 22

def initialize
  @string_table = "\0"
  @string_index = 1
end

Instance Attribute Details

#string_indexObject (readonly)

Returns the value of attribute string_index.



20
21
22
# File 'lib/hashtable/traits.rb', line 20

def string_index
  @string_index
end

#string_tableObject (readonly)

Returns the value of attribute string_table.



20
21
22
# File 'lib/hashtable/traits.rb', line 20

def string_table
  @string_table
end

Instance Method Details

#hash_lookup_key(key) ⇒ Object



27
28
29
30
31
# File 'lib/hashtable/traits.rb', line 27

def hash_lookup_key(key)
  result = 0
  key.each_byte { |byte| result += byte * 13 }
  result
end

#lookup_key_to_storage_key(key) ⇒ Object



33
34
35
36
37
38
# File 'lib/hashtable/traits.rb', line 33

def lookup_key_to_storage_key(key)
  @string_table += "#{key}\0"
  old_si = @string_index
  @string_index += key.length + 1
  old_si
end

#storage_key_to_lookup_key(offset) ⇒ Object



40
41
42
# File 'lib/hashtable/traits.rb', line 40

def storage_key_to_lookup_key(offset)
  @string_table[offset..][/[^\0]+/]
end