forked from mattiasgustavsson/libs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
array.h
322 lines (262 loc) · 12.8 KB
/
array.h
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
/*
------------------------------------------------------------------------------
Licensing information can be found at the end of the file.
------------------------------------------------------------------------------
array.h - v0.1 - Dynamic array library for C/C++.
Do this:
#define ARRAY_IMPLEMENTATION
before you include this file in *one* C/C++ file to create the implementation.
*/
#ifndef array_h
#define array_h
#ifndef ARRAY_BOOL_T
#define _CRT_NONSTDC_NO_DEPRECATE
#define _CRT_SECURE_NO_WARNINGS
#include <stdbool.h>
#define ARRAY_BOOL_T bool
#endif
#define array_t( type ) struct { int count; type* items; }
#define array_param_t( type ) void
#define array_create( type ) ARRAY_CAST( (void*)internal_array_create( sizeof( type ), NULL ) )
#define array_create_memctx( type, memctx ) ARRAY_CAST( (void*)internal_array_create( sizeof( type ), (memctx) ) )
#define array_destroy( array ) internal_array_destroy( (struct internal_array_t*) (array) )
#define array_add( array, item ) ARRAY_CAST( internal_array_add( (struct internal_array_t*) (array), (void*) (item), (int)sizeof( *item ) ) )
#define array_remove( array, index ) internal_array_remove( (struct internal_array_t*) (array), (index) )
#define array_remove_ordered( array, index ) internal_array_remove_ordered( (struct internal_array_t*) (array), (index) )
#define array_get( array, index, item ) internal_array_get( (struct internal_array_t*) (array), (index), (void*) (item) )
#define array_set( array, index, item ) internal_array_set( (struct internal_array_t*) (array), (index), (void*) (item) )
#define array_count( array ) internal_array_count( (struct internal_array_t*) (array) )
#define array_sort( array, compare ) internal_array_sort( (struct internal_array_t*) (array), (compare) )
#define array_bsearch( array, key, compare ) internal_array_bsearch( (struct internal_array_t*) (array), (void*) (key), (compare) )
#define array_find( array, item ) internal_array_find( (struct internal_array_t*) (array), (void*) (item) )
#define array_item( array, index ) ARRAY_CAST( internal_array_item( (struct internal_array_t*) (array), (index) ) )
// In C, a void* can be implicitly cast to any other kind of pointer, while in C++ you need an explicit cast. In most
// cases, the explicit cast works for both C and C++, but if we consider the case where we have nested structs, then
// the way you refer to them differs between C and C++ (in C++, `parent_type::nested_type`, in C just `nested_type`).
// In addition, with the automatic cast in C, it is possible to use unnamed nested structs and still dynamically
// allocate arrays of that type - this would be desirable when the code is compiled from C++ as well.
// This VOID_CAST macro allows for automatic cast from void* in C++. In C, it does nothing, but for C++ it uses a
// simple template function to define a cast-to-anything operator.
// Use like this:
// struct {
// struct {
// int x;
// } *nested;
// } parent;
// parent.nested = VOID_CAST( malloc( sizeof( *parent.nested ) * count ) );
//
#ifndef ARRAY_CAST
#ifdef __cplusplus
struct array_cast {
inline array_cast( void* x_ ) : x( x_ ) { }
inline array_cast( void const* x_ ) : x( (void*) x_ ) { }
template< typename T > inline operator T() { return (T)x; } // cast to whatever requested
void* x;
};
#define ARRAY_CAST( x ) array_cast( x )
#else
#define ARRAY_CAST( x ) x
#endif
#endif
struct internal_array_t;
struct internal_array_t* internal_array_create( int item_size, void* memctx );
void internal_array_destroy( struct internal_array_t* array );
void* internal_array_add( struct internal_array_t* array, void* item, int item_size );
void internal_array_remove( struct internal_array_t* array, int index );
void internal_array_remove_ordered( struct internal_array_t* array, int index );
ARRAY_BOOL_T internal_array_get( struct internal_array_t* array, int index, void* item );
ARRAY_BOOL_T internal_array_set( struct internal_array_t* array, int index, void const* item );
int internal_array_count( struct internal_array_t* array );
void internal_array_sort( struct internal_array_t* array, int (*compare)( void const*, void const* ) );
int internal_array_bsearch( struct internal_array_t* array, void* key, int (*compare)( void const*, void const* ) );
int internal_array_find( struct internal_array_t* array, void* item );
void* internal_array_item( struct internal_array_t* array, int index );
#endif /* array_h */
/*
----------------------
IMPLEMENTATION
----------------------
*/
#ifdef ARRAY_IMPLEMENTATION
#undef ARRAY_IMPLEMENTATION
#ifndef ARRAY_ASSERT
#define _CRT_NONSTDC_NO_DEPRECATE
#define _CRT_SECURE_NO_WARNINGS
#include <assert.h>
#define ARRAY_ASSERT( condition, message ) assert( condition && message );
#endif
#ifndef ARRAY_MALLOC
#define _CRT_NONSTDC_NO_DEPRECATE
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#define ARRAY_MALLOC( ctx, size ) ( malloc( size ) )
#define ARRAY_FREE( ctx, ptr ) ( free( ptr ) )
#endif
#ifndef ARRAY_MEMCPY
#define _CRT_NONSTDC_NO_DEPRECATE
#define _CRT_SECURE_NO_WARNINGS
#include <string.h>
#define ARRAY_MEMCPY( dst, src, cnt ) ( memcpy( (dst), (src), (cnt) ) )
#endif
#ifndef ARRAY_MEMMOVE
#define _CRT_NONSTDC_NO_DEPRECATE
#define _CRT_SECURE_NO_WARNINGS
#include <string.h>
#define ARRAY_MEMMOVE( dst, src, cnt ) ( memcpy( (dst), (src), (cnt) ) )
#endif
#ifndef ARRAY_MEMCMP
#define _CRT_NONSTDC_NO_DEPRECATE
#define _CRT_SECURE_NO_WARNINGS
#include <string.h>
#define ARRAY_MEMCMP( a, b, cnt ) ( memcmp( (a), (b), (cnt) ) )
#endif
#ifndef ARRAY_QSORT
#define _CRT_NONSTDC_NO_DEPRECATE
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#define ARRAY_QSORT( base, num, size, cmp ) ( qsort( (base), (num), (size), (cmp) ) )
#endif
#ifndef ARRAY_BSEARCH
#define _CRT_NONSTDC_NO_DEPRECATE
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#define ARRAY_BSEARCH( key, base, num, size, cmp ) ( bsearch( (key), (base), (num), (size), (cmp) ) )
#endif
struct internal_array_t {
int count;
void* items;
void* memctx;
int item_size;
int capacity;
};
struct internal_array_t* internal_array_create( int item_size, void* memctx ) {
struct internal_array_t* array = (struct internal_array_t*) ARRAY_MALLOC( memctx, sizeof( struct internal_array_t ) );
array->memctx = memctx;
array->item_size = item_size;
array->capacity = 256;
array->count = 0;
array->items = ARRAY_MALLOC( memctx, (size_t) array->capacity * item_size );
return array;
}
void internal_array_destroy( struct internal_array_t* array ) {
ARRAY_FREE( array->memctx, array->items );
ARRAY_FREE( array->memctx, array );
}
void* internal_array_add( struct internal_array_t* array, void* item, int item_size ) {
ARRAY_ASSERT( item_size == array->item_size, "Invalid item" );
if( array->count >= array->capacity ) {
array->capacity *= 2;
void* items = array->items;
array->items = ARRAY_MALLOC( array->memctx, (size_t) array->capacity * array->item_size );
ARRAY_MEMCPY( array->items, items, (size_t)array->count * array->item_size );
ARRAY_FREE( array->memctx, items );
}
ARRAY_MEMCPY( (void*)( ( (uintptr_t) array->items ) + array->count * array->item_size ), item,
(size_t)array->item_size );
++array->count;
return (void*)( ( (uintptr_t) array->items ) + ( array->count - 1 ) * array->item_size );
}
void internal_array_remove( struct internal_array_t* array, int index ) {
if( index >= 0 && index < array->count ) {
--array->count;
ARRAY_MEMMOVE( (void*)( ( (uintptr_t) array->items ) + index * array->item_size ),
(void*)( ( (uintptr_t) array->items ) + array->count * array->item_size ), (size_t) array->item_size );
}
}
void internal_array_remove_ordered( struct internal_array_t* array, int index ) {
if( index >= 0 && index < array->count ) {
--array->count;
ARRAY_MEMMOVE( (void*)( ( (uintptr_t) array->items ) + index * array->item_size ),
(void*)( ( (uintptr_t) array->items ) + ( index + 1 ) * array->item_size ),
(size_t)array->item_size * ( array->count - index ) );
}
}
ARRAY_BOOL_T internal_array_get( struct internal_array_t* array, int index, void* item ) {
ARRAY_BOOL_T result = index >= 0 && index < array->count;
if( result ) {
ARRAY_MEMCPY( item, (void*)( ( (uintptr_t) array->items ) + index * array->item_size ),
(size_t) array->item_size );
}
return result;
}
ARRAY_BOOL_T internal_array_set( struct internal_array_t* array, int index, void const* item ) {
ARRAY_BOOL_T result = index >= 0 && index < array->count;
if( result ) {
ARRAY_MEMCPY( (void*)( ( (uintptr_t) array->items ) + index * array->item_size ), item,
(size_t) array->item_size );
}
return result;
}
int internal_array_count( struct internal_array_t* array ) {
int count = array->count;
return count;
}
void internal_array_sort( struct internal_array_t* array, int (*compare)( void const*, void const* ) ) {
ARRAY_QSORT( array->items, (size_t) array->count, (size_t) array->item_size, compare );
}
int internal_array_bsearch( struct internal_array_t* array, void* key, int (*compare)( void const*, void const* ) ) {
void* item = ARRAY_BSEARCH( key, array->items, (size_t) array->count, (size_t) array->item_size, compare );
int result = -1;
if( item ) {
result = (int)( ( ((uintptr_t)item) - ((uintptr_t)array->items) ) / array->item_size );
}
return result;
}
int internal_array_find( struct internal_array_t* array, void* item ) {
for( int i = 0; i < array->count; ++i ) {
if( ARRAY_MEMCMP( (void*)( ( (uintptr_t) array->items ) + i * array->item_size ), item,
(size_t) array->item_size) == 0 ) {
return i;
}
}
return -1;
}
void* internal_array_item( struct internal_array_t* array, int index ) {
if( index >= 0 && index < array->count ) {
return (void*)( ( (uintptr_t) array->items ) + index * array->item_size );
} else {
return NULL;
}
}
#endif /* ARRAY_IMPLEMENTATION */
/*
------------------------------------------------------------------------------
This software is available under 2 licenses - you may choose the one you like.
------------------------------------------------------------------------------
ALTERNATIVE A - MIT License
Copyright (c) 2022 Mattias Gustavsson
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
------------------------------------------------------------------------------
ALTERNATIVE B - Public Domain (www.unlicense.org)
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
software, either in source code form or as a compiled binary, for any purpose,
commercial or non-commercial, and by any means.
In jurisdictions that recognize copyright laws, the author or authors of this
software dedicate any and all copyright interest in the software to the public
domain. We make this dedication for the benefit of the public at large and to
the detriment of our heirs and successors. We intend this dedication to be an
overt act of relinquishment in perpetuity of all present and future rights to
this software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
------------------------------------------------------------------------------
*/