style cleanup: format 'for' loop macros the same as for loops, some renaming to BLI_a...
[blender.git] / source / blender / blenlib / BLI_array.h
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17  *
18  * The Original Code is Copyright (C) 2008 Blender Foundation.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): Joseph Eagar.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /*
29  * this library needs to be changed to not use macros quite so heavily,
30  * and to be more of a complete array API.  The way arrays are
31  * exposed to client code as normal C arrays is very useful though, imho.
32  * it does require some use of macros, however.
33  *
34  * anyway, it's used a bit too heavily to simply rewrite as a
35  * more "correct" solution without macros entirely.  I originally wrote this
36  * to be very easy to use, without the normal pain of most array libraries.
37  * This was especially helpful when it came to the massive refactors necessary
38  * for bmesh, and really helped to speed the process up. - joeedh
39  *
40  * little array macro library.  example of usage:
41  *
42  * int *arr = NULL;
43  * BLI_array_declare(arr);
44  * int i;
45  *
46  * for (i=0; i<10; i++) {
47  *     BLI_array_grow_one(arr);
48  *          arr[i] = something;
49  * }
50  * BLI_array_free(arr);
51  *
52  * arrays are buffered, using double-buffering (so on each reallocation,
53  * the array size is doubled).  supposedly this should give good Big Oh
54  * behavior, though it may not be the best in practice.
55  */
56
57 #define BLI_array_declare(arr)                                                \
58         int   _##arr##_count = 0;                                                 \
59         void *_##arr##_tmp;                                                       \
60         void *_##arr##_static = NULL
61
62 /* this will use stack space, up to maxstatic array elements, before
63  * switching to dynamic heap allocation */
64 #define BLI_array_staticdeclare(arr, maxstatic)                               \
65         int   _##arr##_count = 0;                                                 \
66         void *_##arr##_tmp;                                                       \
67         char  _##arr##_static[maxstatic * sizeof(arr)]
68
69
70 /* this returns the entire size of the array, including any buffering. */
71 #define BLI_array_totalsize_dyn(arr)  (                                       \
72         ((arr) == NULL) ?                                                         \
73             0 :                                                                   \
74             MEM_allocN_len(arr) / sizeof(*arr)                                    \
75 )
76
77
78 #define BLI_array_totalsize(arr)  (                                           \
79         (size_t)                                                                  \
80         (((void *)(arr) == (void *)_##arr##_static && (void *)(arr) != NULL) ?    \
81             (sizeof(_##arr##_static) / sizeof(*arr)) :                            \
82             BLI_array_totalsize_dyn(arr))                                         \
83 )
84
85
86 /* this returns the logical size of the array, not including buffering. */
87 #define BLI_array_count(arr) _##arr##_count
88
89 /* Grow the array by a fixed number of items. zeroes the new elements.
90  *
91  * Allow for a large 'num' value when the new size is more then double
92  * to allocate the exact sized array. */
93 #define _bli_array_grow_items(arr, num)  (                                    \
94         (BLI_array_totalsize(arr) >= _##arr##_count + num) ?                      \
95             (_##arr##_count += num) :                                             \
96             (                                                                     \
97                 (void) (_##arr##_tmp = MEM_callocN(                               \
98                         sizeof(*arr) * (num < _##arr##_count ?                    \
99                                         (_##arr##_count * 2 + 2) :                \
100                                         (_##arr##_count + num)),                  \
101                         #arr " " __FILE__ ":" STRINGIFY(__LINE__)                 \
102                         )                                                         \
103                         ),                                                        \
104                 (void) (arr && memcpy(_##arr##_tmp,                               \
105                                       arr,                                        \
106                                       sizeof(*arr) * _##arr##_count)              \
107                         ),                                                        \
108                 (void) (arr && ((void *)(arr) != (void*)_##arr##_static ?         \
109                         (MEM_freeN(arr), arr) :                                   \
110                         arr)                                                      \
111                         ),                                                        \
112                 (void) (arr = _##arr##_tmp                                        \
113                         ),                                                        \
114                 (_##arr##_count += num)                                           \
115             )                                                                     \
116 )
117
118 /* grow an array by a specified number of items */
119 #define BLI_array_grow_items(arr, num)  (                                     \
120         ((void *)(arr) == NULL && (void *)(_##arr##_static) != NULL) ?            \
121             ((arr = (void*)_##arr##_static), (_##arr##_count += num)) :           \
122             _bli_array_grow_items(arr, num)                                       \
123 )
124
125 /* returns length of array */
126 #define BLI_array_grow_one(arr)  BLI_array_grow_items(arr, 1)
127
128
129 /* appends an item to the array. */
130 #define BLI_array_append(arr, item)  (                                        \
131         (void) BLI_array_grow_one(arr),                                           \
132         (void) (arr[_##arr##_count - 1] = item)                                   \
133 )
134
135 /* appends an item to the array and returns a pointer to the item in the array.
136  * item is not a pointer, but actual data value.*/
137 #define BLI_array_append_r(arr, item)  (                                      \
138         (void) BLI_array_grow_one(arr),                                           \
139         (void) (arr[_##arr##_count - 1] = item),                                  \
140         (&arr[_##arr##_count - 1])                                                \
141 )
142
143 #define BLI_array_reserve(arr, num)                                           \
144         BLI_array_grow_items(arr, num), (void)(_##arr##_count -= (num))
145
146
147 #define BLI_array_free(arr)                                                   \
148         if (arr && (char *)arr != _##arr##_static) {                              \
149             BLI_array_fake_user(arr);                                             \
150             MEM_freeN(arr);                                                       \
151         }
152
153 #define BLI_array_pop(arr)  (                                                 \
154         (arr && _##arr##_count) ?                                                 \
155             arr[--_##arr##_count] :                                               \
156             NULL                                                                  \
157 )
158
159 /* resets the logical size of an array to zero, but doesn't
160  * free the memory. */
161 #define BLI_array_empty(arr)                                                  \
162         _##arr##_count = 0
163
164 /* set the count of the array, doesn't actually increase the allocated array
165  * size.  don't use this unless you know what you're doing. */
166 #define BLI_array_length_set(arr, count)                                      \
167         _##arr##_count = (count)
168
169 /* only to prevent unused warnings */
170 #define BLI_array_fake_user(arr)                                              \
171         (void)_##arr##_count,                                                     \
172         (void)_##arr##_tmp,                                                       \
173         (void)_##arr##_static
174
175
176 /* not part of the 'API' but handy funcs,
177  * same purpose as BLI_array_staticdeclare()
178  * but use when the max size is known ahead of time */
179 #define BLI_array_fixedstack_declare(arr, maxstatic, realsize, allocstr)      \
180         char _##arr##_static[maxstatic * sizeof(*(arr))];                         \
181         const int _##arr##_is_static = ((void *)_##arr##_static) != (             \
182             arr = ((realsize) <= maxstatic) ?                                     \
183                 (void *)_##arr##_static :                                         \
184                 MEM_mallocN(sizeof(*(arr)) * (realsize), allocstr)                \
185             )                                                                     \
186
187 #define BLI_array_fixedstack_free(arr)                                        \
188         if (_##arr##_is_static) MEM_freeN(arr)                                    \
189