@@ -20,8 +20,8 @@ static constexpr double EXPAND_WHEN_THIS_FULL = 0.95;
2020static constexpr unsigned int EXPAND_WITH_FACTOR_OF = 2 ;
2121static constexpr uint8_t EMPTY_BIT = 0x80 ;
2222
23- FastHashTable::FastHashTable (std:: size_t key_size, std:: size_t value_size ) :
24- m_key_size (key_size), m_value_size(value_size )
23+ FastHashTable::FastHashTable (const View &view ) :
24+ m_view (view )
2525{
2626 init_blocks ();
2727}
@@ -45,7 +45,8 @@ FastHashTable::init_blocks()
4545bool
4646FastHashTable::lookup (uint8_t *key, uint8_t *&item, bool create_if_not_found)
4747{
48- uint64_t hash = XXH3_64bits (key, m_key_size); // The hash of the key
48+ auto key_size = m_view.key_size (key);
49+ uint64_t hash = XXH3_64bits (key, key_size); // The hash of the key
4950 uint64_t index = (hash >> 7 ) & (m_block_count - 1 ); // The starting block index
5051
5152 for (;;) {
@@ -66,7 +67,7 @@ FastHashTable::lookup(uint8_t *key, uint8_t *&item, bool create_if_not_found)
6667 item_index += one_index;
6768
6869 uint8_t *record = block.items [item_index]; // The record whose item tag matched
69- if (memcmp (record, key, m_key_size ) == 0 ) { // Does the key match as well or was it just a hash collision?
70+ if (m_view. key_size (record) == key_size && memcmp (record, key, key_size ) == 0 ) { // Does the key match as well or was it just a hash collision?
7071 item = record;
7172 return true ; // We found the item
7273 }
@@ -90,12 +91,12 @@ FastHashTable::lookup(uint8_t *key, uint8_t *&item, bool create_if_not_found)
9091 auto empty_index = __builtin_ctz (empty_match);
9192 block.tags [empty_index] = item_tag;
9293
93- uint8_t *record = m_allocator.allocate (m_key_size + m_value_size );
94+ uint8_t *record = m_allocator.allocate (key_size + m_view. value_size () );
9495 block.items [empty_index] = record;
9596 m_items.push_back (record);
9697 m_record_count++;
9798
98- memcpy (record, key, m_key_size ); // Copy the key, leave the value part uninitialized
99+ memcpy (record, key, key_size ); // Copy the key, leave the value part uninitialized
99100 item = record;
100101
101102 // If the hash table has reached a specified percentage of fullness, expand the hash table
@@ -121,7 +122,8 @@ FastHashTable::expand()
121122
122123 // Reassign all the items to the newly initialized blocks
123124 for (uint8_t *item : m_items) {
124- uint64_t hash = XXH3_64bits (item, m_key_size);
125+ auto key_size = m_view.key_size (item);
126+ uint64_t hash = XXH3_64bits (item, key_size);
125127 uint64_t index = (hash >> 7 ) & (m_block_count - 1 );
126128 uint8_t item_tag = (hash & 0xFF ) & ~EMPTY_BIT;
127129
0 commit comments