Ruby 4.1.0dev (2026-05-18 revision e4e1c92a1dde6e7bcdadc254306940d005835642)
st.h
1/* This is a public domain general purpose hash table package
2 originally written by Peter Moore @ UCB.
3
4 The hash table data structures were redesigned and the package was
5 rewritten by Vladimir Makarov <vmakarov@redhat.com>. */
6
7#ifndef RUBY_ST_H
8#define RUBY_ST_H 1
9
10#if defined(__cplusplus)
11extern "C" {
12#if 0
13} /* satisfy cc-mode */
14#endif
15#endif
16
17#include "ruby/defines.h"
18
19RUBY_SYMBOL_EXPORT_BEGIN
20
21#if SIZEOF_LONG == SIZEOF_VOIDP
22typedef unsigned long st_data_t;
23#elif SIZEOF_LONG_LONG == SIZEOF_VOIDP
24typedef unsigned LONG_LONG st_data_t;
25#else
26# error ---->> st.c requires sizeof(void*) == sizeof(long) or sizeof(LONG_LONG) to be compiled. <<----
27#endif
28#define ST_DATA_T_DEFINED
29
30#ifndef CHAR_BIT
31# ifdef HAVE_LIMITS_H
32# include <limits.h>
33# else
34# define CHAR_BIT 8
35# endif
36#endif
37#ifndef _
38# define _(args) args
39#endif
40#ifndef ANYARGS
41# ifdef __cplusplus
42# define ANYARGS ...
43# else
44# define ANYARGS
45# endif
46#endif
47
48typedef struct st_table st_table;
49
50typedef st_data_t st_index_t;
51
52/* Maximal value of unsigned integer type st_index_t. */
53#define MAX_ST_INDEX_VAL (~(st_index_t) 0)
54
55typedef int st_compare_func(st_data_t, st_data_t);
56typedef st_index_t st_hash_func(st_data_t);
57
58typedef char st_check_for_sizeof_st_index_t[SIZEOF_VOIDP == (int)sizeof(st_index_t) ? 1 : -1];
59#define SIZEOF_ST_INDEX_T SIZEOF_VOIDP
60
62 int (*compare)(st_data_t, st_data_t); /* st_compare_func* */
63 st_index_t (*hash)(st_data_t); /* st_hash_func* */
64};
65
66#define ST_INDEX_BITS (SIZEOF_ST_INDEX_T * CHAR_BIT)
67
68#if defined(HAVE_BUILTIN___BUILTIN_CHOOSE_EXPR) && defined(HAVE_BUILTIN___BUILTIN_TYPES_COMPATIBLE_P)
69# define ST_DATA_COMPATIBLE_P(type) \
70 __builtin_choose_expr(__builtin_types_compatible_p(type, st_data_t), 1, 0)
71#else
72# define ST_DATA_COMPATIBLE_P(type) 0
73#endif
74
75typedef struct st_table_entry st_table_entry;
76
77struct st_table_entry; /* defined in st.c */
78
79struct st_table {
80 /* Cached features of the table -- see st.c for more details. */
81 unsigned char entry_power, bin_power, size_ind;
82 /* How many times the table was rebuilt. */
83 unsigned int rebuilds_num;
84 const struct st_hash_type *type;
85 /* Number of entries currently in the table. */
86 st_index_t num_entries;
87 /* Start and bound index of entries in array entries.
88 entries_starts and entries_bound are in interval
89 [0,allocated_entries]. */
90 st_index_t entries_start, entries_bound;
91 /* Array of size 2^entry_power.
92 Optionnally followed by an array of bins used for access by keys. */
93 st_table_entry *entries;
94};
95
96#define st_is_member(table,key) st_lookup((table),(key),(st_data_t *)0)
97
98enum st_retval {ST_CONTINUE, ST_STOP, ST_DELETE, ST_CHECK, ST_REPLACE};
99
100size_t rb_st_table_size(const struct st_table *tbl);
101#define st_table_size rb_st_table_size
102st_table *rb_st_init_table(const struct st_hash_type *);
103#define st_init_table rb_st_init_table
104st_table *rb_st_init_table_with_size(const struct st_hash_type *, st_index_t);
105#define st_init_table_with_size rb_st_init_table_with_size
106st_table *rb_st_init_numtable(void);
107#define st_init_numtable rb_st_init_numtable
108st_table *rb_st_init_numtable_with_size(st_index_t);
109#define st_init_numtable_with_size rb_st_init_numtable_with_size
110st_table *rb_st_init_strtable(void);
111#define st_init_strtable rb_st_init_strtable
112st_table *rb_st_init_strtable_with_size(st_index_t);
113#define st_init_strtable_with_size rb_st_init_strtable_with_size
114st_table *rb_st_init_strcasetable(void);
115#define st_init_strcasetable rb_st_init_strcasetable
116st_table *rb_st_init_strcasetable_with_size(st_index_t);
117#define st_init_strcasetable_with_size rb_st_init_strcasetable_with_size
118int rb_st_delete(st_table *, st_data_t *, st_data_t *); /* returns 0:notfound 1:deleted */
119#define st_delete rb_st_delete
120int rb_st_delete_safe(st_table *, st_data_t *, st_data_t *, st_data_t);
121#define st_delete_safe rb_st_delete_safe
122int rb_st_shift(st_table *, st_data_t *, st_data_t *); /* returns 0:notfound 1:deleted */
123#define st_shift rb_st_shift
124int rb_st_insert(st_table *, st_data_t, st_data_t);
125#define st_insert rb_st_insert
126int rb_st_insert2(st_table *, st_data_t, st_data_t, st_data_t (*)(st_data_t));
127#define st_insert2 rb_st_insert2
128int rb_st_lookup(st_table *, st_data_t, st_data_t *);
129#define st_lookup rb_st_lookup
130int rb_st_get_key(st_table *, st_data_t, st_data_t *);
131#define st_get_key rb_st_get_key
132typedef int st_update_callback_func(st_data_t *key, st_data_t *value, st_data_t arg, int existing);
133/* *key may be altered, but must equal to the old key, i.e., the
134 * results of hash() are same and compare() returns 0, otherwise the
135 * behavior is undefined */
136int rb_st_update(st_table *table, st_data_t key, st_update_callback_func *func, st_data_t arg);
137#define st_update rb_st_update
138typedef int st_foreach_callback_func(st_data_t, st_data_t, st_data_t);
139typedef int st_foreach_check_callback_func(st_data_t, st_data_t, st_data_t, int);
140int rb_st_foreach_with_replace(st_table *tab, st_foreach_check_callback_func *func, st_update_callback_func *replace, st_data_t arg);
141#define st_foreach_with_replace rb_st_foreach_with_replace
142int rb_st_foreach(st_table *, st_foreach_callback_func *, st_data_t);
143#define st_foreach rb_st_foreach
144int rb_st_foreach_check(st_table *, st_foreach_check_callback_func *, st_data_t, st_data_t);
145#define st_foreach_check rb_st_foreach_check
146st_index_t rb_st_keys(st_table *table, st_data_t *keys, st_index_t size);
147#define st_keys rb_st_keys
148st_index_t rb_st_keys_check(st_table *table, st_data_t *keys, st_index_t size, st_data_t never);
149#define st_keys_check rb_st_keys_check
150st_index_t rb_st_values(st_table *table, st_data_t *values, st_index_t size);
151#define st_values rb_st_values
152st_index_t rb_st_values_check(st_table *table, st_data_t *values, st_index_t size, st_data_t never);
153#define st_values_check rb_st_values_check
154void rb_st_add_direct(st_table *, st_data_t, st_data_t);
155#define st_add_direct rb_st_add_direct
156void rb_st_free_table(st_table *);
157#define st_free_table rb_st_free_table
158void rb_st_cleanup_safe(st_table *, st_data_t);
159#define st_cleanup_safe rb_st_cleanup_safe
160void rb_st_clear(st_table *);
161#define st_clear rb_st_clear
162st_table *rb_st_copy(st_table *);
163#define st_copy rb_st_copy
164CONSTFUNC(int rb_st_numcmp(st_data_t, st_data_t));
165#define st_numcmp rb_st_numcmp
166CONSTFUNC(st_index_t rb_st_numhash(st_data_t));
167#define st_numhash rb_st_numhash
168PUREFUNC(int rb_st_locale_insensitive_strcasecmp(const char *s1, const char *s2));
169#define st_locale_insensitive_strcasecmp rb_st_locale_insensitive_strcasecmp
170PUREFUNC(int rb_st_locale_insensitive_strncasecmp(const char *s1, const char *s2, size_t n));
171#define st_locale_insensitive_strncasecmp rb_st_locale_insensitive_strncasecmp
172#define st_strcasecmp rb_st_locale_insensitive_strcasecmp
173#define st_strncasecmp rb_st_locale_insensitive_strncasecmp
174PUREFUNC(size_t rb_st_memsize(const st_table *));
175#define st_memsize rb_st_memsize
176PUREFUNC(st_index_t rb_st_hash(const void *ptr, size_t len, st_index_t h));
177#define st_hash rb_st_hash
178CONSTFUNC(st_index_t rb_st_hash_uint32(st_index_t h, uint32_t i));
179#define st_hash_uint32 rb_st_hash_uint32
180CONSTFUNC(st_index_t rb_st_hash_uint(st_index_t h, st_index_t i));
181#define st_hash_uint rb_st_hash_uint
182CONSTFUNC(st_index_t rb_st_hash_end(st_index_t h));
183#define st_hash_end rb_st_hash_end
184CONSTFUNC(st_index_t rb_st_hash_start(st_index_t h));
185#define st_hash_start(h) ((st_index_t)(h))
186
187void rb_hash_bulk_insert_into_st_table(long, const VALUE *, VALUE);
188
189RUBY_SYMBOL_EXPORT_END
190
191#if defined(__cplusplus)
192#if 0
193{ /* satisfy cc-mode */
194#endif
195} /* extern "C" { */
196#endif
197
198#endif /* RUBY_ST_H */
#define LONG_LONG
Definition long_long.h:38
int len
Length of the buffer.
Definition io.h:8
Definition st.c:137
Definition st.h:79
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40