From bbb26c5ee5a18d53a63e8740e261bf3589ac0005 Mon Sep 17 00:00:00 2001 From: Max Wash Date: Fri, 29 May 2026 19:59:00 +0100 Subject: [PATCH] fx: pointer: implement hash() and compare() --- fx/pointer.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/fx/pointer.c b/fx/pointer.c index 9bde9a3..f75330a 100644 --- a/fx/pointer.c +++ b/fx/pointer.c @@ -1,17 +1,51 @@ +#include #include #include +#include + +static fx_status hash(const fx_value *v, uint64_t *out_hash) +{ + *out_hash = (uint64_t)v->v_pointer; + return FX_SUCCESS; +} + +static i32 compare(const fx_value *left, const fx_value *right) +{ + void *left_v = left->v_pointer; + void *right_v = 0; + if (!FX_OK(fx_value_get_pointer(right, &right_v))) { + return -1; + } + + uintptr_t left_p = (uintptr_t)left_v; + uintptr_t right_p = (uintptr_t)right_v; + + if (left_p < right_p) { + return -1; + } else if (left_p > right_p) { + return 1; + } else { + return 0; + } +} /*** CLASS DEFINITION *********************************************************/ FX_TYPE_CLASS_BEGIN(fx_pointer) FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_object, FX_TYPE_OBJECT) FX_INTERFACE_ENTRY(to_string) = NULL; + FX_INTERFACE_ENTRY(hash) = hash; FX_TYPE_VTABLE_INTERFACE_END(fx_object, FX_TYPE_OBJECT) + + FX_TYPE_VTABLE_INTERFACE_BEGIN(fx_comparable, FX_TYPE_COMPARABLE) + FX_INTERFACE_ENTRY(c_compare) = compare; + FX_TYPE_VTABLE_INTERFACE_END(fx_comparable, FX_TYPE_COMPARABLE) FX_TYPE_CLASS_END(fx_pointer) FX_TYPE_DEFINITION_BEGIN(fx_pointer) __FX_VALUE_TYPE_ID(POINTER); FX_TYPE_EXTENDS(FX_TYPE_VALUE_TYPE); + FX_TYPE_IMPLEMENTS(FX_TYPE_COMPARABLE); FX_TYPE_NAME("fx.pointer"); FX_TYPE_CLASS(fx_pointer_class); FX_TYPE_INSTANCE_PRIVATE(void *);