| 1 |
/* The MIT License |
| 2 |
|
| 3 |
Copyright (c) 2008, by Attractive Chaos <[email protected]> |
| 4 |
|
| 5 |
Permission is hereby granted, free of charge, to any person obtaining |
| 6 |
a copy of this software and associated documentation files (the |
| 7 |
"Software"), to deal in the Software without restriction, including |
| 8 |
without limitation the rights to use, copy, modify, merge, publish, |
| 9 |
distribute, sublicense, and/or sell copies of the Software, and to |
| 10 |
permit persons to whom the Software is furnished to do so, subject to |
| 11 |
the following conditions: |
| 12 |
|
| 13 |
The above copyright notice and this permission notice shall be |
| 14 |
included in all copies or substantial portions of the Software. |
| 15 |
|
| 16 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 18 |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 |
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 20 |
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 21 |
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 22 |
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 |
SOFTWARE. |
| 24 |
*/ |
| 25 |
|
| 26 |
/* |
| 27 |
An example: |
| 28 |
|
| 29 |
#include "kvec.h" |
| 30 |
int main() { |
| 31 |
kvec_t(int) array; |
| 32 |
kv_init(array); |
| 33 |
kv_push(int, array, 10); // append |
| 34 |
kv_a(int, array, 20) = 5; // dynamic |
| 35 |
kv_A(array, 20) = 4; // static |
| 36 |
kv_destroy(array); |
| 37 |
return 0; |
| 38 |
} |
| 39 |
*/ |
| 40 |
|
| 41 |
/* |
| 42 |
2008-09-22 (0.1.0): |
| 43 |
|
| 44 |
* The initial version. |
| 45 |
|
| 46 |
*/ |
| 47 |
|
| 48 |
#ifndef AC_KVEC_H |
| 49 |
#define AC_KVEC_H |
| 50 |
|
| 51 |
#include <stdlib.h> |
| 52 |
#include "kalloc.h" |
| 53 |
|
| 54 |
#define kv_roundup32(x) (--(x), (x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, ++(x)) |
| 55 |
|
| 56 |
#define kvec_t(type) struct { size_t n, m; type *a; } |
| 57 |
#define kv_init(v) ((v).n = (v).m = 0, (v).a = 0) |
| 58 |
#define kv_destroy(v) free((v).a) |
| 59 |
#define kv_A(v, i) ((v).a[(i)]) |
| 60 |
#define kv_pop(v) ((v).a[--(v).n]) |
| 61 |
#define kv_size(v) ((v).n) |
| 62 |
#define kv_max(v) ((v).m) |
| 63 |
|
| 64 |
#define kv_resize(type, km, v, s) do { \ |
| 65 |
if ((v).m < (s)) { \ |
| 66 |
(v).m = (s); \ |
| 67 |
kv_roundup32((v).m); \ |
| 68 |
(v).a = (type*)krealloc((km), (v).a, sizeof(type) * (v).m); \ |
| 69 |
} \ |
| 70 |
} while (0) |
| 71 |
|
| 72 |
#define kv_copy(type, km, v1, v0) do { \ |
| 73 |
if ((v1).m < (v0).n) kv_resize(type, (km), (v1), (v0).n); \ |
| 74 |
(v1).n = (v0).n; \ |
| 75 |
memcpy((v1).a, (v0).a, sizeof(type) * (v0).n); \ |
| 76 |
} while (0) \ |
| 77 |
|
| 78 |
#define kv_push(type, km, v, x) do { \ |
| 79 |
if ((v).n == (v).m) { \ |
| 80 |
(v).m = (v).m? (v).m<<1 : 2; \ |
| 81 |
(v).a = (type*)krealloc((km), (v).a, sizeof(type) * (v).m); \ |
| 82 |
} \ |
| 83 |
(v).a[(v).n++] = (x); \ |
| 84 |
} while (0) |
| 85 |
|
| 86 |
#define kv_pushp(type, km, v, p) do { \ |
| 87 |
if ((v).n == (v).m) { \ |
| 88 |
(v).m = (v).m? (v).m<<1 : 2; \ |
| 89 |
(v).a = (type*)krealloc((km), (v).a, sizeof(type) * (v).m); \ |
| 90 |
} \ |
| 91 |
*(p) = &(v).a[(v).n++]; \ |
| 92 |
} while (0) |
| 93 |
|
| 94 |
#define kv_reverse(type, v, start) do { \ |
| 95 |
if ((v).m > 0 && (v).n > (start)) { \ |
| 96 |
size_t __i, __end = (v).n - (start); \ |
| 97 |
type *__a = (v).a + (start); \ |
| 98 |
for (__i = 0; __i < __end>>1; ++__i) { \ |
| 99 |
type __t = __a[__end - 1 - __i]; \ |
| 100 |
__a[__end - 1 - __i] = __a[__i]; __a[__i] = __t; \ |
| 101 |
} \ |
| 102 |
} \ |
| 103 |
} while (0) |
| 104 |
|
| 105 |
#endif |