Vector.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /**
  2. * @file Vector.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. // Preprocessor inputs:
  30. // VECTOR_NAME - prefix of functions and types to be defined
  31. // VECTOR_ELEM_TYPE - type of vector elements
  32. #include <limits.h>
  33. #include <string.h>
  34. #include <stddef.h>
  35. #include <misc/debug.h>
  36. #include <misc/balloc.h>
  37. #include <misc/merge.h>
  38. #define Vector VECTOR_NAME
  39. #define VectorElem VECTOR_ELEM_TYPE
  40. #define Vector_Init MERGE(VECTOR_NAME, _Init)
  41. #define Vector_Free MERGE(VECTOR_NAME, _Free)
  42. #define Vector_Get MERGE(VECTOR_NAME, _Get)
  43. #define Vector_AllocAppend MERGE(VECTOR_NAME, _AllocAppend)
  44. #define Vector_DoAppend MERGE(VECTOR_NAME, _DoAppend)
  45. #define Vector_AppendValue MERGE(VECTOR_NAME, _AppendValue)
  46. #define Vector_Push MERGE(VECTOR_NAME, _Push)
  47. #define Vector_Pop MERGE(VECTOR_NAME, _Pop)
  48. typedef struct {
  49. VectorElem *elems;
  50. size_t capacity;
  51. size_t count;
  52. } Vector;
  53. static int Vector_Init (Vector *o, size_t capacity) WARN_UNUSED;
  54. static void Vector_Free (Vector *o);
  55. static VectorElem * Vector_Get (Vector *o, size_t index);
  56. static int Vector_AllocAppend (Vector *o, size_t count, VectorElem **out_ptr) WARN_UNUSED;
  57. static void Vector_DoAppend (Vector *o, size_t count);
  58. static int Vector_AppendValue (Vector *o, VectorElem value, size_t *out_index) WARN_UNUSED;
  59. static VectorElem * Vector_Push (Vector *o, size_t *out_index) WARN_UNUSED;
  60. static VectorElem * Vector_Pop (Vector *o, size_t *out_index);
  61. static int Vector_Init (Vector *o, size_t capacity)
  62. {
  63. if (capacity == 0) {
  64. o->elems = NULL;
  65. } else {
  66. o->elems = BAllocArray(capacity, sizeof(VectorElem));
  67. if (!o->elems) {
  68. return 0;
  69. }
  70. }
  71. o->capacity = capacity;
  72. o->count = 0;
  73. return 1;
  74. }
  75. static void Vector_Free (Vector *o)
  76. {
  77. BFree(o->elems);
  78. }
  79. static VectorElem * Vector_Get (Vector *o, size_t index)
  80. {
  81. ASSERT(index < o->count)
  82. return &o->elems[index];
  83. }
  84. static int Vector_AllocAppend (Vector *o, size_t count, VectorElem **out_ptr)
  85. {
  86. ASSERT(count > 0)
  87. if (count > o->capacity - o->count) {
  88. size_t new_capacity = o->capacity;
  89. do {
  90. if (new_capacity > SIZE_MAX / 2) {
  91. return 0;
  92. }
  93. new_capacity = (new_capacity == 0) ? 1 : (2 * new_capacity);
  94. } while (count > new_capacity - o->count);
  95. VectorElem *new_elems = BAllocArray(new_capacity, sizeof(VectorElem));
  96. if (!new_elems) {
  97. return 0;
  98. }
  99. if (o->count > 0) {
  100. memcpy(new_elems, o->elems, o->count * sizeof(VectorElem));
  101. }
  102. BFree(o->elems);
  103. o->elems = new_elems;
  104. o->capacity = new_capacity;
  105. }
  106. if (out_ptr) {
  107. *out_ptr = &o->elems[o->count];
  108. }
  109. return 1;
  110. }
  111. static void Vector_DoAppend (Vector *o, size_t count)
  112. {
  113. ASSERT(count <= o->capacity - o->count)
  114. o->count += count;
  115. }
  116. static int Vector_AppendValue (Vector *o, VectorElem value, size_t *out_index)
  117. {
  118. VectorElem *ptr;
  119. if (!Vector_AllocAppend(o, 1, &ptr)) {
  120. return 0;
  121. }
  122. *ptr = value;
  123. if (out_index) {
  124. *out_index = o->count;
  125. }
  126. Vector_DoAppend(o, 1);
  127. return 1;
  128. }
  129. static VectorElem * Vector_Push (Vector *o, size_t *out_index)
  130. {
  131. VectorElem *ptr;
  132. if (!Vector_AllocAppend(o, 1, &ptr)) {
  133. return NULL;
  134. }
  135. if (out_index) {
  136. *out_index = o->count;
  137. }
  138. Vector_DoAppend(o, 1);
  139. return ptr;
  140. }
  141. static VectorElem * Vector_Pop (Vector *o, size_t *out_index)
  142. {
  143. ASSERT(o->count > 0)
  144. o->count--;
  145. if (out_index) {
  146. *out_index = o->count;
  147. }
  148. return &o->elems[o->count];
  149. }
  150. #undef VECTOR_NAME
  151. #undef VECTOR_ELEM_TYPE
  152. #undef Vector
  153. #undef VectorElem
  154. #undef Vector_Init
  155. #undef Vector_Free
  156. #undef Vector_Get
  157. #undef Vector_AllocAppend
  158. #undef Vector_DoAppend
  159. #undef Vector_AppendValue
  160. #undef Vector_Push
  161. #undef Vector_Pop