Class: HashTable::StringIdentityHashTraits

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

Overview

StringIdentityHashTraits

Direct Known Subclasses

StringHashTraits

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ StringIdentityHashTraits

Returns a new instance of StringIdentityHashTraits.



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

def initialize(&block)
  super
  @string_table = "\0"
  @buckets = {}
  @indexs = {}
end

Instance Attribute Details

#bucketsObject (readonly)

Returns the value of attribute buckets.



15
16
17
# File 'lib/hashtable/traits.rb', line 15

def buckets
  @buckets
end

#string_tableObject (readonly)

Returns the value of attribute string_table.



15
16
17
# File 'lib/hashtable/traits.rb', line 15

def string_table
  @string_table
end

Instance Method Details

#hash_lookup_key(key) ⇒ Object



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

def hash_lookup_key(key)
  key.downcase.bytes.inject(:+) * 13
end

#lookup_key_to_storage_key(key) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/hashtable/traits.rb', line 28

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

  old_si = @string_table.length
  @buckets[key] = old_si
  @string_table << "#{key}\0".b
  @indexs[old_si] = key
  old_si
end

#storage_key_to_lookup_key(offset) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/hashtable/traits.rb', line 38

def storage_key_to_lookup_key(offset)
  return @indexs[offset] unless @indexs[offset].nil?

  key = @string_table[offset..][/[^\0]+/]
  @indexs[offset] = key
  key
end