bool TRI_ReserveAssociativePointer (TRI_associative_pointer_t* array, int32_t nrElements) { uint32_t targetSize = array->_nrUsed + nrElements; if (array->_nrAlloc < 2 * targetSize) { // we must resize return ResizeAssociativePointer(array, (uint32_t) (2 * targetSize) + 1); } // no seed to resize return true; }
void* TRI_InsertKeyAssociativePointer (TRI_associative_pointer_t* array, void const* key, void* element, bool overwrite) { uint64_t hash; uint64_t i; void* old; // check for out-of-memory if (array->_nrAlloc == array->_nrUsed) { TRI_set_errno(TRI_ERROR_OUT_OF_MEMORY); return NULL; } // compute the hash hash = array->hashKey(array, key); i = hash % array->_nrAlloc; #ifdef TRI_INTERNAL_STATS // update statistics array->_nrAdds++; #endif // search the table while (array->_table[i] != NULL && ! array->isEqualKeyElement(array, key, array->_table[i])) { i = TRI_IncModU64(i, array->_nrAlloc); #ifdef TRI_INTERNAL_STATS array->_nrProbesA++; #endif } old = array->_table[i]; // if we found an element, return if (old != NULL) { if (overwrite) { array->_table[i] = element; } return old; } // add a new element to the associative array array->_table[i] = element; array->_nrUsed++; // if we were adding and the table is more than half full, extend it if (array->_nrAlloc < 2 * array->_nrUsed) { ResizeAssociativePointer(array, (uint32_t) (2 * array->_nrAlloc) + 1); } return NULL; }
void* TRI_InsertKeyAssociativePointer (TRI_associative_pointer_t* array, void const* key, void* element, bool overwrite) { uint64_t hash; uint64_t i; void* old; // compute the hash hash = array->hashKey(array, key); i = hash % array->_nrAlloc; // update statistics array->_nrAdds++; // search the table while (array->_table[i] != NULL && ! array->isEqualKeyElement(array, key, array->_table[i])) { i = (i + 1) % array->_nrAlloc; array->_nrProbesA++; } old = array->_table[i]; // if we found an element, return if (old != NULL) { if (overwrite) { array->_table[i] = element; } return old; } // add a new element to the associative array array->_table[i] = element; array->_nrUsed++; // if we were adding and the table is more than half full, extend it if (array->_nrAlloc < 2 * array->_nrUsed) { ResizeAssociativePointer(array); } return NULL; }
int TRI_InsertKeyAssociativePointer2 (TRI_associative_pointer_t* array, void const* key, void* element, void const** found) { uint64_t hash; uint64_t i; void* old; if (found != NULL) { *found = NULL; } // check for out-of-memory if (array->_nrAlloc == array->_nrUsed) { return TRI_ERROR_OUT_OF_MEMORY; } // compute the hash hash = array->hashKey(array, key); i = hash % array->_nrAlloc; #ifdef TRI_INTERNAL_STATS // update statistics array->_nrAdds++; #endif // search the table while (array->_table[i] != NULL && ! array->isEqualKeyElement(array, key, array->_table[i])) { i = TRI_IncModU64(i, array->_nrAlloc); #ifdef TRI_INTERNAL_STATS array->_nrProbesA++; #endif } old = array->_table[i]; // if we found an element, return if (old != NULL) { if (found != NULL) { *found = old; } return TRI_ERROR_NO_ERROR; } // if we were adding and the table is more than half full, extend it if (array->_nrAlloc < 2 * array->_nrUsed) { if (! ResizeAssociativePointer(array, (uint32_t) (2 * array->_nrAlloc) + 1)) { return TRI_ERROR_OUT_OF_MEMORY; } // now we need to recalc the position i = hash % array->_nrAlloc; // search the table while (array->_table[i] != NULL && ! array->isEqualKeyElement(array, key, array->_table[i])) { i = TRI_IncModU64(i, array->_nrAlloc); #ifdef TRI_INTERNAL_STATS array->_nrProbesA++; #endif } } // add a new element to the associative array array->_table[i] = element; array->_nrUsed++; return TRI_ERROR_NO_ERROR; }