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ů.
 
 
 
 

179 řádky
4.5 KiB

  1. #pragma once
  2. #include <stdlib.h>
  3. #include <utility>
  4. #include <initializer_list>
  5. #include "types.hpp"
  6. // template<class E> class my_initializer_list {
  7. // public:
  8. // using value_type = E;
  9. // using reference = const E&;
  10. // using const_reference = const E&;
  11. // using size_type = size_t;
  12. // using iterator = const E*;
  13. // using const_iterator = const E*;
  14. // constexpr my_initializer_list() noexcept;
  15. // constexpr size_t size() const noexcept; // number of elements
  16. // constexpr const E* begin() const noexcept; // first element
  17. // constexpr const E* end() const noexcept; // one past the last element
  18. // };
  19. // // initializer list range access
  20. // template<class E> constexpr const E* begin(my_initializer_list<E> il) noexcept;
  21. // template<class E> constexpr const E* end(my_initializer_list<E> il) noexcept;
  22. template <typename type>
  23. struct Array_List {
  24. type* data;
  25. u32 length;
  26. u32 next_index;
  27. Array_List(u32 initial_capacity = 16) {
  28. data = (type*)malloc(initial_capacity * sizeof(type));
  29. next_index = 0;
  30. length = initial_capacity;
  31. }
  32. Array_List(std::initializer_list<type> list)
  33. : Array_List(list.size())
  34. {
  35. for (type e : list) {
  36. append(e);
  37. }
  38. }
  39. Array_List(Array_List<type>&& other) {
  40. data = other.data;
  41. length = other.length;
  42. next_index = other.next_index;
  43. other.data = nullptr;
  44. }
  45. ~Array_List() {
  46. free(data);
  47. data = nullptr;
  48. }
  49. void clear() {
  50. next_index = 0;
  51. }
  52. Array_List<type> clone() {
  53. Array_List<type> ret;
  54. ret.length = length;
  55. ret.next_index = next_index;
  56. ret.data = (type*)malloc(length * sizeof(type));
  57. for (u32 i = 0; i < next_index; ++i) {
  58. ret.data[i] = data[i];
  59. }
  60. return ret;
  61. }
  62. type* begin() {
  63. return data;
  64. }
  65. type* end() {
  66. return data+(next_index);
  67. }
  68. void remove_index(u32 index) {
  69. data[index] = data[--next_index];
  70. }
  71. void append(type element) {
  72. if (next_index == length) {
  73. length *= 2;
  74. data = (type*)realloc(data, length * sizeof(type));
  75. }
  76. data[next_index] = std::move(element);
  77. next_index++;
  78. }
  79. void reserve(u32 count) {
  80. if (next_index+count >= (u32)length) {
  81. length *= 2;
  82. data = (type*)realloc(data, length * sizeof(type));
  83. }
  84. }
  85. type& operator[](u32 index) {
  86. return data[index];
  87. }
  88. void _merge(u32 start, u32 mid, u32 end) {
  89. u32 start2 = mid + 1;
  90. /* If the direct merge is already sorted */
  91. if ((size_t)data[mid] <= (size_t)data[start2]) {
  92. return;
  93. }
  94. /* Two pointers to maintain start of both arrays to merge */
  95. while (start <= mid && start2 <= end) {
  96. if ((size_t)data[start] <= (size_t)data[start2]) {
  97. start++;
  98. }
  99. else {
  100. type value = data[start2];
  101. u32 index = start2;
  102. /* Shift all the elements between element 1; element 2, right by 1. */
  103. while (index != start) {
  104. data[index] = data[index - 1];
  105. index--;
  106. }
  107. data[start] = value;
  108. /* Update all the pointers */
  109. start++;
  110. mid++;
  111. start2++;
  112. }
  113. }
  114. }
  115. void sort(s32 left=-1, s32 right=-1) {
  116. if (left == -1) {
  117. if (next_index == 0)
  118. return;
  119. sort(0, next_index - 1);
  120. return;
  121. } else if (left == right) {
  122. return;
  123. }
  124. u32 middle = left + (right-left) / 2;
  125. sort(left, middle);
  126. sort(middle+1, right);
  127. _merge(left, middle, right);
  128. }
  129. u32 sorted_find(type elem, s32 left=-1, s32 right=-1) {
  130. if (left == -1) {
  131. return sorted_find(elem, 0, next_index - 1);
  132. } else if (left == right) {
  133. if ((size_t)data[left] == (size_t)elem)
  134. return left;
  135. return -1;
  136. } else if (right < left)
  137. return -1;
  138. u32 middle = left + (right-left) / 2;
  139. if ((size_t)data[middle] < (size_t)elem)
  140. return sorted_find(elem, middle+1, right);
  141. if ((size_t)data[middle] > (size_t)elem)
  142. return sorted_find(elem, left, middle-1);
  143. return middle;
  144. }
  145. };