Deluge Firmware 1.3.0
Build date: 2025.04.16
Loading...
Searching...
No Matches
ordered_resizeable_array.h
1/*
2 * Copyright © 2017-2023 Synthstrom Audible Limited
3 *
4 * This file is part of The Synthstrom Audible Deluge Firmware.
5 *
6 * The Synthstrom Audible Deluge Firmware is free software: you can redistribute it and/or modify it under the
7 * terms of the GNU General Public License as published by the Free Software Foundation,
8 * either version 3 of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
11 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * See the GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along with this program.
15 * If not, see <https://www.gnu.org/licenses/>.
16 */
17
18#pragma once
19
20#include "util/container/array/resizeable_array.h"
21
22class OrderedResizeableArray : public ResizeableArray {
23public:
24 OrderedResizeableArray(int32_t newElementSize, int32_t keyNumBits, int32_t newKeyOffset = 0,
25 int32_t newMaxNumEmptySpacesToKeep = 16, int32_t newNumExtraSpacesToAllocate = 15);
26 int32_t search(int32_t key, int32_t comparison, int32_t rangeBegin, int32_t rangeEnd);
27 inline int32_t search(int32_t key, int32_t comparison, int32_t rangeBegin = 0) {
28 return search(key, comparison, rangeBegin, numElements);
29 }
30
31 int32_t searchExact(int32_t key);
32 int32_t insertAtKey(int32_t key, bool isDefinitelyLast = false);
33 void deleteAtKey(int32_t key);
34
35#if TEST_VECTOR
36 // TODO: move to unit tests
37 void test();
38#endif
39#if TEST_VECTOR_DUPLICATES
40 // TODO: move to unit tests
41 void testDuplicates();
42#endif
46 void testSequentiality(char const* errorCode);
47
48 inline int32_t getKeyAtIndex(int32_t i) { return getKeyAtMemoryLocation(getElementAddress(i)); }
49
50 inline void setKeyAtIndex(int32_t key, int32_t i) { setKeyAtMemoryLocation(key, getElementAddress(i)); }
51
52protected:
53 inline int32_t getKeyAtMemoryLocation(void* address) {
54 int32_t keyBig = *(uint32_t*)((uint32_t)address + keyOffset) << keyShiftAmount;
55 return keyBig >> keyShiftAmount; // We use shifting instead of a mask so negative numbers get treated directly.
56 }
57
58 inline void setKeyAtMemoryLocation(int32_t key, void* address) {
59 uint32_t offsetAddress = (uint32_t)address + keyOffset;
60 uint32_t prevContents = *(uint32_t*)offsetAddress;
61 *(uint32_t*)offsetAddress = (key & keyMask) | (prevContents & ~keyMask);
62 }
63
64private:
65 const uint32_t keyMask;
66 const int32_t keyOffset;
67 const int32_t keyShiftAmount;
68};
69
70// The purpose of this is not so much that special functionality is required for 32-bit keys, but that some further
71// child classes inherit from this, which require that the key be 32-bit.
72class OrderedResizeableArrayWith32bitKey : public OrderedResizeableArray {
73public:
74 explicit OrderedResizeableArrayWith32bitKey(int32_t newElementSize, int32_t newMaxNumEmptySpacesToKeep = 16,
75 int32_t newNumExtraSpacesToAllocate = 15);
76 void shiftHorizontal(int32_t amount, int32_t effectiveLength);
77 void searchDual(int32_t const* __restrict__ searchTerms, int32_t* __restrict__ resultingIndexes);
78 void searchMultiple(int32_t* __restrict__ searchTerms, int32_t numSearchTerms, int32_t rangeEnd = -1);
79 bool generateRepeats(int32_t wrapPoint, int32_t endPos);
80#if TEST_VECTOR_SEARCH_MULTIPLE
81 // TODO: move to unit tests
82 void testSearchMultiple();
83#endif
84
85 inline int32_t getKeyAtIndex(int32_t i) { return getKeyAtMemoryLocation(getElementAddress(i)); }
86
87 inline void setKeyAtIndex(int32_t key, int32_t i) { setKeyAtMemoryLocation(key, getElementAddress(i)); }
88
89protected:
90 // These shadow - they don't override. Might give a tiny bit of efficiency
91 inline int32_t getKeyAtMemoryLocation(void* address) { return *(int32_t*)address; }
92
93 // Shadows - doesn't override
94 inline void setKeyAtMemoryLocation(int32_t key, void* address) { *(int32_t*)address = key; }
95};
void testSequentiality(char const *errorCode)
test that the keys in this array are sorted in ascending order.
Definition ordered_resizeable_array.cpp:265