Ruby 3.0.5p211 (2022-11-24 revision ba5cf0f7c52d4d35cc6a173c89eda98ceffa2dcf)
hash.h
Go to the documentation of this file.
1#ifndef INTERNAL_HASH_H /*-*-C-*-vi:se ft=c:*/
2#define INTERNAL_HASH_H
13#include <stddef.h> /* for size_t */
14#include "ruby/internal/stdbool.h" /* for bool */
15#include "ruby/ruby.h" /* for struct RBasic */
16#include "ruby/st.h" /* for struct st_table */
17
18#define RHASH_AR_TABLE_MAX_SIZE SIZEOF_VALUE
19
20struct ar_table_struct;
21typedef unsigned char ar_hint_t;
22
31
32#if USE_TRANSIENT_HEAP
33 RHASH_TRANSIENT_FLAG = FL_USER12, /* FL 12 */
34#endif
35
36 // we can not put it in "enum" because it can exceed "int" range.
37#define RHASH_LEV_MASK (FL_USER13 | FL_USER14 | FL_USER15 | /* FL 13..19 */ \
38 FL_USER16 | FL_USER17 | FL_USER18 | FL_USER19)
39
41 RHASH_LEV_MAX = 127, /* 7 bits */
42};
43
44struct RHash {
45 struct RBasic basic;
46 union {
48 struct ar_table_struct *ar; /* possibly 0 */
49 } as;
51 union {
55};
56
57#define RHASH(obj) ((struct RHash *)(obj))
58
59#ifdef RHASH_IFNONE
60# undef RHASH_IFNONE
61#endif
62
63#ifdef RHASH_SIZE
64# undef RHASH_SIZE
65#endif
66
67#ifdef RHASH_EMPTY_P
68# undef RHASH_EMPTY_P
69#endif
70
71/* hash.c */
72void rb_hash_st_table_set(VALUE hash, st_table *st);
75long rb_dbl_long_hash(double d);
83int rb_hash_stlike_delete(VALUE hash, st_data_t *pkey, st_data_t *pval);
86extern st_table *rb_hash_st_table(VALUE hash);
87
88static inline unsigned RHASH_AR_TABLE_SIZE_RAW(VALUE h);
89static inline VALUE RHASH_IFNONE(VALUE h);
90static inline size_t RHASH_SIZE(VALUE h);
91static inline bool RHASH_EMPTY_P(VALUE h);
92static inline bool RHASH_AR_TABLE_P(VALUE h);
93static inline bool RHASH_ST_TABLE_P(VALUE h);
94static inline struct ar_table_struct *RHASH_AR_TABLE(VALUE h);
95static inline st_table *RHASH_ST_TABLE(VALUE h);
96static inline size_t RHASH_ST_SIZE(VALUE h);
97static inline void RHASH_ST_CLEAR(VALUE h);
98static inline bool RHASH_TRANSIENT_P(VALUE h);
99static inline void RHASH_SET_TRANSIENT_FLAG(VALUE h);
100static inline void RHASH_UNSET_TRANSIENT_FLAG(VALUE h);
101
102RUBY_SYMBOL_EXPORT_BEGIN
103/* hash.c (export) */
107RUBY_SYMBOL_EXPORT_END
108
116
117st_table *rb_hash_tbl_raw(VALUE hash, const char *file, int line);
118#define RHASH_TBL_RAW(h) rb_hash_tbl_raw(h, __FILE__, __LINE__)
120
121#if 0 /* for debug */
122
123static inline bool
124RHASH_AR_TABLE_P(VALUE h)
125{
126 extern int rb_hash_ar_table_p(VALUE hash);
127 return rb_hash_ar_table_p(h)
128}
129
130static inline struct ar_table_struct *
131RHASH_AR_TABLE(VALUE h)
132{
133 extern struct ar_table_struct *rb_hash_ar_table(VALUE hash);
134 return rb_hash_ar_table(h)
135}
136
137static inline st_table *
138RHASH_ST_TABLE(VALUE h)
139{
140 return rb_hash_st_table(h)
141}
142
143#else
144
145static inline bool
146RHASH_AR_TABLE_P(VALUE h)
147{
148 return ! FL_TEST_RAW(h, RHASH_ST_TABLE_FLAG);
149}
150
151static inline struct ar_table_struct *
152RHASH_AR_TABLE(VALUE h)
153{
154 return RHASH(h)->as.ar;
155}
156
157static inline st_table *
158RHASH_ST_TABLE(VALUE h)
159{
160 return RHASH(h)->as.st;
161}
162
163#endif
164
165static inline VALUE
167{
168 return RHASH(h)->ifnone;
169}
170
171static inline size_t
173{
174 if (RHASH_AR_TABLE_P(h)) {
175 return RHASH_AR_TABLE_SIZE_RAW(h);
176 }
177 else {
178 return RHASH_ST_SIZE(h);
179 }
180}
181
182static inline bool
184{
185 return RHASH_SIZE(h) == 0;
186}
187
188static inline bool
189RHASH_ST_TABLE_P(VALUE h)
190{
191 return ! RHASH_AR_TABLE_P(h);
192}
193
194static inline size_t
195RHASH_ST_SIZE(VALUE h)
196{
197 return RHASH_ST_TABLE(h)->num_entries;
198}
199
200static inline void
201RHASH_ST_CLEAR(VALUE h)
202{
204 RHASH(h)->as.ar = NULL;
205}
206
207static inline unsigned
208RHASH_AR_TABLE_SIZE_RAW(VALUE h)
209{
212 return (unsigned)ret;
213}
214
215static inline bool
216RHASH_TRANSIENT_P(VALUE h)
217{
218#if USE_TRANSIENT_HEAP
219 return FL_TEST_RAW(h, RHASH_TRANSIENT_FLAG);
220#else
221 return false;
222#endif
223}
224
225static inline void
226RHASH_SET_TRANSIENT_FLAG(VALUE h)
227{
228#if USE_TRANSIENT_HEAP
229 FL_SET_RAW(h, RHASH_TRANSIENT_FLAG);
230#endif
231}
232
233static inline void
234RHASH_UNSET_TRANSIENT_FLAG(VALUE h)
235{
236#if USE_TRANSIENT_HEAP
237 FL_UNSET_RAW(h, RHASH_TRANSIENT_FLAG);
238#endif
239}
240
241#endif /* INTERNAL_HASH_H */
#define MJIT_SYMBOL_EXPORT_END
Definition: dllexport.h:63
#define MJIT_SYMBOL_EXPORT_BEGIN
Definition: dllexport.h:62
#define FL_USER3
Definition: fl_type.h:66
#define FL_USER7
Definition: fl_type.h:70
#define FL_USER10
Definition: fl_type.h:73
#define FL_USER6
Definition: fl_type.h:69
#define FL_USER1
Definition: fl_type.h:64
#define FL_USER12
Definition: fl_type.h:75
#define FL_USER11
Definition: fl_type.h:74
#define FL_USER8
Definition: fl_type.h:71
#define FL_USER2
Definition: fl_type.h:65
#define FL_USER9
Definition: fl_type.h:72
#define FL_USER5
Definition: fl_type.h:68
#define FL_USHIFT
Definition: fl_type.h:61
#define FL_USER4
Definition: fl_type.h:67
#define FL_UNSET_RAW
Definition: fl_type.h:133
#define FL_TEST_RAW
Definition: fl_type.h:131
#define FL_SET_RAW
Definition: fl_type.h:129
VALUE rb_ident_hash_new(void)
Definition: hash.c:4443
int rb_hash_ar_table_p(VALUE hash)
Definition: hash.c:548
ar_table * rb_hash_ar_table(VALUE hash)
Definition: hash.c:560
Thin wrapper to ruby/config.h.
VALUE rb_hash_default_value(VALUE hash, VALUE key)
Definition: hash.c:1999
int rb_hash_stlike_foreach_with_replace(VALUE hash, st_foreach_check_callback_func *func, st_update_callback_func *replace, st_data_t arg)
Definition: hash.c:1468
VALUE rb_hash_values(VALUE hash)
Definition: hash.c:3593
VALUE rb_hash_resurrect(VALUE hash)
Definition: hash.c:1590
void rb_hash_st_table_set(VALUE hash, st_table *st)
Definition: hash.c:574
int rb_hash_add_new_element(VALUE hash, VALUE key, VALUE val)
Definition: hash.c:4734
int rb_hash_stlike_delete(VALUE hash, st_data_t *pkey, st_data_t *pval)
Definition: hash.c:2293
VALUE rb_hash_set_pair(VALUE hash, VALUE pair)
Definition: hash.c:3468
VALUE rb_hash_rehash(VALUE hash)
Definition: hash.c:1960
VALUE rb_hash_has_key(VALUE hash, VALUE key)
Definition: hash.c:3638
VALUE rb_hash_key_str(VALUE)
Definition: hash.c:2852
unsigned char ar_hint_t
Definition: hash.h:21
VALUE rb_hash_new_with_size(st_index_t size)
Definition: hash.c:1544
VALUE rb_to_hash_type(VALUE obj)
Definition: hash.c:1853
VALUE rb_hash_compare_by_id_p(VALUE hash)
Definition: hash.c:4432
#define RHASH_AR_TABLE_MAX_SIZE
Definition: hash.h:18
VALUE rb_hash_set_default_proc(VALUE hash, VALUE proc)
Definition: hash.c:2234
VALUE rb_hash_keys(VALUE hash)
Definition: hash.c:3549
st_table * rb_hash_st_table(VALUE hash)
Definition: hash.c:567
int rb_hash_stlike_foreach(VALUE hash, st_foreach_callback_func *func, st_data_t arg)
Definition: hash.c:1457
VALUE rb_hash_delete_entry(VALUE hash, VALUE key)
Definition: hash.c:2309
#define RHASH(obj)
Definition: hash.h:57
ruby_rhash_flags
Definition: hash.h:23
@ RHASH_PASS_AS_KEYWORDS
Definition: hash.h:24
@ RHASH_LEV_MAX
Definition: hash.h:41
@ RHASH_AR_TABLE_BOUND_SHIFT
Definition: hash.h:30
@ RHASH_AR_TABLE_BOUND_MASK
Definition: hash.h:29
@ RHASH_AR_TABLE_SIZE_MASK
Definition: hash.h:27
@ RHASH_LEV_SHIFT
Definition: hash.h:40
@ RHASH_ST_TABLE_FLAG
Definition: hash.h:26
@ RHASH_AR_TABLE_SIZE_SHIFT
Definition: hash.h:28
@ RHASH_PROC_DEFAULT
Definition: hash.h:25
long rb_dbl_long_hash(double d)
Definition: hash.c:180
int rb_hash_stlike_update(VALUE hash, st_data_t key, st_update_callback_func *func, st_data_t arg)
Definition: hash.c:1659
st_table * rb_hash_tbl_raw(VALUE hash, const char *file, int line)
Definition: hash.c:1603
int rb_hash_stlike_lookup(VALUE hash, st_data_t key, st_data_t *pval)
Definition: hash.c:2026
st_table * rb_init_identtable(void)
Definition: hash.c:4451
voidpf void uLong size
Definition: ioapi.h:138
#define NULL
Definition: regenc.h:69
#define RHASH_IFNONE(h)
Definition: rhash.h:49
#define RHASH_SIZE(h)
Definition: rhash.h:50
#define RHASH_EMPTY_P(h)
Definition: rhash.h:51
unsigned long st_data_t
Definition: st.h:22
int st_foreach_check_callback_func(st_data_t, st_data_t, st_data_t, int)
Definition: st.h:138
int st_update_callback_func(st_data_t *key, st_data_t *value, st_data_t arg, int existing)
Definition: st.h:131
st_data_t st_index_t
Definition: st.h:50
int st_foreach_callback_func(st_data_t, st_data_t, st_data_t)
Definition: st.h:137
C99 shim for <stdbool.h>
Definition: rbasic.h:47
Definition: hash.h:44
VALUE word
Definition: hash.h:53
struct RBasic basic
Definition: hash.h:45
st_table * st
Definition: hash.h:47
struct ar_table_struct * ar
Definition: hash.h:48
union RHash::@111 ar_hint
union RHash::@110 as
const VALUE ifnone
Definition: hash.h:50
Definition: gzappend.c:170
Definition: st.h:79
st_index_t num_entries
Definition: st.h:86
unsigned long VALUE
Definition: value.h:38