slice.odin 6.4 KB

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