forked from awesomeWM/awesome
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray.h
More file actions
178 lines (166 loc) · 10.7 KB
/
Copy patharray.h
File metadata and controls
178 lines (166 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
* array.h - useful array handling header
*
* Copyright © 2009 Julien Danjou <julien@danjou.info>
* Copyright © 2008 Pierre Habouzit <madcoder@debian.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef AWESOME_COMMON_ARRAY_H
#define AWESOME_COMMON_ARRAY_H
#include <stdlib.h>
#include "common/util.h"
/** Common array type */
#define ARRAY_TYPE(type_t, pfx) \
typedef struct pfx##_array_t { \
type_t *tab; \
int len, size; \
} pfx##_array_t;
#define foreach(var, array) \
for(int __foreach_index_##var = 0; \
__foreach_index_##var < (array).len; \
__foreach_index_##var = (array).len) \
for(typeof((array).tab) var = &(array).tab[__foreach_index_##var]; \
(__foreach_index_##var < (array).len) && \
(var = &(array).tab[__foreach_index_##var]); \
++__foreach_index_##var)
#define foreach_rev(var, array) \
for(int __foreach_index_##var = (array).len - 1; \
__foreach_index_##var >= 0; \
__foreach_index_##var = -1) \
for(typeof((array).tab) var = &(array).tab[__foreach_index_##var]; \
(__foreach_index_##var >= 0) && \
(var = &(array).tab[__foreach_index_##var]); \
--__foreach_index_##var)
/** Common array functions */
#define ARRAY_COMMON_FUNCS(type_t, pfx, dtor) \
static inline pfx##_array_t * pfx##_array_new(void) { \
return p_new(pfx##_array_t, 1); \
} \
static inline void pfx##_array_init(pfx##_array_t *arr) { \
p_clear(arr, 1); \
} \
static inline void pfx##_array_wipe(pfx##_array_t *arr) { \
for (int i = 0; i < arr->len; i++) { \
dtor(&arr->tab[i]); \
} \
p_delete(&arr->tab); \
} \
static inline void pfx##_array_delete(pfx##_array_t **arrp) { \
if (*arrp) { \
pfx##_array_wipe(*arrp); \
p_delete(arrp); \
} \
} \
\
static inline void pfx##_array_grow(pfx##_array_t *arr, int newlen) { \
p_grow(&arr->tab, newlen, &arr->size); \
} \
static inline void \
pfx##_array_splice(pfx##_array_t *arr, int pos, int len, \
type_t items[], int count) \
{ \
assert (pos >= 0 && len >= 0 && count >= 0); \
assert (pos <= arr->len && pos + len <= arr->len); \
if (len != count) { \
pfx##_array_grow(arr, arr->len + count - len); \
memmove(arr->tab + pos + count, arr->tab + pos + len, \
(arr->len - pos - len) * sizeof(*items)); \
arr->len += count - len; \
} \
memcpy(arr->tab + pos, items, count * sizeof(*items)); \
} \
static inline type_t pfx##_array_take(pfx##_array_t *arr, int pos) { \
type_t res = arr->tab[pos]; \
pfx##_array_splice(arr, pos, 1, NULL, 0); \
return res; \
} \
static inline int pfx##_array_indexof(pfx##_array_t *arr, type_t *e) \
{ \
return e - arr->tab; \
} \
static inline type_t pfx##_array_remove(pfx##_array_t *arr, type_t *e) \
{ \
return pfx##_array_take(arr, pfx##_array_indexof(arr, e)); \
}
/** Non-ordered array functions */
#define ARRAY_FUNCS(type_t, pfx, dtor) \
ARRAY_COMMON_FUNCS(type_t, pfx, dtor) \
static inline void \
pfx##_array_push(pfx##_array_t *arr, type_t e) \
{ \
pfx##_array_splice(arr, 0, 0, &e, 1); \
} \
static inline void pfx##_array_append(pfx##_array_t *arr, type_t e) { \
pfx##_array_splice(arr, arr->len, 0, &e, 1); \
} \
static inline type_t *pfx##_array_find(pfx##_array_t *arr, type_t e) \
{ \
foreach(item, *arr) \
if(!memcmp(item, &e, sizeof(e))) \
return item; \
return NULL; \
} \
static inline void pfx##_array_find_and_remove(pfx##_array_t *arr, \
type_t e) \
{ \
type_t *item = pfx##_array_find(arr, e); \
if(item) \
pfx##_array_remove(arr, item); \
}
/** Binary ordered array functions */
#define BARRAY_FUNCS(type_t, pfx, dtor, cmp) \
ARRAY_COMMON_FUNCS(type_t, pfx, dtor) \
static inline void \
pfx##_array_insert(pfx##_array_t *arr, type_t e) \
{ \
int l = 0, r = arr->len; \
while(l < r) \
{ \
int i = (r + l) / 2; \
switch(cmp(&e, &arr->tab[i])) \
{ \
case -1: \
r = i; \
break; \
case 0: \
return; \
case 1: \
l = i + 1; \
break; \
} \
} \
pfx##_array_splice(arr, r, 0, &e, 1); \
} \
static inline type_t * \
pfx##_array_lookup(const pfx##_array_t *arr, type_t e) \
{ \
return bsearch(&e, arr->tab, arr->len, sizeof(type_t), cmp); \
} \
static inline void pfx##_array_lookup_and_remove(pfx##_array_t *arr, \
type_t e) \
{ \
type_t *item = pfx##_array_lookup(arr, e); \
if(item) \
pfx##_array_remove(arr, item); \
}
#define DO_ARRAY(type_t, pfx, dtor) \
ARRAY_TYPE(type_t, pfx) \
ARRAY_FUNCS(type_t, pfx, dtor)
#define DO_BARRAY(type_t, pfx, dtor, cmp) \
ARRAY_TYPE(type_t, pfx) \
BARRAY_FUNCS(type_t, pfx, dtor, cmp)
#endif
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=80