doc.odin 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. A dynamic array-like interface on a stack-allocated, fixed-size array.
  3. The `Small_Array` type is optimal for scenarios where you need
  4. a container for a fixed number of elements of a specific type,
  5. with the total number known at compile time but the exact
  6. number to be used determined at runtime.
  7. Example:
  8. import "core:fmt"
  9. import "core:container/small_array"
  10. create :: proc() -> (result: small_array.Small_Array(10, rune)) {
  11. // appending single elements
  12. small_array.push(&result, 'e')
  13. // pushing a bunch of elements at once
  14. small_array.push(&result, 'l', 'i', 'x', '-', 'e')
  15. // pre-pending
  16. small_array.push_front(&result, 'H')
  17. // removing elements
  18. small_array.ordered_remove(&result, 4)
  19. // resizing to the desired length (the capacity will stay unchanged)
  20. small_array.resize(&result, 7)
  21. // inserting elements
  22. small_array.inject_at(&result, 'p', 5)
  23. // updating elements
  24. small_array.set(&result, 3, 'l')
  25. // getting pointers to elements
  26. o := small_array.get_ptr(&result, 4)
  27. o^ = 'o'
  28. // and much more ....
  29. return
  30. }
  31. // the `Small_Array` can be an ordinary parameter 'generic' over
  32. // the actual length to be usable with different sizes
  33. print_elements :: proc(arr: ^small_array.Small_Array($N, rune)) {
  34. for r in small_array.slice(arr) {
  35. fmt.print(r)
  36. }
  37. }
  38. main :: proc() {
  39. arr := create()
  40. // ...
  41. print_elements(&arr)
  42. }
  43. Output:
  44. Hellope
  45. */
  46. package container_small_array