naughty: icon_size added to config and notify()
[awesome.git] / common / array.h
blobfcda00d9c7c134c134b1e5ef44271a2610723479
1 /*
2 * array.h - useful array handling header
4 * Copyright © 2008 Pierre Habouzit <madcoder@debian.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #ifndef AWESOME_COMMON_ARRAY_H
23 #define AWESOME_COMMON_ARRAY_H
25 #include "common/util.h"
27 #define ARRAY_TYPE(type_t, pfx) \
28 typedef struct pfx##_array_t { \
29 type_t *tab; \
30 int len, size; \
31 } pfx##_array_t;
33 #define ARRAY_FUNCS(type_t, pfx, dtor) \
34 static inline pfx##_array_t * pfx##_array_new(void) { \
35 return p_new(pfx##_array_t, 1); \
36 } \
37 static inline void pfx##_array_init(pfx##_array_t *arr) { \
38 p_clear(arr, 1); \
39 } \
40 static inline void pfx##_array_wipe(pfx##_array_t *arr) { \
41 for (int i = 0; i < arr->len; i++) { \
42 dtor(&arr->tab[i]); \
43 } \
44 p_delete(&arr->tab); \
45 } \
46 static inline void pfx##_array_delete(pfx##_array_t **arrp) { \
47 if (*arrp) { \
48 pfx##_array_wipe(*arrp); \
49 p_delete(arrp); \
50 } \
51 } \
53 static inline void pfx##_array_grow(pfx##_array_t *arr, int newlen) { \
54 p_grow(&arr->tab, newlen, &arr->size); \
55 } \
56 static inline void \
57 pfx##_array_splice(pfx##_array_t *arr, int pos, int len, \
58 type_t items[], int count) \
59 { \
60 assert (pos >= 0 && len >= 0 && count >= 0); \
61 assert (pos <= arr->len && pos + len <= arr->len); \
62 if (len != count) { \
63 pfx##_array_grow(arr, arr->len + count - len); \
64 memmove(arr->tab + pos + count, arr->tab + pos + len, \
65 (arr->len - pos - len) * sizeof(*items)); \
66 arr->len += count - len; \
67 } \
68 memcpy(arr->tab + pos, items, count * sizeof(*items)); \
69 } \
70 static inline type_t pfx##_array_take(pfx##_array_t *arr, int pos) { \
71 type_t res = arr->tab[pos]; \
72 pfx##_array_splice(arr, pos, 1, NULL, 0); \
73 return res; \
74 } \
75 static inline void pfx##_array_append(pfx##_array_t *arr, type_t e) { \
76 pfx##_array_splice(arr, arr->len, 0, &e, 1); \
79 #define DO_ARRAY(type_t, pfx, dtor) \
80 ARRAY_TYPE(type_t, pfx) \
81 ARRAY_FUNCS(type_t, pfx, dtor)
83 #endif
84 // vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80