ddfddf65dc7f45a875350a95433cc61c8d36e284
[retroray] / src / darray.h
1 /*
2 Deep Runner - 6dof shooter game for the SGI O2.
3 Copyright (C) 2023  John Tsiombikas <nuclear@mutantstargoat.com>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program.  If not, see <https://www.gnu.org/licenses/>.
17 */
18 #ifndef DYNAMIC_ARRAY_H_
19 #define DYNAMIC_ARRAY_H_
20
21 void *darr_alloc(int elem, int szelem);
22 void darr_free(void *da);
23 void *darr_resize_impl(void *da, int elem);
24 #define darr_resize(da, elem)   do { (da) = darr_resize_impl(da, elem); } while(0)
25
26 int darr_empty(void *da);
27 int darr_size(void *da);
28
29 void *darr_clear_impl(void *da);
30 #define darr_clear(da)                  do { (da) = darr_clear_impl(da); } while(0)
31
32 /* stack semantics */
33 void *darr_push_impl(void *da, void *item);
34 #define darr_push(da, item)             do { (da) = darr_push_impl(da, item); } while(0)
35 void *darr_pop_impl(void *da);
36 #define darr_pop(da)                    do { (da) = darr_pop_impl(da); } while(0)
37
38 /* Finalize the array. No more resizing is possible after this call.
39  * Use free() instead of dynarr_free() to deallocate a finalized array.
40  * Returns pointer to the finalized array.
41  * Complexity: O(n)
42  */
43 void *darr_finalize(void *da);
44
45 /* utility macros to push characters to a string. assumes and maintains
46  * the invariant that the last element is always a zero
47  */
48 #define darr_strpush(da, c) \
49         do { \
50                 char cnull = 0, ch = (char)(c); \
51                 (da) = dynarr_pop_impl(da); \
52                 (da) = dynarr_push_impl((da), &ch); \
53                 (da) = dynarr_push_impl((da), &cnull); \
54         } while(0)
55
56 #define darr_strpop(da) \
57         do { \
58                 char cnull = 0; \
59                 (da) = dynarr_pop_impl(da); \
60                 (da) = dynarr_pop_impl(da); \
61                 (da) = dynarr_push_impl((da), &cnull); \
62         } while(0)
63
64
65 #endif  /* DYNAMIC_ARRAY_H_ */