Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 

204 řádky
4.8 KiB

  1. #pragma once
  2. #include <stdlib.h>
  3. #include <initializer_list>
  4. #include "types.hpp"
  5. #include "macros.hpp"
  6. template <typename type>
  7. struct Array_List {
  8. type* data;
  9. u32 length;
  10. u32 count;
  11. void alloc(u32 initial_capacity = 16) {
  12. data = (type*)malloc(initial_capacity * sizeof(type));
  13. count = 0;
  14. length = initial_capacity;
  15. }
  16. static Array_List<type> create_from(std::initializer_list<type> l) {
  17. Array_List<type> ret;
  18. ret.alloc_from(l);
  19. return ret;
  20. }
  21. void alloc_from(std::initializer_list<type> l) {
  22. length = max(l.size(), 1); // alloc at least one
  23. data = (type*)malloc(length * sizeof(type));
  24. count = 0;
  25. // TODO(Felix): Use memcpy here
  26. for (type t : l) {
  27. data[count++] = t;
  28. }
  29. }
  30. void extend(std::initializer_list<type> l) {
  31. reserve(l.size());
  32. // TODO(Felix): Use memcpy here
  33. for (type e : l) {
  34. append(e);
  35. }
  36. }
  37. void dealloc() {
  38. free(data);
  39. data = nullptr;
  40. }
  41. void clear() {
  42. count = 0;
  43. }
  44. Array_List<type> clone() {
  45. Array_List<type> ret;
  46. ret.length = length;
  47. ret.count = count;
  48. ret.data = (type*)malloc(length * sizeof(type));
  49. // TODO(Felix): Maybe use memcpy here
  50. for (u32 i = 0; i < count; ++i) {
  51. ret.data[i] = data[i];
  52. }
  53. return ret;
  54. }
  55. void copy_values_from(Array_List<type> other) {
  56. // clear the array
  57. count = 0;
  58. // make sure we have allocated enough
  59. reserve(other.count);
  60. // copy stuff
  61. count = other.count;
  62. memcpy(data, other.data, sizeof(type) * other.count);
  63. }
  64. type* begin() {
  65. return data;
  66. }
  67. type* end() {
  68. return data+(count);
  69. }
  70. void remove_index(u32 index) {
  71. data[index] = data[--count];
  72. }
  73. void append(type element) {
  74. if (count == length) {
  75. length *= 2;
  76. data = (type*)realloc(data, length * sizeof(type));
  77. }
  78. data[count] = element;
  79. count++;
  80. }
  81. void reserve(u32 amount) {
  82. if (count+amount >= (u32)length) {
  83. length *= 2;
  84. data = (type*)realloc(data, length * sizeof(type));
  85. }
  86. }
  87. type& operator[](u32 index) {
  88. return data[index];
  89. }
  90. void _merge(u32 start, u32 mid, u32 end) {
  91. u32 start2 = mid + 1;
  92. /* If the direct merge is already sorted */
  93. if ((size_t)data[mid] <= (size_t)data[start2]) {
  94. return;
  95. }
  96. /* Two pointers to maintain start of both arrays to merge */
  97. while (start <= mid && start2 <= end) {
  98. if ((size_t)data[start] <= (size_t)data[start2]) {
  99. start++;
  100. }
  101. else {
  102. type value = data[start2];
  103. u32 index = start2;
  104. /* Shift all the elements between element 1; element 2, right by 1. */
  105. while (index != start) {
  106. data[index] = data[index - 1];
  107. index--;
  108. }
  109. data[start] = value;
  110. /* Update all the pointers */
  111. start++;
  112. mid++;
  113. start2++;
  114. }
  115. }
  116. }
  117. void sort(s32 left=-1, s32 right=-1) {
  118. if (left == -1) {
  119. if (count == 0)
  120. return;
  121. sort(0, count - 1);
  122. return;
  123. } else if (left == right) {
  124. return;
  125. }
  126. u32 middle = left + (right-left) / 2;
  127. sort(left, middle);
  128. sort(middle+1, right);
  129. _merge(left, middle, right);
  130. }
  131. s32 sorted_find(type elem, s32 left=-1, s32 right=-1) {
  132. if (left == -1) {
  133. return sorted_find(elem, 0, count - 1);
  134. } else if (left == right) {
  135. if ((size_t)data[left] == (size_t)elem)
  136. return left;
  137. return -1;
  138. } else if (right < left)
  139. return -1;
  140. u32 middle = left + (right-left) / 2;
  141. if ((size_t)data[middle] < (size_t)elem)
  142. return sorted_find(elem, middle+1, right);
  143. if ((size_t)data[middle] > (size_t)elem)
  144. return sorted_find(elem, left, middle-1);
  145. return middle;
  146. }
  147. };
  148. template <typename type>
  149. struct Auto_Array_List : public Array_List<type> {
  150. Auto_Array_List(u32 length) {
  151. this->alloc(length);
  152. }
  153. Auto_Array_List() {
  154. this->alloc(16);
  155. }
  156. Auto_Array_List(std::initializer_list<type> l) {
  157. this->alloc(l.size());
  158. for (type e : l) {
  159. this->append(e);
  160. }
  161. }
  162. ~Auto_Array_List() {
  163. free(this->data);
  164. this->data = nullptr;
  165. }
  166. };