| 1 | /* |
|---|
| 2 | Copyright (C) 2008 Tomash Brechko. All rights reserved. |
|---|
| 3 | |
|---|
| 4 | When used to build Perl module: |
|---|
| 5 | |
|---|
| 6 | This library is free software; you can redistribute it and/or modify |
|---|
| 7 | it under the same terms as Perl itself, either Perl version 5.8.8 |
|---|
| 8 | or, at your option, any later version of Perl 5 you may have |
|---|
| 9 | available. |
|---|
| 10 | |
|---|
| 11 | When used as a standalone library: |
|---|
| 12 | |
|---|
| 13 | This library is free software; you can redistribute it and/or modify |
|---|
| 14 | it under the terms of the GNU Lesser General Public License as |
|---|
| 15 | published by the Free Software Foundation; either version 2.1 of the |
|---|
| 16 | License, or (at your option) any later version. |
|---|
| 17 | |
|---|
| 18 | This library is distributed in the hope that it will be useful, but |
|---|
| 19 | WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|---|
| 21 | Lesser General Public License for more details. |
|---|
| 22 | */ |
|---|
| 23 | |
|---|
| 24 | #ifndef ARRAY_H |
|---|
| 25 | #define ARRAY_H 1 |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | struct array |
|---|
| 29 | { |
|---|
| 30 | void *buf; |
|---|
| 31 | int capacity; |
|---|
| 32 | int elems; |
|---|
| 33 | }; |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | extern |
|---|
| 37 | void |
|---|
| 38 | array_init(struct array *a); |
|---|
| 39 | |
|---|
| 40 | |
|---|
| 41 | extern |
|---|
| 42 | void |
|---|
| 43 | array_destroy(struct array *a); |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | enum e_array_extend { ARRAY_EXTEND_EXACT, ARRAY_EXTEND_TWICE }; |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | extern |
|---|
| 50 | int |
|---|
| 51 | array_resize(struct array *a, int elem_size, int elems, |
|---|
| 52 | enum e_array_extend extend); |
|---|
| 53 | |
|---|
| 54 | |
|---|
| 55 | #define array_extend(array, type, add, extend) \ |
|---|
| 56 | array_resize(&(array), sizeof(type), (array).elems + add, extend) |
|---|
| 57 | |
|---|
| 58 | #define array_push(array) ++(array).elems |
|---|
| 59 | |
|---|
| 60 | #define array_pop(array) --(array).elems |
|---|
| 61 | |
|---|
| 62 | #define array_append(array, add) (array).elems += add |
|---|
| 63 | |
|---|
| 64 | #define array_size(array) ((array).elems) |
|---|
| 65 | |
|---|
| 66 | #define array_empty(array) ((array).elems == 0) |
|---|
| 67 | |
|---|
| 68 | #define array_clear(array) (array).elems = 0 |
|---|
| 69 | |
|---|
| 70 | #define array_elem(array, type, index) ((type *) (array).buf + index) |
|---|
| 71 | |
|---|
| 72 | #define array_beg(array, type) ((type *) (array).buf) |
|---|
| 73 | |
|---|
| 74 | #define array_end(array, type) ((type *) (array).buf + (array).elems) |
|---|
| 75 | |
|---|
| 76 | #define array_each(array, type, p) \ |
|---|
| 77 | (p) = array_beg(array, type); (p) != array_end(array, type); ++(p) |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | #endif /* ! ARRAY_H */ |
|---|