diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2024-02-23 13:24:06 -0500 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2024-02-23 13:24:06 -0500 |
| commit | 9f56266ae4a2d458576821d74906ab5bc4498b5b (patch) | |
| tree | 19cd055dd1020af788896132e6905bec4b59697f /builtins/array.c | |
| parent | 9aec32149fae328d73ee4816bc12e56c65327cbf (diff) | |
Implement Array__concat and ++=
Diffstat (limited to 'builtins/array.c')
| -rw-r--r-- | builtins/array.c | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/builtins/array.c b/builtins/array.c index a615a806..4f9070fe 100644 --- a/builtins/array.c +++ b/builtins/array.c @@ -228,6 +228,33 @@ public array_t Array__slice(array_t *array, int64_t first, int64_t stride, int64 }; } +array_t Array__concat(array_t *x, array_t *y, const TypeInfo *type) +{ + int64_t item_size = get_item_size(type); + void *data = x->atomic ? GC_MALLOC_ATOMIC(item_size*(x->length + y->length)) : GC_MALLOC(item_size*(x->length + y->length)); + if (x->stride == item_size) { + memcpy(data, x->data, item_size*x->length); + } else { + for (int64_t i = 0; i < x->length; i++) + memcpy(data + i*item_size, x->data + i*item_size, item_size); + } + + if (y->stride == item_size) { + memcpy(data + item_size*x->length, y->data, item_size*y->length); + } else { + for (int64_t i = 0; i < x->length; i++) + memcpy(data + (x->length + i)*item_size, y->data + i*item_size, item_size); + } + + return (array_t){ + .data=data, + .length=x->length + y->length, + .stride=item_size, + .copy_on_write=0, + .atomic=x->atomic, + }; +} + public bool Array__contains(array_t array, void *item, const TypeInfo *type) { TypeInfo *item_type = type->ArrayInfo.item; |
