void Assign(HashTable * h, int key, int val){ int i = key % h->m; Elem * x = ListSearch(h->l+i, key); if (x == NULL){ if(val) Insert(h->l+i, key, val); }else{ if(val == 0) Delete(h->l+i, x); else x->val = val; } }
int Lookup(struct elem ** table, int k, int m) { int i = k % m; struct elem * result = ListSearch(table[i], k); if(result == NULL) { return 0; } else { return result->v; } }
bool TestListSearch(List *list) { ListDeleteAll(list); // Already tested int i; for (i = 0; i < length; i++) ListAppend(list, numbers[i]); for (i = length - 1; i >= 0; i--) if (ListItorNull(ListSearch(list, numbers[i])) || ListValue(ListSearch(list, numbers[i])) != numbers[i]) { sprintf(error, "Cannot find existing element [%d]", i); return false; } if (!ListItorNull(ListSearch(list, TestMaxValue + 1))) { sprintf(error, "Found nonexisting element - bigger than max"); return false; } if (!ListItorNull(ListSearch(list, TestMinValue - 1))) { sprintf(error, "Found nonexisting element - smaller than min"); return false; } return true; }
list_t* ListAdd(list_t* list, void* g, compareFunc funct) { list_t* node; list_t* nodeToBeAfterNew; ASSERT(list); node = (list_t*) malloc(sizeof(list_t)); if ( !node ) { /* FIXME: No more memory -- do something? */ return NULL; } nodeToBeAfterNew = ListSearch(list, g, funct); node->data = g; node->next = nodeToBeAfterNew; nodeToBeAfterNew->prev->next = node; node->prev = nodeToBeAfterNew->prev; nodeToBeAfterNew->prev = node; return node; }
SceneObjectT *GetObject(SceneT *self, const StrT name) { return ListSearch(self->objects, (CompareFuncT)CompareName, name); }
MatrixStack3D *GetObjectTranslation(SceneT *self, const StrT name) { SceneObjectT *object = ListSearch(self->objects, (CompareFuncT)CompareName, name); return object ? object->ms : NULL; }
void main (void) { uMCONFIG M; // Configuration structure int index; // Loop variable LARGE_INTEGER C1, C2, D;// Profiling variables double seconds, Freq; // Profiler output variables int * A [9000]; // Memory to allocate int const N = sizeof A / sizeof(int); // Count of elements in array hLIST List; // Linked list FILE * fp; // Output file int Int = 4, * Item;// Integer data used to test list /* Initialize timer */ QueryPerformanceFrequency (&D); Freq = 1.0 / (double)D.QuadPart; /* Initialize memory; allocate just enough for maximum allocation */ M.PoolSize = N * (sizeof(int) + 0x14); MemInit (&M); fp = fopen ("Log.txt","wt"); fprintf (fp, "%d ints:\n", N); /* Test speed of MemAlloc */ QueryPerformanceCounter (&C1); for (index = 0; index < N; ++index) A [index] = (int *) MemAlloc (sizeof(int), 0); QueryPerformanceCounter (&C2); seconds = (double)(C2.QuadPart - C1.QuadPart) * Freq; fprintf (fp, "With MemAlloc: %f seconds, %.8f p/int\n", seconds, seconds / N); /* Test speed of MemFree */ QueryPerformanceCounter (&C1); for (index = 0; index < N; ++index) MemFree (A [index]); QueryPerformanceCounter (&C2); seconds = (double)(C2.QuadPart - C1.QuadPart) * Freq; fprintf (fp, "With MemFree: %f seconds, %.8f p/int\n", seconds, seconds / N); /* Test speed of malloc */ QueryPerformanceCounter (&C1); for (index = 0; index < N; ++index) A [index] = (int *) malloc (sizeof(int)); QueryPerformanceCounter (&C2); seconds = (double)(C2.QuadPart - C1.QuadPart) * Freq; fprintf (fp, "With malloc: %f seconds, %.8f p/int\n", seconds, seconds / N); /* Test speed of free */ QueryPerformanceCounter (&C1); for (index = 0; index < N; ++index) free (A [index]); QueryPerformanceCounter (&C2); seconds = (double)(C2.QuadPart - C1.QuadPart) * Freq; fprintf (fp, "With free: %f seconds, %.8f p/int\n", seconds, seconds / N); /* Test speed of ListCreate */ QueryPerformanceCounter (&C1); List = ListCreate (5, sizeof(int), L_DYNAMIC); QueryPerformanceCounter (&C2); seconds = (double)(C2.QuadPart - C1.QuadPart) * Freq; fprintf (fp, "With ListCreate: %f seconds\n", seconds); fprintf (fp, "500 items:\n"); /* Test speed of ListToFront */ QueryPerformanceCounter (&C1); for (index = 0; index < 500; ++index) ListToFront (List, &index); QueryPerformanceCounter (&C2); seconds = (double)(C2.QuadPart - C1.QuadPart) * Freq; fprintf (fp, "With ListToFront: %f seconds, %.8f p/int\n", seconds, seconds / 500); for (index = 0; index < 500; ++index) I [index] = 0; /* Test speed of ListExecute */ QueryPerformanceCounter (&C1); Int = 9; ListExecute (List, PrintMult, &Int); QueryPerformanceCounter (&C2); for (index = 0; index < 500; ++index) { if (index % 5 == 0) printf ("\n"); printf ("%dx%d:%d\t", index, Int, I [index]); } seconds = (double)(C2.QuadPart - C1.QuadPart) * Freq; fprintf (fp, "With ListExecute: %f seconds, %.8f p/int\n", seconds, seconds / 500); /* Test speed of ListSearch */ QueryPerformanceCounter (&C1); Int = 205; Item = ListSearch (List, Equal, &Int); QueryPerformanceCounter (&C2); printf ("Datum found: %d\n", *(int*) Item); seconds = (double)(C2.QuadPart - C1.QuadPart) * Freq; fprintf (fp, "With ListSearch: %f seconds, %.8f p/int\n", seconds, seconds / 500); /* Test speed of ListDestroy */ QueryPerformanceCounter (&C1); ListDestroy (List); QueryPerformanceCounter (&C2); seconds = (double)(C2.QuadPart - C1.QuadPart) * Freq; fprintf (fp, "With ListDestroy: %f seconds, %.8f p/int\n", seconds, seconds / 500); fclose (fp); /* Terminate memory; output results to file */ MemTerm ("Mem.txt"); }
Bool HashSearch(HashMap *hash, ThreadGlobals* tg, int key, Data* data) { int bucket = HASH(key, hash->logLen); _assert(bucket>=0 && bucket<hash->len); return ListSearch(&hash->array[bucket], tg, key, data); }
int At(HashTable * h, int key){ int i = key % h->m; Elem * x = ListSearch(h->l+i, key); return x == NULL ? 0 : x->val; }