aboutsummaryrefslogtreecommitdiff
path: root/core/container/small_array
diff options
context:
space:
mode:
authorgingerBill <gingerBill@users.noreply.github.com>2025-10-30 10:54:26 +0000
committergingerBill <gingerBill@users.noreply.github.com>2025-10-30 10:54:26 +0000
commit35a28053b8779ea7ab9845c31ad52fd55888667a (patch)
tree0808c843adc86b33da72bfa2739ca0f5597a6532 /core/container/small_array
parentbb44b02b3e220d7019aba8b547a6d3e6fbd49dba (diff)
parent074a8d7df5e024117d7f91944ccf053cad27ca0e (diff)
Merge branch 'master' into vendor/curl
Diffstat (limited to 'core/container/small_array')
-rw-r--r--core/container/small_array/doc.odin9
-rw-r--r--core/container/small_array/small_array.odin66
2 files changed, 60 insertions, 15 deletions
diff --git a/core/container/small_array/doc.odin b/core/container/small_array/doc.odin
index f3e9acd57..21d000a10 100644
--- a/core/container/small_array/doc.odin
+++ b/core/container/small_array/doc.odin
@@ -1,8 +1,7 @@
/*
-Package small_array implements a dynamic array like
-interface on a stack-allocated, fixed-size array.
+A dynamic array-like interface on a stack-allocated, fixed-size array.
-The Small_Array type is optimal for scenarios where you need
+The `Small_Array` type is optimal for scenarios where you need
a container for a fixed number of elements of a specific type,
with the total number known at compile time but the exact
number to be used determined at runtime.
@@ -33,7 +32,7 @@ Example:
return
}
- // the Small_Array can be an ordinary parameter 'generic' over
+ // the `Small_Array` can be an ordinary parameter 'generic' over
// the actual length to be usable with different sizes
print_elements :: proc(arr: ^small_array.Small_Array($N, rune)) {
for r in small_array.slice(arr) {
@@ -52,4 +51,4 @@ Output:
Hellope
*/
-package container_small_array
+package container_small_array \ No newline at end of file
diff --git a/core/container/small_array/small_array.odin b/core/container/small_array/small_array.odin
index 49d441079..1d9795db8 100644
--- a/core/container/small_array/small_array.odin
+++ b/core/container/small_array/small_array.odin
@@ -1,8 +1,8 @@
package container_small_array
import "base:builtin"
-import "base:runtime"
-_ :: runtime
+@require import "base:intrinsics"
+@require import "base:runtime"
/*
A fixed-size stack-allocated array operated on in a dynamic fashion.
@@ -169,7 +169,7 @@ Output:
x
*/
-get_safe :: proc(a: $A/Small_Array($N, $T), index: int) -> (T, bool) #no_bounds_check {
+get_safe :: proc "contextless" (a: $A/Small_Array($N, $T), index: int) -> (T, bool) #no_bounds_check {
if index < 0 || index >= a.len {
return {}, false
}
@@ -183,11 +183,11 @@ Get a pointer to the item at the specified position.
- `a`: A pointer to the small-array
- `index`: The position of the item to get
-**Returns**
+**Returns**
- the pointer to the element at the specified position
- true if element exists, false otherwise
*/
-get_ptr_safe :: proc(a: ^$A/Small_Array($N, $T), index: int) -> (^T, bool) #no_bounds_check {
+get_ptr_safe :: proc "contextless" (a: ^$A/Small_Array($N, $T), index: int) -> (^T, bool) #no_bounds_check {
if index < 0 || index >= a.len {
return {}, false
}
@@ -231,7 +231,7 @@ Example:
fmt.println(small_array.slice(&a))
// resizing makes the change visible
- small_array.resize(&a, 100)
+ small_array.non_zero_resize(&a, 100)
fmt.println(small_array.slice(&a))
}
@@ -250,6 +250,8 @@ set :: proc "contextless" (a: ^$A/Small_Array($N, $T), index: int, item: T) {
/*
Tries to resize the small-array to the specified length.
+The memory of added elements will be zeroed out.
+
The new length will be:
- `length` if `length` <= capacity
- capacity if length > capacity
@@ -259,7 +261,7 @@ The new length will be:
- `length`: The new desired length
Example:
-
+
import "core:container/small_array"
import "core:fmt"
@@ -269,7 +271,7 @@ Example:
small_array.push_back(&a, 1)
small_array.push_back(&a, 2)
fmt.println(small_array.slice(&a))
-
+
small_array.resize(&a, 1)
fmt.println(small_array.slice(&a))
@@ -278,12 +280,56 @@ Example:
}
Output:
-
+
+ [1, 2]
+ [1]
+ [1, 0, 0, 0, 0]
+*/
+resize :: proc "contextless" (a: ^$A/Small_Array($N, $T), length: int) {
+ prev_len := a.len
+ a.len = min(length, builtin.len(a.data))
+ if prev_len < a.len {
+ intrinsics.mem_zero(&a.data[prev_len], size_of(T)*(a.len-prev_len))
+ }
+}
+
+/*
+Tries to resize the small-array to the specified length.
+
+The new length will be:
+ - `length` if `length` <= capacity
+ - capacity if length > capacity
+
+**Inputs**
+- `a`: A pointer to the small-array
+- `length`: The new desired length
+
+Example:
+
+ import "core:container/small_array"
+ import "core:fmt"
+
+ non_zero_resize :: proc() {
+ a: small_array.Small_Array(5, int)
+
+ small_array.push_back(&a, 1)
+ small_array.push_back(&a, 2)
+ fmt.println(small_array.slice(&a))
+
+ small_array.non_zero_resize(&a, 1)
+ fmt.println(small_array.slice(&a))
+
+ small_array.non_zero_resize(&a, 100)
+ fmt.println(small_array.slice(&a))
+ }
+
+Output:
+
[1, 2]
[1]
[1, 2, 0, 0, 0]
*/
-resize :: proc "contextless" (a: ^$A/Small_Array, length: int) {
+non_zero_resize :: proc "contextless" (a: ^$A/Small_Array, length: int) {
a.len = min(length, builtin.len(a.data))
}