aboutsummaryrefslogtreecommitdiff
path: root/stdlib/arrays.c
diff options
context:
space:
mode:
Diffstat (limited to 'stdlib/arrays.c')
-rw-r--r--stdlib/arrays.c63
1 files changed, 30 insertions, 33 deletions
diff --git a/stdlib/arrays.c b/stdlib/arrays.c
index fd4dcf96..c8ee55bd 100644
--- a/stdlib/arrays.c
+++ b/stdlib/arrays.c
@@ -386,43 +386,14 @@ public Array_t Array$sample(Array_t arr, Int_t int_n, Array_t weights, RNG_t rng
return selected;
}
-public Array_t Array$from(Array_t array, Int_t int_first)
+public Array_t Array$from(Array_t array, Int_t first)
{
- int64_t first = Int_to_Int64(int_first, false);
- 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,
- };
+ return Array$slice(array, first, I_small(-1));
}
-public Array_t Array$to(Array_t array, Int_t int_last)
+public Array_t Array$to(Array_t array, Int_t last)
{
- int64_t last = Int_to_Int64(int_last, false);
- if (last < 0)
- last = array.length + last + 1;
-
- 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,
- .length=last,
- .stride=array.stride,
- .data_refcount=array.data_refcount,
- };
+ return Array$slice(array, I_small(1), last);
}
public Array_t Array$by(Array_t array, Int_t int_stride, int64_t padded_item_size)
@@ -459,6 +430,32 @@ public Array_t Array$by(Array_t array, Int_t int_stride, int64_t padded_item_siz
};
}
+public Array_t Array$slice(Array_t array, Int_t int_first, Int_t int_last)
+
+{
+ int64_t first = Int_to_Int64(int_first, false);
+ if (first < 0)
+ first = array.length + first + 1;
+
+ int64_t last = Int_to_Int64(int_last, false);
+ if (last < 0)
+ last = array.length + last + 1;
+
+ if (last > array.length)
+ last = array.length;
+
+ if (first < 1 || first > array.length || 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,
+ .stride=array.stride,
+ .data_refcount=array.data_refcount,
+ };
+}
+
public Array_t Array$reversed(Array_t array, int64_t padded_item_size)
{
// Just in case negating the stride gives a value that doesn't fit into a