diff options
Diffstat (limited to 'stdlib')
| -rw-r--r-- | stdlib/arrays.c | 63 | ||||
| -rw-r--r-- | stdlib/arrays.h | 1 | ||||
| -rw-r--r-- | stdlib/text.c | 10 | ||||
| -rw-r--r-- | stdlib/text.h | 2 |
4 files changed, 43 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 diff --git a/stdlib/arrays.h b/stdlib/arrays.h index e880c643..5e0ca7e4 100644 --- a/stdlib/arrays.h +++ b/stdlib/arrays.h @@ -83,6 +83,7 @@ PUREFUNC bool Array$has(Array_t array, void *item, const TypeInfo_t *type); PUREFUNC Array_t Array$from(Array_t array, Int_t first); PUREFUNC Array_t Array$to(Array_t array, Int_t last); PUREFUNC Array_t Array$by(Array_t array, Int_t stride, int64_t padded_item_size); +PUREFUNC Array_t Array$slice(Array_t array, Int_t int_first, Int_t int_last); PUREFUNC Array_t Array$reversed(Array_t array, int64_t padded_item_size); Array_t Array$concat(Array_t x, Array_t y, int64_t padded_item_size); PUREFUNC uint64_t Array$hash(const void *arr, const TypeInfo_t *type); diff --git a/stdlib/text.c b/stdlib/text.c index 4879ead9..384bf7ac 100644 --- a/stdlib/text.c +++ b/stdlib/text.c @@ -562,6 +562,16 @@ public Text_t Text$slice(Text_t text, Int_t first_int, Int_t last_int) } } +public Text_t Text$from(Text_t text, Int_t first) +{ + return Text$slice(text, first, I_small(-1)); +} + +public Text_t Text$to(Text_t text, Int_t last) +{ + return Text$slice(text, I_small(1), last); +} + public Text_t Text$cluster(Text_t text, Int_t index_int) { int64_t index = Int_to_Int64(index_int, false); diff --git a/stdlib/text.h b/stdlib/text.h index b493f4ae..6e2ac413 100644 --- a/stdlib/text.h +++ b/stdlib/text.h @@ -29,6 +29,8 @@ Text_t Text$_concat(int n, Text_t items[n]); #define Text$concat(...) Text$_concat(sizeof((Text_t[]){__VA_ARGS__})/sizeof(Text_t), (Text_t[]){__VA_ARGS__}) #define Texts(...) Text$concat(__VA_ARGS__) Text_t Text$slice(Text_t text, Int_t first_int, Int_t last_int); +Text_t Text$from(Text_t text, Int_t first); +Text_t Text$to(Text_t text, Int_t last); Text_t Text$cluster(Text_t text, Int_t index_int); OptionalText_t Text$from_str(const char *str); OptionalText_t Text$from_strn(const char *str, size_t len); |
