/* Copyright (c) 2015 Alex Diener This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. Alex Diener alex@ludobloom.com */ #include "dynamictypes/DataHashTable.h" #include "utilities/printfFormats.h" #include #include #include DataHashTable * hashCreate() { DataHashTable * hash = malloc(sizeof(DataHashTable)); hash->hashTable = HashTable_create(sizeof(DataValue)); return hash; } DataHashTable * hashCreateWithKeysAndValues(const char * key, DataValue value, ...) { DataHashTable * hash = hashCreate(); hashSet(hash, key, value); va_list args; va_start(args, value); while ((key = va_arg(args, const char *)) != NULL) { value = va_arg(args, DataValue); hashSet(hash, key, value); } return hash; } static bool setValueCopy(HashTable * hashTable, HashTable_key key, void * value, void * context) { DataHashTable * copy = context; DataValue * dataValue = value; hashSet(copy, key.data.string.characters, valueCopy(dataValue)); return true; } DataHashTable * hashCopy(DataHashTable * hash) { DataHashTable * copy = hashCreate(); HashTable_foreach(hash->hashTable, setValueCopy, copy); return copy; } static bool disposeValue(HashTable * hashTable, HashTable_key key, void * value, void * context) { DataValue * dataValue = value; valueDispose(dataValue); return true; } void hashDispose(DataHashTable * hash) { HashTable_foreach(hash->hashTable, disposeValue, NULL); HashTable_dispose(hash->hashTable); } bool hashHas(DataHashTable * hash, const char * key) { return HashTable_get(hash->hashTable, HashTable_stringKey(key)) != NULL; } bool hashDelete(DataHashTable * hash, const char * key) { DataValue * value = hashGet(hash, key); if (value != NULL) { valueDispose(value); return HashTable_delete(hash->hashTable, HashTable_stringKey(key)); } return false; } DataValue * hashGet(DataHashTable * hash, const char * key) { return HashTable_get(hash->hashTable, HashTable_stringKey(key)); } void hashSet(DataHashTable * hash, const char * key, DataValue value) { DataValue * oldValue = hashGet(hash, key); if (oldValue != NULL) { valueDispose(oldValue); } HashTable_set(hash->hashTable, HashTable_stringKey(key), &value); } size_t hashGetCount(DataHashTable * hash) { return hash->hashTable->count; } struct hashGetKeysContext { const char ** stringKeys; size_t keyIndex; }; static bool listStringKeys(HashTable * hashTable, HashTable_key key, void * value, void * context) { struct hashGetKeysContext * contextStruct = context; if (key.type == HASH_KEY_TYPE_STRING) { contextStruct->stringKeys[contextStruct->keyIndex++] = key.data.string.characters; } return true; } const char ** hashGetKeys(DataHashTable * hash, size_t * outCount) { const char ** stringKeys = malloc(sizeof(*stringKeys) * hash->hashTable->count); struct hashGetKeysContext context = {stringKeys, 0}; HashTable_foreach(hash->hashTable, listStringKeys, &context); if (outCount != NULL) { *outCount = context.keyIndex; } return stringKeys; } const char * hashGetKeyAtIndex(DataHashTable * hash, size_t index) { HashTable_key * key = HashTable_keyAtIndex(hash->hashTable, index); if (key == NULL || key->type != HASH_KEY_TYPE_STRING) { return NULL; } return key->data.string.characters; }