diff options
Diffstat (limited to 'stdlib')
| -rw-r--r-- | stdlib/enums.h | 10 | ||||
| -rw-r--r-- | stdlib/structs.c | 14 | ||||
| -rw-r--r-- | stdlib/structs.h | 12 |
3 files changed, 36 insertions, 0 deletions
diff --git a/stdlib/enums.h b/stdlib/enums.h index 35c3a22a..a61daacc 100644 --- a/stdlib/enums.h +++ b/stdlib/enums.h @@ -25,4 +25,14 @@ void Enum$deserialize(FILE *in, void *outval, Array_t *pointers, const TypeInfo_ .deserialize=Enum$deserialize, \ }) +#define PackedDataEnum$metamethods ((metamethods_t){ \ + .hash=PackedData$hash, \ + .compare=Enum$compare, \ + .equal=PackedData$equal, \ + .as_text=Enum$as_text, \ + .is_none=Enum$is_none, \ + .serialize=Enum$serialize, \ + .deserialize=Enum$deserialize, \ +}) + // vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/stdlib/structs.c b/stdlib/structs.c index 010b49f7..1ff5a918 100644 --- a/stdlib/structs.c +++ b/stdlib/structs.c @@ -53,6 +53,14 @@ PUREFUNC public uint64_t Struct$hash(const void *obj, const TypeInfo_t *type) } #pragma GCC diagnostic pop +PUREFUNC public uint64_t PackedData$hash(const void *obj, const TypeInfo_t *type) +{ + if (type->StructInfo.num_fields == 0) + return 0; + + return siphash24(obj, (size_t)type->size); +} + PUREFUNC public int32_t Struct$compare(const void *x, const void *y, const TypeInfo_t *type) { if (x == y) @@ -122,6 +130,12 @@ PUREFUNC public bool Struct$equal(const void *x, const void *y, const TypeInfo_t return true; } +PUREFUNC public bool PackedData$equal(const void *x, const void *y, const TypeInfo_t *type) +{ + if (x == y) return true; + return (memcmp(x, y, (size_t)type->size) == 0); +} + PUREFUNC public Text_t Struct$as_text(const void *obj, bool colorize, const TypeInfo_t *type) { if (!obj) return Text$from_str(type->StructInfo.name); diff --git a/stdlib/structs.h b/stdlib/structs.h index 6553cc86..b027a01e 100644 --- a/stdlib/structs.h +++ b/stdlib/structs.h @@ -8,8 +8,10 @@ #include "util.h" PUREFUNC uint64_t Struct$hash(const void *obj, const TypeInfo_t *type); +PUREFUNC uint64_t PackedData$hash(const void *obj, const TypeInfo_t *type); PUREFUNC int32_t Struct$compare(const void *x, const void *y, const TypeInfo_t *type); PUREFUNC bool Struct$equal(const void *x, const void *y, const TypeInfo_t *type); +PUREFUNC bool PackedData$equal(const void *x, const void *y, const TypeInfo_t *type); PUREFUNC Text_t Struct$as_text(const void *obj, bool colorize, const TypeInfo_t *type); PUREFUNC bool Struct$is_none(const void *obj, const TypeInfo_t *type); void Struct$serialize(const void *obj, FILE *out, Table_t *pointers, const TypeInfo_t *type); @@ -25,4 +27,14 @@ void Struct$deserialize(FILE *in, void *outval, Array_t *pointers, const TypeInf .deserialize=Struct$deserialize, \ }) +#define PackedData$metamethods ((metamethods_t){ \ + .hash=PackedData$hash, \ + .compare=Struct$compare, \ + .equal=PackedData$equal, \ + .as_text=Struct$as_text, \ + .is_none=Struct$is_none, \ + .serialize=Struct$serialize, \ + .deserialize=Struct$deserialize, \ +}) + // vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 |
