/* 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/IndexPairSelection.h" #include #define stemobject_implementation IndexPairSelection v_begin(); v_func(dispose); v_end(); IndexPairSelection * IndexPairSelection_create(void) { stemobject_create_implementation(init) } bool IndexPairSelection_init(IndexPairSelection * self) { call_super(init, self); self->pairCount = 0; self->private_ivar(pairAllocatedCount) = 0; self->pairs = NULL; self->reversePairLookup = HashTable_create(sizeof(unsigned int)); self->selectionDirtyValue = 0; self->reverseLookupDirtyValue = 0; self->lastReverseLookupDirtyValue = UINT_MAX; return true; } void IndexPairSelection_dispose(IndexPairSelection * self) { free(self->pairs); HashTable_dispose(self->reversePairLookup); call_super_virtual(dispose, self); } IndexPairSelection * IndexPairSelection_copy(IndexPairSelection * self) { IndexPairSelection * copy = IndexPairSelection_create(); copy->private_ivar(pairAllocatedCount) = copy->pairCount = self->pairCount; if (copy->pairCount > 0) { copy->pairs = malloc(copy->pairCount * sizeof(*copy->pairs)); memcpy(copy->pairs, self->pairs, copy->pairCount * sizeof(*copy->pairs)); } return copy; } static void resizeAllocation(IndexPairSelection * self, unsigned int allocationCount) { if (self->private_ivar(pairAllocatedCount) <= allocationCount) { if (self->private_ivar(pairAllocatedCount) == 0) { self->private_ivar(pairAllocatedCount) = 16; } while (self->private_ivar(pairAllocatedCount) < allocationCount) { self->private_ivar(pairAllocatedCount) *= 2; } self->pairs = realloc(self->pairs, sizeof(*self->pairs) * self->private_ivar(pairAllocatedCount)); } } void IndexPairSelection_copySelectionFrom(IndexPairSelection * self, IndexPairSelection * selection) { resizeAllocation(self, selection->pairCount); memcpy(self->pairs, selection->pairs, selection->pairCount * sizeof(*self->pairs)); self->pairCount = selection->pairCount; self->lastReverseLookupDirtyValue = UINT_MAX; } #define sortIndexPair(index0, index1) \ if (index0 > index1) { \ unsigned int swap = index0; \ index0 = index1; \ index1 = swap; \ } bool IndexPairSelection_selectIndexPair(IndexPairSelection * self, unsigned int index0, unsigned int index1, bool assumeUnique) { sortIndexPair(index0, index1); if (!assumeUnique && IndexPairSelection_isPairSelected(self, index0, index1, NULL)) { return false; } resizeAllocation(self, self->pairCount + 1); self->pairs[self->pairCount].index0 = index0; self->pairs[self->pairCount].index1 = index1; HashTable_set(self->reversePairLookup, HashTable_uint32PairKey(index0, index1), &self->pairCount); self->pairCount++; self->selectionDirtyValue++; return true; } bool IndexPairSelection_deselectIndexPair(IndexPairSelection * self, unsigned int index0, unsigned int index1, bool scribble) { if (scribble) { unsigned int itemIndex; if (IndexPairSelection_isPairSelected(self, index0, index1, &itemIndex)) { IndexPairSelection_deselectItemAtIndex(self, itemIndex, true); return true; } return false; } sortIndexPair(index0, index1); for (unsigned int pairIndex = 0; pairIndex < self->pairCount; pairIndex++) { if (self->pairs[pairIndex].index0 == index0 && self->pairs[pairIndex].index1 == index1) { self->pairCount--; for (; pairIndex < self->pairCount; pairIndex++) { self->pairs[pairIndex] = self->pairs[pairIndex + 1]; } self->selectionDirtyValue++; self->reverseLookupDirtyValue++; return true; } } return false; } bool IndexPairSelection_deselectAll(IndexPairSelection * self) { bool anythingSelected = self->pairCount > 0; self->pairCount = 0; HashTable_deleteAll(self->reversePairLookup); return anythingSelected; } bool IndexPairSelection_toggleIndexPair(IndexPairSelection * self, unsigned int index0, unsigned int index1, bool scribble) { unsigned int itemIndex; if (IndexPairSelection_isPairSelected(self, index0, index1, &itemIndex)) { IndexPairSelection_deselectItemAtIndex(self, itemIndex, scribble); return false; } return IndexPairSelection_selectIndexPair(self, index0, index1, true); } void IndexPairSelection_deselectItemAtIndex(IndexPairSelection * self, unsigned int pairIndex, bool scribble) { if (scribble) { HashTable_delete(self->reversePairLookup, HashTable_uint32PairKey(self->pairs[pairIndex].index0, self->pairs[pairIndex].index1)); self->pairs[pairIndex].index0 = self->pairs[pairIndex].index1 = UINT_MAX; } else { self->pairCount--; for (; pairIndex < self->pairCount; pairIndex++) { self->pairs[pairIndex] = self->pairs[pairIndex + 1]; } self->selectionDirtyValue++; self->reverseLookupDirtyValue++; } } void IndexPairSelection_adjustIndexes(IndexPairSelection * self, unsigned int indexStart, int indexDelta) { unsigned int offset = 0; for (unsigned int pairIndex = 0; pairIndex < self->pairCount; pairIndex++) { self->pairs[pairIndex] = self->pairs[pairIndex + offset]; if (self->pairs[pairIndex].index0 >= indexStart) { if (self->pairs[pairIndex].index0 < indexStart - indexDelta) { offset++; pairIndex--; self->pairCount--; continue; } else { self->pairs[pairIndex].index0 += indexDelta; } } if (self->pairs[pairIndex].index1 >= indexStart) { if (self->pairs[pairIndex].index1 < indexStart - indexDelta) { offset++; pairIndex--; self->pairCount--; } else { self->pairs[pairIndex].index1 += indexDelta; } } } self->selectionDirtyValue++; self->reverseLookupDirtyValue++; } static void updateReverseLookup(IndexPairSelection * self) { if (self->lastReverseLookupDirtyValue == self->reverseLookupDirtyValue) { return; } HashTable_deleteAll(self->reversePairLookup); for (unsigned int pairIndex = 0; pairIndex < self->pairCount; pairIndex++) { HashTable_set(self->reversePairLookup, HashTable_uint32PairKey(self->pairs[pairIndex].index0, self->pairs[pairIndex].index1), &pairIndex); } self->lastReverseLookupDirtyValue = self->reverseLookupDirtyValue; } bool IndexPairSelection_isPairSelected(IndexPairSelection * self, unsigned int index0, unsigned int index1, unsigned int * outSelectionIndex) { sortIndexPair(index0, index1); updateReverseLookup(self); unsigned int * entry = HashTable_get(self->reversePairLookup, HashTable_uint32PairKey(index0, index1)); if (entry != NULL) { if (outSelectionIndex != NULL) { *outSelectionIndex = *entry; } return true; } return false; } bool IndexPairSelection_truncateSelectionToMaxIndexCount(IndexPairSelection * self, unsigned int maxIndexCount) { unsigned int offset = 0; for (unsigned int pairIndex = 0; pairIndex < self->pairCount; pairIndex++) { self->pairs[pairIndex] = self->pairs[pairIndex + offset]; if (self->pairs[pairIndex].index0 >= maxIndexCount || self->pairs[pairIndex].index1 >= maxIndexCount) { offset++; pairIndex--; self->pairCount--; } } if (offset > 0) { self->selectionDirtyValue++; self->reverseLookupDirtyValue++; return true; } return false; } bool IndexPairSelection_setLastSelectedPair(IndexPairSelection * self, unsigned int index0, unsigned int index1) { unsigned int selectionIndex; if (IndexPairSelection_isPairSelected(self, index0, index1, &selectionIndex) && selectionIndex < self->pairCount - 1) { IndexPairSelection_deselectItemAtIndex(self, selectionIndex, false); IndexPairSelection_selectIndexPair(self, index0, index1, true); return true; } return false; } unsigned int IndexPairSelection_removeDuplicates(IndexPairSelection * self) { unsigned int removedCount = 0; for (unsigned int pairIndex = 1; pairIndex < self->pairCount; pairIndex++) { self->pairs[pairIndex] = self->pairs[pairIndex + removedCount]; for (unsigned int pairIndex2 = 0; pairIndex2 < pairIndex; pairIndex2++) { if (self->pairs[pairIndex].index0 == self->pairs[pairIndex2].index0 && self->pairs[pairIndex].index1 == self->pairs[pairIndex2].index1) { removedCount++; pairIndex--; self->pairCount--; break; } } } if (removedCount > 0) { self->selectionDirtyValue++; self->reverseLookupDirtyValue++; } return removedCount; } void IndexPairSelection_removeScribbles(IndexPairSelection * self) { IndexPairSelection_truncateSelectionToMaxIndexCount(self, UINT_MAX); }