-
-
Notifications
You must be signed in to change notification settings - Fork 184
/
Copy pathArrayIntMap.c
313 lines (240 loc) · 7.85 KB
/
ArrayIntMap.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
* Copyright (c) scott.cgi All Rights Reserved.
*
* This source code belongs to project Mojoc, which is a pure C Game Engine hosted on GitHub.
* The Mojoc Game Engine is licensed under the MIT License, and will continue to be iterated with coding passion.
*
* License : https://github.com/scottcgi/Mojoc/blob/master/LICENSE
* GitHub : https://github.com/scottcgi/Mojoc
* CodeStyle: https://github.com/scottcgi/Mojoc/blob/master/Docs/CodeStyle.md
*
* Since : 2013-5-20
* Update : 2019-1-17
* Author : scott.cgi
*/
#include <string.h>
#include <stdlib.h>
#include "Engine/Toolkit/Utils/ArrayIntMap.h"
#include "Engine/Toolkit/Platform/Log.h"
#define CheckIndex(tag) \
ALog_A \
( \
index >= 0 && index < arrayIntMap->elementList->size, \
"AArrayIntMap" tag "index = %d, size = %d, invalid", \
index, \
arrayIntMap->elementList->size \
)
#define CheckInsertIndex(tag) \
ALog_A \
( \
index >= 0 && index <= arrayIntMap->elementList->size, \
"AArrayIntMap" tag "index = %d, size = %d, invalid", \
index, \
arrayIntMap->elementList->size \
)
/**
* Search index of key, if negative not found then return "-insertIndex - 1",
* so insert index is "-BinarySearch() - 1".
*/
static inline int BinarySearch(ArrayList(intptr_t)* elementList, intptr_t key)
{
int high = elementList->size;
int low = -1;
int guess = -1;
while (high - low > 1) // prevent infinite loops
{
// (high + low) always positive, so convert to unsigned
// then the '>>' is unsigned move right
// so the overflow will be handled correctly
// because sign bit shift to right and 0 will be added
guess = (unsigned int) (high + low) >> 1;
intptr_t elementKey = AArrayList_Get(elementList, guess, ArrayIntMapElement*)->key;
if (elementKey < key)
{
low = guess;
}
else if (elementKey > key)
{
high = guess;
}
else if (elementKey == key)
{
// find the key, the guess is positive value
return guess;
}
}
// if guess == high
// the guess is bigger than key index and insert value at guess
if (guess == low)
{
// the guess is smaller than key index and insert value behind,
// or if list empty then the guess is -1, so do this make guess at 0
++guess;
}
// when list empty the guess is 0, so we -1 make sure return negative value
return -guess - 1;
}
static void* TryPut(ArrayIntMap* arrayIntMap, intptr_t key, void* valuePtr)
{
int guess = BinarySearch(arrayIntMap->elementList, key);
if (guess < 0)
{
ArrayIntMapElement* element = malloc(sizeof(ArrayIntMapElement) + arrayIntMap->valueTypeSize);
element->key = key;
element->valuePtr = (char*) element + sizeof(ArrayIntMapElement);
AArrayList_Insert(arrayIntMap->elementList, -guess - 1, element);
return memcpy(element->valuePtr, valuePtr, (size_t) arrayIntMap->valueTypeSize);
}
else
{
return NULL;
}
}
static void* Get(ArrayIntMap* arrayIntMap, intptr_t key, void* defaultValuePtr)
{
int guess = BinarySearch(arrayIntMap->elementList, key);
return guess >= 0 ?
AArrayList_Get(arrayIntMap->elementList, guess, ArrayIntMapElement*)->valuePtr : defaultValuePtr;
}
static void* TrySet(ArrayIntMap* arrayIntMap, intptr_t key, void* valuePtr)
{
int guess = BinarySearch(arrayIntMap->elementList, key);
if (guess >= 0)
{
return memcpy
(
AArrayList_Get(arrayIntMap->elementList, guess, ArrayIntMapElement*)->valuePtr,
valuePtr,
(size_t) arrayIntMap->valueTypeSize
);
}
else
{
return NULL;
}
}
static bool TryRemove(ArrayIntMap* arrayIntMap, intptr_t key)
{
int guess = BinarySearch(arrayIntMap->elementList, key);
if (guess >= 0)
{
free
(
AArrayList_Get(arrayIntMap->elementList, guess, ArrayIntMapElement*)
);
AArrayList->Remove(arrayIntMap->elementList, guess);
return true;
}
return false;
}
static void Clear(ArrayIntMap* arrayIntMap)
{
for (int i = 0; i < arrayIntMap->elementList->size; ++i)
{
free
(
AArrayList_Get(arrayIntMap->elementList, i, ArrayIntMapElement*)
);
}
AArrayList->Clear(arrayIntMap->elementList);
}
static void* InsertAt(ArrayIntMap* arrayIntMap, intptr_t key, int index, void* valuePtr)
{
CheckInsertIndex("InsertAt");
ArrayIntMapElement* element = malloc(sizeof(ArrayIntMapElement) + arrayIntMap->valueTypeSize);
element->key = key;
element->valuePtr = (char*) element + sizeof(ArrayIntMapElement);
AArrayList_Insert(arrayIntMap->elementList, index, element);
return memcpy(element->valuePtr, valuePtr, (size_t) arrayIntMap->valueTypeSize);
}
static int GetIndex(ArrayIntMap* arrayIntMap, intptr_t key)
{
return BinarySearch(arrayIntMap->elementList, key);
}
static intptr_t GetKey(ArrayIntMap* arrayIntMap, int index)
{
CheckIndex("GetKey");
return AArrayList_Get(arrayIntMap->elementList, index, ArrayIntMapElement*)->key;
}
static void* GetAt(ArrayIntMap* arrayIntMap, int index)
{
CheckIndex("GetAt");
return AArrayList_Get(arrayIntMap->elementList, index, ArrayIntMapElement*)->valuePtr;
}
static void* SetAt(ArrayIntMap* arrayIntMap, int index, void* valuePtr)
{
CheckIndex("SetAt");
return memcpy
(
AArrayList_Get(arrayIntMap->elementList, index, ArrayIntMapElement*)->valuePtr,
valuePtr,
(size_t) arrayIntMap->valueTypeSize
);
}
static void RemoveAt(ArrayIntMap* arrayIntMap, int index)
{
CheckIndex("RemoveAt");
free
(
AArrayList_Get(arrayIntMap->elementList, index, ArrayIntMapElement*)
);
AArrayList->Remove(arrayIntMap->elementList, index);
}
static void Release(ArrayIntMap* arrayIntMap)
{
for (int i = 0; i < arrayIntMap->elementList->size; ++i)
{
free
(
AArrayList_Get(arrayIntMap->elementList, i, ArrayIntMapElement*)
);
}
AArrayList->Release(arrayIntMap->elementList);
}
static void InitWithCapacity(int valueTypeSize, int capacity, ArrayIntMap* outArrayIntMap)
{
if (capacity == 0)
{
AArrayList->Init(sizeof(ArrayIntMapElement*), outArrayIntMap->elementList);
}
else
{
AArrayList->InitWithCapacity(sizeof(ArrayIntMapElement*), capacity, outArrayIntMap->elementList);
}
outArrayIntMap->valueTypeSize = valueTypeSize;
}
static ArrayIntMap* CreateWithCapacity(int valueTypeSize, int capacity)
{
ArrayIntMap* arrayIntMap = malloc(sizeof(ArrayIntMap));
InitWithCapacity(valueTypeSize, capacity, arrayIntMap);
return arrayIntMap;
}
static void Init(int valueTypeSize, ArrayIntMap* outArrayIntMap)
{
InitWithCapacity(valueTypeSize, 0, outArrayIntMap);
}
static ArrayIntMap* Create(int valueTypeSize)
{
return CreateWithCapacity(valueTypeSize, 0);
}
struct AArrayIntMap AArrayIntMap[1] =
{{
Create,
Init,
CreateWithCapacity,
InitWithCapacity,
Release,
TryPut,
Get,
TrySet,
TryRemove,
Clear,
InsertAt,
GetIndex,
GetKey,
GetAt,
SetAt,
RemoveAt,
}};
#undef CheckIndex
#undef CheckInsertIndex