comparison array.h @ 62:d10ab6bc555d

HG self failure
author David Demelier <markand@malikania.fr>
date Wed, 09 Nov 2011 19:26:09 +0100
parents
children f773c76b1f3c
comparison
equal deleted inserted replaced
61:e1f3ed0bf507 62:d10ab6bc555d
1 /*
2 * array.h -- manipulate dynamic arrays
3 *
4 * Copyright (c) 2011, David Demelier <markand@malikania.fr>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #ifndef _ARRAY_H_
20 #define _ARRAY_H_
21
22 /* Add some nonnull attributes for gcc/clang */
23 #ifdef __GNUC__
24 # define __at_malloc __attribute__ ((malloc))
25 # define __at_nonnull(...) __attribute__ ((nonnull (__VA_ARGS__)))
26 #else
27 # define __at_malloc
28 # define __at_nonnull(...)
29 #endif
30
31 #define ARRAY_DEFAULT_BSIZE 128
32
33 enum array_type {
34 ARRAY_FIXED = 0,
35 ARRAY_AUTO = 1
36 };
37
38 struct array {
39 enum array_type type; /* array's type (default FIXED) */
40 void *data; /* array of data */
41 int length; /* number of element inside */
42 size_t size; /* current buffer size (allocated memory) */
43 size_t unit; /* unit size (sizeof the object) */
44 int bsize; /* block size (used when growing array) */
45 int i; /* only for ARRAY_FOREACH(_R) */
46 };
47
48 typedef void (*array_map_fn)(void *, void *);
49 typedef int (*array_cmp_fn)(void *, void *);
50
51 struct array *array_new(enum array_type, size_t, int) __at_malloc;
52 int array_push(struct array *, const void *) __at_nonnull(1, 2);
53 int array_insert(struct array *, const void *, int) __at_nonnull(1, 2);
54 int array_append(struct array *, const void *) __at_nonnull(1, 2);
55 void array_pop(struct array *) __at_nonnull(1);
56 void array_unqueue(struct array *) __at_nonnull(1);
57 void array_remove(struct array *, int) __at_nonnull(1);
58 void array_unref(struct array *, const void *) __at_nonnull(1);
59 int array_iswap(struct array *, int, int) __at_nonnull(1);
60 int array_pswap(struct array *, const void *, const void *) __at_nonnull(1);
61 void array_map(const struct array *, array_map_fn, void *) __at_nonnull(1, 2);
62 void *array_find(const struct array *, array_cmp_fn, int *, void *) __at_nonnull(1, 2);
63 void array_clear(struct array *) __at_nonnull(1);
64 void array_free(struct array *) __at_nonnull(1);
65
66 #define ARRAY_HEAD(a, type) \
67 (((type *)a->data)[0])
68 #define ARRAY_TAIL(a, type) \
69 (((type *)a->data)[(a->length == 0) ? 0 : a->length - 1])
70 #define ARRAY_INDEX(a, i, type) \
71 ((type *)(a)->data)[((i) < 0) \
72 ? 0 : ((i) >= (a)->length) ? (a)->length - 1 : (i)]
73
74 #define ARRAY_FOREACH_R(a, var) \
75 for ((a)->i = 0, var = ARRAY_TAIL((a)); \
76 (a)->i < (a)->length; ++(a)->i, --var)
77
78 #define ARRAY_FOREACH(a, var, type) \
79 for ((a)->i = 0, var = ARRAY_HEAD((a), type); \
80 (a)->i < (a)->length; \
81 ++(a)->i, var = ARRAY_INDEX((a), (a)->i, type))
82
83 #endif /* _ARRAY_H_ */