diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2024-03-08 14:23:16 -0500 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2024-03-08 14:23:16 -0500 |
| commit | 07c2b0ec26600d8609870d9f5c0867d48b801db4 (patch) | |
| tree | 9c7f5cf8c394312e3da4a2371081d2a64b622b6e /builtins | |
| parent | 8e62018546cd9beb357b0452535cf216c7cfa0cc (diff) | |
Array methods
Diffstat (limited to 'builtins')
| -rw-r--r-- | builtins/array.c | 15 | ||||
| -rw-r--r-- | builtins/array.h | 4 |
2 files changed, 6 insertions, 13 deletions
diff --git a/builtins/array.c b/builtins/array.c index 245c2202..8ee771ea 100644 --- a/builtins/array.c +++ b/builtins/array.c @@ -54,7 +54,7 @@ public void Array__insert(array_t *arr, const void *item, int64_t index, const T arr->free = 4; arr->data = arr->atomic ? GC_MALLOC_ATOMIC(arr->free * item_size) : GC_MALLOC(arr->free * item_size); arr->stride = item_size; - } else if (arr->free < 1 || (int64_t)arr->stride != item_size) { + } else if (arr->free < 1 || arr->data_refcount || (int64_t)arr->stride != item_size) { arr->free = MAX(15, MIN(1, arr->length/4)); void *copy = arr->atomic ? GC_MALLOC_ATOMIC((arr->length + arr->free) * item_size) : GC_MALLOC((arr->length + arr->free) * item_size); for (int64_t i = 0; i < index-1; i++) @@ -65,9 +65,6 @@ public void Array__insert(array_t *arr, const void *item, int64_t index, const T arr->data_refcount = 0; arr->stride = item_size; } else { - if (arr->data_refcount) - Array__compact(arr, type); - if (index != arr->length+1) memmove((void*)arr->data + index*item_size, arr->data + (index-1)*item_size, (arr->length - index)*item_size); } @@ -88,7 +85,7 @@ public void Array__insert_all(array_t *arr, array_t to_insert, int64_t index, co if (!arr->data) { arr->free = to_insert.length; arr->data = arr->atomic ? GC_MALLOC_ATOMIC(item_size*arr->free) : GC_MALLOC(item_size*arr->free); - } else if ((int64_t)arr->free < (int64_t)to_insert.length || (int64_t)arr->stride != item_size) { + } else if ((int64_t)arr->free < (int64_t)to_insert.length || arr->data_refcount || (int64_t)arr->stride != item_size) { arr->free = to_insert.length; void *copy = arr->atomic ? GC_MALLOC_ATOMIC((arr->length + arr->free) * item_size) : GC_MALLOC((arr->length + arr->free) * item_size); for (int64_t i = 0; i < index-1; i++) @@ -98,9 +95,6 @@ public void Array__insert_all(array_t *arr, array_t to_insert, int64_t index, co arr->data = copy; arr->data_refcount = 0; } else { - if (arr->data_refcount) - Array__compact(arr, type); - if (index != arr->length+1) memmove((void*)arr->data + index*item_size, arr->data + (index-1)*item_size, (arr->length - index + to_insert.length-1)*item_size); } @@ -179,7 +173,7 @@ public void *Array__random(array_t arr) return arr.data + arr.stride*index; } -public array_t Array__slice(array_t *array, int64_t first, int64_t stride, int64_t length, const TypeInfo *type) +public array_t Array__slice(array_t *array, int64_t first, int64_t length, int64_t stride, const TypeInfo *type) { if (stride > MAX_STRIDE || stride < MIN_STRIDE) fail("Stride is too big: %ld", stride); @@ -263,9 +257,8 @@ public bool Array__contains(array_t array, void *item, const TypeInfo *type) return false; } -public void Array__clear(array_t *array, const TypeInfo *type) +public void Array__clear(array_t *array) { - (void)type; *array = (array_t){.data=0, .length=0}; } diff --git a/builtins/array.h b/builtins/array.h index 926dd4af..52fd65e3 100644 --- a/builtins/array.h +++ b/builtins/array.h @@ -52,10 +52,10 @@ void Array__remove(array_t *arr, int64_t index, int64_t count, const TypeInfo *t void Array__sort(array_t *arr, const TypeInfo *type); void Array__shuffle(array_t *arr, const TypeInfo *type); void *Array__random(array_t arr); -void Array__clear(array_t *array, const TypeInfo *type); +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__slice(array_t *array, int64_t first, int64_t stride, int64_t length, const TypeInfo *type); +array_t Array__slice(array_t *array, int64_t first, int64_t length, int64_t stride, const TypeInfo *type); array_t Array__concat(array_t x, array_t y, const TypeInfo *type); uint32_t Array__hash(const array_t *arr, const TypeInfo *type); int32_t Array__compare(const array_t *x, const array_t *y, const TypeInfo *type); |
