-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibex_array.c
executable file
·390 lines (303 loc) · 14 KB
/
libex_array.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/* -------------------------------------------------------------------------- */
/* (c) ali@balarabe.com [libex_array.c] */
/* -------------------------------------------------------------------------- */
#include "libex_c_precompiled.h"
#include "libex_array_.h"
#if defined INCLUDED_LIBEX_ARRAY_H
#include <string.h> /* for memset() */
#include "libex_.h"
#include "libex_call_.h"
#include "libex_debug_.h"
#include "libex_error_.h"
#include "libex_macro_.h"
#include "libex_stringc_.h"
#include "libex_type_.h"
#if defined __cplusplus_cli
#pragma unmanaged
#endif
#if defined _MSC_VER
#pragma warning (disable:4710) /* W:L4 function not inlined */
#endif
/* -------------------------------------------------------------------------- */
NAMESPACE(c_)
/* -------------------------------------------------------------------------- */
/* Array Structures: */
typedef struct _array_t {
size_t item_size; /* _array_t */
/* Number of bytes used by each */
/* item in the array. */
size_t cap; /* _array_t */
/* Allocated number of items, */
/* some of which may be unused. */
/* (always _cap >= me->count) */
size_t count; /* _array_t */
/* Actual number of items */
/* contained by the array. */
size_t grow_by; /* _array_t */
/* Number of items by which the array is */
/* to be grown when re-allocating memory. */
uint8_t* items; /* _array_t */
/* Pointer to allocated memory */
/* containing a sequence of items. */
}
_array_t;
/* -------------------------------------------------------------------------- */
/* Functions: Private */
#define _BASE_OBJ_WRAP_INC
#define _OBJ_HANDLE_TYPE array_t
#define _OBJ_INNER_TYPE _array_t
#define _OBJ_ALLOC_FN_UID UID(FFF665)
#define _OBJ_FREE_FN_UID UID(F0E495)
#define _OBJ_CONST_FN_UID UID(F888CB)
#define _OBJ_FN_UID UID(F0BA56)
#define _OBJ_WRAP_FN_UID UID(F695DF)
#include "libex_object_wrapper.inc.c"
/* -------------------------------------------------------------------------- */
/* Constructor: */
PUBLIC new_array_t Array_init( /*C*/
const size_t item_size_in_bytes_, /*-*/
const size_t capacity_, /*-*/
const size_t grow_by_ ) { /*-*/
GO (UID(F5DDCD));
new_array_t ret = { NULL };
/* validate arguments */
if (item_size_in_bytes_ < 1) {
ERR(_T("item_size_in_bytes_ < 1"), UID(ED4791));
} else if (capacity_ < 1) {
ERR(_T("capacity_ < 1"), UID(E53754));
} else {
_array_t* new_ob = _object_alloc();
new_ob->item_size = item_size_in_bytes_;
new_ob->cap = capacity_;
new_ob->count = 0;
new_ob->grow_by = grow_by_;
new_ob->items = NULL;
{
/* allocate initial space for key and items data */
const size_t item_size = sizeof(char_t*) + item_size_in_bytes_;
const size_t data_bytes = capacity_ * item_size;
if (data_bytes > 0) {
new_ob->items = MALLOC(data_bytes);
MEMSET(new_ob->items, 0x00, data_bytes);
}
}
ret = _object_wrap(new_ob);
}
RETURN(ret);
} /* Array_init */
/* -------------------------------------------------------------------------- */
/* Destructor: */
PUBLIC void Array_free( array_t* object_ ) { /*M*/
GO (UID(F7FB85));
if (object_ != NULL && object_->ob != NULL) {
Array_removeItems(object_);
}
_object_free(object_);
RETURN(NIL);
} /* Array_free */
/* -------------------------------------------------------------------------- */
/* Methods: Informative */
PUBLIC size_t Array_count( const array_t object_ ) { /*M*/
GO (UID(F63C14));
const _array_t* const me = _object_const(&object_, UID(E3885D));
const size_t ret = me->count;
RETURN(ret);
} /* Array_count */
PUBLIC size_t Array_getKeyIndex( /*M*/
const array_t object_, /*-*/
chars_t key_ ) { /*-*/
GO (UID(F24108));
const _array_t* const me = _object_const(&object_, UID(EA2944));
const size_t count = Array_count(object_);
const size_t item_size = sizeof(char_t*) /*key*/ + me->item_size;
chars_t key = CAST(chars_t, me->items);
size_t i = 0;
for (i = 0; i < count; i++) {
if (STRCMP_T(key_, key) == 0) {
RETURN(i);
}
key = CAST(chars_t, CAST(uint8_t*, key) + item_size);
}
RETURN(SIZE_MAX);
} /* Array_getKeyIndex */
PUBLIC bool Array_keyExists( /*M*/
const array_t object_, /*-*/
chars_t key_ ) { /*-*/
GO (UID(F43C2C));
const size_t index = Array_getKeyIndex(object_, key_);
const bool ret = (index != SIZE_MAX);
RETURN(ret);
} /* Array_keyExists */
/* -------------------------------------------------------------------------- */
/* Methods: Manipulative */
PUBLIC void Array_addItem( /*M*/
array_t* object_, /*-*/
const void* item_data_, /*-*/
const size_t item_size_in_bytes_, /*-*/
chars_t key_ ) { /*-*/
GO (UID(F4E2D4));
_array_t* const me = _object(object_, UID(EB8805));
const size_t item_size = sizeof(char_t*) /*key*/ + me->item_size;
UNUSED(key_);
if (item_size_in_bytes_ != me->item_size) {
ERR(_T("item_size_in_bytes_ != me->item_size"), UID(E8D2A9));
RETURN(NIL);
}
/* ensure enough capacity for adding an item (re-allocate data if needed) */
if ((me->count + 1) > me->cap || !me->items) {
const size_t old_cap = me->cap; /* calculate new capacity */
if (me->grow_by < 1) {
me->grow_by = GREATER(10, me->cap / 5);
}
me->cap += me->grow_by;
me->items = REALLOC(me->items, me->cap * item_size);
MEMSET(me->items + (old_cap * item_size), 0x00, /* wipe old mem */
(me->cap - old_cap) * item_size);
}
/* add item's data to array. */
me->count++;
{
uint8_t* item = me->items + (me->count - 1) * item_size;
{
/* store item's key: */
const size_t key_len = STRLEN_T(key_);
new_chars_t* key = CAST(new_chars_t*, item);
if (key_len == 0) {
*key = NULL;
} else {
*key = MALLOC((key_len + 1) * sizeof(char_t));
if (*key == NULL) {
WARN(_T("MALLOC() failed."), UID(E4CDC6));
}
STRCPY_S_T(*key, key_len + 1, key_);
}
item += sizeof(chars_t);
}
/* store item's data */
if (item_data_ == NULL) {
MEMSET(item, 0x00, me->item_size);
} else {
MEMCPY(item, item_data_, me->item_size);
}
}
RETURN(NIL);
} /* Array_addItem */
PUBLIC uint8_t* Array_getItemAt( /*M*/
array_t* object_, /*-*/
const size_t index_ ) { /*-*/
GO (UID(FA47DF));
_array_t* const me = _object(object_, UID(E832F3));
uint8_t* ret = NULL;
if (me->count == 0 || index_ > (me->count - 1)) {
WARN(_T("index_ out of range."), UID(ECB76B));
} else {
const size_t item_size = sizeof(char_t*) + me->item_size;
ret = sizeof(char_t*) + (me->items + index_ * item_size);
}
RETURN(ret);
} /* Array_getItemAt */
PUBLIC uint8_t* Array_getItemByKey( /*M*/
array_t* object_, /*-*/
chars_t key_ ) { /*-*/
GO (UID(FFD1C8));
uint8_t* ret = NULL;
const size_t index = Array_getKeyIndex(*object_, key_);
if (index == SIZE_MAX) {
WARN(_T("key_ not found."), UID(E575CB));
} else {
ret = Array_getItemAt(object_, index);
}
RETURN(ret);
} /* Array_getItemByKey */
PUBLIC uint8_t* Array_getLastItem( array_t* object_ ) { /*M*/
GO (UID(F82915));
_array_t* const me = _object(object_, UID(ECCF7C));
uint8_t* ret = NULL;
if (me->count > 0) {
const size_t item_size = sizeof(char_t*) + me->item_size;
ret = sizeof(char_t*) + (me->items + (me->count - 1) * item_size);
}
RETURN(ret);
} /* Array_getLastItem */
PUBLIC void Array_removeAt( /*M*/
array_t* object_, /*-*/
const size_t index_ ) { /*-*/
GO (UID(FFEF1F));
_array_t* const me = _object(object_, UID(E57813));
if (me->count == 0 || index_ > (me->count - 1)) {
WARN(_T("index_ out of range."), UID(E5393C));
} else {
const size_t item_size = sizeof(char_t*) /*key*/ + me->item_size;
const size_t bytes_size = (me->count - 1 - index_) * item_size;
const uint8_t* from = me->items + index_ * item_size;
uint8_t* to = me->items + (me->count - 1) * item_size;
MEMMOVE(to, from, bytes_size);
to = me->items + (me->count - 1) * item_size;
MEMSET(to, 0x00, bytes_size);
me->count--;
}
RETURN(NIL);
} /* Array_removeAt */
PUBLIC void Array_removeItemByKey( /*M*/
array_t* object_, /*-*/
chars_t key_ ) { /*-*/
GO (UID(F7C025));
const size_t index = Array_getKeyIndex(*object_, key_);
if (index == SIZE_MAX) {
WARN(_T("key_ not found."), UID(ECF8F0));
} else {
Array_removeAt(object_, index);
}
RETURN(NIL);
} /* Array_removeItemByKey */
PUBLIC void Array_removeItems( array_t* object_ ) { /*M*/
GO (UID(F37D92));
_array_t* const me = _object(object_, UID(EE3D6B));
/* release all the key strings */
if (me->count > 0) {
const size_t key_size = sizeof(char_t*) /*key*/ + me->item_size;
size_t i = me->count;
while (i--) {
new_chars_t* key = CAST(new_chars_t*, me->items);
if (*key) {
freeT(key);
}
key = CAST(new_chars_t*, CAST(uint8_t*, key) + key_size);
}
}
/* release the array data and the array structure */
if (me != NULL && me->items != NULL) {
MEMSET(me->items, 0xFF, me->cap * me->item_size);
FREE(me->items);
me->items = NULL;
me->cap = 0;
me->count = 0;
}
RETURN(NIL);
} /* Array_removeItems */
PUBLIC void Array_removeLast( array_t* object_ ) { /*M*/
GO (UID(FDDD44));
_array_t* const me = _object(object_, UID(E257C8));
if (me->count > 0) {
Array_removeAt(object_, me->count - 1);
}
RETURN(NIL);
} /* Array_removeLast */
PUBLIC void Array_resize( /*M*/
array_t* object_, /*-*/
const size_t new_count_ ) { /*-*/
GO (UID(F4C59E));
_array_t* const me = _object(object_, UID(E3BE36));
if (new_count_ > me->count) {
while (new_count_ > me->count) {
Array_addItem(object_, NULL, me->item_size, NULL);
}
} else if (new_count_ < me->count) {
while (new_count_ < me->count) {
Array_removeLast(object_);
}
}
RETURN(NIL);
} /* Array_resize */
END_NAMESPACE /*c_*/
#endif /*eof*/