tag: Improve tag property::index support (FS#1229)
[awesome.git] / common / array.h
blob72ab0da4405ac9779b2df010d780aaf435e537be
1 /*
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
26 #include <stdlib.h>
28 #include "common/util.h"
30 /** Common array type */
31 #define ARRAY_TYPE(type_t, pfx) \
32 typedef struct pfx##_array_t { \
33 type_t *tab; \
34 int len, size; \
35 } pfx##_array_t;
37 #define ARRAY_TYPE_EXTRA(type_t, pfx, extra) \
38 typedef struct pfx##_array_t { \
39 type_t *tab; \
40 int len, size; \
41 extra; \
42 } pfx##_array_t;
44 #define foreach(var, array) \
45 for(int __foreach_index_##var = 0; \
46 __foreach_index_##var < (array).len; \
47 __foreach_index_##var = (array).len) \
48 for(typeof((array).tab) var = &(array).tab[__foreach_index_##var]; \
49 (__foreach_index_##var < (array).len) && \
50 (var = &(array).tab[__foreach_index_##var]); \
51 ++__foreach_index_##var)
53 /** Common array functions */
54 #define ARRAY_COMMON_FUNCS(type_t, pfx, dtor) \
55 static inline pfx##_array_t * pfx##_array_new(void) { \
56 return p_new(pfx##_array_t, 1); \
57 } \
58 static inline void pfx##_array_init(pfx##_array_t *arr) { \
59 p_clear(arr, 1); \
60 } \
61 static inline void pfx##_array_wipe(pfx##_array_t *arr) { \
62 for (int i = 0; i < arr->len; i++) { \
63 dtor(&arr->tab[i]); \
64 } \
65 p_delete(&arr->tab); \
66 } \
67 static inline void pfx##_array_delete(pfx##_array_t **arrp) { \
68 if (*arrp) { \
69 pfx##_array_wipe(*arrp); \
70 p_delete(arrp); \
71 } \
72 } \
74 static inline void pfx##_array_grow(pfx##_array_t *arr, int newlen) { \
75 p_grow(&arr->tab, newlen, &arr->size); \
76 } \
77 static inline void \
78 pfx##_array_splice(pfx##_array_t *arr, int pos, int len, \
79 type_t items[], int count) \
80 { \
81 assert (pos >= 0 && len >= 0 && count >= 0); \
82 assert (pos <= arr->len && pos + len <= arr->len); \
83 if (len != count) { \
84 pfx##_array_grow(arr, arr->len + count - len); \
85 memmove(arr->tab + pos + count, arr->tab + pos + len, \
86 (arr->len - pos - len) * sizeof(*items)); \
87 arr->len += count - len; \
88 } \
89 memcpy(arr->tab + pos, items, count * sizeof(*items)); \
90 } \
91 static inline type_t pfx##_array_take(pfx##_array_t *arr, int pos) { \
92 type_t res = arr->tab[pos]; \
93 pfx##_array_splice(arr, pos, 1, NULL, 0); \
94 return res; \
95 } \
96 static inline int pfx##_array_indexof(pfx##_array_t *arr, type_t *e) \
97 { \
98 return e - arr->tab; \
99 } \
100 static inline type_t pfx##_array_remove(pfx##_array_t *arr, type_t *e) \
102 return pfx##_array_take(arr, pfx##_array_indexof(arr, e)); \
105 /** Non-ordered array functions */
106 #define ARRAY_FUNCS(type_t, pfx, dtor) \
107 ARRAY_COMMON_FUNCS(type_t, pfx, dtor) \
108 static inline void \
109 pfx##_array_push(pfx##_array_t *arr, type_t e) \
111 pfx##_array_splice(arr, 0, 0, &e, 1); \
113 static inline void pfx##_array_append(pfx##_array_t *arr, type_t e) { \
114 pfx##_array_splice(arr, arr->len, 0, &e, 1); \
117 /** Binary ordered array functions */
118 #define BARRAY_FUNCS(type_t, pfx, dtor, cmp) \
119 ARRAY_COMMON_FUNCS(type_t, pfx, dtor) \
120 static inline void \
121 pfx##_array_insert(pfx##_array_t *arr, type_t e) \
123 int l = 0, r = arr->len; \
124 while(l < r) \
126 int i = (r + l) / 2; \
127 int res = cmp(&e, &arr->tab[i]); \
128 if(res == 0) \
129 return; /* Already added, ignore */ \
130 if(res < 0) \
131 r = i; \
132 else \
133 l = i + 1; \
135 pfx##_array_splice(arr, r, 0, &e, 1); \
137 static inline type_t * \
138 pfx##_array_lookup(pfx##_array_t *arr, type_t *e) \
140 return bsearch(e, arr->tab, arr->len, sizeof(type_t), cmp); \
143 #define DO_ARRAY(type_t, pfx, dtor) \
144 ARRAY_TYPE(type_t, pfx) \
145 ARRAY_FUNCS(type_t, pfx, dtor)
147 #define DO_BARRAY(type_t, pfx, dtor, cmp) \
148 ARRAY_TYPE(type_t, pfx) \
149 BARRAY_FUNCS(type_t, pfx, dtor, cmp)
151 #endif
152 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80