Class: HashTable::StringTraits2

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStringTraits2

Returns a new instance of StringTraits2.



48
49
50
51
52
53
# File 'lib/hashtable/traits.rb', line 48

def initialize
  @string_table = "\0"
  @string_index = 1
  @buckets = {}
  @hash_h = {}
end

Instance Attribute Details

#bucketsObject (readonly)

Returns the value of attribute buckets.



46
47
48
# File 'lib/hashtable/traits.rb', line 46

def buckets
  @buckets
end

#hash_hObject (readonly)

Returns the value of attribute hash_h.



46
47
48
# File 'lib/hashtable/traits.rb', line 46

def hash_h
  @hash_h
end

#string_tableObject (readonly)

Returns the value of attribute string_table.



46
47
48
# File 'lib/hashtable/traits.rb', line 46

def string_table
  @string_table
end

Instance Method Details

#hash_lookup_key(key) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/hashtable/traits.rb', line 55

def hash_lookup_key(key)
  return @hash_h[key] unless @hash_h[key].nil?

  result = 0
  key.each_byte { |byte| result += byte * 13 }
  @hash_h[key] = result
  result
end

#lookup_key_and_value(key, value) ⇒ Object



74
75
76
77
78
# File 'lib/hashtable/traits.rb', line 74

def lookup_key_and_value(key, value)
  value.inject([@buckets[key]]) do |sum, v|
    sum << lookup_key_to_storage_key(v)
  end
end

#lookup_key_to_storage_key(key) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/hashtable/traits.rb', line 64

def lookup_key_to_storage_key(key)
  return @buckets[key] unless @buckets[key].nil?

  @string_table += "#{key}\0"
  old_si = @string_index
  @string_index += key.length + 1
  @buckets[key] = old_si
  old_si
end

#storage_key_to_lookup_key(offset) ⇒ Object



80
81
82
# File 'lib/hashtable/traits.rb', line 80

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