32a7f051b082da240f3bc95d5419687a2d1fff93 kent Thu Jun 1 21:51:52 2017 -0700 Adding hashReverseAllBucketLists, refactored out of hashExpand(). diff --git src/lib/hash.c src/lib/hash.c index 774cd92..7653201 100644 --- src/lib/hash.c +++ src/lib/hash.c @@ -360,30 +360,43 @@ /* Make size of memory block for allocator vary between * 256 bytes and 64k depending on size of table. */ if (powerOfTwoSize < 8) memBlockPower = 8; else if (powerOfTwoSize < 16) memBlockPower = powerOfTwoSize; if (useLocalMem) hash->lm = lmInit(1<mask = hash->size-1; AllocArray(hash->table, hash->size); hash->autoExpand = TRUE; hash->expansionFactor = defaultExpansionFactor; /* Expand when elCount > size*expansionFactor */ return hash; } +void hashReverseAllBucketLists(struct hash *hash) +/* Reverse all hash bucket list. You might do this to + * get them back in the same order things were added to the hash */ +{ +int i; +for (i=0; isize; ++i) + { + struct hashEl *hel = hash->table[i]; + if (hel != NULL && hel->next != NULL) + slReverse(&hash->table[i]); + } +} + void hashResize(struct hash *hash, int powerOfTwoSize) /* Resize the hash to a new size */ { int oldHashSize = hash->size; struct hashEl **oldTable = hash->table; if (powerOfTwoSize == 0) powerOfTwoSize = 12; if (powerOfTwoSize > hashMaxSize) powerOfTwoSize = hashMaxSize; if (hash->powerOfTwoSize == powerOfTwoSize) return; assert(powerOfTwoSize <= hashMaxSize && powerOfTwoSize > 0); hash->powerOfTwoSize = powerOfTwoSize; @@ -393,36 +406,32 @@ AllocArray(hash->table, hash->size); int i; struct hashEl *hel, *next; for (i=0; inext; int hashVal = hel->hashVal & hash->mask; hel->next = hash->table[hashVal]; hash->table[hashVal] = hel; } } /* restore original list order */ -for (i=0; isize; ++i) - { - struct hashEl *hel = hash->table[i]; - if (hel != NULL && hel->next != NULL) - slReverse(&hash->table[i]); - } +hashReverseAllBucketLists(hash); + freeMem(oldTable); hash->numResizes++; } struct hash *hashFromSlNameList(void *list) /* Create a hash out of a list of slNames. */ { struct hash *hash = NULL; struct slName *namedList = list, *item; if (!list) return NULL; hash = newHash(0); for (item = namedList; item != NULL; item = item->next) hashAdd(hash, item->name, item);