aboutsummaryrefslogtreecommitdiff
path: root/builtins/array.c
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-08-15 02:17:53 -0400
committerBruce Hill <bruce@bruce-hill.com>2024-08-15 02:17:53 -0400
commited1667cb377dd8da51583e703c6677969addc993 (patch)
tree531424aa9647ef49549000bb145e2b798c868154 /builtins/array.c
parent7a472752e5be47816f756d854a1b0756594fef98 (diff)
Add array:shuffled() and checks for array insertion
Diffstat (limited to 'builtins/array.c')
-rw-r--r--builtins/array.c13
1 files changed, 11 insertions, 2 deletions
diff --git a/builtins/array.c b/builtins/array.c
index d749778d..84a0b7a0 100644
--- a/builtins/array.c
+++ b/builtins/array.c
@@ -54,7 +54,8 @@ public void Array$insert(array_t *arr, const void *item, Int_t int_index, int64_
if (index <= 0) index = arr->length + index + 1;
if (index < 1) index = 1;
- else if (index > (int64_t)arr->length + 1) index = (int64_t)arr->length + 1;
+ else if (index > (int64_t)arr->length + 1)
+ fail("Invalid insertion index %ld for an array with length %ld", index, arr->length);
if (!arr->data) {
arr->free = 4;
@@ -98,7 +99,8 @@ public void Array$insert_all(array_t *arr, array_t to_insert, Int_t int_index, i
if (index < 1) index = arr->length + index + 1;
if (index < 1) index = 1;
- else if (index > (int64_t)arr->length + 1) index = (int64_t)arr->length + 1;
+ else if (index > (int64_t)arr->length + 1)
+ fail("Invalid insertion index %ld for an array with length %ld", index, arr->length);
if ((int64_t)arr->free >= (int64_t)to_insert.length // Adequate free space
&& arr->data_refcount == 0 // Not aliased memory
@@ -219,6 +221,13 @@ public void Array$shuffle(array_t *arr, int64_t padded_item_size)
}
}
+public array_t Array$shuffled(array_t arr, int64_t padded_item_size)
+{
+ Array$compact(&arr, padded_item_size);
+ Array$shuffle(&arr, padded_item_size);
+ return arr;
+}
+
public void *Array$random(array_t arr)
{
if (arr.length == 0)