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(int, array, 10); // append
34 	kv_a(int, array, 20) = 5; // dynamic
35 	kv_A(array, 20) = 4; // static
36 	kv_destroy(array);
37 	return 0;
38 }
39 */
40 
41 /*
42   2008-09-22 (0.1.0):
43 
44 	* The initial version.
45 
46 */
47 
48 #ifndef AC_KVEC_H
49 #define AC_KVEC_H
50 
51 #include <stdlib.h>
52 
53 #define kv_roundup32(x) (--(x), (x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, ++(x))
54 
55 #define kvec_t(type) struct { size_t n, m; type *a; }
56 #define kv_init(v) ((v).n = (v).m = 0, (v).a = 0)
57 #define kv_destroy(v) free((v).a)
58 #define kv_A(v, i) ((v).a[(i)])
59 #define kv_pop(v) ((v).a[--(v).n])
60 #define kv_size(v) ((v).n)
61 #define kv_max(v) ((v).m)
62 
63 #define xrealloc(type, p,l) do {											\
64 		(p) = (type*)realloc((p), (l));										\
65 		if ((p) == NULL) {													\
66 			abort();														\
67 		}																	\
68 	} while (0)
69 
70 #define kv_resize(type, v, s)  do {											\
71 		(v).m = (s);														\
72 		xrealloc(type, (v).a, sizeof(type) * (v).m));						\
73 	} while (0)
74 
75 #define kv_grow_factor 1.5
76 #define kv_grow(type, v) do {												\
77 		(v).m = ((v).m > 1 ? (v).m * kv_grow_factor : 2); 					\
78 		xrealloc(type, (v).a, sizeof(type) * (v).m);						\
79 	} while (0)
80 
81 #define kv_reserve(type, v, c)  do {										\
82 		((v).m = ((v).m + (c)) * kv_grow_factor); 							\
83 		xrealloc(type, (v).a, sizeof(type) * (v).m);						\
84 	} while (0)
85 
86 #define kv_copy(type, v1, v0) do {											\
87 		if ((v1).m < (v0).n) kv_resize(type, v1, (v0).n);					\
88 		(v1).n = (v0).n;													\
89 		memcpy((v1).a, (v0).a, sizeof(type) * (v0).n);						\
90 	} while (0)																\
91 
92 #define kv_push(type, v, x) do {											\
93 		if ((v).n == (v).m) {												\
94 			kv_grow(type, v);												\
95 		}																	\
96 		(v).a[(v).n++] = (x);												\
97 	} while (0)
98 
99 #define kv_push_a(type, v, x, c) do {										\
100 		while ((v).m <= ((v).n + (c))) {									\
101 			kv_reserve(type, v, c);											\
102 		}																	\
103 		memcpy(&(v).a[(v).n], (x), (c) * sizeof(type));						\
104 		(v).n += (c);														\
105 	} while (0)
106 
107 #define kv_prepend(type, v, x) do {											\
108 	if ((v).n == (v).m) {													\
109 		kv_grow(type, v);													\
110 	}																		\
111 	memmove((v).a + 1, (v).a, sizeof(type) * (v).n);						\
112 	(v).a[0] = (x);															\
113 	(v).n ++;																\
114 } while (0)
115 
116 #define kv_concat(type, v1, v0) do {										\
117 	if ((v1).m < (v0).n + (v1).n) kv_resize(type, v1, (v0).n + (v1).n);		\
118 		memcpy((v1).a + (v1).n, (v0).a, sizeof(type) * ((v0).n + (v1).n));	\
119 		(v1).n = (v0).n + (v1).n;											\
120 	} while (0)
121 
122 #define kv_del(type, v, i) do {												\
123 	if ((i) < (v).n) {														\
124 		memmove((v).a + (i), (v).a + ((i) + 1), sizeof(type) * ((v).n - (i) - 1)); \
125 		(v).n --;															\
126 	}																		\
127 } while (0)
128 
129 #endif
130