aboutsummaryrefslogtreecommitdiff
path: root/builtins
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-08-10 16:03:41 -0400
committerBruce Hill <bruce@bruce-hill.com>2024-08-10 16:03:41 -0400
commitb37bd70b602b7ac6427dcf29f7cd9241b0a7ae09 (patch)
treeafa3ba4caebb728c43b96e176b8a569b3ad5f469 /builtins
parent671f81137ee2e5de632526109e02c4b79197e432 (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')
-rw-r--r--builtins/datatypes.h1
-rw-r--r--builtins/table.c33
-rw-r--r--builtins/table.h9
3 files changed, 7 insertions, 36 deletions
diff --git a/builtins/datatypes.h b/builtins/datatypes.h
index 12c7cbc4..b0f62d03 100644
--- a/builtins/datatypes.h
+++ b/builtins/datatypes.h
@@ -47,7 +47,6 @@ typedef struct table_s {
array_t entries;
bucket_info_t *bucket_info;
struct table_s *fallback;
- void *default_value;
} table_t;
typedef struct {
diff --git a/builtins/table.c b/builtins/table.c
index 5329ec24..e2044a2d 100644
--- a/builtins/table.c
+++ b/builtins/table.c
@@ -137,9 +137,6 @@ public void *Table$get(table_t t, const void *key, const TypeInfo *type)
void *ret = Table$get_raw(*iter, key, type);
if (ret) return ret;
}
- for (const table_t *iter = &t; iter; iter = iter->fallback) {
- if (iter->default_value) return iter->default_value;
- }
return NULL;
}
@@ -259,9 +256,6 @@ public void *Table$reserve(table_t *t, const void *key, const void *value, const
value = Table$get_raw(*iter, key, type);
if (value) break;
}
- for (table_t *iter = t; !value && iter; iter = iter->fallback) {
- if (iter->default_value) value = iter->default_value;
- }
}
maybe_copy_on_write(t, type);
@@ -404,9 +398,6 @@ public bool Table$equal(const table_t *x, const table_t *y, const TypeInfo *type
if (Table$length(*x) != Table$length(*y))
return false;
- if ((x->default_value != NULL) != (y->default_value != NULL))
- return false;
-
if ((x->fallback != NULL) != (y->fallback != NULL))
return false;
@@ -433,13 +424,6 @@ public int32_t Table$compare(const table_t *x, const table_t *y, const TypeInfo
if (diff != 0) return diff;
}
- if (!x->default_value != !y->default_value) {
- return (!x->default_value) - (!y->default_value);
- } else if (x->default_value && y->default_value) {
- int32_t diff = generic_compare(x->default_value, y->default_value, table.value);
- if (diff != 0) return diff;
- }
-
if (!x->fallback != !y->fallback) {
return (!x->fallback) - (!y->fallback);
} else if (x->fallback && y->fallback) {
@@ -460,7 +444,6 @@ public uint32_t Table$hash(const table_t *t, const TypeInfo *type)
Array$hash(&t->entries, $ArrayInfo(table.key)),
Array$hash(&t->entries + value_offset(type), $ArrayInfo(table.value)),
t->fallback ? Table$hash(t->fallback, type) : 0,
- t->default_value ? generic_hash(t->default_value, table.value) : 0,
};
uint32_t hash;
halfsiphash(&components, sizeof(components), TOMO_HASH_KEY, (uint8_t*)&hash, sizeof(hash));
@@ -495,11 +478,6 @@ public CORD Table$as_text(const table_t *t, bool colorize, const TypeInfo *type)
c = CORD_cat(c, Table$as_text(t->fallback, colorize, type));
}
- if (t->default_value) {
- c = CORD_cat(c, "; default=");
- c = CORD_cat(c, generic_as_text(t->default_value, colorize, table.value));
- }
-
c = CORD_cat(c, "}");
return c;
}
@@ -545,9 +523,6 @@ public table_t Table$overlap(table_t a, table_t b, const TypeInfo *type)
*result.fallback = Table$overlap(*a.fallback, b, type);
}
- if (a.default_value && b.default_value && generic_equal(a.default_value, b.default_value, type->TableInfo.value))
- result.default_value = a.default_value;
-
return result;
}
@@ -573,9 +548,6 @@ public table_t Table$with(table_t a, table_t b, const TypeInfo *type)
result.fallback = a.fallback ? a.fallback : b.fallback;
}
- // B's default value takes precedence over A's
- result.default_value = b.default_value ? b.default_value : a.default_value;
-
return result;
}
@@ -598,11 +570,6 @@ public table_t Table$without(table_t a, table_t b, const TypeInfo *type)
*result.fallback = Table$without(*a.fallback, b, type);
}
- if (a.default_value) {
- if (!b.default_value || !generic_equal(a.default_value, b.default_value, type->TableInfo.value))
- result.default_value = a.default_value;
- }
-
return result;
}
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); })