swindow: fix draw_context_update for north and south orientation
[awesome.git] / common / array.h
blob16a9b4d810a7252079e624f17a84f4c9125fd90b
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 "common/util.h"
28 #define ARRAY_TYPE(type_t, pfx) \
29 typedef struct pfx##_array_t { \
30 type_t *tab; \
31 int len, size; \
32 } pfx##_array_t;
34 #define foreach(var, array) \
35 for(int __foreach_index_##var = 0; \
36 __foreach_index_##var < (array).len; \
37 __foreach_index_##var = (array).len) \
38 for(typeof((array).tab) var = &(array).tab[__foreach_index_##var]; \
39 (__foreach_index_##var < (array).len) && \
40 (var = &(array).tab[__foreach_index_##var]); \
41 ++__foreach_index_##var)
43 #define ARRAY_FUNCS(type_t, pfx, dtor) \
44 static inline pfx##_array_t * pfx##_array_new(void) { \
45 return p_new(pfx##_array_t, 1); \
46 } \
47 static inline void pfx##_array_init(pfx##_array_t *arr) { \
48 p_clear(arr, 1); \
49 } \
50 static inline void pfx##_array_wipe(pfx##_array_t *arr) { \
51 for (int i = 0; i < arr->len; i++) { \
52 dtor(&arr->tab[i]); \
53 } \
54 p_delete(&arr->tab); \
55 } \
56 static inline void pfx##_array_delete(pfx##_array_t **arrp) { \
57 if (*arrp) { \
58 pfx##_array_wipe(*arrp); \
59 p_delete(arrp); \
60 } \
61 } \
63 static inline void pfx##_array_grow(pfx##_array_t *arr, int newlen) { \
64 p_grow(&arr->tab, newlen, &arr->size); \
65 } \
66 static inline void \
67 pfx##_array_splice(pfx##_array_t *arr, int pos, int len, \
68 type_t items[], int count) \
69 { \
70 assert (pos >= 0 && len >= 0 && count >= 0); \
71 assert (pos <= arr->len && pos + len <= arr->len); \
72 if (len != count) { \
73 pfx##_array_grow(arr, arr->len + count - len); \
74 memmove(arr->tab + pos + count, arr->tab + pos + len, \
75 (arr->len - pos - len) * sizeof(*items)); \
76 arr->len += count - len; \
77 } \
78 memcpy(arr->tab + pos, items, count * sizeof(*items)); \
79 } \
80 static inline type_t pfx##_array_take(pfx##_array_t *arr, int pos) { \
81 type_t res = arr->tab[pos]; \
82 pfx##_array_splice(arr, pos, 1, NULL, 0); \
83 return res; \
84 } \
85 static inline int pfx##_array_indexof(pfx##_array_t *arr, type_t *e) \
86 { \
87 return e - arr->tab; \
88 } \
89 static inline type_t pfx##_array_remove(pfx##_array_t *arr, type_t *e) \
90 { \
91 return pfx##_array_take(arr, pfx##_array_indexof(arr, e)); \
92 } \
93 static inline void \
94 pfx##_array_push(pfx##_array_t *arr, type_t e) \
95 { \
96 pfx##_array_splice(arr, 0, 0, &e, 1); \
97 } \
98 static inline void pfx##_array_append(pfx##_array_t *arr, type_t e) { \
99 pfx##_array_splice(arr, arr->len, 0, &e, 1); \
102 #define DO_ARRAY(type_t, pfx, dtor) \
103 ARRAY_TYPE(type_t, pfx) \
104 ARRAY_FUNCS(type_t, pfx, dtor)
106 #endif
107 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80