/* ============================================================================= * hashtable_alloc * -- Returns NULL on failure * -- Negative values for resizeRatio or growthFactor select default values * ============================================================================= */ hashtable_t* hashtable_alloc (long initNumBucket, ulong_t (*hash)(const void*), long (*comparePairs)(const pair_t*, const pair_t*), long resizeRatio, long growthFactor) { hashtable_t* hashtablePtr; hashtablePtr = (hashtable_t*)malloc(sizeof(hashtable_t)); if (hashtablePtr == NULL) { return NULL; } hashtablePtr->buckets = allocBuckets(initNumBucket, comparePairs); if (hashtablePtr->buckets == NULL) { free(hashtablePtr); return NULL; } hashtablePtr->numBucket = initNumBucket; #ifdef HASHTABLE_SIZE_FIELD hashtablePtr->size = 0; #endif hashtablePtr->hash = hash; hashtablePtr->comparePairs = comparePairs; hashtablePtr->resizeRatio = ((resizeRatio < 0) ? HASHTABLE_DEFAULT_RESIZE_RATIO : resizeRatio); hashtablePtr->growthFactor = ((growthFactor < 0) ? HASHTABLE_DEFAULT_GROWTH_FACTOR : growthFactor); return hashtablePtr; }
hashtable_t* hashtable_alloc (long initNumBucket, ulong_t (*hash)(const void*), comparator_t* comparePairs, long resizeRatio, long growthFactor) { if(comparePairs==NULL){ comparePairs = &default_hashtable_comparator; } hashtable_t* hashtablePtr; resizeRatio = -1; growthFactor = -1; hashtablePtr = (hashtable_t*)SEQ_MALLOC(sizeof(hashtable_t)); if (hashtablePtr == NULL) { return NULL; } if(hash==NULL){ hash = &func_hash; } hashtablePtr->buckets = allocBuckets(initNumBucket, comparePairs); if (hashtablePtr->buckets == NULL) { SEQ_FREE(hashtablePtr); return NULL; } hashtablePtr->numBucket = initNumBucket; #ifdef HASHTABLE_SIZE_FIELD hashtablePtr->size = 0; #endif hashtablePtr->hash = hash; hashtablePtr->comparePairs = comparePairs; hashtablePtr->resizeRatio = ((resizeRatio < 0) ? HASHTABLE_DEFAULT_RESIZE_RATIO : resizeRatio); hashtablePtr->growthFactor = ((growthFactor < 0) ? HASHTABLE_DEFAULT_GROWTH_FACTOR : growthFactor); return hashtablePtr; }