tommyarrayof.h
Go to the documentation of this file.
1// SPDX-License-Identifier: BSD-2-Clause
2// Copyright (C) 2013 Andrea Mazzoleni
3
24#ifndef TOMMY_ARRAYOF_H
25#define TOMMY_ARRAYOF_H
26
27#include "tommytypes.h"
28
29#include <assert.h> /* for assert */
30#include <string.h> /* for memset */
31
32/******************************************************************************/
33/* array */
34
39#define TOMMY_ARRAYOF_BIT 6
40
45typedef struct tommy_arrayof_struct {
46 void* bucket[TOMMY_SIZE_BIT];
52
58TOMMY_API void tommy_arrayof_init(tommy_arrayof* array, tommy_size_t element_size);
59
64TOMMY_API void tommy_arrayof_done(tommy_arrayof* array);
65
74
81tommy_inline void tommy_arrayof_grow(tommy_arrayof* array, tommy_size_t size)
82{
83 if (size > array->count) {
84 array->count = size;
85
86 if (size > array->bucket_max)
87 tommy_arrayof_reserve(array, size);
88 }
89}
90
98TOMMY_API void tommy_arrayof_resize(tommy_arrayof* array, tommy_size_t size);
99
106tommy_inline void tommy_arrayof_clear(tommy_arrayof* array)
107{
108 tommy_arrayof_resize(array, 0);
109}
110
116TOMMY_API void tommy_arrayof_shrink(tommy_arrayof* array);
117
126tommy_inline void* tommy_arrayof_ref(tommy_arrayof* array, tommy_size_t pos)
127{
128 assert(pos < array->count);
129
130 /* get the highest bit set, in case of all 0, return 0 */
131 tommy_uint_t bsr = tommy_ilog2(pos | 1);
132
133 unsigned char* ptr = tommy_cast(unsigned char*, array->bucket[bsr]);
134
135 return ptr + pos * array->element_size;
136}
137
144tommy_inline void* tommy_arrayof_tail(tommy_arrayof* array)
145{
146 assert(array->count != 0);
147 return tommy_arrayof_ref(array, array->count - 1);
148}
149
155tommy_inline void* tommy_arrayof_insert_tail(tommy_arrayof* array)
156{
157 tommy_size_t pos = array->count;
158
159 ++array->count;
160 if (array->count > array->bucket_max)
161 tommy_arrayof_reserve(array, array->count);
162
163 return tommy_arrayof_ref(array, pos);
164}
165
172{
173 assert(array->count != 0);
174
175 tommy_size_t pos = array->count - 1;
176 void* ptr = tommy_arrayof_ref(array, pos);
177
178 /* keep unused slots zero so growing within capacity needs no initialization */
179 memset(ptr, 0, array->element_size);
180 array->count = pos;
181}
182
189{
190 return array->count == 0;
191}
192
199{
200 return array->count;
201}
202
209{
210 return array->bucket_max;
211}
212
221tommy_inline void tommy_arrayof_swap(tommy_arrayof* first, tommy_arrayof* second)
222{
223 /* keep record size with the storage it describes */
224 tommy_arrayof tmp = *first;
225 *first = *second;
226 *second = tmp;
227}
228
243
251TOMMY_API void tommy_arrayof_foreach_arg(tommy_arrayof* array, tommy_foreach_arg_func* func, void* arg);
252
259
260#endif
261
Array container type.
Definition: tommyarrayof.h:45
void * bucket[TOMMY_SIZE_BIT]
Dynamic array of buckets.
Definition: tommyarrayof.h:46
tommy_size_t count
Number of initialized elements in the array.
Definition: tommyarrayof.h:49
tommy_size_t bucket_max
Number of buckets.
Definition: tommyarrayof.h:48
tommy_uint_t bucket_bit
Bits used in the bit mask.
Definition: tommyarrayof.h:50
tommy_size_t element_size
Size of the stored element in bytes.
Definition: tommyarrayof.h:47
void * tommy_arrayof_ref(tommy_arrayof *array, tommy_size_t pos)
Gets a reference to the element at the specified position.
Definition: tommyarrayof.h:126
void tommy_arrayof_clear(tommy_arrayof *array)
Removes all elements, preserving the allocated capacity.
Definition: tommyarrayof.h:106
TOMMY_API tommy_size_t tommy_arrayof_memory_usage(const tommy_arrayof *array)
Gets the size of allocated memory.
tommy_size_t tommy_arrayof_size(const tommy_arrayof *array)
Gets the initialized size of the array.
Definition: tommyarrayof.h:198
void tommy_arrayof_swap(tommy_arrayof *first, tommy_arrayof *second)
Exchanges two initialized arrays without copying their elements.
Definition: tommyarrayof.h:221
void * tommy_arrayof_tail(tommy_arrayof *array)
Gets a reference to the last element.
Definition: tommyarrayof.h:144
void * tommy_arrayof_insert_tail(tommy_arrayof *array)
Adds a zero-initialized element and returns its reference.
Definition: tommyarrayof.h:155
TOMMY_API void tommy_arrayof_init(tommy_arrayof *array, tommy_size_t element_size)
Initializes the array.
TOMMY_API void tommy_arrayof_resize(tommy_arrayof *array, tommy_size_t size)
Changes the initialized size, preserving the common prefix.
TOMMY_API void tommy_arrayof_reserve(tommy_arrayof *array, tommy_size_t size)
Allocates space for at least the specified number of elements.
TOMMY_API void tommy_arrayof_foreach(tommy_arrayof *array, tommy_foreach_func *func)
Calls the specified function for each element in the array.
TOMMY_API void tommy_arrayof_shrink(tommy_arrayof *array)
Releases unused allocated memory.
void tommy_arrayof_remove_tail(tommy_arrayof *array)
Removes the last element without copying it.
Definition: tommyarrayof.h:171
struct tommy_arrayof_struct tommy_arrayof
Array container type.
tommy_bool_t tommy_arrayof_empty(const tommy_arrayof *array)
Checks whether the array is empty.
Definition: tommyarrayof.h:188
tommy_size_t tommy_arrayof_capacity(const tommy_arrayof *array)
Gets the number of elements that fit without further allocation.
Definition: tommyarrayof.h:208
TOMMY_API void tommy_arrayof_done(tommy_arrayof *array)
Deinitializes the array.
void tommy_arrayof_grow(tommy_arrayof *array, tommy_size_t size)
Grows the size up to the specified value.
Definition: tommyarrayof.h:81
TOMMY_API void tommy_arrayof_foreach_arg(tommy_arrayof *array, tommy_foreach_arg_func *func, void *arg)
Calls the specified function with an argument for each element in the array.
Generic types.
void tommy_foreach_func(void *elem)
Foreach function.
Definition: tommytypes.h:346
uint64_t tommy_size_t
Generic size_t type.
Definition: tommytypes.h:40
#define tommy_ilog2
Bit depth mapping.
Definition: tommytypes.h:573
void tommy_foreach_arg_func(void *arg, void *elem)
Foreach function with an argument.
Definition: tommytypes.h:353
tommy_uint32_t tommy_uint_t
Generic unsigned integer type.
Definition: tommytypes.h:61
int tommy_bool_t
Generic boolean type.
Definition: tommytypes.h:53