25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 

177 satır
4.1 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. void alloc_from(std::initializer_list<type> l) {
  17. length = max(l.size(), 1); // alloc at least one
  18. data = (type*)malloc(length * sizeof(type));
  19. count = 0;
  20. for (type t : l) {
  21. data[count++] = t;
  22. }
  23. }
  24. void dealloc() {
  25. free(data);
  26. data = nullptr;
  27. }
  28. void clear() {
  29. count = 0;
  30. }
  31. Array_List<type> clone() {
  32. Array_List<type> ret;
  33. ret.length = length;
  34. ret.count = count;
  35. ret.data = (type*)malloc(length * sizeof(type));
  36. for (u32 i = 0; i < count; ++i) {
  37. ret.data[i] = data[i];
  38. }
  39. return ret;
  40. }
  41. type* begin() {
  42. return data;
  43. }
  44. type* end() {
  45. return data+(count);
  46. }
  47. void remove_index(u32 index) {
  48. data[index] = data[--count];
  49. }
  50. void append(type element) {
  51. if (count == length) {
  52. length *= 2;
  53. data = (type*)realloc(data, length * sizeof(type));
  54. }
  55. data[count] = element;
  56. count++;
  57. }
  58. void reserve(u32 amount) {
  59. if (count+amount >= (u32)length) {
  60. length *= 2;
  61. data = (type*)realloc(data, length * sizeof(type));
  62. }
  63. }
  64. type& operator[](u32 index) {
  65. return data[index];
  66. }
  67. void _merge(u32 start, u32 mid, u32 end) {
  68. u32 start2 = mid + 1;
  69. /* If the direct merge is already sorted */
  70. if ((size_t)data[mid] <= (size_t)data[start2]) {
  71. return;
  72. }
  73. /* Two pointers to maintain start of both arrays to merge */
  74. while (start <= mid && start2 <= end) {
  75. if ((size_t)data[start] <= (size_t)data[start2]) {
  76. start++;
  77. }
  78. else {
  79. type value = data[start2];
  80. u32 index = start2;
  81. /* Shift all the elements between element 1; element 2, right by 1. */
  82. while (index != start) {
  83. data[index] = data[index - 1];
  84. index--;
  85. }
  86. data[start] = value;
  87. /* Update all the pointers */
  88. start++;
  89. mid++;
  90. start2++;
  91. }
  92. }
  93. }
  94. void sort(s32 left=-1, s32 right=-1) {
  95. if (left == -1) {
  96. if (count == 0)
  97. return;
  98. sort(0, count - 1);
  99. return;
  100. } else if (left == right) {
  101. return;
  102. }
  103. u32 middle = left + (right-left) / 2;
  104. sort(left, middle);
  105. sort(middle+1, right);
  106. _merge(left, middle, right);
  107. }
  108. u32 sorted_find(type elem, s32 left=-1, s32 right=-1) {
  109. if (left == -1) {
  110. return sorted_find(elem, 0, count - 1);
  111. } else if (left == right) {
  112. if ((size_t)data[left] == (size_t)elem)
  113. return left;
  114. return -1;
  115. } else if (right < left)
  116. return -1;
  117. u32 middle = left + (right-left) / 2;
  118. if ((size_t)data[middle] < (size_t)elem)
  119. return sorted_find(elem, middle+1, right);
  120. if ((size_t)data[middle] > (size_t)elem)
  121. return sorted_find(elem, left, middle-1);
  122. return middle;
  123. }
  124. };
  125. template <typename type>
  126. struct Auto_Array_List : public Array_List<type> {
  127. Auto_Array_List(u32 length) {
  128. this->alloc(length);
  129. }
  130. Auto_Array_List() {
  131. this->alloc(16);
  132. }
  133. Auto_Array_List(std::initializer_list<type> l) {
  134. this->alloc(l.size());
  135. for (type e : l) {
  136. this->append(e);
  137. }
  138. }
  139. ~Auto_Array_List() {
  140. free(this->data);
  141. this->data = nullptr;
  142. }
  143. };