diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2024-08-10 16:03:41 -0400 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2024-08-10 16:03:41 -0400 |
| commit | b37bd70b602b7ac6427dcf29f7cd9241b0a7ae09 (patch) | |
| tree | afa3ba4caebb728c43b96e176b8a569b3ad5f469 /builtins/table.h | |
| parent | 671f81137ee2e5de632526109e02c4b79197e432 (diff) | |
For tables, deprecate support for square bracket indexing and .default
values, replacing them with a `:bump()` function for tables with numeric
values. This means that counters can be implemented easily without the
need to mask complexity.
Diffstat (limited to 'builtins/table.h')
| -rw-r--r-- | builtins/table.h | 9 |
1 files changed, 7 insertions, 2 deletions
diff --git a/builtins/table.h b/builtins/table.h index 3b1c7f98..842131e8 100644 --- a/builtins/table.h +++ b/builtins/table.h @@ -11,7 +11,7 @@ #include "types.h" #include "util.h" -#define Table(key_t, val_t, key_info, value_info, fb, def, N, ...) ({ \ +#define Table(key_t, val_t, key_info, value_info, fb, N, ...) ({ \ struct { key_t k; val_t v; } ents[N] = {__VA_ARGS__}; \ table_t table = Table$from_entries((array_t){ \ .data=memcpy(GC_MALLOC(sizeof(ents)), ents, sizeof(ents)), \ @@ -19,7 +19,6 @@ .stride=(void*)&ents[1] - (void*)&ents[0], \ }, $TableInfo(key_info, value_info)); \ table.fallback = fb; \ - table.default_value = def; \ table; }) #define Set(item_t, item_info, N, ...) ({ \ item_t ents[N] = {__VA_ARGS__}; \ @@ -52,6 +51,12 @@ void Table$set(table_t *t, const void *key, const void *value, const TypeInfo *t #define Table$set_value(t, key_expr, value_expr, type) ({ __typeof(key_expr) k = key_expr; __typeof(value_expr) v = value_expr; \ Table$set(t, &k, &v, type); }) #define Table$reserve_value(t, key_expr, type) ({ __typeof(key_expr) k = key_expr; Table$reserve(t, &k, NULL, type); }) +#define Table$bump(t_expr, key_expr, amount_expr, type) ({ __typeof(key_expr) key = key_expr; \ + table_t *t = t_expr; \ + __typeof(amount_expr) *val = Table$get_raw(*t, &key, type); \ + if (val) *val += amount_expr; \ + else { __typeof(amount_expr) init = amount_expr; Table$set(t, &key, &init, type); } (void)0; }) + void Table$remove(table_t *t, const void *key, const TypeInfo *type); #define Table$remove_value(t, key_expr, type) ({ __typeof(key_expr) k = key_expr; Table$remove(t, &k, type); }) |
