2008-06-22 13:43:28 +02:00
|
|
|
/*
|
|
|
|
* array.h - useful array handling header
|
|
|
|
*
|
2009-04-28 08:34:13 +02:00
|
|
|
* Copyright © 2009 Julien Danjou <julien@danjou.info>
|
2008-06-22 13:43:28 +02:00
|
|
|
* 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 2 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, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef AWESOME_COMMON_ARRAY_H
|
|
|
|
#define AWESOME_COMMON_ARRAY_H
|
|
|
|
|
2009-06-03 11:52:38 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2008-06-22 13:43:28 +02:00
|
|
|
#include "common/util.h"
|
|
|
|
|
2009-06-03 11:52:38 +02:00
|
|
|
/** Common array type */
|
2008-06-23 16:31:24 +02:00
|
|
|
#define ARRAY_TYPE(type_t, pfx) \
|
2008-06-22 13:43:28 +02:00
|
|
|
typedef struct pfx##_array_t { \
|
|
|
|
type_t *tab; \
|
|
|
|
int len, size; \
|
2008-07-24 16:25:02 +02:00
|
|
|
} pfx##_array_t;
|
2008-06-23 16:31:24 +02:00
|
|
|
|
2009-04-09 17:16:08 +02:00
|
|
|
#define foreach(var, array) \
|
2009-04-29 18:32:44 +02:00
|
|
|
for(int __foreach_index_##var = 0; \
|
|
|
|
__foreach_index_##var < (array).len; \
|
2009-04-29 20:37:45 +02:00
|
|
|
__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)
|
2009-04-09 17:16:08 +02:00
|
|
|
|
2015-07-25 16:42:50 +02:00
|
|
|
#define foreach_reverse(var, array) \
|
|
|
|
for(int __foreach_index_##var = (array).len-1; \
|
|
|
|
__foreach_index_##var > -1; \
|
|
|
|
__foreach_index_##var = -1) \
|
|
|
|
for(typeof((array).tab) var = &(array).tab[__foreach_index_##var]; \
|
|
|
|
(__foreach_index_##var > -1) && \
|
|
|
|
(var = &(array).tab[__foreach_index_##var]); \
|
|
|
|
--__foreach_index_##var)
|
|
|
|
|
2009-06-03 11:52:38 +02:00
|
|
|
/** Common array functions */
|
|
|
|
#define ARRAY_COMMON_FUNCS(type_t, pfx, dtor) \
|
2008-06-22 13:43:28 +02:00
|
|
|
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); \
|
2008-06-25 00:08:20 +02:00
|
|
|
assert (pos <= arr->len && pos + len <= arr->len); \
|
2008-06-22 13:43:28 +02:00
|
|
|
if (len != count) { \
|
|
|
|
pfx##_array_grow(arr, arr->len + count - len); \
|
|
|
|
memmove(arr->tab + pos + count, arr->tab + pos + len, \
|
2008-06-23 17:26:27 +02:00
|
|
|
(arr->len - pos - len) * sizeof(*items)); \
|
2008-06-22 13:43:28 +02:00
|
|
|
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; \
|
|
|
|
} \
|
2009-04-09 17:16:39 +02:00
|
|
|
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)); \
|
2009-06-03 11:52:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Non-ordered array functions */
|
|
|
|
#define ARRAY_FUNCS(type_t, pfx, dtor) \
|
|
|
|
ARRAY_COMMON_FUNCS(type_t, pfx, dtor) \
|
2009-04-09 17:16:39 +02:00
|
|
|
static inline void \
|
|
|
|
pfx##_array_push(pfx##_array_t *arr, type_t e) \
|
|
|
|
{ \
|
|
|
|
pfx##_array_splice(arr, 0, 0, &e, 1); \
|
|
|
|
} \
|
2008-06-22 13:43:28 +02:00
|
|
|
static inline void pfx##_array_append(pfx##_array_t *arr, type_t e) { \
|
|
|
|
pfx##_array_splice(arr, arr->len, 0, &e, 1); \
|
2009-06-03 11:52:38 +02:00
|
|
|
} \
|
|
|
|
|
|
|
|
/** 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; \
|
2010-09-01 15:37:21 +02:00
|
|
|
int res = cmp(&e, &arr->tab[i]); \
|
|
|
|
if(res == 0) \
|
|
|
|
return; /* Already added, ignore */ \
|
|
|
|
if(res < 0) \
|
2009-06-03 11:52:38 +02:00
|
|
|
r = i; \
|
2010-09-01 15:37:21 +02:00
|
|
|
else \
|
2009-06-03 11:52:38 +02:00
|
|
|
l = i + 1; \
|
|
|
|
} \
|
|
|
|
pfx##_array_splice(arr, r, 0, &e, 1); \
|
|
|
|
} \
|
|
|
|
static inline type_t * \
|
|
|
|
pfx##_array_lookup(pfx##_array_t *arr, type_t *e) \
|
|
|
|
{ \
|
|
|
|
return bsearch(e, arr->tab, arr->len, sizeof(type_t), cmp); \
|
2008-06-22 13:43:28 +02:00
|
|
|
}
|
|
|
|
|
2008-06-23 16:31:24 +02:00
|
|
|
#define DO_ARRAY(type_t, pfx, dtor) \
|
2008-07-28 14:38:37 +02:00
|
|
|
ARRAY_TYPE(type_t, pfx) \
|
|
|
|
ARRAY_FUNCS(type_t, pfx, dtor)
|
2008-06-23 16:31:24 +02:00
|
|
|
|
2009-06-03 11:52:38 +02:00
|
|
|
#define DO_BARRAY(type_t, pfx, dtor, cmp) \
|
|
|
|
ARRAY_TYPE(type_t, pfx) \
|
|
|
|
BARRAY_FUNCS(type_t, pfx, dtor, cmp)
|
|
|
|
|
2008-06-22 13:43:28 +02:00
|
|
|
#endif
|
2011-09-11 16:50:01 +02:00
|
|
|
// vim: filetype=c:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|