slice.odin 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. package slice
  2. import "core:intrinsics"
  3. import "core:builtin"
  4. import "core:math/bits"
  5. import "core:runtime"
  6. _ :: intrinsics
  7. _ :: builtin
  8. _ :: bits
  9. _ :: runtime
  10. /*
  11. Turn a pointer and a length into a slice.
  12. */
  13. @(require_results)
  14. from_ptr :: proc "contextless" (ptr: ^$T, count: int) -> []T {
  15. return ([^]T)(ptr)[:count]
  16. }
  17. /*
  18. Turn a pointer and a length into a byte slice.
  19. */
  20. @(require_results)
  21. bytes_from_ptr :: proc "contextless" (ptr: rawptr, byte_count: int) -> []byte {
  22. return ([^]byte)(ptr)[:byte_count]
  23. }
  24. /*
  25. Turn a slice into a byte slice.
  26. See `slice.reinterpret` to go the other way.
  27. */
  28. @(require_results)
  29. to_bytes :: proc "contextless" (s: []$T) -> []byte {
  30. return ([^]byte)(raw_data(s))[:len(s) * size_of(T)]
  31. }
  32. /*
  33. Turn a slice of one type, into a slice of another type.
  34. Only converts the type and length of the slice itself.
  35. The length is rounded down to the nearest whole number of items.
  36. ```
  37. large_items := []i64{1, 2, 3, 4}
  38. small_items := slice.reinterpret([]i32, large_items)
  39. assert(len(small_items) == 8)
  40. ```
  41. ```
  42. small_items := []byte{1, 0, 0, 0, 0, 0, 0, 0,
  43. 2, 0, 0, 0}
  44. large_items := slice.reinterpret([]i64, small_items)
  45. assert(len(large_items) == 1) // only enough bytes to make 1 x i64; two would need at least 8 bytes.
  46. ```
  47. */
  48. @(require_results)
  49. reinterpret :: proc "contextless" ($T: typeid/[]$U, s: []$V) -> []U {
  50. when size_of(U) == 0 || size_of(V) == 0 {
  51. return nil
  52. } else {
  53. bytes := to_bytes(s)
  54. n := len(bytes) / size_of(U)
  55. return ([^]U)(raw_data(bytes))[:n]
  56. }
  57. }
  58. swap :: proc(array: $T/[]$E, a, b: int) {
  59. when size_of(E) > 8 {
  60. ptr_swap_non_overlapping(&array[a], &array[b], size_of(E))
  61. } else {
  62. array[a], array[b] = array[b], array[a]
  63. }
  64. }
  65. swap_between :: proc(a, b: $T/[]$E) {
  66. n := builtin.min(len(a), len(b))
  67. if n >= 0 {
  68. ptr_swap_overlapping(&a[0], &b[0], size_of(E)*n)
  69. }
  70. }
  71. reverse :: proc(array: $T/[]$E) {
  72. n := len(array)/2
  73. for i in 0..<n {
  74. swap(array, i, len(array)-i-1)
  75. }
  76. }
  77. @(require_results)
  78. contains :: proc(array: $T/[]$E, value: E) -> bool where intrinsics.type_is_comparable(E) {
  79. _, found := linear_search(array, value)
  80. return found
  81. }
  82. @(require_results)
  83. linear_search :: proc(array: $A/[]$T, key: T) -> (index: int, found: bool)
  84. where intrinsics.type_is_comparable(T) #no_bounds_check {
  85. for x, i in array {
  86. if x == key {
  87. return i, true
  88. }
  89. }
  90. return -1, false
  91. }
  92. @(require_results)
  93. linear_search_proc :: proc(array: $A/[]$T, f: proc(T) -> bool) -> (index: int, found: bool) #no_bounds_check {
  94. for x, i in array {
  95. if f(x) {
  96. return i, true
  97. }
  98. }
  99. return -1, false
  100. }
  101. @(require_results)
  102. binary_search :: proc(array: $A/[]$T, key: T) -> (index: int, found: bool)
  103. where intrinsics.type_is_ordered(T) #no_bounds_check {
  104. n := len(array)
  105. switch n {
  106. case 0:
  107. return -1, false
  108. case 1:
  109. if array[0] == key {
  110. return 0, true
  111. }
  112. return -1, false
  113. }
  114. lo, hi := 0, n-1
  115. for array[hi] != array[lo] && key >= array[lo] && key <= array[hi] {
  116. when intrinsics.type_is_ordered_numeric(T) {
  117. // NOTE(bill): This is technically interpolation search
  118. m := lo + int((key - array[lo]) * T(hi - lo) / (array[hi] - array[lo]))
  119. } else {
  120. m := lo + (hi - lo)/2
  121. }
  122. switch {
  123. case array[m] < key:
  124. lo = m + 1
  125. case key < array[m]:
  126. hi = m - 1
  127. case:
  128. return m, true
  129. }
  130. }
  131. if key == array[lo] {
  132. return lo, true
  133. }
  134. return -1, false
  135. }
  136. @(require_results)
  137. equal :: proc(a, b: $T/[]$E) -> bool where intrinsics.type_is_comparable(E) {
  138. if len(a) != len(b) {
  139. return false
  140. }
  141. when intrinsics.type_is_simple_compare(E) {
  142. return runtime.memory_compare(raw_data(a), raw_data(b), len(a)*size_of(E)) == 0
  143. } else {
  144. for i in 0..<len(a) {
  145. if a[i] != b[i] {
  146. return false
  147. }
  148. }
  149. return true
  150. }
  151. }
  152. @(require_results)
  153. simple_equal :: proc(a, b: $T/[]$E) -> bool where intrinsics.type_is_simple_compare(E) {
  154. if len(a) != len(b) {
  155. return false
  156. }
  157. return runtime.memory_compare(raw_data(a), raw_data(b), len(a)*size_of(E)) == 0
  158. }
  159. /*
  160. return the prefix length common between slices `a` and `b`.
  161. slice.prefix_length([]u8{1, 2, 3, 4}, []u8{1}) -> 1
  162. slice.prefix_length([]u8{1, 2, 3, 4}, []u8{1, 2, 3}) -> 3
  163. slice.prefix_length([]u8{1, 2, 3, 4}, []u8{2, 3, 4}) -> 0
  164. */
  165. @(require_results)
  166. prefix_length :: proc(a, b: $T/[]$E) -> (n: int) where intrinsics.type_is_comparable(E) {
  167. _len := builtin.min(len(a), len(b))
  168. #no_bounds_check for n < _len && a[n] == b[n] {
  169. n += 1
  170. }
  171. return
  172. }
  173. @(require_results)
  174. has_prefix :: proc(array: $T/[]$E, needle: E) -> bool where intrinsics.type_is_comparable(E) {
  175. n := len(needle)
  176. if len(array) >= n {
  177. return equal(array[:n], needle)
  178. }
  179. return false
  180. }
  181. @(require_results)
  182. has_suffix :: proc(array: $T/[]$E, needle: E) -> bool where intrinsics.type_is_comparable(E) {
  183. array := array
  184. m, n := len(array), len(needle)
  185. if m >= n {
  186. return equal(array[m-n:], needle)
  187. }
  188. return false
  189. }
  190. zero :: proc(array: $T/[]$E) #no_bounds_check {
  191. if len(array) > 0 {
  192. intrinsics.mem_zero(raw_data(array), size_of(E)*len(array))
  193. }
  194. }
  195. fill :: proc(array: $T/[]$E, value: E) #no_bounds_check {
  196. if len(array) <= 0 {
  197. return
  198. }
  199. array[0] = value
  200. for i := 1; i < len(array); i *= 2 {
  201. copy(array[i:], array[:i])
  202. }
  203. }
  204. rotate_left :: proc(array: $T/[]$E, mid: int) {
  205. n := len(array)
  206. m := mid %% n
  207. k := n - m
  208. // FIXME: (ap29600) this cast is a temporary fix for the compiler not matching
  209. // [^T] with $P/^$T
  210. p := cast(^E)raw_data(array)
  211. ptr_rotate(m, ptr_add(p, m), k)
  212. }
  213. rotate_right :: proc(array: $T/[]$E, k: int) {
  214. rotate_left(array, -k)
  215. }
  216. swap_with_slice :: proc(a, b: $T/[]$E, loc := #caller_location) {
  217. assert(len(a) == len(b), "miss matching slice lengths", loc)
  218. ptr_swap_non_overlapping(raw_data(a), raw_data(b), len(a)*size_of(E))
  219. }
  220. @(require_results)
  221. concatenate :: proc(a: []$T/[]$E, allocator := context.allocator) -> (res: T, err: runtime.Allocator_Error) #optional_allocator_error {
  222. if len(a) == 0 {
  223. return
  224. }
  225. n := 0
  226. for s in a {
  227. n += len(s)
  228. }
  229. res = make(T, n, allocator) or_return
  230. i := 0
  231. for s in a {
  232. i += copy(res[i:], s)
  233. }
  234. return
  235. }
  236. // copies a slice into a new slice
  237. @(require_results)
  238. clone :: proc(a: $T/[]$E, allocator := context.allocator, loc := #caller_location) -> ([]E, runtime.Allocator_Error) #optional_allocator_error {
  239. d, err := make([]E, len(a), allocator, loc)
  240. copy(d[:], a)
  241. return d, err
  242. }
  243. // copies slice into a new dynamic array
  244. clone_to_dynamic :: proc(a: $T/[]$E, allocator := context.allocator, loc := #caller_location) -> ([dynamic]E, runtime.Allocator_Error) #optional_allocator_error {
  245. d, err := make([dynamic]E, len(a), allocator, loc)
  246. copy(d[:], a)
  247. return d, err
  248. }
  249. to_dynamic :: clone_to_dynamic
  250. // Converts slice into a dynamic array without cloning or allocating memory
  251. @(require_results)
  252. into_dynamic :: proc(a: $T/[]$E) -> [dynamic]E {
  253. s := transmute(runtime.Raw_Slice)a
  254. d := runtime.Raw_Dynamic_Array{
  255. data = s.data,
  256. len = 0,
  257. cap = s.len,
  258. allocator = runtime.nil_allocator(),
  259. }
  260. return transmute([dynamic]E)d
  261. }
  262. @(require_results)
  263. length :: proc(a: $T/[]$E) -> int {
  264. return len(a)
  265. }
  266. @(require_results)
  267. is_empty :: proc(a: $T/[]$E) -> bool {
  268. return len(a) == 0
  269. }
  270. @(require_results)
  271. split_at :: proc(array: $T/[]$E, index: int) -> (a, b: T) {
  272. return array[:index], array[index:]
  273. }
  274. @(require_results)
  275. split_first :: proc(array: $T/[]$E) -> (first: E, rest: T) {
  276. return array[0], array[1:]
  277. }
  278. @(require_results)
  279. split_last :: proc(array: $T/[]$E) -> (rest: T, last: E) {
  280. n := len(array)-1
  281. return array[:n], array[n]
  282. }
  283. @(require_results)
  284. first :: proc(array: $T/[]$E) -> E {
  285. return array[0]
  286. }
  287. @(require_results)
  288. last :: proc(array: $T/[]$E) -> E {
  289. return array[len(array)-1]
  290. }
  291. @(require_results)
  292. first_ptr :: proc(array: $T/[]$E) -> ^E {
  293. if len(array) != 0 {
  294. return &array[0]
  295. }
  296. return nil
  297. }
  298. @(require_results)
  299. last_ptr :: proc(array: $T/[]$E) -> ^E {
  300. if len(array) != 0 {
  301. return &array[len(array)-1]
  302. }
  303. return nil
  304. }
  305. @(require_results)
  306. get :: proc(array: $T/[]$E, index: int) -> (value: E, ok: bool) {
  307. if uint(index) < len(array) {
  308. value = array[index]
  309. ok = true
  310. }
  311. return
  312. }
  313. @(require_results)
  314. get_ptr :: proc(array: $T/[]$E, index: int) -> (value: ^E, ok: bool) {
  315. if uint(index) < len(array) {
  316. value = &array[index]
  317. ok = true
  318. }
  319. return
  320. }
  321. @(require_results)
  322. as_ptr :: proc(array: $T/[]$E) -> [^]E {
  323. return raw_data(array)
  324. }
  325. @(require_results)
  326. mapper :: proc(s: $S/[]$U, f: proc(U) -> $V, allocator := context.allocator) -> (r: []V, err: runtime.Allocator_Error) #optional_allocator_error {
  327. r = make([]V, len(s), allocator) or_return
  328. for v, i in s {
  329. r[i] = f(v)
  330. }
  331. return
  332. }
  333. @(require_results)
  334. reduce :: proc(s: $S/[]$U, initializer: $V, f: proc(V, U) -> V) -> V {
  335. r := initializer
  336. for v in s {
  337. r = f(r, v)
  338. }
  339. return r
  340. }
  341. @(require_results)
  342. filter :: proc(s: $S/[]$U, f: proc(U) -> bool, allocator := context.allocator) -> S {
  343. r := make([dynamic]U, 0, 0, allocator)
  344. for v in s {
  345. if f(v) {
  346. append(&r, v)
  347. }
  348. }
  349. return r[:]
  350. }
  351. @(require_results)
  352. scanner :: proc (s: $S/[]$U, initializer: $V, f: proc(V, U) -> V, allocator := context.allocator) -> (res: []V, err: runtime.Allocator_Error) #optional_allocator_error {
  353. if len(s) == 0 { return }
  354. res = make([]V, len(s), allocator) or_return
  355. p := as_ptr(s)
  356. q := as_ptr(res)
  357. r := initializer
  358. for l := len(s); l > 0; l -= 1 {
  359. r = f(r, p[0])
  360. q[0] = r
  361. p = p[1:]
  362. q = q[1:]
  363. }
  364. return
  365. }
  366. @(require_results)
  367. min :: proc(s: $S/[]$T) -> (res: T, ok: bool) where intrinsics.type_is_ordered(T) #optional_ok {
  368. if len(s) != 0 {
  369. res = s[0]
  370. ok = true
  371. for v in s[1:] {
  372. res = builtin.min(res, v)
  373. }
  374. }
  375. return
  376. }
  377. @(require_results)
  378. max :: proc(s: $S/[]$T) -> (res: T, ok: bool) where intrinsics.type_is_ordered(T) #optional_ok {
  379. if len(s) != 0 {
  380. res = s[0]
  381. ok = true
  382. for v in s[1:] {
  383. res = builtin.max(res, v)
  384. }
  385. }
  386. return
  387. }
  388. @(require_results)
  389. min_max :: proc(s: $S/[]$T) -> (min, max: T, ok: bool) where intrinsics.type_is_ordered(T) {
  390. if len(s) != 0 {
  391. min, max = s[0], s[0]
  392. ok = true
  393. for v in s[1:] {
  394. min = builtin.min(min, v)
  395. max = builtin.max(max, v)
  396. }
  397. }
  398. return
  399. }
  400. @(require_results)
  401. any_of :: proc(s: $S/[]$T, value: T) -> bool where intrinsics.type_is_comparable(T) {
  402. for v in s {
  403. if v == value {
  404. return true
  405. }
  406. }
  407. return false
  408. }
  409. @(require_results)
  410. none_of :: proc(s: $S/[]$T, value: T) -> bool where intrinsics.type_is_comparable(T) {
  411. for v in s {
  412. if v == value {
  413. return false
  414. }
  415. }
  416. return true
  417. }
  418. @(require_results)
  419. all_of :: proc(s: $S/[]$T, value: T) -> bool where intrinsics.type_is_comparable(T) {
  420. if len(s) == 0 {
  421. return false
  422. }
  423. for v in s {
  424. if v != value {
  425. return false
  426. }
  427. }
  428. return true
  429. }
  430. @(require_results)
  431. any_of_proc :: proc(s: $S/[]$T, f: proc(T) -> bool) -> bool {
  432. for v in s {
  433. if f(v) {
  434. return true
  435. }
  436. }
  437. return false
  438. }
  439. @(require_results)
  440. none_of_proc :: proc(s: $S/[]$T, f: proc(T) -> bool) -> bool {
  441. for v in s {
  442. if f(v) {
  443. return false
  444. }
  445. }
  446. return true
  447. }
  448. @(require_results)
  449. all_of_proc :: proc(s: $S/[]$T, f: proc(T) -> bool) -> bool {
  450. if len(s) == 0 {
  451. return false
  452. }
  453. for v in s {
  454. if !f(v) {
  455. return false
  456. }
  457. }
  458. return true
  459. }
  460. @(require_results)
  461. count :: proc(s: $S/[]$T, value: T) -> (n: int) where intrinsics.type_is_comparable(T) {
  462. for v in s {
  463. if v == value {
  464. n += 1
  465. }
  466. }
  467. return
  468. }
  469. @(require_results)
  470. count_proc :: proc(s: $S/[]$T, f: proc(T) -> bool) -> (n: int) {
  471. for v in s {
  472. if f(v) {
  473. n += 1
  474. }
  475. }
  476. return
  477. }
  478. @(require_results)
  479. dot_product :: proc(a, b: $S/[]$T) -> (r: T, ok: bool)
  480. where intrinsics.type_is_numeric(T) {
  481. if len(a) != len(b) {
  482. return
  483. }
  484. #no_bounds_check for _, i in a {
  485. r += a[i] * b[i]
  486. }
  487. return r, true
  488. }
  489. // Convert a pointer to an enumerated array to a slice of the element type
  490. @(require_results)
  491. enumerated_array :: proc(ptr: ^$T) -> []intrinsics.type_elem_type(T)
  492. where intrinsics.type_is_enumerated_array(T) {
  493. return ([^]intrinsics.type_elem_type(T))(ptr)[:len(T)]
  494. }