MCPcopy Create free account
hub / github.com/GregoryFaust/samblaster / hashTableInsert

Function hashTableInsert

sbhash.cpp:268–332  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

266}
267
268bool hashTableInsert(hashTable_t * ht, UINT64 value)
269{
270 // See if we have reached our size limit.
271 if (ht->entries == ht->size) resizeHashTable(ht);
272 int bucket = hash(value) % ht->size;
273 // We need to empty the low order bit so that we can tell the difference between values and ptrs.
274 value = makeValue(value);
275 UINT64 curvalue = ht->table[bucket];
276 // The empty case should be most common.
277 if (isEmpty(curvalue))
278 {
279 ht->table[bucket] = value;
280 ht->entries += 1;
281 return true;
282 }
283 // The value case should be next most common.
284 if (isValue(curvalue))
285 {
286 // The value is already here.
287 if (curvalue == value) return false;
288 // We have a collision and need to add an overflow node.
289 hashNode_t * node = getHashNode();
290 ht->table[bucket] = (UINT64)node;
291 node->values[0] = curvalue;
292 // Note that this test doesn't cost us anything as it happens at compile time.
293 if (HASHNODE_PAYLOAD_SIZE >= 2)
294 {
295 node->values[1] = value;
296 }
297 else
298 {
299 // We need to add a second new node.
300 hashNode_t * secondNode = getHashNode();
301 node->next = secondNode;
302 secondNode->values[0] = value;
303 }
304 ht->entries += 1;
305 return true;
306 }
307 // The overflow node case.
308 hashNode_t * curNode = makePtr(curvalue);
309 while (true)
310 {
311 for (int i=0; i<HASHNODE_PAYLOAD_SIZE; i++)
312 {
313 // Check if we have an empty slot.
314 if (curNode->values[i] == 0)
315 {
316 curNode->values[i] = value;
317 ht->entries += 1;
318 return true;
319 }
320 // Check if the value matches the current value.
321 if (curNode->values[i] == value) return false;
322 }
323 if (curNode->next == NULL) break;
324 curNode = curNode->next;
325 }

Callers 2

resizeHashTableFunction · 0.85
markDupsDiscordantsFunction · 0.85

Calls 7

resizeHashTableFunction · 0.85
hashFunction · 0.85
makeValueFunction · 0.85
isEmptyFunction · 0.85
isValueFunction · 0.85
getHashNodeFunction · 0.85
makePtrFunction · 0.85

Tested by

no test coverage detected