Mercurial > code
comparison parray.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 pointer 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 _PARRAY_H_ | |
20 #define _PARRAY_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 PARRAY_DEFAULT_BSIZE 128 | |
32 | |
33 enum parray_type { | |
34 PARRAY_FIXED = 0, | |
35 PARRAY_AUTO = 1 | |
36 }; | |
37 | |
38 struct parray { | |
39 enum parray_type type; /* array type (default FIXED) */ | |
40 void **datas; /* array of data */ | |
41 int length; /* number of element inside */ | |
42 size_t size; /* current buffer size (allocated memory) */ | |
43 int bsize; /* block size (used when growing array) */ | |
44 int i; /* only for PARRAY_FOREACH(_R) */ | |
45 }; | |
46 | |
47 typedef void (*parray_map_fn)(void *, void *); | |
48 typedef int (*parray_cmp_fn)(void *, void *); | |
49 | |
50 struct parray *parray_new(enum parray_type, int) __at_malloc; | |
51 int parray_push(struct parray *, void *) __at_nonnull(1); | |
52 int parray_insert(struct parray *, void *, int) __at_nonnull(1); | |
53 int parray_append(struct parray *, void *) __at_nonnull(1); | |
54 void parray_pop(struct parray *) __at_nonnull(1); | |
55 void parray_unqueue(struct parray *) __at_nonnull(1); | |
56 void parray_remove(struct parray *, int) __at_nonnull(1); | |
57 void parray_unref(struct parray *, const void *) __at_nonnull(1); | |
58 int parray_iswap(struct parray *, int, int) __at_nonnull(1); | |
59 int parray_pswap(struct parray *, const void *, const void *) __at_nonnull(1); | |
60 void parray_map(const struct parray *, parray_map_fn, void *) __at_nonnull(1, 2); | |
61 void *parray_find(const struct parray *, parray_cmp_fn, int *, void *) __at_nonnull(1, 2); | |
62 void parray_clear(struct parray *) __at_nonnull(1); | |
63 void parray_free(struct parray *) __at_nonnull(1); | |
64 | |
65 #define PARRAY_HEAD(a) \ | |
66 (a->datas[0]) | |
67 #define PARRAY_TAIL(a) \ | |
68 (a->datas[(a->length == 0) ? 0 : a->length - 1]) | |
69 #define PARRAY_INDEX(a, i) \ | |
70 (((i) < 0 || (a)->length == 0) ? (PARRAY_HEAD((a))) /* < 0 head */ \ | |
71 : ((i) >= (a)->length) ? (PARRAY_TAIL((a))) /* > l tail */ \ | |
72 : ((a)->datas[i])) /* correct */ | |
73 | |
74 #define PARRAY_FOREACH_R(a, var) \ | |
75 for ((a)->i = 0, var = PARRAY_TAIL((a)); \ | |
76 (a)->i < (a)->length; ++(a)->i, var = PARRAY_INDEX((a), (a)->i)) | |
77 | |
78 #define PARRAY_FOREACH(a, var) \ | |
79 for ((a)->i = 0, var = PARRAY_INDEX((a), (a)->i); \ | |
80 (a)->i < (a)->length; ++(a)->i, var = PARRAY_INDEX((a), (a)->i)) | |
81 | |
82 #define PARRAY_FLUSH(a) \ | |
83 do { \ | |
84 void *i; \ | |
85 \ | |
86 PARRAY_FOREACH_R(a, i) { \ | |
87 free(i); \ | |
88 parray_unqueue((a)); \ | |
89 } \ | |
90 \ | |
91 (a)->length = 0; \ | |
92 } while (/* CONSTCOND */ 0); | |
93 | |
94 #endif /* _PARRAY_H_ */ |