Ruby 3.5.0dev (2025-05-16 revision a0fe0095ab2711ba54b29cdd2a5957f993cfc1de)
hash.h
1#ifndef INTERNAL_HASH_H /*-*-C-*-vi:se ft=c:*/
2#define INTERNAL_HASH_H
11#include "ruby/internal/config.h"
12#include <stddef.h> /* for size_t */
13#include "ruby/internal/stdbool.h" /* for bool */
14#include "ruby/ruby.h" /* for struct RBasic */
15#include "ruby/st.h" /* for struct st_table */
16
17#define RHASH_AR_TABLE_MAX_SIZE SIZEOF_VALUE
18
19struct ar_table_struct;
20typedef unsigned char ar_hint_t;
21
22enum ruby_rhash_flags {
23 RHASH_PASS_AS_KEYWORDS = FL_USER1, /* FL 1 */
24 RHASH_PROC_DEFAULT = FL_USER2, /* FL 2 */
25 RHASH_ST_TABLE_FLAG = FL_USER3, /* FL 3 */
26 RHASH_AR_TABLE_SIZE_MASK = (FL_USER4|FL_USER5|FL_USER6|FL_USER7), /* FL 4..7 */
27 RHASH_AR_TABLE_SIZE_SHIFT = (FL_USHIFT+4),
28 RHASH_AR_TABLE_BOUND_MASK = (FL_USER8|FL_USER9|FL_USER10|FL_USER11), /* FL 8..11 */
29 RHASH_AR_TABLE_BOUND_SHIFT = (FL_USHIFT+8),
30
31 // we can not put it in "enum" because it can exceed "int" range.
32#define RHASH_LEV_MASK (FL_USER13 | FL_USER14 | FL_USER15 | /* FL 13..19 */ \
33 FL_USER16 | FL_USER17 | FL_USER18 | FL_USER19)
34
35 RHASH_LEV_SHIFT = (FL_USHIFT + 13),
36 RHASH_LEV_MAX = 127, /* 7 bits */
37};
38
39typedef struct ar_table_pair_struct {
40 VALUE key;
41 VALUE val;
43
44typedef struct ar_table_struct {
45 union {
46 ar_hint_t ary[RHASH_AR_TABLE_MAX_SIZE];
47 VALUE word;
48 } ar_hint;
49 /* 64bit CPU: 8B * 2 * 8 = 128B */
50 ar_table_pair pairs[RHASH_AR_TABLE_MAX_SIZE];
51} ar_table;
52
53struct RHash {
54 struct RBasic basic;
55 const VALUE ifnone;
56};
57
58#define RHASH(obj) ((struct RHash *)(obj))
59
60#ifdef RHASH_IFNONE
61# undef RHASH_IFNONE
62#endif
63
64#ifdef RHASH_SIZE
65# undef RHASH_SIZE
66#endif
67
68#ifdef RHASH_EMPTY_P
69# undef RHASH_EMPTY_P
70#endif
71
72/* hash.c */
73void rb_hash_st_table_set(VALUE hash, st_table *st);
74VALUE rb_hash_default_value(VALUE hash, VALUE key);
75VALUE rb_hash_set_default(VALUE hash, VALUE ifnone);
76VALUE rb_hash_set_default_proc(VALUE hash, VALUE proc);
77long rb_dbl_long_hash(double d);
78st_table *rb_init_identtable(void);
79st_index_t rb_any_hash(VALUE a);
80int rb_any_cmp(VALUE a, VALUE b);
81VALUE rb_to_hash_type(VALUE obj);
82VALUE rb_hash_key_str(VALUE);
83VALUE rb_hash_values(VALUE hash);
84VALUE rb_hash_rehash(VALUE hash);
85int rb_hash_add_new_element(VALUE hash, VALUE key, VALUE val);
86VALUE rb_hash_set_pair(VALUE hash, VALUE pair);
87int rb_hash_stlike_delete(VALUE hash, st_data_t *pkey, st_data_t *pval);
88int rb_hash_stlike_foreach_with_replace(VALUE hash, st_foreach_check_callback_func *func, st_update_callback_func *replace, st_data_t arg);
89int rb_hash_stlike_update(VALUE hash, st_data_t key, st_update_callback_func *func, st_data_t arg);
90bool rb_hash_default_unredefined(VALUE hash);
91VALUE rb_ident_hash_new_with_size(st_index_t size);
92void rb_hash_free(VALUE hash);
93RUBY_EXTERN VALUE rb_cHash_empty_frozen;
94
95static inline unsigned RHASH_AR_TABLE_SIZE_RAW(VALUE h);
96static inline VALUE RHASH_IFNONE(VALUE h);
97static inline size_t RHASH_SIZE(VALUE h);
98static inline bool RHASH_EMPTY_P(VALUE h);
99static inline bool RHASH_AR_TABLE_P(VALUE h);
100static inline bool RHASH_ST_TABLE_P(VALUE h);
101static inline struct ar_table_struct *RHASH_AR_TABLE(VALUE h);
102static inline st_table *RHASH_ST_TABLE(VALUE h);
103static inline size_t RHASH_ST_SIZE(VALUE h);
104static inline void RHASH_ST_CLEAR(VALUE h);
105
106RUBY_SYMBOL_EXPORT_BEGIN
107/* hash.c (export) */
108VALUE rb_hash_delete_entry(VALUE hash, VALUE key);
109VALUE rb_ident_hash_new(void);
110int rb_hash_stlike_foreach(VALUE hash, st_foreach_callback_func *func, st_data_t arg);
111RUBY_SYMBOL_EXPORT_END
112
113VALUE rb_hash_new_with_size(st_index_t size);
114VALUE rb_hash_resurrect(VALUE hash);
115int rb_hash_stlike_lookup(VALUE hash, st_data_t key, st_data_t *pval);
116VALUE rb_hash_keys(VALUE hash);
117VALUE rb_hash_has_key(VALUE hash, VALUE key);
118VALUE rb_hash_compare_by_id_p(VALUE hash);
119
120st_table *rb_hash_tbl_raw(VALUE hash, const char *file, int line);
121#define RHASH_TBL_RAW(h) rb_hash_tbl_raw(h, __FILE__, __LINE__)
122
123VALUE rb_hash_compare_by_id(VALUE hash);
124
125static inline bool
126RHASH_AR_TABLE_P(VALUE h)
127{
128 return ! FL_TEST_RAW(h, RHASH_ST_TABLE_FLAG);
129}
130
132static inline struct ar_table_struct *
133RHASH_AR_TABLE(VALUE h)
134{
135 return (struct ar_table_struct *)((uintptr_t)h + sizeof(struct RHash));
136}
137
139static inline st_table *
140RHASH_ST_TABLE(VALUE h)
141{
142 return (st_table *)((uintptr_t)h + sizeof(struct RHash));
143}
144
145static inline VALUE
147{
148 return RHASH(h)->ifnone;
149}
150
151static inline size_t
153{
154 if (RHASH_AR_TABLE_P(h)) {
155 return RHASH_AR_TABLE_SIZE_RAW(h);
156 }
157 else {
158 return RHASH_ST_SIZE(h);
159 }
160}
161
162static inline bool
164{
165 return RHASH_SIZE(h) == 0;
166}
167
168static inline bool
169RHASH_ST_TABLE_P(VALUE h)
170{
171 return ! RHASH_AR_TABLE_P(h);
172}
173
174static inline size_t
175RHASH_ST_SIZE(VALUE h)
176{
177 return RHASH_ST_TABLE(h)->num_entries;
178}
179
180static inline void
181RHASH_ST_CLEAR(VALUE h)
182{
183 memset(RHASH_ST_TABLE(h), 0, sizeof(st_table));
184}
185
186static inline unsigned
187RHASH_AR_TABLE_SIZE_RAW(VALUE h)
188{
189 VALUE ret = FL_TEST_RAW(h, RHASH_AR_TABLE_SIZE_MASK);
190 ret >>= RHASH_AR_TABLE_SIZE_SHIFT;
191 return (unsigned)ret;
192}
193
194#endif /* INTERNAL_HASH_H */
#define RUBY_EXTERN
Declaration of externally visible global variables.
Definition dllexport.h:45
#define FL_USER3
Old name of RUBY_FL_USER3.
Definition fl_type.h:73
#define FL_USER7
Old name of RUBY_FL_USER7.
Definition fl_type.h:77
#define FL_USER10
Old name of RUBY_FL_USER10.
Definition fl_type.h:80
#define FL_USER6
Old name of RUBY_FL_USER6.
Definition fl_type.h:76
#define FL_USER1
Old name of RUBY_FL_USER1.
Definition fl_type.h:71
#define FL_USER11
Old name of RUBY_FL_USER11.
Definition fl_type.h:81
#define FL_USER8
Old name of RUBY_FL_USER8.
Definition fl_type.h:78
#define FL_TEST_RAW
Old name of RB_FL_TEST_RAW.
Definition fl_type.h:131
#define FL_USER2
Old name of RUBY_FL_USER2.
Definition fl_type.h:72
#define FL_USER9
Old name of RUBY_FL_USER9.
Definition fl_type.h:79
#define FL_USER5
Old name of RUBY_FL_USER5.
Definition fl_type.h:75
#define FL_USHIFT
Old name of RUBY_FL_USHIFT.
Definition fl_type.h:68
#define FL_USER4
Old name of RUBY_FL_USER4.
Definition fl_type.h:74
#define inline
Old Visual Studio versions do not support the inline keyword, so we need to define it to be __inline.
Definition defines.h:91
#define RBIMPL_ATTR_RETURNS_NONNULL()
Wraps (or simulates) __attribute__((returns_nonnull))
#define RHASH_IFNONE(h)
Definition rhash.h:59
#define RHASH_SIZE(h)
Queries the size of the hash.
Definition rhash.h:69
#define RHASH_EMPTY_P(h)
Checks if the hash is empty.
Definition rhash.h:79
C99 shim for <stdbool.h>
Ruby object's base components.
Definition rbasic.h:63
Definition hash.h:53
Definition st.h:79
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40