Deluge Firmware 1.3.0
Build date: 2025.04.16
Loading...
Searching...
No Matches
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 "definitions_cxx.hpp"
21#include <cstdint>
22
23#define GREATER_OR_EQUAL 0
24#define LESS (-1)
25
26#define RESIZEABLE_ARRAY_DO_LOCKS (ALPHA_OR_BETA_VERSION)
27
28class ResizeableArray {
29public:
30 ResizeableArray(int32_t newElementSize, int32_t newMaxNumEmptySpacesToKeep = 16,
31 int32_t newNumExtrarSpacesToAllocate = 15);
32 ~ResizeableArray();
33 void init();
34 bool cloneFrom(ResizeableArray const* other);
35 void empty();
36 void swapStateWith(ResizeableArray* other);
37 void deleteAtIndex(int32_t i, int32_t numToDelete = 1, bool mayShortenMemoryAfter = true);
38 bool ensureEnoughSpaceAllocated(int32_t numAdditionalElementsNeeded);
39 Error insertAtIndex(int32_t i, int32_t numToInsert = 1, void* thingNotToStealFrom = nullptr);
40 void swapElements(int32_t i1, int32_t i2);
41 void repositionElement(int32_t iFrom, int32_t iTo);
42 Error beenCloned();
43 void setMemory(void* newMemory, int32_t newMemorySize);
44 void setStaticMemory(void* newMemory, int32_t newMemorySize);
45
46 void moveElementsLeft(int32_t oldStartIndex, int32_t oldStopIndex, int32_t distance);
47 void moveElementsRight(int32_t oldStartIndex, int32_t oldStopIndex, int32_t distance);
48
49 [[gnu::always_inline]] inline void* getElementAddress(int32_t index) {
50 int32_t absoluteIndex = index + memoryStart;
51 if (absoluteIndex >= memorySize)
52 absoluteIndex -= memorySize;
53 return (char* __restrict__)memory + (absoluteIndex * elementSize);
54 }
55
56 [[gnu::always_inline]] inline int32_t getNumElements() { return numElements; }
57
58 uint32_t elementSize;
59 bool emptyingShouldFreeMemory;
60 uint32_t staticMemoryAllocationSize;
61
62protected:
63 void* memory;
64 int32_t numElements;
65 int32_t memorySize; // In elements, not bytes
66 int32_t memoryStart;
67#if TEST_VECTOR
68 int32_t moveCount;
69#endif
70
71#if RESIZEABLE_ARRAY_DO_LOCKS
72 bool lock;
73 void freezeOnLock();
74 void exitLock();
75#endif
76
77private:
78 void attemptMemoryShorten();
79 bool attemptMemoryExpansion(int32_t minNumToExtend, int32_t idealNumToExtend, bool mayExtendAllocation,
80 void* thingNotToStealFrom);
81 void copyToNewMemory(void* newMemory, uint32_t destinationIndex, void* source, uint32_t numElementsToCopy,
82 uint32_t newMemorySize, uint32_t newMemoryStartIndex);
83 Error copyElementsFromOldMemory(void* otherMemory, int32_t otherMemorySize, int32_t otherMemoryStart);
84
85 void moveElementsRightNoWrap(int32_t oldStartIndex, int32_t oldStopIndex, int32_t distance);
86 void moveElementsLeftNoWrap(int32_t oldStartIndex, int32_t oldStopIndex, int32_t distance);
87
88 void* memoryAllocationStart; // This might be slightly to the left of "memory"
89
90 const int32_t maxNumEmptySpacesToKeep; // Can go down to 0
91 const int32_t numExtraSpacesToAllocate; // Can go down to 0
92};