mruby 3.3.0
mruby is the lightweight implementation of the Ruby language
Loading...
Searching...
No Matches
hash.h
Go to the documentation of this file.
1
7#ifndef MRUBY_HASH_H
8#define MRUBY_HASH_H
9
10#include "common.h"
11
16
17/* offset of `iv` must be 3 words */
18struct RHash {
19 MRB_OBJECT_HEADER;
20#ifdef MRB_64BIT
21 uint32_t size;
22 struct iv_tbl *iv;
23 uint32_t ea_capa;
24 uint32_t ea_n_used;
25#else
26 struct iv_tbl *iv;
27 uint32_t size;
28#endif
29 union {
30 struct hash_entry *ea;
31 struct hash_table *ht;
32 } hsh;
33};
34
35#define mrb_hash_ptr(v) ((struct RHash*)(mrb_ptr(v)))
36#define mrb_hash_value(p) mrb_obj_value((void*)(p))
37
38MRB_API mrb_value mrb_hash_new_capa(mrb_state *mrb, mrb_int capa);
39
40/*
41 * Initializes a new hash.
42 *
43 * Equivalent to:
44 *
45 * Hash.new
46 *
47 * @param mrb The mruby state reference.
48 * @return The initialized hash.
49 */
50MRB_API mrb_value mrb_hash_new(mrb_state *mrb);
51
52/*
53 * Sets a keys and values to hashes.
54 *
55 * Equivalent to:
56 *
57 * hash[key] = val
58 *
59 * @param mrb The mruby state reference.
60 * @param hash The target hash.
61 * @param key The key to set.
62 * @param val The value to set.
63 * @return The value.
64 */
65MRB_API void mrb_hash_set(mrb_state *mrb, mrb_value hash, mrb_value key, mrb_value val);
66
67/*
68 * Gets a value from a key. If the key is not found, the default of the
69 * hash is used.
70 *
71 * Equivalent to:
72 *
73 * hash[key]
74 *
75 * @param mrb The mruby state reference.
76 * @param hash The target hash.
77 * @param key The key to get.
78 * @return The found value.
79 */
80MRB_API mrb_value mrb_hash_get(mrb_state *mrb, mrb_value hash, mrb_value key);
81
82/*
83 * Gets a value from a key. If the key is not found, the default parameter is
84 * used.
85 *
86 * Equivalent to:
87 *
88 * hash.key?(key) ? hash[key] : def
89 *
90 * @param mrb The mruby state reference.
91 * @param hash The target hash.
92 * @param key The key to get.
93 * @param def The default value.
94 * @return The found value.
95 */
96MRB_API mrb_value mrb_hash_fetch(mrb_state *mrb, mrb_value hash, mrb_value key, mrb_value def);
97
98/*
99 * Deletes hash key and value pair.
100 *
101 * Equivalent to:
102 *
103 * hash.delete(key)
104 *
105 * @param mrb The mruby state reference.
106 * @param hash The target hash.
107 * @param key The key to delete.
108 * @return The deleted value. This value is not protected from GC. Use `mrb_gc_protect()` if necessary.
109 */
110MRB_API mrb_value mrb_hash_delete_key(mrb_state *mrb, mrb_value hash, mrb_value key);
111
112/*
113 * Gets an array of keys.
114 *
115 * Equivalent to:
116 *
117 * hash.keys
118 *
119 * @param mrb The mruby state reference.
120 * @param hash The target hash.
121 * @return An array with the keys of the hash.
122 */
123MRB_API mrb_value mrb_hash_keys(mrb_state *mrb, mrb_value hash);
124/*
125 * Check if the hash has the key.
126 *
127 * Equivalent to:
128 *
129 * hash.key?(key)
130 *
131 * @param mrb The mruby state reference.
132 * @param hash The target hash.
133 * @param key The key to check existence.
134 * @return True if the hash has the key
135 */
136MRB_API mrb_bool mrb_hash_key_p(mrb_state *mrb, mrb_value hash, mrb_value key);
137
138/*
139 * Check if the hash is empty
140 *
141 * Equivalent to:
142 *
143 * hash.empty?
144 *
145 * @param mrb The mruby state reference.
146 * @param self The target hash.
147 * @return True if the hash is empty, false otherwise.
148 */
149MRB_API mrb_bool mrb_hash_empty_p(mrb_state *mrb, mrb_value self);
150
151/*
152 * Gets an array of values.
153 *
154 * Equivalent to:
155 *
156 * hash.values
157 *
158 * @param mrb The mruby state reference.
159 * @param hash The target hash.
160 * @return An array with the values of the hash.
161 */
162MRB_API mrb_value mrb_hash_values(mrb_state *mrb, mrb_value hash);
163
164/*
165 * Clears the hash.
166 *
167 * Equivalent to:
168 *
169 * hash.clear
170 *
171 * @param mrb The mruby state reference.
172 * @param hash The target hash.
173 * @return The hash
174 */
175MRB_API mrb_value mrb_hash_clear(mrb_state *mrb, mrb_value hash);
176
177/*
178 * Get hash size.
179 *
180 * Equivalent to:
181 *
182 * hash.size
183 *
184 * @param mrb The mruby state reference.
185 * @param hash The target hash.
186 * @return The hash size.
187 */
188MRB_API mrb_int mrb_hash_size(mrb_state *mrb, mrb_value hash);
189
190/*
191 * Copies the hash. This function does NOT copy the instance variables
192 * (except for the default value). Use mrb_obj_dup() to copy the instance
193 * variables as well.
194 *
195 * @param mrb The mruby state reference.
196 * @param hash The target hash.
197 * @return The copy of the hash
198 */
199MRB_API mrb_value mrb_hash_dup(mrb_state *mrb, mrb_value hash);
200
201/*
202 * Merges two hashes. The first hash will be modified by the
203 * second hash.
204 *
205 * @param mrb The mruby state reference.
206 * @param hash1 The target hash.
207 * @param hash2 Updating hash
208 */
209MRB_API void mrb_hash_merge(mrb_state *mrb, mrb_value hash1, mrb_value hash2);
210
211#define RHASH(hash) ((struct RHash*)(mrb_ptr(hash)))
212
213#define MRB_HASH_IB_BIT_BIT 5
214#define MRB_HASH_AR_EA_CAPA_BIT 5
215#define MRB_HASH_IB_BIT_SHIFT 0
216#define MRB_HASH_AR_EA_CAPA_SHIFT 0
217#define MRB_HASH_AR_EA_N_USED_SHIFT MRB_HASH_AR_EA_CAPA_BIT
218#define MRB_HASH_SIZE_FLAGS_SHIFT (MRB_HASH_AR_EA_CAPA_BIT * 2)
219#define MRB_HASH_IB_BIT_MASK ((1 << MRB_HASH_IB_BIT_BIT) - 1)
220#define MRB_HASH_AR_EA_CAPA_MASK ((1 << MRB_HASH_AR_EA_CAPA_BIT) - 1)
221#define MRB_HASH_AR_EA_N_USED_MASK (MRB_HASH_AR_EA_CAPA_MASK << MRB_HASH_AR_EA_N_USED_SHIFT)
222#define MRB_HASH_DEFAULT (1 << (MRB_HASH_SIZE_FLAGS_SHIFT + 0))
223#define MRB_HASH_PROC_DEFAULT (1 << (MRB_HASH_SIZE_FLAGS_SHIFT + 1))
224#define MRB_HASH_HT (1 << (MRB_HASH_SIZE_FLAGS_SHIFT + 2))
225#define MRB_RHASH_DEFAULT_P(hash) (RHASH(hash)->flags & MRB_HASH_DEFAULT)
226#define MRB_RHASH_PROCDEFAULT_P(hash) (RHASH(hash)->flags & MRB_HASH_PROC_DEFAULT)
227
228/* return non zero to break the loop */
229typedef int (mrb_hash_foreach_func)(mrb_state *mrb, mrb_value key, mrb_value val, void *data);
230MRB_API void mrb_hash_foreach(mrb_state *mrb, struct RHash *hash, mrb_hash_foreach_func *func, void *p);
231
233
234#endif /* MRUBY_HASH_H */
mruby Boolean.
mruby common platform definition"
#define MRB_END_DECL
End declarations in C mode.
Definition common.h:28
#define MRB_BEGIN_DECL
Start declarations in C mode.
Definition common.h:26
#define MRB_API
Declare a public mruby API function.
Definition common.h:79
Hash class.
Definition hash.h:18
Definition hash.c:76
Definition hash.c:81
Definition variable.c:17
Definition mruby.h:256
Definition boxing_nan.h:40