You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
3.4 KiB

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