doc.odin 1.4 KB

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