/* Copyright (c) 2022 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 "utilities/ReverseIndexLookup.h" #include #define stemobject_implementation ReverseIndexLookup stemobject_vtable_begin(); stemobject_vtable_entry(dispose); stemobject_vtable_end(); ReverseIndexLookup * ReverseIndexLookup_create(void) { stemobject_create_implementation(init) } bool ReverseIndexLookup_init(ReverseIndexLookup * self) { call_super(init, self); self->hashTable = HashTable_create(sizeof(struct ReverseIndexLookup_entry)); return true; } static bool freeEntry(HashTable * hashTable, HashTable_key key, void * value, void * context) { struct ReverseIndexLookup_entry * entry = value; free(entry->values); return true; } void ReverseIndexLookup_dispose(ReverseIndexLookup * self) { HashTable_foreach(self->hashTable, freeEntry, NULL); HashTable_dispose(self->hashTable); call_super_virtual(dispose, self); } void ReverseIndexLookup_empty(ReverseIndexLookup * self) { HashTable_foreach(self->hashTable, freeEntry, NULL); HashTable_deleteAll(self->hashTable); } void ReverseIndexLookup_set(ReverseIndexLookup * self, unsigned int key, unsigned int value) { struct ReverseIndexLookup_entry * entry = HashTable_get(self->hashTable, HashTable_uint32Key(key)); if (entry == NULL) { struct ReverseIndexLookup_entry newEntry = {1, 8, malloc(8 * sizeof(*newEntry.values))}; newEntry.values[0] = value; HashTable_set(self->hashTable, HashTable_uint32Key(key), &newEntry); } else { if (entry->allocatedCount <= entry->count) { entry->allocatedCount *= 2; entry->values = realloc(entry->values, entry->allocatedCount * sizeof(*entry->values)); } entry->values[entry->count++] = value; } } void ReverseIndexLookup_unset(ReverseIndexLookup * self, unsigned int key, unsigned int value) { struct ReverseIndexLookup_entry * entry = HashTable_get(self->hashTable, HashTable_uint32Key(key)); if (entry != NULL) { for (unsigned int valueIndex = 0; valueIndex < entry->count; valueIndex++) { if (entry->values[valueIndex] == value) { entry->count--; for (; valueIndex < entry->count; valueIndex++) { entry->values[valueIndex] = entry->values[valueIndex + 1]; } break; } } } } struct ReverseIndexLookup_entry * ReverseIndexLookup_get(ReverseIndexLookup * self, unsigned int key) { return HashTable_get(self->hashTable, HashTable_uint32Key(key)); } unsigned int ReverseIndexLookup_getCount(ReverseIndexLookup * self, unsigned int key) { struct ReverseIndexLookup_entry * entry = HashTable_get(self->hashTable, HashTable_uint32Key(key)); if (entry == NULL) { return 0; } return entry->count; } unsigned int ReverseIndexLookup_getValue(ReverseIndexLookup * self, unsigned int key, unsigned int valueIndex) { struct ReverseIndexLookup_entry * entry = HashTable_get(self->hashTable, HashTable_uint32Key(key)); if (entry == NULL || valueIndex >= entry->count) { return UINT_MAX; } return entry->values[valueIndex]; }