diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2024-08-10 16:36:50 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2024-08-10 16:36:50 -0400 |
| commit | 948d6b4c22b07d0ec7affd79b29ba0200f3498fa (patch) | |
| tree | 0908cf76f535ac0a9e828cf33720cf91fd9d4a74 /builtins/array.c | |
| parent | 4265728cbbe80dd993ef38ef60e32a06e57cb7ff (diff) | |
Add array:counts()
Diffstat (limited to 'builtins/array.c')
| -rw-r--r-- | builtins/array.c | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/builtins/array.c b/builtins/array.c index aa6929d5..c5d52380 100644 --- a/builtins/array.c +++ b/builtins/array.c @@ -14,6 +14,7 @@ #include "functions.h" #include "halfsiphash.h" #include "integers.h" +#include "table.h" #include "types.h" #include "util.h" @@ -219,6 +220,20 @@ public void *Array$random(array_t arr) return arr.data + arr.stride*index; } +public table_t Array$counts(array_t arr, const TypeInfo *type) +{ + table_t counts = {}; + const TypeInfo count_type = {.size=sizeof(table_t), .align=__alignof__(table_t), + .tag=TableInfo, .TableInfo.key=type->ArrayInfo.item, .TableInfo.value=&$Int}; + for (int64_t i = 0; i < arr.length; i++) { + void *key = arr.data + i*arr.stride; + int64_t *count = Table$get(counts, key, &count_type); + int64_t val = count ? *count + 1 : 1; + Table$set(&counts, key, &val, &count_type); + } + return counts; +} + public array_t Array$sample(array_t arr, int64_t n, array_t weights, int64_t padded_item_size) { if (arr.length == 0 || n <= 0) |
