-
Notifications
You must be signed in to change notification settings - Fork 0
/
TLArray.c
213 lines (182 loc) · 7.08 KB
/
TLArray.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*
* TLArray.c
* Mercatalog
*
* Created by Nathan Vander Wilt on 5/19/08.
* Copyright 2008 Calf Trail Software, LLC. All rights reserved.
*
*/
#include "TLArray.h"
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include "TLToolbag.h"
struct TL_Array {
tl_uint_t retainCount;
void* items;
tl_uint_t itemSize;
tl_uint_t itemCount;
tl_uint_t currentAllocatedLimit;
tl_uint_t hardItemCountLimit;
};
typedef enum {
TLArrayInternalSuccess = 0,
TLArrayInternalLimitReached,
TLArrayInternalNoMemory
} TLArrayInternalError;
TLMutableArrayRef TLArrayCreateMutable(tl_uint_t itemSize, tl_uint_t countLimit) {
TLMutableArrayRef newArray = (TLMutableArrayRef)malloc( sizeof(struct TL_Array) );
if (newArray) {
newArray->retainCount = 1;
// Set up item storage and parameters
newArray->itemSize = itemSize;
newArray->itemCount = 0;
newArray->hardItemCountLimit = countLimit;
// If there's no hard limit, begin with a reasonable initial size
tl_uint_t itemsToAllocate = countLimit ? countLimit : 4;
newArray->currentAllocatedLimit = itemsToAllocate;
newArray->items = malloc(itemsToAllocate*itemSize);
}
if (newArray && !newArray->items) {
free(newArray);
newArray = NULL;
}
return newArray;
}
TLMutableArrayRef TLArrayCreateMutableCopy(TLArrayRef oldArray) {
TLMutableArrayRef newArray = TLArrayCreateMutable(oldArray->itemSize, oldArray->hardItemCountLimit);
if (newArray) {
TLArrayAppendArray(newArray, oldArray);
}
return newArray;
}
TLArrayRef TLArrayCreateCopy(TLArrayRef oldArray) {
return TLArrayCreateMutableCopy(oldArray);
}
static TLArrayInternalError TLArrayCanEnsureAdditionalSpace(TLMutableArrayRef array, tl_uint_t extraItemCount) {
tl_uint_t necessaryLimit = array->itemCount + extraItemCount;
if (array->hardItemCountLimit && (necessaryLimit > array->hardItemCountLimit)) {
return TLArrayInternalLimitReached;
}
if (array->currentAllocatedLimit < necessaryLimit) {
tl_uint_t paddedLimit = TLNextPowerOfTwo(necessaryLimit);
void* newItems = realloc(array->items, paddedLimit*(array->itemSize));
if (!newItems) { // try again with just necessaryLimit
paddedLimit = necessaryLimit;
newItems = realloc(array->items, paddedLimit*(array->itemSize));
}
if (!newItems) return TLArrayInternalNoMemory;
array->currentAllocatedLimit = paddedLimit;
array->items = newItems;
}
return TLArrayInternalSuccess;
}
static void TLArrayVacuumExtraSpace(TLMutableArrayRef array) {
if (array->hardItemCountLimit) return;
tl_uint_t acceptablePaddedSize = TLNextPowerOfTwo(array->itemCount);
if (array->currentAllocatedLimit > acceptablePaddedSize) {
void* shrunkItems = realloc(array->items, acceptablePaddedSize*(array->itemSize));
if (shrunkItems) {
array->currentAllocatedLimit = acceptablePaddedSize;
array->items = shrunkItems;
}
}
}
static void TLArrayDestroy(TLMutableArrayRef array) {
free(array->items);
free(array);
}
TLArrayRef TLArrayRetain(TLArrayRef array) {
TLMutableArrayRef mutableArray = (TLMutableArrayRef)array;
mutableArray->retainCount += 1;
return array;
}
void TLArrayRelease(TLArrayRef array) {
if (!array) return;
TLMutableArrayRef mutableArray = (TLMutableArrayRef)array;
mutableArray->retainCount -= 1;
if (!mutableArray->retainCount) TLArrayDestroy(mutableArray);
}
tl_uint_t TLArrayGetCount(TLArrayRef array) {
return array->itemCount;
}
TL_INLINE tl_uint_t TLArrayGetItemSize(TLArrayRef array) {
return array->itemSize;
}
static inline void* TLArrayGetItemPointer(TLArrayRef array, tl_uint_t itemIndex) {
tl_uint_t itemOffset = itemIndex * TLArrayGetItemSize(array);
return array->items + itemOffset;
}
const void* TLArrayGetItemAtIndex(TLArrayRef array, tl_uint_t itemIndex) {
return TLArrayGetItemPointer(array, itemIndex);
}
static void TLArraySetExistingItem(TLMutableArrayRef mutableArray, tl_uint_t itemIdx, const void* itemPtr) {
void* targetItemLocation = TLArrayGetItemPointer(mutableArray, itemIdx);
memmove(targetItemLocation, itemPtr, TLArrayGetItemSize(mutableArray));
}
static void TLFailIfError(TLArrayInternalError status) {
TLAssert(status != TLArrayInternalLimitReached, "Too many items for fixed-size array!");
if (status == TLArrayInternalNoMemory) {
TLOutOfMemoryBail();
}
TLAssert(status == TLArrayInternalSuccess, "Unknown internal array error '%i'.", status);
}
static bool TLArraysAreCompatible(TLArrayRef array1, TLArrayRef array2) {
bool arrayItemsCompatible = (TLArrayGetItemSize(array1) == TLArrayGetItemSize(array2));
return arrayItemsCompatible;
}
// Append an item, assuming that there is enough space
TL_INLINE void TLArrayDoAppending(TLMutableArrayRef mutableArray, const void* item) {
tl_uint_t newIdx = mutableArray->itemCount;
mutableArray->itemCount += 1;
TLArraySetExistingItem(mutableArray, newIdx, item);
}
void TLArrayAppendItem(TLMutableArrayRef mutableArray, const void* item) {
TLArrayInternalError canAdd = TLArrayCanEnsureAdditionalSpace(mutableArray, 1);
TLFailIfError(canAdd);
TLArrayDoAppending(mutableArray, item);
}
void TLArrayAppendArray(TLMutableArrayRef mutableArray, TLArrayRef otherArray) {
bool canAppendCompatibly = TLArraysAreCompatible(mutableArray, otherArray);
TLAssert(canAppendCompatibly, "Appended array must be compatible.");
tl_uint_t otherArraySize = TLArrayGetCount(otherArray);
TLArrayInternalError canAdd = TLArrayCanEnsureAdditionalSpace(mutableArray, otherArraySize);
TLFailIfError(canAdd);
// copy the items in bulk
tl_uint_t firstDestinationIdx = TLArrayGetCount(mutableArray);
void* destLocation = TLArrayGetItemPointer(mutableArray, firstDestinationIdx);
const void* sourceLocation = TLArrayGetItemPointer(otherArray, 0);
memmove(destLocation, sourceLocation, otherArraySize * TLArrayGetItemSize(mutableArray));
mutableArray->itemCount += otherArraySize;
}
void TLArrayRemoveItemAtIndex(TLMutableArrayRef array, tl_uint_t removedItemIdx) {
void* removedItemLocation = TLArrayGetItemPointer(array, removedItemIdx);
// shift items down
tl_uint_t nextItemIdx = removedItemIdx + 1;
tl_uint_t originalCount = TLArrayGetCount(array);
if (nextItemIdx < originalCount) {
const void* nextItemLocation = TLArrayGetItemPointer(array, nextItemIdx);
tl_uint_t numItemsPastMovedItem = originalCount - nextItemIdx;
memmove(removedItemLocation, nextItemLocation, numItemsPastMovedItem * TLArrayGetItemSize(array));
}
// update bookkeeping
array->itemCount -= 1;
TLArrayVacuumExtraSpace(array);
}
typedef struct {
TLArraySortCallback realCallback;
void* realContext;
} TLArrayCompareContext;
static TLCompareResult TLArraySortCallbackWrapper(void* context, const void* item1, const void* item2) {
TLArrayCompareContext* wrappedContext = (TLArrayCompareContext*)context;
TLArraySortCallback compareFunction = wrappedContext->realCallback;
void* compareContext = wrappedContext->realContext;
return compareFunction(item1, item2, compareContext);
}
void TLArraySort(TLMutableArrayRef array, TLArraySortCallback compareFunction, void* compareContext) {
TLArrayCompareContext wrappedContext = {
.realCallback = compareFunction,
.realContext = compareContext
};
qsort_r(array->items, array->itemCount, array->itemSize, &wrappedContext, TLArraySortCallbackWrapper);
}