/* 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/IndexSelection.h" #include #define stemobject_implementation IndexSelection stemobject_vtable_begin(); stemobject_vtable_entry(dispose); stemobject_vtable_end(); IndexSelection * IndexSelection_create(void) { stemobject_create_implementation(init) } bool IndexSelection_init(IndexSelection * self) { call_super(init, self); self->indexCount = 0; self->private_ivar(indexAllocatedCount) = 0; self->indexes = NULL; self->reverseLookup = ReverseIndexLookup_create(); self->selectionDirtyValue = 0; self->reverseLookupDirtyValue = 0; self->lastReverseLookupDirtyValue = UINT_MAX; return true; } void IndexSelection_dispose(IndexSelection * self) { ReverseIndexLookup_dispose(self->reverseLookup); free(self->indexes); call_super_virtual(dispose, self); } IndexSelection * IndexSelection_copy(IndexSelection * self) { IndexSelection * copy = IndexSelection_create(); copy->private_ivar(indexAllocatedCount) = copy->indexCount = self->indexCount; if (copy->indexCount > 0) { copy->indexes = malloc(copy->indexCount * sizeof(*copy->indexes)); memcpy(copy->indexes, self->indexes, copy->indexCount * sizeof(*copy->indexes)); } return copy; } static void resizeAllocation(IndexSelection * self, unsigned int allocationCount) { if (self->private_ivar(indexAllocatedCount) <= allocationCount) { if (self->private_ivar(indexAllocatedCount) == 0) { self->private_ivar(indexAllocatedCount) = 16; } while (self->private_ivar(indexAllocatedCount) < allocationCount) { self->private_ivar(indexAllocatedCount) *= 2; } self->indexes = realloc(self->indexes, self->private_ivar(indexAllocatedCount) * sizeof(*self->indexes)); } } void IndexSelection_copySelectionFrom(IndexSelection * self, IndexSelection * selection) { resizeAllocation(self, selection->indexCount); memcpy(self->indexes, selection->indexes, selection->indexCount * sizeof(*self->indexes)); self->indexCount = selection->indexCount; self->selectionDirtyValue++; self->lastReverseLookupDirtyValue = UINT_MAX; } bool IndexSelection_selectIndex(IndexSelection * self, unsigned int index, bool assumeUnique) { if (!assumeUnique && IndexSelection_isIndexSelected(self, index, NULL)) { return false; } resizeAllocation(self, self->indexCount + 1); self->indexes[self->indexCount] = index; ReverseIndexLookup_set(self->reverseLookup, index, self->indexCount); self->selectionDirtyValue++; self->indexCount++; return true; } bool IndexSelection_deselectIndex(IndexSelection * self, unsigned int index) { for (unsigned int itemIndex = 0; itemIndex < self->indexCount; itemIndex++) { if (self->indexes[itemIndex] == index) { self->indexCount--; for (; itemIndex < self->indexCount; itemIndex++) { self->indexes[itemIndex] = self->indexes[itemIndex + 1]; } self->selectionDirtyValue++; self->reverseLookupDirtyValue++; return true; } } return false; } bool IndexSelection_deselectAll(IndexSelection * self) { bool anythingSelected = self->indexCount > 0; self->indexCount = 0; if (anythingSelected) { self->selectionDirtyValue++; self->reverseLookupDirtyValue++; return true; } return false; } bool IndexSelection_toggleIndex(IndexSelection * self, unsigned int index) { self->selectionDirtyValue++; for (unsigned int itemIndex = 0; itemIndex < self->indexCount; itemIndex++) { if (self->indexes[itemIndex] == index) { self->indexCount--; for (; itemIndex < self->indexCount; itemIndex++) { self->indexes[itemIndex] = self->indexes[itemIndex + 1]; } self->reverseLookupDirtyValue++; return false; } } resizeAllocation(self, self->indexCount + 1); self->indexes[self->indexCount] = index; ReverseIndexLookup_set(self->reverseLookup, index, self->indexCount); self->indexCount++; return true; } void IndexSelection_deselectItemAtIndex(IndexSelection * self, unsigned int selectionIndex) { self->indexCount--; for (; selectionIndex < self->indexCount; selectionIndex++) { self->indexes[selectionIndex] = self->indexes[selectionIndex + 1]; } self->selectionDirtyValue++; self->reverseLookupDirtyValue++; } bool IndexSelection_selectRange(IndexSelection * self, unsigned int start, unsigned int count, bool assumeUnique) { bool changed = false; for (unsigned int index = start; index < start + count; index++) { changed |= IndexSelection_selectIndex(self, index, assumeUnique); } return changed; } void IndexSelection_toggleRange(IndexSelection * self, unsigned int start, unsigned int count) { for (unsigned int index = start; index < start + count; index++) { IndexSelection_toggleIndex(self, index); } } void IndexSelection_adjustIndexes(IndexSelection * self, unsigned int indexStart, int indexDelta) { unsigned int offset = 0; for (unsigned int itemIndex = 0; itemIndex < self->indexCount; itemIndex++) { self->indexes[itemIndex] = self->indexes[itemIndex + offset]; if (self->indexes[itemIndex] >= indexStart) { if (self->indexes[itemIndex] < indexStart - indexDelta) { offset++; itemIndex--; self->indexCount--; } else { self->indexes[itemIndex] += indexDelta; } } } self->selectionDirtyValue++; self->reverseLookupDirtyValue++; } static void updateReverseLookup(IndexSelection * self) { if (self->lastReverseLookupDirtyValue == self->reverseLookupDirtyValue) { return; } ReverseIndexLookup_empty(self->reverseLookup); for (unsigned int itemIndex = 0; itemIndex < self->indexCount; itemIndex++) { ReverseIndexLookup_set(self->reverseLookup, self->indexes[itemIndex], itemIndex); } self->lastReverseLookupDirtyValue = self->reverseLookupDirtyValue; } bool IndexSelection_isIndexSelected(IndexSelection * self, unsigned int index, unsigned int * outSelectionIndex) { updateReverseLookup(self); struct ReverseIndexLookup_entry * entry = ReverseIndexLookup_get(self->reverseLookup, index); if (entry != NULL) { if (outSelectionIndex != NULL) { *outSelectionIndex = entry->values[0]; } return true; } return false; } bool IndexSelection_truncateSelectionToMaxIndexCount(IndexSelection * self, unsigned int maxIndexCount) { unsigned int offset = 0; for (unsigned int itemIndex = 0; itemIndex < self->indexCount; itemIndex++) { self->indexes[itemIndex] = self->indexes[itemIndex + offset]; if (self->indexes[itemIndex] >= maxIndexCount) { offset++; itemIndex--; self->indexCount--; } } if (offset > 0) { self->selectionDirtyValue++; self->reverseLookupDirtyValue++; return true; } return false; } unsigned int IndexSelection_removeDuplicates(IndexSelection * self) { unsigned int removedCount = 0; for (unsigned int itemIndex = 1; itemIndex < self->indexCount; itemIndex++) { self->indexes[itemIndex] = self->indexes[itemIndex + removedCount]; for (unsigned int itemIndex2 = 0; itemIndex2 < itemIndex; itemIndex2++) { if (self->indexes[itemIndex] == self->indexes[itemIndex2]) { removedCount++; itemIndex--; self->indexCount--; break; } } } if (removedCount > 0) { self->selectionDirtyValue++; self->reverseLookupDirtyValue++; } return removedCount; } unsigned int IndexSelection_getMinSelectedIndex(IndexSelection * self, unsigned int * outSelectionIndex) { unsigned int result = UINT_MAX; unsigned int minItemIndex = UINT_MAX; for (unsigned int itemIndex = 0; itemIndex < self->indexCount; itemIndex++) { if (self->indexes[itemIndex] < result) { result = self->indexes[itemIndex]; minItemIndex = itemIndex; } } if (minItemIndex != UINT_MAX && outSelectionIndex != NULL) { *outSelectionIndex = minItemIndex; } return result; } unsigned int IndexSelection_getMaxSelectedIndex(IndexSelection * self, unsigned int * outSelectionIndex) { unsigned int result = 0; unsigned int maxItemIndex = UINT_MAX; for (unsigned int itemIndex = 0; itemIndex < self->indexCount; itemIndex++) { if (self->indexes[itemIndex] > result) { result = self->indexes[itemIndex]; maxItemIndex = itemIndex; } } if (maxItemIndex == UINT_MAX) { return UINT_MAX; } if (outSelectionIndex != NULL) { *outSelectionIndex = maxItemIndex; } return result; }