2 * array.h - useful array handling header
4 * Copyright © 2009 Julien Danjou <julien@danjou.info>
5 * Copyright © 2008 Pierre Habouzit <madcoder@debian.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #ifndef AWESOME_COMMON_ARRAY_H
24 #define AWESOME_COMMON_ARRAY_H
28 #include "common/util.h"
30 /** Common array type */
31 #define ARRAY_TYPE(type_t, pfx) \
32 typedef struct pfx##_array_t { \
37 #define foreach(var, array) \
38 for(int __foreach_index_##var = 0; \
39 __foreach_index_##var < (array).len; \
40 __foreach_index_##var = (array).len) \
41 for(typeof((array).tab) var = &(array).tab[__foreach_index_##var]; \
42 (__foreach_index_##var < (array).len) && \
43 (var = &(array).tab[__foreach_index_##var]); \
44 ++__foreach_index_##var)
46 /** Common array functions */
47 #define ARRAY_COMMON_FUNCS(type_t, pfx, dtor) \
48 static inline pfx##_array_t * pfx##_array_new(void) { \
49 return p_new(pfx##_array_t, 1); \
51 static inline void pfx##_array_init(pfx##_array_t *arr) { \
54 static inline void pfx##_array_wipe(pfx##_array_t *arr) { \
55 for (int i = 0; i < arr->len; i++) { \
58 p_delete(&arr->tab); \
60 static inline void pfx##_array_delete(pfx##_array_t **arrp) { \
62 pfx##_array_wipe(*arrp); \
67 static inline void pfx##_array_grow(pfx##_array_t *arr, int newlen) { \
68 p_grow(&arr->tab, newlen, &arr->size); \
71 pfx##_array_splice(pfx##_array_t *arr, int pos, int len, \
72 type_t items[], int count) \
74 assert (pos >= 0 && len >= 0 && count >= 0); \
75 assert (pos <= arr->len && pos + len <= arr->len); \
77 pfx##_array_grow(arr, arr->len + count - len); \
78 memmove(arr->tab + pos + count, arr->tab + pos + len, \
79 (arr->len - pos - len) * sizeof(*items)); \
80 arr->len += count - len; \
82 memcpy(arr->tab + pos, items, count * sizeof(*items)); \
84 static inline type_t pfx##_array_take(pfx##_array_t *arr, int pos) { \
85 type_t res = arr->tab[pos]; \
86 pfx##_array_splice(arr, pos, 1, NULL, 0); \
89 static inline int pfx##_array_indexof(pfx##_array_t *arr, type_t *e) \
91 return e - arr->tab; \
93 static inline type_t pfx##_array_remove(pfx##_array_t *arr, type_t *e) \
95 return pfx##_array_take(arr, pfx##_array_indexof(arr, e)); \
98 /** Non-ordered array functions */
99 #define ARRAY_FUNCS(type_t, pfx, dtor) \
100 ARRAY_COMMON_FUNCS(type_t, pfx, dtor) \
102 pfx##_array_push(pfx##_array_t *arr, type_t e) \
104 pfx##_array_splice(arr, 0, 0, &e, 1); \
106 static inline void pfx##_array_append(pfx##_array_t *arr, type_t e) { \
107 pfx##_array_splice(arr, arr->len, 0, &e, 1); \
110 /** Binary ordered array functions */
111 #define BARRAY_FUNCS(type_t, pfx, dtor, cmp) \
112 ARRAY_COMMON_FUNCS(type_t, pfx, dtor) \
114 pfx##_array_insert(pfx##_array_t *arr, type_t e) \
116 int l = 0, r = arr->len; \
119 int i = (r + l) / 2; \
120 switch(cmp(&e, &arr->tab[i])) \
132 pfx##_array_splice(arr, r, 0, &e, 1); \
134 static inline type_t * \
135 pfx##_array_lookup(pfx##_array_t *arr, type_t *e) \
137 return bsearch(e, arr->tab, arr->len, sizeof(type_t), cmp); \
140 #define DO_ARRAY(type_t, pfx, dtor) \
141 ARRAY_TYPE(type_t, pfx) \
142 ARRAY_FUNCS(type_t, pfx, dtor)
144 #define DO_BARRAY(type_t, pfx, dtor, cmp) \
145 ARRAY_TYPE(type_t, pfx) \
146 BARRAY_FUNCS(type_t, pfx, dtor, cmp)
149 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80