@@ -41,14 +41,17 @@ extern "C" {
41
41
#ifndef free
42
42
extern void free (void * ptr );
43
43
#endif
44
- return size ? (realloc (ptr , size )) : (free (ptr ),NULL );
44
+ return size ? (realloc (ptr , size )) : (free (ptr ),( void * ) NULL );
45
45
}
46
46
#endif
47
47
48
48
49
49
//------------------------------------------------------------------------------
50
50
51
51
52
+ #define array_t (T ) T*
53
+
54
+
52
55
// void array_alloc(T*& a, size_t capacity, void (*destructor)(T* begin, T* end))
53
56
#define array_alloc (a , capacity , destructor ) \
54
57
(_array_alloc(_array_ptr((a)), (capacity) * _array_stride((a)), (array_allocator), (_array_destructor_t)(destructor)))
@@ -157,78 +160,38 @@ An assertion will fail if the array is empty.
157
160
@hideinitializer **/
158
161
159
162
160
- // T& array_append(T*& a )
161
- #define array_append (a ) \
162
- ((a)[ _array_append(_array_ptr((a)), _array_stride((a))) / _array_stride((a)) ] )
163
+ // void array_append(T*& array, T value )
164
+ #define array_append (a , v ) \
165
+ ( _array_append(_array_ptr((a)), _array_stride((a))), array_back(a) = v )
163
166
/**< Appends a single element to the dynamic array, allocating additional
164
- storage if necessary. The appended element is uninitialized. Returns a
165
- reference to the appended element.
167
+ storage if necessary.
166
168
167
169
@code{.c}
168
170
array_t(int) ia = NULL;
169
171
array_alloc(ia, 16, NULL);
170
172
// ...
171
- array_append(ia) = 123; // assign value 123 to ia[0]
173
+ array_append(ia, 123);
172
174
assert(ia[0] == 123);
173
175
@endcode
174
176
@hideinitializer **/
175
177
176
178
177
- // T* array_append_n(T*& a, size_t count)
178
- #define array_append_n (a , count ) \
179
- ((a) + _array_append(_array_ptr((a)), _array_offset((a), (count))) / _array_stride((a)))
180
- /**< Appends count elements to the dynamic array, allocating additional storage
181
- if necessary. Appended elements are uninitialized. Returns a pointer to the
182
- first appended element.
183
-
184
- @code{.c}
185
- array_t(int) ia = NULL;
186
- array_alloc(ia, 16, NULL);
187
- // ...
188
- const size_t count = 3;
189
- int* itr = array_append_n(ia, count);
190
- int* end = itr + count;
191
- for (; itr < end; ++itr) { *itr = 0; }
192
- @endcode
193
- @hideinitializer **/
194
-
195
-
196
- // T& array_insert(T*& a, size_t index)
197
- #define array_insert (a , index ) \
198
- ((a)[ _array_insert(_array_ptr((a)), _array_offset((a), (index)), _array_stride((a))) / _array_stride((a)) ])
179
+ // void array_insert(T*& a, size_t index, T value)
180
+ #define array_insert (a , index , v ) \
181
+ ( _array_insert(_array_ptr((a)), _array_offset((a), (index)), _array_stride((a))), (a)[index] = v )
199
182
/**< Inserts a single element at the provided index, allocating additional
200
- storage if necessary. The inserted element is uninitialized. Returns a
201
- reference to the inserted element.
183
+ storage if necessary.
202
184
203
185
@code{.c}
204
186
array_t(int) ia = NULL;
205
187
array_alloc(ia, 16, NULL);
206
188
// ...
207
- array_insert(ia, 1) = 123; // assign value 123 to ia[0]
189
+ array_insert(ia, 1, 123);
208
190
assert(ia[1] == 123);
209
191
@endcode
210
192
@hideinitializer **/
211
193
212
194
213
- // T* array_insert_n(T*& a, sizet index, size_t count)
214
- #define array_insert_n (a , index , count ) \
215
- ((a) + _array_insert(_array_ptr((a)), _array_offset((a), (index)), _array_offset((a), (count))) / _array_stride((a)))
216
- /**< Inserts count elements into the dynamic array starting at index and
217
- allocating additional storage if necessary. Appended elements are
218
- uninitialized. Returns a pointer to the first appended element.
219
-
220
- @code{.c}
221
- array_t(int) ia = NULL;
222
- array_alloc(ia, 16, NULL);
223
- // ...
224
- const size_t count = 3;
225
- int* itr = array_insert_n(ia, 0, count);
226
- int* end = itr + count;
227
- for (; itr < end; ++itr) { *itr = 0; }
228
- @endcode
229
- @hideinitializer **/
230
-
231
-
232
195
// void array_remove(T*& a, size_t index)
233
196
#define array_remove (a , index ) \
234
197
(_array_remove(_array_ptr((a)), _array_offset((a), (index)), _array_stride((a))))
@@ -266,7 +229,7 @@ to the array's destructor if it is not NULL.
266
229
// T* array_begin(T* array)
267
230
#define array_begin (a ) (a)
268
231
/**< Returns a pointer to the first element of the array, or NULL if the array
269
- is empty or NULL.
232
+ is NULL.
270
233
@hideinitializer **/
271
234
272
235
@@ -458,6 +421,7 @@ void _array_reserve(_array_t* a, const size_t capacity) {
458
421
_array_assert ((* a ), "array uninitialized" );
459
422
if (_array_capacity (a ) < capacity ) {
460
423
_array_grow (a , _array_ceilpow2 (capacity ));
424
+ _array_assert (_array_capacity (a ) >= capacity , "_array_grow() failed" );
461
425
}
462
426
}
463
427
@@ -598,9 +562,4 @@ void _array_clear(_array_t* const a) {
598
562
599
563
#if __cplusplus
600
564
} // extern "C"
601
- #endif // __cplusplus
602
-
603
-
604
- #if array_inline
605
- #include "array.inl"
606
- #endif
565
+ #endif // __cplusplus
0 commit comments