comparison C/array.h @ 186:d4b8416e9ab1

Move C
author David Demelier <markand@malikania.fr>
date Sat, 23 Nov 2013 16:14:05 +0100
parents array.h@970e491d93cb
children
comparison
equal deleted inserted replaced
185:523156bb3af5 186:d4b8416e9ab1
1 /*
2 * array.h -- manipulate dynamic arrays
3 *
4 * Copyright (c) 2011, 2012, 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 #include <stdarg.h>
23
24 #ifndef ARRAY_DEFAULT_CHKSIZE
25 #define ARRAY_DEFAULT_CHKSIZE 128
26 #endif
27
28 enum array_flags {
29 ARRAY_AUTO = 0, /* array grows automatically */
30 ARRAY_FIXED = (1 << 0), /* fixed size length */
31 ARRAY_FASTREMOVE = (1 << 1), /* use last object when removing */
32 ARRAY_CLEARBITS = (1 << 2), /* clear data when inserting/removing */
33 ARRAY_INSERTSAFE = (1 << 3) /* insertion must have valid indexes */
34 };
35
36 struct array {
37 int flags; /* (ro) array flags (default AUTO) */
38 void *data; /* (rw) array of data */
39 int length; /* (ro) number of element inside */
40 size_t size; /* (ro) current buffer size (allocated memory) */
41 size_t unit; /* (ro) unit size (sizeof the object) */
42 int chksize; /* (rw) chunk size (used when growing array) */
43
44 /* Own allocation functions */
45 void * (*malloc)(size_t);
46 void * (*realloc)(void *, size_t);
47 };
48
49 typedef void (*array_map_t)(void *, void *);
50 typedef int (*array_cmp_t)(const void *, const void *);
51
52 #ifdef __cplusplus
53 extern "C" {
54 #endif
55
56 int
57 array_init(struct array *, size_t);
58
59 void
60 array_set(struct array *, const char *, ...);
61
62 int
63 array_push(struct array *, const void *);
64
65 int
66 array_insert(struct array *, const void *, int);
67
68 int
69 array_append(struct array *, const void *);
70
71 void
72 array_pop(struct array *);
73
74 void
75 array_unqueue(struct array *);
76
77 void
78 array_iremove(struct array *, int);
79
80 void
81 array_premove(struct array *, const void *);
82
83 int
84 array_iswap(struct array *, int, int);
85
86 int
87 array_pswap(struct array *, const void *, const void *);
88
89 void
90 array_map(const struct array *, array_map_t, void *);
91
92 void
93 array_sort(struct array *, array_cmp_t);
94
95 int
96 array_find(const struct array *, array_cmp_t, void *, void *);
97
98 void *
99 array_first(const struct array *);
100
101 void *
102 array_get(const struct array *, int);
103
104 void *
105 array_last(const struct array *);
106
107 void
108 array_clear(struct array *);
109
110 void
111 array_free(struct array *);
112
113 void *
114 array_trim(struct array *);
115
116 #define ARRAY_FOREACH(a, var, i) \
117 for (i = 0, (var) = array_first((a)); \
118 i < (a)->length; \
119 (var) = array_get(a, ++i))
120
121 #ifdef __cplusplus
122 }
123 #endif
124
125 #endif /* _ARRAY_H_ */