slice.odin 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. package slice
  2. import "intrinsics"
  3. import "core:math/bits"
  4. import "core:mem"
  5. _ :: intrinsics;
  6. _ :: bits;
  7. _ :: mem;
  8. swap :: proc(array: $T/[]$E, a, b: int, loc := #caller_location) {
  9. when size_of(E) > 8 {
  10. ptr_swap_non_overlapping(&array[a], &array[b], size_of(E));
  11. } else {
  12. array[a], array[b] = array[b], array[a];
  13. }
  14. }
  15. reverse :: proc(array: $T/[]$E) {
  16. n := len(array)/2;
  17. for i in 0..<n {
  18. a, b := i, len(array)-i-1;
  19. array[a], array[b] = array[b], array[a];
  20. }
  21. }
  22. contains :: proc(array: $T/[]$E, value: E) -> bool where intrinsics.type_is_comparable(E) {
  23. _, found := linear_search(array, value);
  24. return found;
  25. }
  26. linear_search :: proc(array: $A/[]$T, key: T) -> (index: int, found: bool)
  27. where intrinsics.type_is_comparable(T) #no_bounds_check {
  28. for x, i in array {
  29. if x == key {
  30. return i, true;
  31. }
  32. }
  33. return -1, false;
  34. }
  35. linear_search_proc :: proc(array: $A/[]$T, f: proc(T) -> bool) -> (index: int, found: bool) #no_bounds_check {
  36. for x, i in array {
  37. if f(x) {
  38. return i, true;
  39. }
  40. }
  41. return -1, false;
  42. }
  43. binary_search :: proc(array: $A/[]$T, key: T) -> (index: int, found: bool)
  44. where intrinsics.type_is_ordered(T) #no_bounds_check {
  45. n := len(array);
  46. switch n {
  47. case 0:
  48. return -1, false;
  49. case 1:
  50. if array[0] == key {
  51. return 0, true;
  52. }
  53. return -1, false;
  54. }
  55. lo, hi := 0, n-1;
  56. for array[hi] != array[lo] && key >= array[lo] && key <= array[hi] {
  57. when intrinsics.type_is_ordered_numeric(T) {
  58. // NOTE(bill): This is technically interpolation search
  59. m := lo + int((key - array[lo]) * T(hi - lo) / (array[hi] - array[lo]));
  60. } else {
  61. m := lo + (hi - lo)/2;
  62. }
  63. switch {
  64. case array[m] < key:
  65. lo = m + 1;
  66. case key < array[m]:
  67. hi = m - 1;
  68. case:
  69. return m, true;
  70. }
  71. }
  72. if key == array[lo] {
  73. return lo, true;
  74. }
  75. return -1, false;
  76. }
  77. equal :: proc(a, b: $T/[]$E) -> bool where intrinsics.type_is_comparable(E) {
  78. if len(a) != len(b) {
  79. return false;
  80. }
  81. when intrinsics.type_is_simple_compare(E) {
  82. return mem.compare_ptrs(raw_data(a), raw_data(b), len(a)*size_of(E)) == 0;
  83. } else {
  84. for i in 0..<len(a) {
  85. if a[i] != b[i] {
  86. return false;
  87. }
  88. }
  89. return true;
  90. }
  91. }
  92. simple_equal :: proc(a, b: $T/[]$E) -> bool where intrinsics.type_is_simple_compare(E) {
  93. if len(a) != len(b) {
  94. return false;
  95. }
  96. return mem.compare_ptrs(raw_data(a), raw_data(b), len(a)*size_of(E)) == 0;
  97. }
  98. has_prefix :: proc(array: $T/[]$E, needle: E) -> bool where intrinsics.type_is_comparable(E) {
  99. n := len(needle);
  100. if len(array) >= n {
  101. return equal(array[:n], needle);
  102. }
  103. return false;
  104. }
  105. has_suffix :: proc(array: $T/[]$E, needle: E) -> bool where intrinsics.type_is_comparable(E) {
  106. array := array;
  107. m, n := len(array), len(needle);
  108. if m >= n {
  109. return equal(array[m-n:], needle);
  110. }
  111. return false;
  112. }
  113. fill :: proc(array: $T/[]$E, value: E) {
  114. for _, i in array {
  115. array[i] = value;
  116. }
  117. }
  118. rotate_left :: proc(array: $T/[]$E, mid: int) {
  119. n := len(array);
  120. m := mid %% n;
  121. k := n - m;
  122. p := raw_data(array);
  123. ptr_rotate(mid, ptr_add(p, mid), k);
  124. }
  125. rotate_right :: proc(array: $T/[]$E, k: int) {
  126. rotate_left(array, -k);
  127. }
  128. swap_with_slice :: proc(a, b: $T/[]$E, loc := #caller_location) {
  129. assert(len(a) == len(b), "miss matching slice lengths", loc);
  130. ptr_swap_non_overlapping(raw_data(a), raw_data(b), len(a)*size_of(E));
  131. }
  132. concatenate :: proc(a: []$T/[]$E, allocator := context.allocator) -> (res: T) {
  133. if len(a) == 0 {
  134. return;
  135. }
  136. n := 0;
  137. for s in a {
  138. n += len(s);
  139. }
  140. res = make(T, n, allocator);
  141. i := 0;
  142. for s in a {
  143. i += copy(res[i:], s);
  144. }
  145. return;
  146. }
  147. // copies slice into a new dynamic array
  148. clone :: proc(a: $T/[]$E, allocator := context.allocator) -> []E {
  149. d := make([]E, len(a), allocator);
  150. copy(d[:], a);
  151. return d;
  152. }
  153. // copies slice into a new dynamic array
  154. to_dynamic :: proc(a: $T/[]$E, allocator := context.allocator) -> [dynamic]E {
  155. d := make([dynamic]E, len(a), allocator);
  156. copy(d[:], a);
  157. return d;
  158. }
  159. // Converts slice into a dynamic array without cloning or allocating memory
  160. into_dynamic :: proc(a: $T/[]$E) -> [dynamic]E {
  161. s := transmute(mem.Raw_Slice)a;
  162. d := mem.Raw_Dynamic_Array{
  163. data = s.data,
  164. len = 0,
  165. cap = s.len,
  166. allocator = mem.nil_allocator(),
  167. };
  168. return transmute([dynamic]E)d;
  169. }
  170. length :: proc(a: $T/[]$E) -> int {
  171. return len(a);
  172. }
  173. is_empty :: proc(a: $T/[]$E) -> bool {
  174. return len(a) == 0;
  175. }
  176. split_at :: proc(array: $T/[]$E, index: int) -> (a, b: T) {
  177. return array[:index], array[index:];
  178. }
  179. split_first :: proc(array: $T/[]$E) -> (first: E, rest: T) {
  180. return array[0], array[1:];
  181. }
  182. split_last :: proc(array: $T/[]$E) -> (rest: T, last: E) {
  183. n := len(array)-1;
  184. return array[:n], array[n];
  185. }
  186. first :: proc(array: $T/[]$E) -> E {
  187. return array[0];
  188. }
  189. last :: proc(array: $T/[]$E) -> E {
  190. return array[len(array)-1];
  191. }
  192. first_ptr :: proc(array: $T/[]$E) -> ^E {
  193. if len(array) != 0 {
  194. return &array[0];
  195. }
  196. return nil;
  197. }
  198. last_ptr :: proc(array: $T/[]$E) -> ^E {
  199. if len(array) != 0 {
  200. return &array[len(array)-1];
  201. }
  202. return nil;
  203. }
  204. get :: proc(array: $T/[]$E, index: int) -> (value: E, ok: bool) {
  205. if 0 <= index && index < len(array) {
  206. value = array[index];
  207. ok = true;
  208. }
  209. return;
  210. }
  211. get_ptr :: proc(array: $T/[]$E, index: int) -> (value: ^E, ok: bool) {
  212. if 0 <= index && index < len(array) {
  213. value = &array[index];
  214. ok = true;
  215. }
  216. return;
  217. }
  218. as_ptr :: proc(array: $T/[]$E) -> ^E {
  219. return raw_data(array);
  220. }
  221. mapper :: proc(s: $S/[]$U, f: proc(U) -> $V, allocator := context.allocator) -> []V {
  222. r := make([]V, len(s), allocator);
  223. for v, i in s {
  224. r[i] = f(v);
  225. }
  226. return r;
  227. }
  228. reduce :: proc(s: $S/[]$U, initializer: $V, f: proc(V, U) -> V) -> V {
  229. r := initializer;
  230. for v in s {
  231. r = f(r, v);
  232. }
  233. return r;
  234. }
  235. filter :: proc(s: $S/[]$U, f: proc(U) -> bool, allocator := context.allocator) -> S {
  236. r := make([dynamic]U, 0, 0, allocator);
  237. for v in s {
  238. if f(v) {
  239. append(&r, v);
  240. }
  241. }
  242. return r[:];
  243. }
  244. dot_product :: proc(a, b: $S/[]$T) -> T
  245. where intrinsics.type_is_numeric(T) {
  246. if len(a) != len(b) {
  247. panic("slice.dot_product: slices of unequal length");
  248. }
  249. r: T;
  250. #no_bounds_check for _, i in a {
  251. r += a[i] * b[i];
  252. }
  253. return r;
  254. }