xref: /freebsd/contrib/libucl/klib/kvec.h (revision 8881d206f4e68b564c2c5f50fc717086fc3e827a)
1 /* The MIT License
2 
3    Copyright (c) 2008, by Attractive Chaos <attractor@live.co.uk>
4 
5    Permission is hereby granted, free of charge, to any person obtaining
6    a copy of this software and associated documentation files (the
7    "Software"), to deal in the Software without restriction, including
8    without limitation the rights to use, copy, modify, merge, publish,
9    distribute, sublicense, and/or sell copies of the Software, and to
10    permit persons to whom the Software is furnished to do so, subject to
11    the following conditions:
12 
13    The above copyright notice and this permission notice shall be
14    included in all copies or substantial portions of the Software.
15 
16    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20    BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21    ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22    CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23    SOFTWARE.
24 */
25 
26 /*
27   An example:
28 
29 #include "kvec.h"
30 int main() {
31 	kvec_t(int) array;
32 	kv_init(array);
33 	kv_push_safe(int, array, 10, e0); // append
34 	kv_a(int, array, 20) = 5; // dynamic
35 	kv_A(array, 20) = 4; // static
36 	kv_destroy(array);
37 	return 0;
38 e0:
39 	return 1;
40 }
41 */
42 
43 /*
44   2008-09-22 (0.1.0):
45 
46 	* The initial version.
47 
48 */
49 
50 #ifndef AC_KVEC_H
51 #define AC_KVEC_H
52 
53 #include <stdlib.h>
54 
55 #define kv_roundup32(x) (--(x), (x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, ++(x))
56 
57 #define kvec_t(type) struct { size_t n, m; type *a; }
58 #define kv_init(v) ((v).n = (v).m = 0, (v).a = 0)
59 #define kv_destroy(v) free((v).a)
60 #define kv_A(v, i) ((v).a[(i)])
61 #define kv_pop(v) ((v).a[--(v).n])
62 #define kv_size(v) ((v).n)
63 #define kv_max(v) ((v).m)
64 
65 #define kv_resize_safe(type, v, s, el)  do { \
66 		type *_tp = (type*)realloc((v).a, sizeof(type) * (s)); \
67 		if (_tp == NULL) { \
68 			goto el; \
69 		} else { \
70 			(v).a = _tp; \
71 			(v).m = (s); \
72 		} \
73 	} while (0)
74 
75 #define kv_grow_factor 1.5
76 #define kv_grow_safe(type, v, el)  do { \
77 		size_t _ts = ((v).m > 1 ? (v).m * kv_grow_factor : 2); \
78 		type *_tp = (type*)realloc((v).a, sizeof(type) * _ts); \
79 		if (_tp == NULL) { \
80 			goto el; \
81 		} else { \
82 			(v).a = _tp; \
83 			(v).m = _ts; \
84 		} \
85 	} while (0)
86 
87 #define kv_copy_safe(type, v1, v0, el) do { \
88 		if ((v1).m < (v0).n) kv_resize_safe(type, v1, (v0).n, el); \
89 		(v1).n = (v0).n; \
90 		memcpy((v1).a, (v0).a, sizeof(type) * (v0).n); \
91 	} while (0)
92 
93 #define kv_push_safe(type, v, x, el) do { \
94 		if ((v).n == (v).m) { \
95 			kv_grow_safe(type, v, el); \
96 		} \
97 		(v).a[(v).n++] = (x); \
98 	} while (0)
99 
100 #define kv_prepend_safe(type, v, x, el) do { \
101 		if ((v).n == (v).m) { \
102 			kv_grow_safe(type, v, el); \
103 		} \
104 		memmove((v).a + 1, (v).a, sizeof(type) * (v).n); \
105 		(v).a[0] = (x);	\
106 		(v).n ++; \
107 	} while (0)
108 
109 #define kv_concat_safe(type, v1, v0, el) do { \
110 		if ((v1).m < (v0).n + (v1).n) \
111 			kv_resize_safe(type, v1, (v0).n + (v1).n, el); \
112 		memcpy((v1).a + (v1).n, (v0).a, sizeof(type) * (v0).n);	\
113 		(v1).n = (v0).n + (v1).n; \
114 	} while (0)
115 
116 #define kv_del(type, v, i) do {												\
117 	if ((i) < (v).n) {														\
118 		memmove((v).a + (i), (v).a + ((i) + 1), sizeof(type) * ((v).n - (i) - 1)); \
119 		(v).n --;															\
120 	}																		\
121 } while (0)
122 
123 /*
124  * Old (ENOMEM-unsafe) version of kv_xxx macros. Compat-only, not for use in
125  * the new library code.
126  */
127 
128 #define kv_resize(type, v, s)  ((v).m = (s), (v).a = (type*)realloc((v).a, sizeof(type) * (v).m))
129 
130 #define kv_grow(type, v)  ((v).m = ((v).m > 1 ? (v).m * kv_grow_factor : 2), \
131 		(v).a = (type*)realloc((v).a, sizeof(type) * (v).m))
132 
133 #define kv_copy(type, v1, v0) do {											\
134 		if ((v1).m < (v0).n) kv_resize(type, v1, (v0).n);					\
135 		(v1).n = (v0).n;													\
136 		memcpy((v1).a, (v0).a, sizeof(type) * (v0).n);						\
137 	} while (0)																\
138 
139 #define kv_push(type, v, x) do {											\
140 		if ((v).n == (v).m) {												\
141 			kv_grow(type, v);												\
142 		}																	\
143 		(v).a[(v).n++] = (x);												\
144 	} while (0)
145 
146 #define kv_prepend(type, v, x) do {											\
147 	if ((v).n == (v).m) {													\
148 		kv_grow(type, v);													\
149 	}																		\
150 	memmove((v).a + 1, (v).a, sizeof(type) * (v).n);							\
151 	(v).a[0] = (x);															\
152 	(v).n ++;																\
153 } while (0)
154 
155 #define kv_concat(type, v1, v0) do {										\
156 	if ((v1).m < (v0).n + (v1).n) kv_resize(type, v1, (v0).n + (v1).n);		\
157 		memcpy((v1).a + (v1).n, (v0).a, sizeof(type) * (v0).n);	\
158 		(v1).n = (v0).n + (v1).n;											\
159 	} while (0)
160 
161 #endif /* AC_KVEC_H */
162