All pastes #35345 Raw Edit

Untitled

public c v1 · immutable
#35345 ·published 2005-12-31 02:02 UTC
rendered paste body
#include "GhettoChainedHashMap.h"#include <stdlib.h>#include <string.h>__int8 createInitialHashMap(ChainedHashMap ** hashMap, size_t desiredSize) {	void * allocatedMemory = malloc(sizeof(ChainedHashMap) + (sizeof(ChainedHashMapEntry *) * desiredSize));	if (allocatedMemory == 0)		return 0;	*hashMap = (ChainedHashMap *)allocatedMemory;	(*hashMap)->sizeOfMap = desiredSize;	(*hashMap)->mapValues = (ChainedHashMapEntry **)allocatedMemory + sizeof(ChainedHashMap);	memset((*hashMap)->mapValues, 0, sizeof(ChainedHashMapEntry *) * desiredSize);	return 1;}__int8 releaseHashMap(ChainedHashMap * hashMap) {	free((void *)hashMap);	return 1;}__int8 insertIntoHashMap(ChainedHashMap * hashMap, char * key, void * value) {	__int32 hashValue;	ChainedHashMapEntry * hashEntry;	ChainedHashMapEntry * newHashEntry;	ChainedHashMapEntry * prevHashEntry = 0;	__int8 alreadyExists = 0;	hashKey(key, &hashValue);	hashValue %= hashMap->sizeOfMap;	for (hashEntry = hashMap->mapValues[hashValue]; hashEntry != 0; hashEntry = hashEntry->next) {		if (strcmp(key, hashEntry->name) == 0) {			alreadyExists = 1;			break;		}		prevHashEntry = hashEntry;	}	if (alreadyExists)		newHashEntry = (ChainedHashMapEntry *)realloc(hashEntry, sizeof(ChainedHashMapEntry) + strlen(key) + 1);	else		newHashEntry = (ChainedHashMapEntry *)malloc(sizeof(ChainedHashMapEntry) + strlen(key) + 1);	if (newHashEntry == 0)		return 0;	if (!alreadyExists) {		// if the key isn't already there, copy the key into the entry, and set the next pointer to nothing, cause it'll be at the end.		strcpy(newHashEntry->name, key);		newHashEntry->next = 0;	}	newHashEntry->value = value;	if (prevHashEntry == 0)			// first entry being added		hashMap->mapValues[hashValue] = newHashEntry;	else							// chained entry being added		prevHashEntry->next = newHashEntry;	return 1;}__int8 searchHashMap(ChainedHashMap * hashMap, char * key, void ** value) {	__int32 hashValue;	ChainedHashMapEntry * hashEntry;	hashKey(key, &hashValue);	hashValue %= hashMap->sizeOfMap;		for (hashEntry = hashMap->mapValues[hashValue]; hashEntry != 0; hashEntry = hashEntry->next) {		if (strcmp(key, hashEntry->name) == 0) {			*value = hashEntry->value;			return 1;		}	}	return 0;}__int8 hashKey(char * key, __int32 * hashValue) {	*hashValue = 0;	while (*key != 0)		*hashValue += (__int32)*key;	return 1;}__int8 removeFromHashMap(ChainedHashMap * hashMap, char * key) {	__int32 hashValue;	ChainedHashMapEntry * hashEntry;	ChainedHashMapEntry * prevHashEntry = 0;	hashKey(key, &hashValue);	hashValue %= hashMap->sizeOfMap;		for (hashEntry = hashMap->mapValues[hashValue]; hashEntry != 0; hashEntry = hashEntry->next) {		if (strcmp(key, hashEntry->name) == 0)			break;		prevHashEntry = hashEntry;	}	if (hashEntry == 0)		return 0;	if (prevHashEntry == 0)		hashMap->mapValues[hashValue] = hashMap->mapValues[hashValue]->next;	else		prevHashEntry->next = hashEntry->next;	return 1;}