static void CPLHashSetRehash(CPLHashSet* set) { int nNewAllocatedSize = anPrimes[set->nIndiceAllocatedSize]; CPLList** newTabList = (CPLList**) CPLCalloc(sizeof(CPLList*), nNewAllocatedSize); #ifdef HASH_DEBUG CPLDebug("CPLHASH", "hashSet=%p, nSize=%d, nCollisions=%d, fCollisionRate=%.02f", set, set->nSize, set->nCollisions, set->nCollisions * 100.0 / set->nSize); set->nCollisions = 0; #endif for(int i=0;i<set->nAllocatedSize;i++) { CPLList* cur = set->tabList[i]; while(cur) { unsigned long nNewHashVal = set->fnHashFunc(cur->pData) % nNewAllocatedSize; #ifdef HASH_DEBUG if (newTabList[nNewHashVal]) set->nCollisions ++; #endif newTabList[nNewHashVal] = CPLListInsert(newTabList[nNewHashVal], cur->pData, 0); cur = cur->psNext; } CPLListDestroy(set->tabList[i]); } CPLFree(set->tabList); set->tabList = newTabList; set->nAllocatedSize = nNewAllocatedSize; }
int CPLHashSetInsert(CPLHashSet* set, void* elt) { CPLAssert(set != NULL); void** pElt = CPLHashSetFindPtr(set, elt); if (pElt) { if (set->fnFreeEltFunc) set->fnFreeEltFunc(*pElt); *pElt = elt; return FALSE; } if (set->nSize >= 2 * set->nAllocatedSize / 3) { set->nIndiceAllocatedSize++; CPLHashSetRehash(set); } unsigned long nHashVal = set->fnHashFunc(elt) % set->nAllocatedSize; #ifdef HASH_DEBUG if (set->tabList[nHashVal]) set->nCollisions ++; #endif set->tabList[nHashVal] = CPLListInsert(set->tabList[nHashVal], (void*) elt, 0); set->nSize++; return TRUE; }
void object::test<1>() { CPLList* list; list = CPLListInsert(NULL, (void*)0, 0); ensure(CPLListCount(list) == 1); list = CPLListRemove(list, 2); ensure(CPLListCount(list) == 1); list = CPLListRemove(list, 1); ensure(CPLListCount(list) == 1); list = CPLListRemove(list, 0); ensure(CPLListCount(list) == 0); list = NULL; list = CPLListInsert(NULL, (void*)0, 2); ensure(CPLListCount(list) == 3); list = CPLListRemove(list, 2); ensure(CPLListCount(list) == 2); list = CPLListRemove(list, 1); ensure(CPLListCount(list) == 1); list = CPLListRemove(list, 0); ensure(CPLListCount(list) == 0); list = NULL; list = CPLListAppend(list, (void*)1); ensure(CPLListGet(list,0) == list); ensure(CPLListGet(list,1) == NULL); list = CPLListAppend(list, (void*)2); list = CPLListInsert(list, (void*)3, 2); ensure(CPLListCount(list) == 3); CPLListDestroy(list); list = NULL; list = CPLListAppend(list, (void*)1); list = CPLListAppend(list, (void*)2); list = CPLListInsert(list, (void*)4, 3); CPLListGet(list,2)->pData = (void*)3; ensure(CPLListCount(list) == 4); ensure(CPLListGet(list,0)->pData == (void*)1); ensure(CPLListGet(list,1)->pData == (void*)2); ensure(CPLListGet(list,2)->pData == (void*)3); ensure(CPLListGet(list,3)->pData == (void*)4); CPLListDestroy(list); list = NULL; list = CPLListInsert(list, (void*)4, 1); CPLListGet(list,0)->pData = (void*)2; list = CPLListInsert(list, (void*)1, 0); list = CPLListInsert(list, (void*)3, 2); ensure(CPLListCount(list) == 4); ensure(CPLListGet(list,0)->pData == (void*)1); ensure(CPLListGet(list,1)->pData == (void*)2); ensure(CPLListGet(list,2)->pData == (void*)3); ensure(CPLListGet(list,3)->pData == (void*)4); list = CPLListRemove(list, 1); list = CPLListRemove(list, 1); list = CPLListRemove(list, 0); list = CPLListRemove(list, 0); ensure(list == NULL); }