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.

kvec.h 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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_safe(int, array, 10, e0); // append
  28. kv_a(int, array, 20) = 5; // dynamic
  29. kv_A(array, 20) = 4; // static
  30. kv_destroy(array);
  31. return 0;
  32. e0:
  33. return 1;
  34. }
  35. */
  36. /*
  37. 2008-09-22 (0.1.0):
  38. * The initial version.
  39. */
  40. #ifndef AC_KVEC_H
  41. #define AC_KVEC_H
  42. #include <stdlib.h>
  43. #define kv_roundup32(x) (--(x), (x)|=(x)>>1, (x)|=(x)>>2, (x)|=(x)>>4, (x)|=(x)>>8, (x)|=(x)>>16, ++(x))
  44. #define kvec_t(type) struct { size_t n, m; type *a; }
  45. #define kv_init(v) ((v).n = (v).m = 0, (v).a = 0)
  46. #define kv_destroy(v) free((v).a)
  47. #define kv_A(v, i) ((v).a[(i)])
  48. #define kv_pop(v) ((v).a[--(v).n])
  49. #define kv_size(v) ((v).n)
  50. #define kv_max(v) ((v).m)
  51. #define kv_resize_safe(type, v, s, el) do { \
  52. type *_tp = (type*)realloc((v).a, sizeof(type) * (s)); \
  53. if (_tp == NULL) { \
  54. goto el; \
  55. } else { \
  56. (v).a = _tp; \
  57. (v).m = (s); \
  58. } \
  59. } while (0)
  60. #define kv_grow_factor 1.5
  61. #define kv_grow_safe(type, v, el) do { \
  62. size_t _ts = ((v).m > 1 ? (v).m * kv_grow_factor : 2); \
  63. type *_tp = (type*)realloc((v).a, sizeof(type) * _ts); \
  64. if (_tp == NULL) { \
  65. goto el; \
  66. } else { \
  67. (v).a = _tp; \
  68. (v).m = _ts; \
  69. } \
  70. } while (0)
  71. #define kv_copy_safe(type, v1, v0, el) do { \
  72. if ((v1).m < (v0).n) kv_resize_safe(type, v1, (v0).n, el); \
  73. (v1).n = (v0).n; \
  74. memcpy((v1).a, (v0).a, sizeof(type) * (v0).n); \
  75. } while (0)
  76. #define kv_push_safe(type, v, x, el) do { \
  77. if ((v).n == (v).m) { \
  78. kv_grow_safe(type, v, el); \
  79. } \
  80. (v).a[(v).n++] = (x); \
  81. } while (0)
  82. #define kv_prepend_safe(type, v, x, el) do { \
  83. if ((v).n == (v).m) { \
  84. kv_grow_safe(type, v, el); \
  85. } \
  86. memmove((v).a + 1, (v).a, sizeof(type) * (v).n); \
  87. (v).a[0] = (x); \
  88. (v).n ++; \
  89. } while (0)
  90. #define kv_concat_safe(type, v1, v0, el) do { \
  91. if ((v1).m < (v0).n + (v1).n) \
  92. kv_resize_safe(type, v1, (v0).n + (v1).n, el); \
  93. memcpy((v1).a + (v1).n, (v0).a, sizeof(type) * (v0).n); \
  94. (v1).n = (v0).n + (v1).n; \
  95. } while (0)
  96. #define kv_del(type, v, i) do { \
  97. if ((i) < (v).n) { \
  98. memmove((v).a + (i), (v).a + ((i) + 1), sizeof(type) * ((v).n - (i) - 1)); \
  99. (v).n --; \
  100. } \
  101. } while (0)
  102. /*
  103. * Old (ENOMEM-unsafe) version of kv_xxx macros. Compat-only, not for use in
  104. * the new library code.
  105. */
  106. #define kv_resize(type, v, s) ((v).m = (s), (v).a = (type*)realloc((v).a, sizeof(type) * (v).m))
  107. #define kv_grow(type, v) ((v).m = ((v).m > 1 ? (v).m * kv_grow_factor : 2), \
  108. (v).a = (type*)realloc((v).a, sizeof(type) * (v).m))
  109. #define kv_copy(type, v1, v0) do { \
  110. if ((v1).m < (v0).n) kv_resize(type, v1, (v0).n); \
  111. (v1).n = (v0).n; \
  112. memcpy((v1).a, (v0).a, sizeof(type) * (v0).n); \
  113. } while (0) \
  114. #define kv_push(type, v, x) do { \
  115. if ((v).n == (v).m) { \
  116. kv_grow(type, v); \
  117. } \
  118. (v).a[(v).n++] = (x); \
  119. } while (0)
  120. #define kv_prepend(type, v, x) do { \
  121. if ((v).n == (v).m) { \
  122. kv_grow(type, v); \
  123. } \
  124. memmove((v).a + 1, (v).a, sizeof(type) * (v).n); \
  125. (v).a[0] = (x); \
  126. (v).n ++; \
  127. } while (0)
  128. #define kv_concat(type, v1, v0) do { \
  129. if ((v1).m < (v0).n + (v1).n) kv_resize(type, v1, (v0).n + (v1).n); \
  130. memcpy((v1).a + (v1).n, (v0).a, sizeof(type) * (v0).n); \
  131. (v1).n = (v0).n + (v1).n; \
  132. } while (0)
  133. #endif /* AC_KVEC_H */