Mercurial > code
comparison array.h @ 6:25cc379de564
Added array.c and array.h:
Various function to manipulate dynamic array. This let you adding as
much as you want object into the array. You may add object to the head,
end or at a specified index.
author | David Demelier <markand@malikania.fr> |
---|---|
date | Tue, 06 Sep 2011 22:19:18 +0200 |
parents | |
children | 127254037b30 |
comparison
equal
deleted
inserted
replaced
5:0ed27735fa87 | 6:25cc379de564 |
---|---|
1 /* | |
2 * array.h -- manipulate dymanic array | |
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 #define ARRAY_DEFAULT_BSIZE 128 | |
23 | |
24 struct array { | |
25 void **data; /* array of data */ | |
26 size_t length; /* number of element in array */ | |
27 | |
28 #define ARRAY_FIXED 0x00000000 | |
29 #define ARRAY_AUTO 0x00000001 | |
30 int flags; /* array's flags (default FIXED) */ | |
31 | |
32 /* Private should not be modified by user */ | |
33 size_t size; /* current buffer size */ | |
34 size_t bsize; /* block size */ | |
35 }; | |
36 | |
37 struct array *array_new(const void *, size_t, int); | |
38 #define array_new_auto(size) array_new(NULL, size, ARRAY_AUTO) | |
39 #define array_new_fixed(max) array_new(NULL, max, ARRAY_FIXED) | |
40 | |
41 int array_push(struct array *, const void *); | |
42 int array_insert(struct array *, const void *, int); | |
43 int array_append(struct array *, const void *); | |
44 void *array_pop(struct array *); | |
45 void *array_unqueue(struct array *); | |
46 void *array_remove(struct array *, int); | |
47 void array_map(struct array *, void (*fn)(void *, void *), void *); | |
48 void *array_find(struct array *, int (*fn)(void *, void *), void *, int *); | |
49 void array_clear(struct array *, int); | |
50 void array_free(struct array *, int); | |
51 | |
52 #define ARRAY_FOREACH(array, entry, tmp, type) \ | |
53 for ((tmp) = (type **) (array)->data, entry = *tmp; \ | |
54 (entry) != NULL; \ | |
55 ++(tmp), (entry) = (*tmp)) | |
56 | |
57 #endif /* _ARRAY_H */ |