Class: HashTable::StringIdentityHashTraits
- Inherits:
-
IdentityHashTraits
- Object
- IdentityHashTraits
- HashTable::StringIdentityHashTraits
- Defined in:
- lib/hashtable/traits.rb
Overview
StringIdentityHashTraits
Direct Known Subclasses
Instance Attribute Summary collapse
-
#buckets ⇒ Object
readonly
Returns the value of attribute buckets.
-
#string_table ⇒ Object
readonly
Returns the value of attribute string_table.
Instance Method Summary collapse
- #hash_lookup_key(key) ⇒ Object
-
#initialize(&block) ⇒ StringIdentityHashTraits
constructor
A new instance of StringIdentityHashTraits.
- #lookup_key_to_storage_key(key) ⇒ Object
- #storage_key_to_lookup_key(offset) ⇒ Object
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
#buckets ⇒ Object (readonly)
Returns the value of attribute buckets.
15 16 17 |
# File 'lib/hashtable/traits.rb', line 15 def buckets @buckets end |
#string_table ⇒ Object (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 |