2 ** @file mruby/hash.h - Hash class
4 ** See Copyright Notice in mruby.h
17 /* offset of `iv` must be 3 words */
30 struct hash_entry
*ea
;
31 struct hash_table
*ht
;
35 #define mrb_hash_ptr(v) ((struct RHash*)(mrb_ptr(v)))
36 #define mrb_hash_value(p) mrb_obj_value((void*)(p))
38 size_t mrb_hash_memsize(mrb_value obj
);
39 MRB_API mrb_value
mrb_hash_new_capa(mrb_state
*mrb
, mrb_int capa
);
42 * Initializes a new hash.
48 * @param mrb The mruby state reference.
49 * @return The initialized hash.
51 MRB_API mrb_value
mrb_hash_new(mrb_state
*mrb
);
54 * Sets a keys and values to hashes.
60 * @param mrb The mruby state reference.
61 * @param hash The target hash.
62 * @param key The key to set.
63 * @param val The value to set.
66 MRB_API
void mrb_hash_set(mrb_state
*mrb
, mrb_value hash
, mrb_value key
, mrb_value val
);
69 * Gets a value from a key. If the key is not found, the default of the
76 * @param mrb The mruby state reference.
77 * @param hash The target hash.
78 * @param key The key to get.
79 * @return The found value.
81 MRB_API mrb_value
mrb_hash_get(mrb_state
*mrb
, mrb_value hash
, mrb_value key
);
84 * Gets a value from a key. If the key is not found, the default parameter is
89 * hash.key?(key) ? hash[key] : def
91 * @param mrb The mruby state reference.
92 * @param hash The target hash.
93 * @param key The key to get.
94 * @param def The default value.
95 * @return The found value.
97 MRB_API mrb_value
mrb_hash_fetch(mrb_state
*mrb
, mrb_value hash
, mrb_value key
, mrb_value def
);
100 * Deletes hash key and value pair.
106 * @param mrb The mruby state reference.
107 * @param hash The target hash.
108 * @param key The key to delete.
109 * @return The deleted value. This value is not protected from GC. Use `mrb_gc_protect()` if necessary.
111 MRB_API mrb_value
mrb_hash_delete_key(mrb_state
*mrb
, mrb_value hash
, mrb_value key
);
114 * Gets an array of keys.
120 * @param mrb The mruby state reference.
121 * @param hash The target hash.
122 * @return An array with the keys of the hash.
124 MRB_API mrb_value
mrb_hash_keys(mrb_state
*mrb
, mrb_value hash
);
126 * Check if the hash has the key.
132 * @param mrb The mruby state reference.
133 * @param hash The target hash.
134 * @param key The key to check existence.
135 * @return True if the hash has the key
137 MRB_API mrb_bool
mrb_hash_key_p(mrb_state
*mrb
, mrb_value hash
, mrb_value key
);
140 * Check if the hash is empty
146 * @param mrb The mruby state reference.
147 * @param self The target hash.
148 * @return True if the hash is empty, false otherwise.
150 MRB_API mrb_bool
mrb_hash_empty_p(mrb_state
*mrb
, mrb_value self
);
153 * Gets an array of values.
159 * @param mrb The mruby state reference.
160 * @param hash The target hash.
161 * @return An array with the values of the hash.
163 MRB_API mrb_value
mrb_hash_values(mrb_state
*mrb
, mrb_value hash
);
172 * @param mrb The mruby state reference.
173 * @param hash The target hash.
176 MRB_API mrb_value
mrb_hash_clear(mrb_state
*mrb
, mrb_value hash
);
185 * @param mrb The mruby state reference.
186 * @param hash The target hash.
187 * @return The hash size.
189 MRB_API mrb_int
mrb_hash_size(mrb_state
*mrb
, mrb_value hash
);
192 * Copies the hash. This function does NOT copy the instance variables
193 * (except for the default value). Use mrb_obj_dup() to copy the instance
196 * @param mrb The mruby state reference.
197 * @param hash The target hash.
198 * @return The copy of the hash
200 MRB_API mrb_value
mrb_hash_dup(mrb_state
*mrb
, mrb_value hash
);
203 * Merges two hashes. The first hash will be modified by the
206 * @param mrb The mruby state reference.
207 * @param hash1 The target hash.
208 * @param hash2 Updating hash
210 MRB_API
void mrb_hash_merge(mrb_state
*mrb
, mrb_value hash1
, mrb_value hash2
);
212 #define RHASH(hash) ((struct RHash*)(mrb_ptr(hash)))
214 #define MRB_HASH_IB_BIT_BIT 5
215 #define MRB_HASH_AR_EA_CAPA_BIT 5
216 #define MRB_HASH_IB_BIT_SHIFT 0
217 #define MRB_HASH_AR_EA_CAPA_SHIFT 0
218 #define MRB_HASH_AR_EA_N_USED_SHIFT MRB_HASH_AR_EA_CAPA_BIT
219 #define MRB_HASH_SIZE_FLAGS_SHIFT (MRB_HASH_AR_EA_CAPA_BIT * 2)
220 #define MRB_HASH_IB_BIT_MASK ((1 << MRB_HASH_IB_BIT_BIT) - 1)
221 #define MRB_HASH_AR_EA_CAPA_MASK ((1 << MRB_HASH_AR_EA_CAPA_BIT) - 1)
222 #define MRB_HASH_AR_EA_N_USED_MASK (MRB_HASH_AR_EA_CAPA_MASK << MRB_HASH_AR_EA_N_USED_SHIFT)
223 #define MRB_HASH_DEFAULT (1 << (MRB_HASH_SIZE_FLAGS_SHIFT + 0))
224 #define MRB_HASH_PROC_DEFAULT (1 << (MRB_HASH_SIZE_FLAGS_SHIFT + 1))
225 #define MRB_HASH_HT (1 << (MRB_HASH_SIZE_FLAGS_SHIFT + 2))
226 #define MRB_RHASH_DEFAULT_P(hash) (RHASH(hash)->flags & MRB_HASH_DEFAULT)
227 #define MRB_RHASH_PROCDEFAULT_P(hash) (RHASH(hash)->flags & MRB_HASH_PROC_DEFAULT)
230 void mrb_gc_mark_hash(mrb_state
*, struct RHash
*);
231 size_t mrb_gc_mark_hash_size(mrb_state
*, struct RHash
*);
232 void mrb_gc_free_hash(mrb_state
*, struct RHash
*);
234 /* return non zero to break the loop */
235 typedef int (mrb_hash_foreach_func
)(mrb_state
*mrb
, mrb_value key
, mrb_value val
, void *data
);
236 MRB_API
void mrb_hash_foreach(mrb_state
*mrb
, struct RHash
*hash
, mrb_hash_foreach_func
*func
, void *p
);
240 #endif /* MRUBY_HASH_H */