Ruby: internal/hash.h Source File

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
12#include "ruby/internal/config.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;
21 typedef unsigned char ar_hint_t;
22
23 enum ruby_rhash_flags {
24 RHASH_PASS_AS_KEYWORDS = FL_USER1, /* FL 1 */
25 RHASH_PROC_DEFAULT = FL_USER2, /* FL 2 */
26 RHASH_ST_TABLE_FLAG = FL_USER3, /* FL 3 */
27 RHASH_AR_TABLE_SIZE_MASK = (FL_USER4|FL_USER5|FL_USER6|FL_USER7), /* FL 4..7 */
28 RHASH_AR_TABLE_SIZE_SHIFT = (FL_USHIFT+4),
29 RHASH_AR_TABLE_BOUND_MASK = (FL_USER8|FL_USER9|FL_USER10|FL_USER11), /* FL 8..11 */
30 RHASH_AR_TABLE_BOUND_SHIFT = (FL_USHIFT+8),
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
40 RHASH_LEV_SHIFT = (FL_USHIFT + 13),
41 RHASH_LEV_MAX = 127, /* 7 bits */
42};
43
44 struct RHash {
45 struct RBasic basic;
46 union {
47 st_table *st;
48 struct ar_table_struct *ar; /* possibly 0 */
49 } as;
50 const VALUE ifnone;
51 union {
52 ar_hint_t ary[RHASH_AR_TABLE_MAX_SIZE];
53 VALUE word;
54 } ar_hint;
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);
73 VALUE rb_hash_default_value(VALUE hash, VALUE key);
74 VALUE rb_hash_set_default_proc(VALUE hash, VALUE proc);
75long rb_dbl_long_hash(double d);
76 st_table *rb_init_identtable(void);
77 VALUE rb_to_hash_type(VALUE obj);
78 VALUE rb_hash_key_str(VALUE);
79 VALUE rb_hash_values(VALUE hash);
80 VALUE rb_hash_rehash(VALUE hash);
81int rb_hash_add_new_element(VALUE hash, VALUE key, VALUE val);
82 VALUE rb_hash_set_pair(VALUE hash, VALUE pair);
83int rb_hash_stlike_delete(VALUE hash, st_data_t *pkey, st_data_t *pval);
84int rb_hash_stlike_foreach_with_replace(VALUE hash, st_foreach_check_callback_func *func, st_update_callback_func *replace, st_data_t arg);
85int rb_hash_stlike_update(VALUE hash, st_data_t key, st_update_callback_func *func, st_data_t arg);
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) */
104 VALUE rb_hash_delete_entry(VALUE hash, VALUE key);
105 VALUE rb_ident_hash_new(void);
106int rb_hash_stlike_foreach(VALUE hash, st_foreach_callback_func *func, st_data_t arg);
107RUBY_SYMBOL_EXPORT_END
108
109 MJIT_SYMBOL_EXPORT_BEGIN
110 VALUE rb_hash_new_with_size(st_index_t size);
111 VALUE rb_hash_resurrect(VALUE hash);
112int rb_hash_stlike_lookup(VALUE hash, st_data_t key, st_data_t *pval);
113 VALUE rb_hash_keys(VALUE hash);
114 VALUE rb_hash_has_key(VALUE hash, VALUE key);
115 VALUE rb_hash_compare_by_id_p(VALUE hash);
116
117 st_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__)
119 MJIT_SYMBOL_EXPORT_END
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
166 RHASH_IFNONE(VALUE h)
167{
168 return RHASH(h)->ifnone;
169}
170
171static inline size_t
172 RHASH_SIZE(VALUE h)
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
183 RHASH_EMPTY_P(VALUE h)
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{
203 FL_UNSET_RAW(h, RHASH_ST_TABLE_FLAG);
204 RHASH(h)->as.ar = NULL;
205}
206
207static inline unsigned
208RHASH_AR_TABLE_SIZE_RAW(VALUE h)
209{
210 VALUE ret = FL_TEST_RAW(h, RHASH_AR_TABLE_SIZE_MASK);
211 ret >>= RHASH_AR_TABLE_SIZE_SHIFT;
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 */
MJIT_SYMBOL_EXPORT_END
#define MJIT_SYMBOL_EXPORT_END
Definition: dllexport.h:63
MJIT_SYMBOL_EXPORT_BEGIN
#define MJIT_SYMBOL_EXPORT_BEGIN
Definition: dllexport.h:62
FL_USER3
#define FL_USER3
Definition: fl_type.h:66
FL_USER7
#define FL_USER7
Definition: fl_type.h:70
FL_USER10
#define FL_USER10
Definition: fl_type.h:73
FL_USER6
#define FL_USER6
Definition: fl_type.h:69
FL_USER1
#define FL_USER1
Definition: fl_type.h:64
FL_USER12
#define FL_USER12
Definition: fl_type.h:75
FL_USER11
#define FL_USER11
Definition: fl_type.h:74
FL_USER8
#define FL_USER8
Definition: fl_type.h:71
FL_USER2
#define FL_USER2
Definition: fl_type.h:65
FL_USER9
#define FL_USER9
Definition: fl_type.h:72
FL_USER5
#define FL_USER5
Definition: fl_type.h:68
FL_USHIFT
#define FL_USHIFT
Definition: fl_type.h:61
FL_USER4
#define FL_USER4
Definition: fl_type.h:67
FL_UNSET_RAW
#define FL_UNSET_RAW
Definition: fl_type.h:133
FL_TEST_RAW
#define FL_TEST_RAW
Definition: fl_type.h:131
FL_SET_RAW
#define FL_SET_RAW
Definition: fl_type.h:129
rb_ident_hash_new
VALUE rb_ident_hash_new(void)
Definition: hash.c:4443
rb_hash_ar_table_p
int rb_hash_ar_table_p(VALUE hash)
Definition: hash.c:548
rb_hash_ar_table
ar_table * rb_hash_ar_table(VALUE hash)
Definition: hash.c:560
config.h
Thin wrapper to ruby/config.h.
rb_hash_default_value
VALUE rb_hash_default_value(VALUE hash, VALUE key)
Definition: hash.c:1999
rb_hash_stlike_foreach_with_replace
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
rb_hash_values
VALUE rb_hash_values(VALUE hash)
Definition: hash.c:3593
rb_hash_resurrect
VALUE rb_hash_resurrect(VALUE hash)
Definition: hash.c:1590
rb_hash_st_table_set
void rb_hash_st_table_set(VALUE hash, st_table *st)
Definition: hash.c:574
rb_hash_add_new_element
int rb_hash_add_new_element(VALUE hash, VALUE key, VALUE val)
Definition: hash.c:4734
rb_hash_stlike_delete
int rb_hash_stlike_delete(VALUE hash, st_data_t *pkey, st_data_t *pval)
Definition: hash.c:2293
rb_hash_set_pair
VALUE rb_hash_set_pair(VALUE hash, VALUE pair)
Definition: hash.c:3468
rb_hash_rehash
VALUE rb_hash_rehash(VALUE hash)
Definition: hash.c:1960
rb_hash_has_key
VALUE rb_hash_has_key(VALUE hash, VALUE key)
Definition: hash.c:3638
rb_hash_key_str
VALUE rb_hash_key_str(VALUE)
Definition: hash.c:2852
ar_hint_t
unsigned char ar_hint_t
Definition: hash.h:21
rb_hash_new_with_size
VALUE rb_hash_new_with_size(st_index_t size)
Definition: hash.c:1544
rb_to_hash_type
VALUE rb_to_hash_type(VALUE obj)
Definition: hash.c:1853
rb_hash_compare_by_id_p
VALUE rb_hash_compare_by_id_p(VALUE hash)
Definition: hash.c:4432
RHASH_AR_TABLE_MAX_SIZE
#define RHASH_AR_TABLE_MAX_SIZE
Definition: hash.h:18
rb_hash_set_default_proc
VALUE rb_hash_set_default_proc(VALUE hash, VALUE proc)
Definition: hash.c:2234
rb_hash_keys
VALUE rb_hash_keys(VALUE hash)
Definition: hash.c:3549
rb_hash_st_table
st_table * rb_hash_st_table(VALUE hash)
Definition: hash.c:567
rb_hash_stlike_foreach
int rb_hash_stlike_foreach(VALUE hash, st_foreach_callback_func *func, st_data_t arg)
Definition: hash.c:1457
rb_hash_delete_entry
VALUE rb_hash_delete_entry(VALUE hash, VALUE key)
Definition: hash.c:2309
RHASH
#define RHASH(obj)
Definition: hash.h:57
ruby_rhash_flags
ruby_rhash_flags
Definition: hash.h:23
RHASH_PASS_AS_KEYWORDS
@ RHASH_PASS_AS_KEYWORDS
Definition: hash.h:24
RHASH_LEV_MAX
@ RHASH_LEV_MAX
Definition: hash.h:41
RHASH_AR_TABLE_BOUND_SHIFT
@ RHASH_AR_TABLE_BOUND_SHIFT
Definition: hash.h:30
RHASH_AR_TABLE_BOUND_MASK
@ RHASH_AR_TABLE_BOUND_MASK
Definition: hash.h:29
RHASH_AR_TABLE_SIZE_MASK
@ RHASH_AR_TABLE_SIZE_MASK
Definition: hash.h:27
RHASH_LEV_SHIFT
@ RHASH_LEV_SHIFT
Definition: hash.h:40
RHASH_ST_TABLE_FLAG
@ RHASH_ST_TABLE_FLAG
Definition: hash.h:26
RHASH_AR_TABLE_SIZE_SHIFT
@ RHASH_AR_TABLE_SIZE_SHIFT
Definition: hash.h:28
RHASH_PROC_DEFAULT
@ RHASH_PROC_DEFAULT
Definition: hash.h:25
rb_dbl_long_hash
long rb_dbl_long_hash(double d)
Definition: hash.c:180
rb_hash_stlike_update
int rb_hash_stlike_update(VALUE hash, st_data_t key, st_update_callback_func *func, st_data_t arg)
Definition: hash.c:1659
rb_hash_tbl_raw
st_table * rb_hash_tbl_raw(VALUE hash, const char *file, int line)
Definition: hash.c:1603
rb_hash_stlike_lookup
int rb_hash_stlike_lookup(VALUE hash, st_data_t key, st_data_t *pval)
Definition: hash.c:2026
rb_init_identtable
st_table * rb_init_identtable(void)
Definition: hash.c:4451
size
voidpf void uLong size
Definition: ioapi.h:138
key
key
Definition: openssl_missing.h:185
NULL
#define NULL
Definition: regenc.h:69
RHASH_IFNONE
#define RHASH_IFNONE(h)
Definition: rhash.h:49
RHASH_SIZE
#define RHASH_SIZE(h)
Definition: rhash.h:50
RHASH_EMPTY_P
#define RHASH_EMPTY_P(h)
Definition: rhash.h:51
ruby.h
st.h
st_data_t
unsigned long st_data_t
Definition: st.h:22
st_foreach_check_callback_func
int st_foreach_check_callback_func(st_data_t, st_data_t, st_data_t, int)
Definition: st.h:138
st_update_callback_func
int st_update_callback_func(st_data_t *key, st_data_t *value, st_data_t arg, int existing)
Definition: st.h:131
st_index_t
st_data_t st_index_t
Definition: st.h:50
st_foreach_callback_func
int st_foreach_callback_func(st_data_t, st_data_t, st_data_t)
Definition: st.h:137
stdbool.h
C99 shim for <stdbool.h>
RBasic
Definition: rbasic.h:47
RHash
Definition: hash.h:44
RHash::word
VALUE word
Definition: hash.h:53
RHash::basic
struct RBasic basic
Definition: hash.h:45
RHash::st
st_table * st
Definition: hash.h:47
RHash::ar
struct ar_table_struct * ar
Definition: hash.h:48
RHash::ar_hint
union RHash::@111 ar_hint
RHash::as
union RHash::@110 as
RHash::ifnone
const VALUE ifnone
Definition: hash.h:50
ar_table_struct
Definition: hash.c:363
file
Definition: gzappend.c:170
st_table
Definition: st.h:79
st_table::num_entries
st_index_t num_entries
Definition: st.h:86
VALUE
unsigned long VALUE
Definition: value.h:38