Measure the time a main loop iteration takes
[awesome.git] / common / array.h
blob8d3a4e1103b1102bad005fca5934be6f23fe28e3
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 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); \
50 } \
51 static inline void pfx##_array_init(pfx##_array_t *arr) { \
52 p_clear(arr, 1); \
53 } \
54 static inline void pfx##_array_wipe(pfx##_array_t *arr) { \
55 for (int i = 0; i < arr->len; i++) { \
56 dtor(&arr->tab[i]); \
57 } \
58 p_delete(&arr->tab); \
59 } \
60 static inline void pfx##_array_delete(pfx##_array_t **arrp) { \
61 if (*arrp) { \
62 pfx##_array_wipe(*arrp); \
63 p_delete(arrp); \
64 } \
65 } \
67 static inline void pfx##_array_grow(pfx##_array_t *arr, int newlen) { \
68 p_grow(&arr->tab, newlen, &arr->size); \
69 } \
70 static inline void \
71 pfx##_array_splice(pfx##_array_t *arr, int pos, int len, \
72 type_t items[], int count) \
73 { \
74 assert (pos >= 0 && len >= 0 && count >= 0); \
75 assert (pos <= arr->len && pos + len <= arr->len); \
76 if (len != count) { \
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; \
81 } \
82 memcpy(arr->tab + pos, items, count * sizeof(*items)); \
83 } \
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); \
87 return res; \
88 } \
89 static inline int pfx##_array_indexof(pfx##_array_t *arr, type_t *e) \
90 { \
91 return e - arr->tab; \
92 } \
93 static inline type_t pfx##_array_remove(pfx##_array_t *arr, type_t *e) \
94 { \
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) \
101 static inline void \
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) \
113 static inline void \
114 pfx##_array_insert(pfx##_array_t *arr, type_t e) \
116 int l = 0, r = arr->len; \
117 while(l < r) \
119 int i = (r + l) / 2; \
120 int res = cmp(&e, &arr->tab[i]); \
121 if(res == 0) \
122 return; /* Already added, ignore */ \
123 if(res < 0) \
124 r = i; \
125 else \
126 l = i + 1; \
128 pfx##_array_splice(arr, r, 0, &e, 1); \
130 static inline type_t * \
131 pfx##_array_lookup(pfx##_array_t *arr, type_t *e) \
133 return bsearch(e, arr->tab, arr->len, sizeof(type_t), cmp); \
136 #define DO_ARRAY(type_t, pfx, dtor) \
137 ARRAY_TYPE(type_t, pfx) \
138 ARRAY_FUNCS(type_t, pfx, dtor)
140 #define DO_BARRAY(type_t, pfx, dtor, cmp) \
141 ARRAY_TYPE(type_t, pfx) \
142 BARRAY_FUNCS(type_t, pfx, dtor, cmp)
144 #endif
145 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80