From 3e62c2c79a7c3ffe41688733c4c13ead2a43e05b Mon Sep 17 00:00:00 2001 From: Damian Tarnawski Date: Sun, 14 Sep 2025 10:39:33 +0200 Subject: Add "contextless" to small_array get_safe and get_ptr_safe --- core/container/small_array/small_array.odin | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'core/container/small_array/small_array.odin') diff --git a/core/container/small_array/small_array.odin b/core/container/small_array/small_array.odin index 49d441079..124307c52 100644 --- a/core/container/small_array/small_array.odin +++ b/core/container/small_array/small_array.odin @@ -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 } -- cgit v1.2.3 From 3d66625de0b3fe9cceb187b4c7ff8bb767d7babc Mon Sep 17 00:00:00 2001 From: Damian Tarnawski Date: Mon, 15 Sep 2025 14:40:58 +0200 Subject: Zero memory in small_array.resize and add non_zero_resize --- core/container/small_array/small_array.odin | 53 +++++++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 3 deletions(-) (limited to 'core/container/small_array/small_array.odin') diff --git a/core/container/small_array/small_array.odin b/core/container/small_array/small_array.odin index 49d441079..d2f0f8130 100644 --- a/core/container/small_array/small_array.odin +++ b/core/container/small_array/small_array.odin @@ -2,6 +2,7 @@ package container_small_array import "base:builtin" import "base:runtime" +import "core:mem" _ :: runtime /* @@ -250,6 +251,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 +262,7 @@ The new length will be: - `length`: The new desired length Example: - + import "core:container/small_array" import "core:fmt" @@ -269,7 +272,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 +281,56 @@ Example: } Output: - + [1, 2] [1] [1, 2, 0, 0, 0] */ resize :: proc "contextless" (a: ^$A/Small_Array, length: int) { + prev_len := a.len + a.len = min(length, builtin.len(a.data)) + if prev_len < a.len { + mem.zero_slice(a.data[prev_len:a.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" + + resize_example :: 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.resize(&a, 1) + fmt.println(small_array.slice(&a)) + + small_array.resize(&a, 100) + fmt.println(small_array.slice(&a)) + } + +Output: + + [1, 2] + [1] + [1, 2, 0, 0, 0] +*/ +non_zero_resize :: proc "contextless" (a: ^$A/Small_Array, length: int) { a.len = min(length, builtin.len(a.data)) } -- cgit v1.2.3 From 7adc33d5a4c4ba9a84ce7ff3f99b3db65db1eab1 Mon Sep 17 00:00:00 2001 From: Damian Tarnawski Date: Mon, 15 Sep 2025 14:56:46 +0200 Subject: Add `@require` to core:mem import in small_array --- core/container/small_array/small_array.odin | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'core/container/small_array/small_array.odin') diff --git a/core/container/small_array/small_array.odin b/core/container/small_array/small_array.odin index d2f0f8130..31cc8315e 100644 --- a/core/container/small_array/small_array.odin +++ b/core/container/small_array/small_array.odin @@ -1,9 +1,8 @@ package container_small_array import "base:builtin" -import "base:runtime" -import "core:mem" -_ :: runtime +@require import "base:runtime" +@require import "core:mem" /* A fixed-size stack-allocated array operated on in a dynamic fashion. -- cgit v1.2.3 From b986c534a33cb10c99653b8f7eba63f260ad938f Mon Sep 17 00:00:00 2001 From: Damian Tarnawski Date: Mon, 15 Sep 2025 15:01:20 +0200 Subject: Replace `mem.zero_slice` with `intrinsics.mem_zero` in `small_array.resize` --- core/container/small_array/small_array.odin | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'core/container/small_array/small_array.odin') diff --git a/core/container/small_array/small_array.odin b/core/container/small_array/small_array.odin index 31cc8315e..23d5ad6b7 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" +@require import "base:intrinsics" @require import "base:runtime" -@require import "core:mem" /* A fixed-size stack-allocated array operated on in a dynamic fashion. @@ -285,11 +285,11 @@ Output: [1] [1, 2, 0, 0, 0] */ -resize :: proc "contextless" (a: ^$A/Small_Array, length: int) { +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 { - mem.zero_slice(a.data[prev_len:a.len]) + intrinsics.mem_zero(&a.data[prev_len], size_of(T)*(a.len-prev_len)) } } -- cgit v1.2.3 From 40c8f45a81f33f617ebf276658f465935eb843f6 Mon Sep 17 00:00:00 2001 From: Damian Tarnawski Date: Mon, 15 Sep 2025 15:15:44 +0200 Subject: Correct small_array resize examples --- core/container/small_array/small_array.odin | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'core/container/small_array/small_array.odin') diff --git a/core/container/small_array/small_array.odin b/core/container/small_array/small_array.odin index 23d5ad6b7..b7c72ed7a 100644 --- a/core/container/small_array/small_array.odin +++ b/core/container/small_array/small_array.odin @@ -283,7 +283,7 @@ Output: [1, 2] [1] - [1, 2, 0, 0, 0] + [1, 0, 0, 0, 0] */ resize :: proc "contextless" (a: ^$A/Small_Array($N, $T), length: int) { prev_len := a.len @@ -316,10 +316,10 @@ Example: small_array.push_back(&a, 2) fmt.println(small_array.slice(&a)) - small_array.resize(&a, 1) + small_array.non_zero_resize(&a, 1) fmt.println(small_array.slice(&a)) - small_array.resize(&a, 100) + small_array.non_zero_resize(&a, 100) fmt.println(small_array.slice(&a)) } -- cgit v1.2.3 From e163c20a02a27646ba46fc094a077bb8118d2e1e Mon Sep 17 00:00:00 2001 From: Damian Tarnawski Date: Mon, 15 Sep 2025 15:29:17 +0200 Subject: Correct set_example in small_array --- core/container/small_array/small_array.odin | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'core/container/small_array/small_array.odin') diff --git a/core/container/small_array/small_array.odin b/core/container/small_array/small_array.odin index b7c72ed7a..840a8a6fa 100644 --- a/core/container/small_array/small_array.odin +++ b/core/container/small_array/small_array.odin @@ -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)) } @@ -309,7 +309,7 @@ Example: import "core:container/small_array" import "core:fmt" - resize_example :: proc() { + non_zero_resize :: proc() { a: small_array.Small_Array(5, int) small_array.push_back(&a, 1) -- cgit v1.2.3