aboutsummaryrefslogtreecommitdiff
path: root/builtins
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-07-10 13:42:58 -0400
committerBruce Hill <bruce@bruce-hill.com>2024-07-10 13:42:58 -0400
commit068d0e8563dab7708711e0171ba1190d6037f0c9 (patch)
tree97797b983af875147e05ad5a0e233b1d8928c5c2 /builtins
parent10e86153a2c619d7f853ab149e451a9cc05fdb27 (diff)
Add array:to() to split out functionality of array:from()
Diffstat (limited to 'builtins')
-rw-r--r--builtins/array.c26
-rw-r--r--builtins/array.h3
2 files changed, 22 insertions, 7 deletions
diff --git a/builtins/array.c b/builtins/array.c
index 1c31be80..2d45591c 100644
--- a/builtins/array.c
+++ b/builtins/array.c
@@ -261,24 +261,38 @@ public array_t Array$sample(array_t arr, int64_t n, array_t weights, const TypeI
return selected;
}
-public array_t Array$from(array_t *array, int64_t first, int64_t last)
+public array_t Array$from(array_t *array, int64_t first)
{
if (first < 0)
first = array->length + first + 1;
+ if (first < 1 || first > array->length)
+ return (array_t){.atomic=array->atomic};
+
+ return (array_t){
+ .atomic=array->atomic,
+ .data=array->data + array->stride*(first-1),
+ .length=array->length - first + 1,
+ .stride=array->stride,
+ .data_refcount=array->data_refcount,
+ };
+}
+
+public array_t Array$to(array_t *array, int64_t last)
+{
if (last < 0)
last = array->length + last + 1;
- if (first < 1 || first > array->length || last < first)
- return (array_t){.atomic=array->atomic};
-
if (last > array->length)
last = array->length;
+ if (last == 0)
+ return (array_t){.atomic=array->atomic};
+
return (array_t){
.atomic=array->atomic,
- .data=array->data + array->stride*(first-1),
- .length=last - first + 1,
+ .data=array->data,
+ .length=last,
.stride=array->stride,
.data_refcount=array->data_refcount,
};
diff --git a/builtins/array.h b/builtins/array.h
index b2884d99..56794a22 100644
--- a/builtins/array.h
+++ b/builtins/array.h
@@ -64,7 +64,8 @@ array_t Array$sample(array_t arr, int64_t n, array_t weights, const TypeInfo *ty
void Array$clear(array_t *array);
void Array$compact(array_t *arr, const TypeInfo *type);
bool Array$contains(array_t array, void *item, const TypeInfo *type);
-array_t Array$from(array_t *array, int64_t first, int64_t last);
+array_t Array$from(array_t *array, int64_t first);
+array_t Array$to(array_t *array, int64_t last);
array_t Array$by(array_t *array, int64_t stride);
array_t Array$reversed(array_t array);
array_t Array$concat(array_t x, array_t y, const TypeInfo *type);