Ruby 3.3.6p108 (2024-11-05 revision 75015d4c1f6965b5e85e96fb309f1f2129f933c0)
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_proc(VALUE hash, VALUE proc);
76long rb_dbl_long_hash(double d);
77st_table *rb_init_identtable(void);
78st_index_t rb_any_hash(VALUE a);
79int rb_any_cmp(VALUE a, VALUE b);
80VALUE rb_to_hash_type(VALUE obj);
81VALUE rb_hash_key_str(VALUE);
82VALUE rb_hash_values(VALUE hash);
83VALUE rb_hash_rehash(VALUE hash);
84int rb_hash_add_new_element(VALUE hash, VALUE key, VALUE val);
85VALUE rb_hash_set_pair(VALUE hash, VALUE pair);
86int rb_hash_stlike_delete(VALUE hash, st_data_t *pkey, st_data_t *pval);
87int rb_hash_stlike_foreach_with_replace(VALUE hash, st_foreach_check_callback_func *func, st_update_callback_func *replace, st_data_t arg);
88int rb_hash_stlike_update(VALUE hash, st_data_t key, st_update_callback_func *func, st_data_t arg);
89VALUE rb_ident_hash_new_with_size(st_index_t size);
90void rb_hash_free(VALUE hash);
91
92static inline unsigned RHASH_AR_TABLE_SIZE_RAW(VALUE h);
93static inline VALUE RHASH_IFNONE(VALUE h);
94static inline size_t RHASH_SIZE(VALUE h);
95static inline bool RHASH_EMPTY_P(VALUE h);
96static inline bool RHASH_AR_TABLE_P(VALUE h);
97static inline bool RHASH_ST_TABLE_P(VALUE h);
98static inline struct ar_table_struct *RHASH_AR_TABLE(VALUE h);
99static inline st_table *RHASH_ST_TABLE(VALUE h);
100static inline size_t RHASH_ST_SIZE(VALUE h);
101static inline void RHASH_ST_CLEAR(VALUE h);
102
103RUBY_SYMBOL_EXPORT_BEGIN
104/* hash.c (export) */
105VALUE rb_hash_delete_entry(VALUE hash, VALUE key);
106VALUE rb_ident_hash_new(void);
107int rb_hash_stlike_foreach(VALUE hash, st_foreach_callback_func *func, st_data_t arg);
108RUBY_SYMBOL_EXPORT_END
109
110VALUE rb_hash_new_with_size(st_index_t size);
111VALUE rb_hash_resurrect(VALUE hash);
112int rb_hash_stlike_lookup(VALUE hash, st_data_t key, st_data_t *pval);
113VALUE rb_hash_keys(VALUE hash);
114VALUE rb_hash_has_key(VALUE hash, VALUE key);
115VALUE rb_hash_compare_by_id_p(VALUE hash);
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__)
119
120VALUE rb_hash_compare_by_id(VALUE hash);
121
122static inline bool
123RHASH_AR_TABLE_P(VALUE h)
124{
125 return ! FL_TEST_RAW(h, RHASH_ST_TABLE_FLAG);
126}
127
129static inline struct ar_table_struct *
130RHASH_AR_TABLE(VALUE h)
131{
132 return (struct ar_table_struct *)((uintptr_t)h + sizeof(struct RHash));
133}
134
136static inline st_table *
137RHASH_ST_TABLE(VALUE h)
138{
139 return (st_table *)((uintptr_t)h + sizeof(struct RHash));
140}
141
142static inline VALUE
144{
145 return RHASH(h)->ifnone;
146}
147
148static inline size_t
150{
151 if (RHASH_AR_TABLE_P(h)) {
152 return RHASH_AR_TABLE_SIZE_RAW(h);
153 }
154 else {
155 return RHASH_ST_SIZE(h);
156 }
157}
158
159static inline bool
161{
162 return RHASH_SIZE(h) == 0;
163}
164
165static inline bool
166RHASH_ST_TABLE_P(VALUE h)
167{
168 return ! RHASH_AR_TABLE_P(h);
169}
170
171static inline size_t
172RHASH_ST_SIZE(VALUE h)
173{
174 return RHASH_ST_TABLE(h)->num_entries;
175}
176
177static inline void
178RHASH_ST_CLEAR(VALUE h)
179{
180 memset(RHASH_ST_TABLE(h), 0, sizeof(st_table));
181}
182
183static inline unsigned
184RHASH_AR_TABLE_SIZE_RAW(VALUE h)
185{
186 VALUE ret = FL_TEST_RAW(h, RHASH_AR_TABLE_SIZE_MASK);
187 ret >>= RHASH_AR_TABLE_SIZE_SHIFT;
188 return (unsigned)ret;
189}
190
191#endif /* INTERNAL_HASH_H */
#define FL_USER3
Old name of RUBY_FL_USER3.
Definition fl_type.h:74
#define FL_USER7
Old name of RUBY_FL_USER7.
Definition fl_type.h:78
#define FL_USER10
Old name of RUBY_FL_USER10.
Definition fl_type.h:81
#define FL_USER6
Old name of RUBY_FL_USER6.
Definition fl_type.h:77
#define FL_USER1
Old name of RUBY_FL_USER1.
Definition fl_type.h:72
#define FL_USER11
Old name of RUBY_FL_USER11.
Definition fl_type.h:82
#define FL_USER8
Old name of RUBY_FL_USER8.
Definition fl_type.h:79
#define FL_TEST_RAW
Old name of RB_FL_TEST_RAW.
Definition fl_type.h:132
#define FL_USER2
Old name of RUBY_FL_USER2.
Definition fl_type.h:73
#define FL_USER9
Old name of RUBY_FL_USER9.
Definition fl_type.h:80
#define FL_USER5
Old name of RUBY_FL_USER5.
Definition fl_type.h:76
#define FL_USHIFT
Old name of RUBY_FL_USHIFT.
Definition fl_type.h:69
#define FL_USER4
Old name of RUBY_FL_USER4.
Definition fl_type.h:75
#define inline
Old Visual Studio versions do not support the inline keyword, so we need to define it to be __inline.
Definition defines.h:66
#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's object's, base components.
Definition rbasic.h:64
Definition hash.h:53
Definition st.h:79
uintptr_t VALUE
Type that represents a Ruby object.
Definition value.h:40