Vector_impl.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /**
  2. * @file Vector_impl.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. #include "Vector_header.h"
  30. static int Vector_Init (Vector *o, size_t capacity)
  31. {
  32. if (capacity == 0) {
  33. o->elems = NULL;
  34. } else {
  35. o->elems = BAllocArray(capacity, sizeof(VectorElem));
  36. if (!o->elems) {
  37. return 0;
  38. }
  39. }
  40. o->capacity = capacity;
  41. o->count = 0;
  42. return 1;
  43. }
  44. static void Vector_Free (Vector *o)
  45. {
  46. BFree(o->elems);
  47. }
  48. static size_t Vector_Count (Vector *o)
  49. {
  50. return o->count;
  51. }
  52. static VectorElem * Vector_Get (Vector *o, size_t index)
  53. {
  54. ASSERT(index < o->capacity)
  55. return &o->elems[index];
  56. }
  57. static int Vector_AllocAppend (Vector *o, size_t count, VectorElem **out_ptr)
  58. {
  59. ASSERT(count > 0)
  60. if (count > o->capacity - o->count) {
  61. size_t new_capacity = o->capacity;
  62. do {
  63. if (new_capacity > SIZE_MAX / 2) {
  64. return 0;
  65. }
  66. new_capacity = (new_capacity == 0) ? 1 : (2 * new_capacity);
  67. } while (count > new_capacity - o->count);
  68. VectorElem *new_elems = BAllocArray(new_capacity, sizeof(VectorElem));
  69. if (!new_elems) {
  70. return 0;
  71. }
  72. if (o->count > 0) {
  73. memcpy(new_elems, o->elems, o->count * sizeof(VectorElem));
  74. }
  75. BFree(o->elems);
  76. o->elems = new_elems;
  77. o->capacity = new_capacity;
  78. }
  79. if (out_ptr) {
  80. *out_ptr = &o->elems[o->count];
  81. }
  82. return 1;
  83. }
  84. static void Vector_DoAppend (Vector *o, size_t count)
  85. {
  86. ASSERT(count <= o->capacity - o->count)
  87. o->count += count;
  88. }
  89. static int Vector_AppendValue (Vector *o, VectorElem value, size_t *out_index)
  90. {
  91. VectorElem *ptr;
  92. if (!Vector_AllocAppend(o, 1, &ptr)) {
  93. return 0;
  94. }
  95. *ptr = value;
  96. if (out_index) {
  97. *out_index = o->count;
  98. }
  99. Vector_DoAppend(o, 1);
  100. return 1;
  101. }
  102. static VectorElem * Vector_Push (Vector *o, size_t *out_index)
  103. {
  104. VectorElem *ptr;
  105. if (!Vector_AllocAppend(o, 1, &ptr)) {
  106. return NULL;
  107. }
  108. if (out_index) {
  109. *out_index = o->count;
  110. }
  111. Vector_DoAppend(o, 1);
  112. return ptr;
  113. }
  114. static VectorElem * Vector_Pop (Vector *o, size_t *out_index)
  115. {
  116. ASSERT(o->count > 0)
  117. o->count--;
  118. if (out_index) {
  119. *out_index = o->count;
  120. }
  121. return &o->elems[o->count];
  122. }
  123. #include "Vector_footer.h"