tommyarrayblk.h
Go to the documentation of this file.
1// SPDX-License-Identifier: BSD-2-Clause
2// Copyright (C) 2013 Andrea Mazzoleni
3
15#ifndef TOMMY_ARRAYBLK_H
16#define TOMMY_ARRAYBLK_H
17
18#include "tommytypes.h"
19
20#include <assert.h> /* for assert */
21
22/******************************************************************************/
23/* array */
24
28#define TOMMY_ARRAYBLK_BIT 6
29
33#define TOMMY_ARRAYBLK_SIZE (4 * 1024)
34
39typedef struct tommy_arrayblk_struct {
40 void*** block;
45
50TOMMY_API void tommy_arrayblk_init(tommy_arrayblk* array);
51
56TOMMY_API void tommy_arrayblk_done(tommy_arrayblk* array);
57
66
73tommy_inline void tommy_arrayblk_grow(tommy_arrayblk* array, tommy_size_t size)
74{
75 if (size > array->count) {
76 array->count = size;
77
78 if (size > array->block_count * TOMMY_ARRAYBLK_SIZE)
79 tommy_arrayblk_reserve(array, size);
80 }
81}
82
91
99tommy_inline void tommy_arrayblk_clear(tommy_arrayblk* array)
100{
101 tommy_arrayblk_resize(array, 0);
102}
103
110
119tommy_inline void** tommy_arrayblk_ref(tommy_arrayblk* array, tommy_size_t pos)
120{
121 assert(pos < array->count);
122
123 return &array->block[pos / TOMMY_ARRAYBLK_SIZE][pos % TOMMY_ARRAYBLK_SIZE];
124}
125
134tommy_inline void tommy_arrayblk_set(tommy_arrayblk* array, tommy_size_t pos, void* element)
135{
136 *tommy_arrayblk_ref(array, pos) = element;
137}
138
147tommy_inline void* tommy_arrayblk_get(tommy_arrayblk* array, tommy_size_t pos)
148{
149 return *tommy_arrayblk_ref(array, pos);
150}
151
158tommy_inline void* tommy_arrayblk_tail(tommy_arrayblk* array)
159{
160 assert(array->count != 0);
161 return tommy_arrayblk_get(array, array->count - 1);
162}
163
169tommy_inline void tommy_arrayblk_insert_tail(tommy_arrayblk* array, void* element)
170{
171 tommy_size_t pos = array->count;
172
173 ++array->count;
174 if (array->count > array->block_count * TOMMY_ARRAYBLK_SIZE)
175 tommy_arrayblk_reserve(array, array->count);
176
177 tommy_arrayblk_set(array, pos, element);
178}
179
188{
189 assert(array->count != 0);
190
191 tommy_size_t pos = array->count - 1;
192 void** ptr = tommy_arrayblk_ref(array, pos);
193 void* element = *ptr;
194
195 /* keep unused slots zero so growing within capacity needs no initialization */
196 *ptr = 0;
197 array->count = pos;
198
199 return element;
200}
201
208{
209 return array->count == 0;
210}
211
218{
219 return array->count;
220}
221
228{
229 return array->block_count * TOMMY_ARRAYBLK_SIZE;
230}
231
240tommy_inline void tommy_arrayblk_swap(tommy_arrayblk* first, tommy_arrayblk* second)
241{
242 tommy_arrayblk tmp = *first;
243 *first = *second;
244 *second = tmp;
245}
246
260
268TOMMY_API void tommy_arrayblk_foreach_arg(tommy_arrayblk* array, tommy_foreach_arg_func* func, void* arg);
269
276
277#endif
278
Array container type.
Definition: tommyarrayblk.h:39
tommy_size_t block_count
Number of allocated blocks.
Definition: tommyarrayblk.h:41
tommy_size_t count
Number of initialized elements in the array.
Definition: tommyarrayblk.h:43
tommy_size_t block_capacity
Number of directory entries allocated.
Definition: tommyarrayblk.h:42
void *** block
Directory of blocks.
Definition: tommyarrayblk.h:40
void * tommy_arrayblk_remove_tail(tommy_arrayblk *array)
Removes and returns the last element.
Definition: tommyarrayblk.h:187
tommy_size_t tommy_arrayblk_size(const tommy_arrayblk *array)
Gets the initialized size of the array.
Definition: tommyarrayblk.h:217
void tommy_arrayblk_grow(tommy_arrayblk *array, tommy_size_t size)
Grows the size up to the specified value.
Definition: tommyarrayblk.h:73
tommy_bool_t tommy_arrayblk_empty(const tommy_arrayblk *array)
Checks whether the array is empty.
Definition: tommyarrayblk.h:207
TOMMY_API void tommy_arrayblk_done(tommy_arrayblk *array)
Deinitializes the array.
struct tommy_arrayblk_struct tommy_arrayblk
Array container type.
void tommy_arrayblk_insert_tail(tommy_arrayblk *array, void *element)
Grows and inserts a new element at the end of the array.
Definition: tommyarrayblk.h:169
void ** tommy_arrayblk_ref(tommy_arrayblk *array, tommy_size_t pos)
Gets a reference to the element at the specified position.
Definition: tommyarrayblk.h:119
TOMMY_API void tommy_arrayblk_foreach(tommy_arrayblk *array, tommy_foreach_func *func)
Calls the specified function for each element in the array.
TOMMY_API tommy_size_t tommy_arrayblk_memory_usage(const tommy_arrayblk *array)
Gets the size of allocated memory.
void * tommy_arrayblk_get(tommy_arrayblk *array, tommy_size_t pos)
Gets the element at the specified position.
Definition: tommyarrayblk.h:147
TOMMY_API void tommy_arrayblk_shrink(tommy_arrayblk *array)
Releases unused allocated memory.
TOMMY_API void tommy_arrayblk_resize(tommy_arrayblk *array, tommy_size_t size)
Changes the initialized size, preserving the common prefix.
void tommy_arrayblk_swap(tommy_arrayblk *first, tommy_arrayblk *second)
Exchanges two initialized arrays without copying their elements.
Definition: tommyarrayblk.h:240
void tommy_arrayblk_clear(tommy_arrayblk *array)
Removes all elements, preserving the allocated capacity.
Definition: tommyarrayblk.h:99
tommy_size_t tommy_arrayblk_capacity(const tommy_arrayblk *array)
Gets the number of elements that fit without further allocation.
Definition: tommyarrayblk.h:227
TOMMY_API void tommy_arrayblk_init(tommy_arrayblk *array)
Initializes the array.
void tommy_arrayblk_set(tommy_arrayblk *array, tommy_size_t pos, void *element)
Sets the element at the specified position.
Definition: tommyarrayblk.h:134
TOMMY_API void tommy_arrayblk_foreach_arg(tommy_arrayblk *array, tommy_foreach_arg_func *func, void *arg)
Calls the specified function with an argument for each element in the array.
TOMMY_API void tommy_arrayblk_reserve(tommy_arrayblk *array, tommy_size_t size)
Allocates space for at least the specified number of elements.
#define TOMMY_ARRAYBLK_SIZE
Elements for each block.
Definition: tommyarrayblk.h:33
void * tommy_arrayblk_tail(tommy_arrayblk *array)
Gets the last element.
Definition: tommyarrayblk.h:158
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
void tommy_foreach_arg_func(void *arg, void *elem)
Foreach function with an argument.
Definition: tommytypes.h:353
int tommy_bool_t
Generic boolean type.
Definition: tommytypes.h:53