diff options
| author | nodist <kevin.comas.git@gmail.com> | 2026-10-26 09:22:29 -0400 |
|---|---|---|
| committer | nodist <kevin.comas.git@gmail.com> | 2026-10-26 09:22:29 -0400 |
| commit | fcc5294b919856ff04fef859760d48d0da136396 (patch) | |
| tree | 3f7c08bfbe52b47131b6c1c26795dce796e9cf0e | |
| parent | 19eb183b38511998322d99c33515c079d89785e3 (diff) | |
shfit unsigned long number
| -rw-r--r-- | include/kpl/identifier.h | 10 | ||||
| -rw-r--r-- | include/kpl/type.h | 45 | ||||
| -rw-r--r-- | source/identifier.c | 23 | ||||
| -rw-r--r-- | test/identifier.c | 18 |
4 files changed, 82 insertions, 14 deletions
diff --git a/include/kpl/identifier.h b/include/kpl/identifier.h index 092b0c4..a30b4a7 100644 --- a/include/kpl/identifier.h +++ b/include/kpl/identifier.h @@ -13,8 +13,6 @@ #define KPL_IDENTIFIER_VALUES_PER_PART (KPL_IDENTIFIER_BITS_PER_PART / KPL_IDENTIFIER_BITS_PER_VALUE) -#define KPL_IDENTIFIER_VALUES_TOTAL_BITS (KPL_IDENTIFIER_BITS_PER_VALUE * KPL_IDENTIFIER_VALUES_PER_PART) - #define KPL_IDENTIFIER_MAX_LENGTH (KPL_IDENTIFIER_VALUES_PER_PART * KPL_IDENTIFIER_PARTS) #define KPL_IDENTIFIER_C_STRING_SIZE (KPL_IDENTIFIER_MAX_LENGTH + sizeof(char)) @@ -36,6 +34,12 @@ bool kpl_identifier_encode(kpl_identifier *identifier, const char *c_string, siz /* * Converts an identifier into a c string * Does not clear the c_string buffer - * Does not check the c_string buffer assumes the buffer is at least KPL_IDENTIFIER_C_STRING_SIZE + * Does not check the c_string buffer size assumes the buffer is at least KPL_IDENTIFIER_C_STRING_SIZE */ void kpl_identifier_decode(kpl_identifier *identifier, char c_string[KPL_IDENTIFIER_C_STRING_SIZE]); + +/* + * Compares identifiers like strings + * 0 - equal, negative - left < right, positive - left > right + */ +int32_t kpl_identifier_compare(const kpl_identifier *identifier_left, const kpl_identifier *identifier_right); diff --git a/include/kpl/type.h b/include/kpl/type.h index 546a1ed..76c64c1 100644 --- a/include/kpl/type.h +++ b/include/kpl/type.h @@ -25,7 +25,7 @@ typedef enum : uint8_t { // CONTAINERS KPL_TYPE_NAME_STRING, KPL_TYPE_NAME_ARRAY, - KPL_TYPE_NAME_TYPLE, + KPL_TYPE_NAME_TUPLE, KPL_TYPE_NAME_TABLE, KPL_TYPE_NAME_MAP, KPL_TYPE_NAME_SET, @@ -39,25 +39,35 @@ typedef enum : uint8_t { KPL_TYPE_NAME_REGEXP, // STORAGE KPL_TYPE_NAME_MODULE, - KPL_TYPE_NAME_NAMESPACE + KPL_TYPE_NAME_NAMESPACE, + // DEFINITIONS + KPL_TYPE_NAME_ALIAS, + KPL_TYPE_NAME_UNIQUE } kpl_type_name; +/* + * The c struct for this type + */ typedef enum : uint8_t { - KPL_TYPE_KIND_BASE + KPL_TYPE_KIND_BASE, // has no additional type information + KPL_TYPE_KIND_INNER, // type wraps one or two types + KPL_TYPE_KIND_ARRAY, // type wraps an unknown number of types + KPL_TYPE_KIND_TREE_ITEM_TYPE, + KPL_TYPE_KIND_TREE_ITEM_IDENTIFIER, + KPL_TYPE_KIND_TREE // tree of type/type or identifier/type } kpl_type_kind; -typedef enum : uint32_t { - KPL_TYPE_FLAG_EMPTY = 0 +typedef enum : uint16_t { + KPL_TYPE_FLAG_EMPTY = 0, + KPL_TYPE_FLAG_REF = 1 << 0 } kpl_type_flags; // type_kind is for struct polymorphism -// type_specific meaning changes depending on type_name #define KPL_TYPE_HEADER(STRUCT_NAME) \ KPL_OBJECT_HEADER(STRUCT_NAME); \ kpl_type_name type_name; \ kpl_type_kind type_kind; \ - uint16_t type_specific; \ - uint32_t type_flags + uint16_t type_flags /* * Type has no additional information @@ -65,3 +75,22 @@ typedef enum : uint32_t { typedef struct _kpl_type_base { KPL_TYPE_HEADER(_kpl_type_base); } kpl_type_base; + +/* + * Type wraps one or two types + */ +typedef struct _kpl_type_inner { + KPL_TYPE_HEADER(_kpl_type_inner); + kpl_type_base *inner[2]; +} kpl_type_inner; + +/* + * Type wraps an unknown number of types + */ +typedef struct _kpl_type_array { + KPL_TYPE_HEADER(_kpl_type_array); + uint16_t array_size, array_length; + kpl_type_base *array[]; +} kpl_type_array; + +// TODO TREE diff --git a/source/identifier.c b/source/identifier.c index f6a7279..a65d8d4 100644 --- a/source/identifier.c +++ b/source/identifier.c @@ -18,6 +18,8 @@ static uint64_t kpl_identifier_char_to_value(char c, uint64_t value_index) { return value << value_index * KPL_IDENTIFIER_BITS_PER_VALUE; } +#define KPL_IDENTIFIER_VALUES_TOTAL_BITS (KPL_IDENTIFIER_BITS_PER_VALUE * KPL_IDENTIFIER_VALUES_PER_PART) + bool kpl_identifier_encode(kpl_identifier *identifier, const char *c_string, size_t c_string_length) { if (c_string_length > KPL_IDENTIFIER_MAX_LENGTH) return false; @@ -66,3 +68,24 @@ void kpl_identifier_decode(kpl_identifier *identifier, char c_string[KPL_IDENTIF } } } + +#define KPL_IDENTIFIER_PART_LENGTH_MASK ~(1UL << 63 | 1UL << 62 | 1UL << 61 | 1UL << 60) + +int32_t kpl_identifier_compare(const kpl_identifier *identifier_left, const kpl_identifier *identifier_right) { + for (uint32_t part_index = 0; part_index < KPL_IDENTIFIER_PARTS; part_index++) { + uint64_t left_part = identifier_left->parts[part_index], right_part = identifier_right->parts[part_index]; + if (!left_part && !right_part) + return 0; + left_part &= KPL_IDENTIFIER_PART_LENGTH_MASK; + right_part &= KPL_IDENTIFIER_PART_LENGTH_MASK; + while (left_part && right_part) { + char left = kpl_identifier_value_to_char(left_part), right = kpl_identifier_value_to_char(right_part); + int32_t result = left - right; + if (result) + return result; + left_part >>= KPL_IDENTIFIER_BITS_PER_VALUE; + right_part >>= KPL_IDENTIFIER_BITS_PER_VALUE; + } + } + return 0; +} diff --git a/test/identifier.c b/test/identifier.c index 94c57bb..bd50795 100644 --- a/test/identifier.c +++ b/test/identifier.c @@ -2,10 +2,22 @@ #include "kpl_test.h" KPL_TEST(c_string_to_identifier) { - const char *string = "aA0_t4_12w0Z"; + const char *string = "aA0_t4_12w0abcdefghijklmnop456789Z"; kpl_identifier identifier; KPL_ASSERT(kpl_identifier_encode(&identifier, string, strlen(string)), "should encode"); char buffer[100] = {}; - kpl_identifier_decode(&identifier, buffer); - KPL_ASSERT(strcmp(string, buffer) == 0, "should decode to same string"); + const int32_t offset = 10; + kpl_identifier_decode(&identifier, buffer + offset); + KPL_ASSERT(strcmp(string, buffer + offset) == 0, "should decode to same string"); +} + +KPL_TEST(identifier_compare) { + const char *string_a = "abc123456789", *string_b = "abc123456789", *string_c = "abd123456789"; + kpl_identifier identifier_a, identifier_b, identifier_c; + KPL_ASSERT(kpl_identifier_encode(&identifier_a, string_a, strlen(string_a)), "should encode"); + KPL_ASSERT(kpl_identifier_encode(&identifier_b, string_b, strlen(string_b)), "should encode"); + KPL_ASSERT(kpl_identifier_encode(&identifier_c, string_c, strlen(string_c)), "should encode"); + KPL_ASSERT(kpl_identifier_compare(&identifier_a, &identifier_b) == 0, "should be 0"); + int32_t compare_value = strcmp(string_b, string_c); + KPL_ASSERT(kpl_identifier_compare(&identifier_b, &identifier_c) == compare_value, "should equal strcmp"); } |
