19S-Section-Solution

19S-Section-Solution - void *auxData; int keySize; }...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
CS107 Handout 19S Spring 2007 April 27, 2007 Section Solution The C multitable a) typedef struct { hashset mappings; int keySize; int valueSize; } multitable; void MultiTableNew(multitable *mt, int keySizeInBytes, int valueSizeInBytes, int numBuckets, MultiTableHashFunction hash, MultiTableCompareFunction compare) { mt->keySize = keySizeInBytes; mt->valueSize = valueSizeInBytes; HashSetNew(&mt->mappings, keySizeInBytes + sizeof(vector), numBuckets, hash, compare, NULL); } b) void MultiTableEnter(multitable *mt, const void *keyAddr, const void *valueAddr) { char buffer[mt->keySize + sizeof(vector)]; vector *values; void *found = HashSetLookup(&mt->mappings, keyAddr); if (found == NULL) { memcpy(buffer, keyAddr, mt->keySize); values = (vector *)(buffer + mt->keySize); VectorNew(values, mt->valueSize, NULL, 0); VectorAppend(values, valueAddr); HashSetEnter(&mt->mappings, buffer); } else { values = (vector *)((char *) found + mt->keySize); VectorAppend(values, valueAddr); } } c) typedef struct { MultiTableMapFunction map;
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: void *auxData; int keySize; } maphelper; 2 static void HashSetMapper(void *elem, void *auxData) { int i; maphelper *helper = auxData; vector *values = (vector *)((char *) elem + helper->keySize); for (i = 0; i < VectorLength(values); i++) helper->map(elem, VectorNth(values, i), helper->auxData); } void MultiTableMap(multitable *mt, MultiTableMapFunction map, void *auxData) { maphelper helper = {map, auxData, mt->keySize}; HashSetMap(&mt->mappings, HashSetMapper, &helper); } multitable Client Code void ListRecordsInRange(multitable *zipCodes, char *low, char *high) { char *endpoints = {low, high}; MultiTableMap(zipCodes, InRangePrint, endpoints); } static void InRangePrint(void *keyAddr, void *valueAddr, void *auxData) { char *zipcode = (char *) keyAddr; char *city = *(char **) valueAddr; char **endpoints = (char **) auxData; char *low = endpoints[0]; char *high = endpoints[1]; if ((strcmp(zipcode, low) >= 0) && (strcmp(zipcode, high) <= 0)) printf("%5s: %s\n", zipcode, city); }...
View Full Document

Page1 / 2

19S-Section-Solution - void *auxData; int keySize; }...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online