|
| 1 | +/* Type utilities, to apply default equal, compare, hash methods for intergral types. |
| 2 | + See MIT LICENSE. |
| 3 | + */ |
| 4 | + |
| 5 | +// is_integral type utilities, to make equal and compare optional for simple POD types |
| 6 | +/* |
| 7 | +#define _define_integral_compare(T) \ |
| 8 | + static inline int _##T##_compare(T* a, T* b) { return *a < *b; } \ |
| 9 | + static inline int _##T##_equal(T* a, T* b) { \ |
| 10 | + return !_##T##_compare(a, b) && !_##T##_compare(b, a); } |
| 11 | +
|
| 12 | +_define_integral_compare(int) |
| 13 | +_define_integral_compare(long) |
| 14 | +#undef _define_integral_compare |
| 15 | +*/ |
| 16 | + |
| 17 | +#if defined(POD) && !defined(NOT_INTEGRAL) |
| 18 | + |
| 19 | +static inline int _JOIN(A, _default_integral_compare3)(T *a, T *b) |
| 20 | +{ |
| 21 | + return *a > *b ? 1 : *a < *b ? -1 : 0; |
| 22 | +} |
| 23 | +static inline int _JOIN(A, _default_integral_compare)(T *a, T *b) |
| 24 | +{ |
| 25 | + return *a < *b; |
| 26 | +} |
| 27 | + |
| 28 | +static inline int _JOIN(A, _default_integral_equal)(T *a, T *b) |
| 29 | +{ |
| 30 | + return *a == *b; |
| 31 | + // or the slow 2x compare, which is used in _equal. |
| 32 | + /*return _JOIN(A, _default_integral_compare)(a, b) == 0 && |
| 33 | + _JOIN(A, _default_integral_compare)(b, a) == 0; |
| 34 | + */ |
| 35 | +} |
| 36 | + |
| 37 | +static inline size_t _JOIN(A, _default_integral_hash)(T *a) |
| 38 | +{ |
| 39 | + return (size_t)*a; |
| 40 | +} |
| 41 | + |
| 42 | +#include <string.h> |
| 43 | + |
| 44 | +#if defined str || defined u8string || defined charp || defined u8ident || defined ucharp |
| 45 | + |
| 46 | +static inline size_t _JOIN(A, _default_string_hash)(T *key) |
| 47 | +{ |
| 48 | + size_t h; |
| 49 | + /* FNV1a, not wyhash */ |
| 50 | + h = 2166136261u; |
| 51 | + for (unsigned i = 0; i < strlen((char *)key); i++) |
| 52 | + { |
| 53 | + h ^= (unsigned char)key[i]; |
| 54 | + h *= 16777619; |
| 55 | + } |
| 56 | + return h; |
| 57 | +} |
| 58 | + |
| 59 | +#endif |
| 60 | + |
| 61 | +#define CTL_STRINGIFY_HELPER(n) #n |
| 62 | +#define CTL_STRINGIFY(n) CTL_STRINGIFY_HELPER(n) |
| 63 | +#define _strEQcc(s1c, s2c) !strcmp(s1c "", s2c "") |
| 64 | + |
| 65 | +static inline bool _JOIN(A, _type_is_integral)(void) |
| 66 | +{ |
| 67 | + return _strEQcc(CTL_STRINGIFY(T), "int") || _strEQcc(CTL_STRINGIFY(T), "long") || |
| 68 | + _strEQcc(CTL_STRINGIFY(T), "bool") || _strEQcc(CTL_STRINGIFY(T), "char") || |
| 69 | + _strEQcc(CTL_STRINGIFY(T), "short") || _strEQcc(CTL_STRINGIFY(T), "float") || |
| 70 | + _strEQcc(CTL_STRINGIFY(T), "double") || _strEQcc(CTL_STRINGIFY(T), "char8_t") || |
| 71 | + _strEQcc(CTL_STRINGIFY(T), "wchar_t") || _strEQcc(CTL_STRINGIFY(T), "char16_t") || |
| 72 | + _strEQcc(CTL_STRINGIFY(T), "char32_t") || _strEQcc(CTL_STRINGIFY(T), "long_double") || |
| 73 | + _strEQcc(CTL_STRINGIFY(T), "long_long") || _strEQcc(CTL_STRINGIFY(T), "int8_t") || |
| 74 | + _strEQcc(CTL_STRINGIFY(T), "uint8_t") || _strEQcc(CTL_STRINGIFY(T), "uint16_t") || |
| 75 | + _strEQcc(CTL_STRINGIFY(T), "uint32_t") || _strEQcc(CTL_STRINGIFY(T), "uint64_t") || |
| 76 | + _strEQcc(CTL_STRINGIFY(T), "int16_t") || _strEQcc(CTL_STRINGIFY(T), "int32_t") || |
| 77 | + _strEQcc(CTL_STRINGIFY(T), "int64_t") || _strEQcc(CTL_STRINGIFY(T), "unsigned_int") || |
| 78 | + _strEQcc(CTL_STRINGIFY(T), "unsigned_long") || _strEQcc(CTL_STRINGIFY(T), "unsigned_long_long") || |
| 79 | + _strEQcc(CTL_STRINGIFY(T), "unsigned_char") || |
| 80 | + /* and some common abbrevations */ |
| 81 | + _strEQcc(CTL_STRINGIFY(T), "uchar") || _strEQcc(CTL_STRINGIFY(T), "uint") || |
| 82 | + _strEQcc(CTL_STRINGIFY(T), "ulong") || _strEQcc(CTL_STRINGIFY(T), "ldbl") || |
| 83 | + _strEQcc(CTL_STRINGIFY(T), "llong"); |
| 84 | +} |
| 85 | + |
| 86 | +// not C++ |
| 87 | +#ifndef __cplusplus |
| 88 | +#define __set_str_hash(self, t) \ |
| 89 | + { \ |
| 90 | + typeof(t) tmp = (x); \ |
| 91 | + if (__builtin_types_compatible_p(typeof(t), char *)) \ |
| 92 | + self->hash = _JOIN(A, _default_string_hash); \ |
| 93 | + else if (__builtin_types_compatible_p(typeof(t), unsigned char *)) \ |
| 94 | + self->hash = _JOIN(A, _default_string_hash); \ |
| 95 | + } |
| 96 | +#else |
| 97 | +#define __set_str_hash(self, t) self->hash = _JOIN(A, _default_string_hash) |
| 98 | +#endif |
| 99 | + |
| 100 | +static inline void _JOIN(A, _set_default_methods)(A *self) |
| 101 | +{ |
| 102 | +#if !defined CTL_STR |
| 103 | +#if defined str || defined u8string || defined charp || defined u8ident || defined ucharp |
| 104 | + { |
| 105 | +#ifdef CTL_USET |
| 106 | + if (!self->hash) |
| 107 | + __set_str_hash(self, T); |
| 108 | +#else |
| 109 | + if (!self->compare) |
| 110 | + self->compare = str_key_compare; |
| 111 | +#endif |
| 112 | + if (!self->equal) |
| 113 | + self->equal = str_equal; |
| 114 | + } |
| 115 | + else |
| 116 | +#endif |
| 117 | +#endif |
| 118 | +#ifdef CTL_USET |
| 119 | + if (!self->hash) |
| 120 | + self->hash = _JOIN(A, _default_integral_hash); |
| 121 | +#else |
| 122 | + if (!self->compare) |
| 123 | + self->compare = _JOIN(A, _default_integral_compare); |
| 124 | +#endif |
| 125 | + if (!self->equal) |
| 126 | + self->equal = _JOIN(A, _default_integral_equal); |
| 127 | +} |
| 128 | + |
| 129 | +#else |
| 130 | + |
| 131 | +// non-integral types have no default methods. you need to set |
| 132 | +static inline void _JOIN(A, _set_default_methods)(A *self) |
| 133 | +{ |
| 134 | + (void)self; |
| 135 | +} |
| 136 | + |
| 137 | +#endif |
0 commit comments