Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

135 строки
3.3 KiB

  1. #pragma once
  2. #include <stdlib.h>
  3. #include "types.hpp"
  4. template <typename type>
  5. struct Array_List {
  6. type* data;
  7. u32 length;
  8. u32 next_index;
  9. void alloc(u32 initial_capacity = 16) {
  10. data = (type*)malloc(initial_capacity * sizeof(type));
  11. next_index = 0;
  12. length = initial_capacity;
  13. }
  14. void dealloc() {
  15. free(data);
  16. data = 0;
  17. }
  18. Array_List<type> clone() {
  19. Array_List<type> ret;
  20. ret.length = length;
  21. ret.next_index = next_index;
  22. ret.data = (type*)malloc(length * sizeof(type));
  23. for (u32 i = 0; i < next_index; ++i) {
  24. ret.data[i] = data[i];
  25. }
  26. return ret;
  27. }
  28. type* begin() {
  29. return data;
  30. }
  31. type* end() {
  32. return data+(next_index);
  33. }
  34. void remove_index(u32 index) {
  35. data[index] = data[--next_index];
  36. }
  37. void append(type element) {
  38. if (next_index == length) {
  39. length *= 2;
  40. data = (type*)realloc(data, length * sizeof(type));
  41. }
  42. data[next_index] = element;
  43. next_index++;
  44. }
  45. void reserve(u32 count) {
  46. if (next_index+count >= (u32)length) {
  47. length *= 2;
  48. data = (type*)realloc(data, length * sizeof(type));
  49. }
  50. }
  51. type& operator[](u32 index) {
  52. return data[index];
  53. }
  54. void _merge(u32 start, u32 mid, u32 end) {
  55. u32 start2 = mid + 1;
  56. /* If the direct merge is already sorted */
  57. if ((size_t)data[mid] <= (size_t)data[start2]) {
  58. return;
  59. }
  60. /* Two pointers to maintain start of both arrays to merge */
  61. while (start <= mid && start2 <= end) {
  62. if ((size_t)data[start] <= (size_t)data[start2]) {
  63. start++;
  64. }
  65. else {
  66. type value = data[start2];
  67. u32 index = start2;
  68. /* Shift all the elements between element 1; element 2, right by 1. */
  69. while (index != start) {
  70. data[index] = data[index - 1];
  71. index--;
  72. }
  73. data[start] = value;
  74. /* Update all the pointers */
  75. start++;
  76. mid++;
  77. start2++;
  78. }
  79. }
  80. }
  81. void sort(s32 left=-1, s32 right=-1) {
  82. if (left == -1) {
  83. if (next_index == 0)
  84. return;
  85. sort(0, next_index - 1);
  86. return;
  87. } else if (left == right) {
  88. return;
  89. }
  90. u32 middle = left + (right-left) / 2;
  91. sort(left, middle);
  92. sort(middle+1, right);
  93. _merge(left, middle, right);
  94. }
  95. u32 sorted_find(type elem, s32 left=-1, s32 right=-1) {
  96. if (left == -1) {
  97. return sorted_find(elem, 0, next_index - 1);
  98. } else if (left == right) {
  99. if ((size_t)data[left] == (size_t)elem)
  100. return left;
  101. return -1;
  102. } else if (right < left)
  103. return -1;
  104. u32 middle = left + (right-left) / 2;
  105. if ((size_t)data[middle] < (size_t)elem)
  106. return sorted_find(elem, middle+1, right);
  107. if ((size_t)data[middle] > (size_t)elem)
  108. return sorted_find(elem, left, middle-1);
  109. return middle;
  110. }
  111. };