| 1 | /* This is a public domain general purpose hash table package |
| 2 | originally written by Peter Moore @ UCB. |
| 3 | |
| 4 | The hash table data strutures were redesigned and the package was |
| 5 | rewritten by Vladimir Makarov <[email protected]>. */ |
| 6 | |
| 7 | #ifndef RUBY_ST_H |
| 8 | #define RUBY_ST_H 1 |
| 9 | |
| 10 | #if defined(__cplusplus) |
| 11 | extern "C" { |
| 12 | #if 0 |
| 13 | } /* satisfy cc-mode */ |
| 14 | #endif |
| 15 | #endif |
| 16 | |
| 17 | #ifdef RUBY |
| 18 | #include "ruby/defines.h" |
| 19 | #else /* RUBY */ |
| 20 | #ifndef RUBY_SYMBOL_EXPORT_BEGIN |
| 21 | #define RUBY_SYMBOL_EXPORT_BEGIN |
| 22 | #define RUBY_SYMBOL_EXPORT_END |
| 23 | #endif |
| 24 | #endif /* RUBY */ |
| 25 | |
| 26 | RUBY_SYMBOL_EXPORT_BEGIN |
| 27 | |
| 28 | #if SIZEOF_LONG == SIZEOF_VOIDP |
| 29 | typedef unsigned long st_data_t; |
| 30 | #elif SIZEOF_LONG_LONG == SIZEOF_VOIDP |
| 31 | typedef unsigned LONG_LONG st_data_t; |
| 32 | #else |
| 33 | # error ---->> st.c requires sizeof(void*) == sizeof(long) or sizeof(LONG_LONG) to be compiled. <<---- |
| 34 | #endif |
| 35 | #define ST_DATA_T_DEFINED |
| 36 | |
| 37 | #ifndef CHAR_BIT |
| 38 | # ifdef HAVE_LIMITS_H |
| 39 | # include <limits.h> |
| 40 | # else |
| 41 | # define CHAR_BIT 8 |
| 42 | # endif |
| 43 | #endif |
| 44 | #ifndef _ |
| 45 | # define _(args) args |
| 46 | #endif |
| 47 | #ifndef ANYARGS |
| 48 | # ifdef __cplusplus |
| 49 | # define ANYARGS ... |
| 50 | # else |
| 51 | # define ANYARGS |
| 52 | # endif |
| 53 | #endif |
| 54 | |
| 55 | typedef struct st_table st_table; |
| 56 | |
| 57 | typedef st_data_t st_index_t; |
| 58 | |
| 59 | /* Maximal value of unsigned integer type st_index_t. */ |
| 60 | #define MAX_ST_INDEX_VAL (~(st_index_t) 0) |
| 61 | |
| 62 | typedef int st_compare_func(st_data_t, st_data_t); |
| 63 | typedef st_index_t st_hash_func(st_data_t); |
| 64 | |
| 65 | typedef char st_check_for_sizeof_st_index_t[SIZEOF_VOIDP == (int)sizeof(st_index_t) ? 1 : -1]; |
| 66 | #define SIZEOF_ST_INDEX_T SIZEOF_VOIDP |
| 67 | |
| 68 | struct st_hash_type { |
| 69 | int (*compare)(ANYARGS /*st_data_t, st_data_t*/); /* st_compare_func* */ |
| 70 | st_index_t (*hash)(ANYARGS /*st_data_t*/); /* st_hash_func* */ |
| 71 | }; |
| 72 | |
| 73 | #define ST_INDEX_BITS (SIZEOF_ST_INDEX_T * CHAR_BIT) |
| 74 | |
| 75 | #if defined(HAVE_BUILTIN___BUILTIN_CHOOSE_EXPR) && defined(HAVE_BUILTIN___BUILTIN_TYPES_COMPATIBLE_P) |
| 76 | # define ST_DATA_COMPATIBLE_P(type) \ |
| 77 | __builtin_choose_expr(__builtin_types_compatible_p(type, st_data_t), 1, 0) |
| 78 | #else |
| 79 | # define ST_DATA_COMPATIBLE_P(type) 0 |
| 80 | #endif |
| 81 | |
| 82 | typedef struct st_table_entry st_table_entry; |
| 83 | |
| 84 | struct st_table_entry; /* defined in st.c */ |
| 85 | |
| 86 | struct st_table { |
| 87 | /* Cached features of the table -- see st.c for more details. */ |
| 88 | unsigned char entry_power, bin_power, size_ind; |
| 89 | /* How many times the table was rebuilt. */ |
| 90 | unsigned int rebuilds_num; |
| 91 | const struct st_hash_type *type; |
| 92 | /* Number of entries currently in the table. */ |
| 93 | st_index_t num_entries; |
| 94 | /* Array of bins used for access by keys. */ |
| 95 | st_index_t *bins; |
| 96 | /* Start and bound index of entries in array entries. |
| 97 | entries_starts and entries_bound are in interval |
| 98 | [0,allocated_entries]. */ |
| 99 | st_index_t entries_start, entries_bound; |
| 100 | /* Array of size 2^entry_power. */ |
| 101 | st_table_entry *entries; |
| 102 | }; |
| 103 | |
| 104 | #define st_is_member(table,key) st_lookup((table),(key),(st_data_t *)0) |
| 105 | |
| 106 | enum st_retval {ST_CONTINUE, ST_STOP, ST_DELETE, ST_CHECK}; |
| 107 | |
| 108 | st_table *st_init_table(const struct st_hash_type *); |
| 109 | st_table *st_init_table_with_size(const struct st_hash_type *, st_index_t); |
| 110 | st_table *st_init_numtable(void); |
| 111 | st_table *st_init_numtable_with_size(st_index_t); |
| 112 | st_table *st_init_strtable(void); |
| 113 | st_table *st_init_strtable_with_size(st_index_t); |
| 114 | st_table *st_init_strcasetable(void); |
| 115 | st_table *st_init_strcasetable_with_size(st_index_t); |
| 116 | int st_delete(st_table *, st_data_t *, st_data_t *); /* returns 0:notfound 1:deleted */ |
| 117 | int st_delete_safe(st_table *, st_data_t *, st_data_t *, st_data_t); |
| 118 | int st_shift(st_table *, st_data_t *, st_data_t *); /* returns 0:notfound 1:deleted */ |
| 119 | int st_insert(st_table *, st_data_t, st_data_t); |
| 120 | int st_insert2(st_table *, st_data_t, st_data_t, st_data_t (*)(st_data_t)); |
| 121 | int st_lookup(st_table *, st_data_t, st_data_t *); |
| 122 | int st_get_key(st_table *, st_data_t, st_data_t *); |
| 123 | typedef int st_update_callback_func(st_data_t *key, st_data_t *value, st_data_t arg, int existing); |
| 124 | /* *key may be altered, but must equal to the old key, i.e., the |
| 125 | * results of hash() are same and compare() returns 0, otherwise the |
| 126 | * behavior is undefined */ |
| 127 | int st_update(st_table *table, st_data_t key, st_update_callback_func *func, st_data_t arg); |
| 128 | int st_foreach(st_table *, int (*)(ANYARGS), st_data_t); |
| 129 | int st_foreach_check(st_table *, int (*)(ANYARGS), st_data_t, st_data_t); |
| 130 | st_index_t st_keys(st_table *table, st_data_t *keys, st_index_t size); |
| 131 | st_index_t st_keys_check(st_table *table, st_data_t *keys, st_index_t size, st_data_t never); |
| 132 | st_index_t st_values(st_table *table, st_data_t *values, st_index_t size); |
| 133 | st_index_t st_values_check(st_table *table, st_data_t *values, st_index_t size, st_data_t never); |
| 134 | void st_add_direct(st_table *, st_data_t, st_data_t); |
| 135 | void st_free_table(st_table *); |
| 136 | void st_cleanup_safe(st_table *, st_data_t); |
| 137 | void st_clear(st_table *); |
| 138 | st_table *st_copy(st_table *); |
| 139 | int st_numcmp(st_data_t, st_data_t); |
| 140 | st_index_t st_numhash(st_data_t); |
| 141 | int st_locale_insensitive_strcasecmp(const char *s1, const char *s2); |
| 142 | int st_locale_insensitive_strncasecmp(const char *s1, const char *s2, size_t n); |
| 143 | #define st_strcasecmp st_locale_insensitive_strcasecmp |
| 144 | #define st_strncasecmp st_locale_insensitive_strncasecmp |
| 145 | size_t st_memsize(const st_table *); |
| 146 | st_index_t st_hash(const void *ptr, size_t len, st_index_t h); |
| 147 | st_index_t st_hash_uint32(st_index_t h, uint32_t i); |
| 148 | st_index_t st_hash_uint(st_index_t h, st_index_t i); |
| 149 | st_index_t st_hash_end(st_index_t h); |
| 150 | st_index_t st_hash_start(st_index_t h); |
| 151 | #define st_hash_start(h) ((st_index_t)(h)) |
| 152 | |
| 153 | RUBY_SYMBOL_EXPORT_END |
| 154 | |
| 155 | #if defined(__cplusplus) |
| 156 | #if 0 |
| 157 | { /* satisfy cc-mode */ |
| 158 | #endif |
| 159 | } /* extern "C" { */ |
| 160 | #endif |
| 161 | |
| 162 | #endif /* RUBY_ST_H */ |