2 ** mruby/hash.h - Hash class
4 ** See Copyright Notice in mruby.h
23 #define mrb_hash_ptr(v) ((struct RHash*)(mrb_ptr(v)))
24 #define mrb_hash_value(p) mrb_obj_value((void*)(p))
26 MRB_API mrb_value mrb_hash_new_capa(mrb_state*, mrb_int);
27 MRB_API mrb_value mrb_ensure_hash_type(mrb_state *mrb, mrb_value hash);
28 MRB_API mrb_value mrb_check_hash_type(mrb_state *mrb, mrb_value hash);
31 * Initializes a new hash.
37 * @param mrb The mruby state reference.
38 * @return The initialized hash.
40 MRB_API mrb_value mrb_hash_new(mrb_state *mrb);
43 * Sets a keys and values to hashes.
49 * @param mrb The mruby state reference.
50 * @param hash The target hash.
51 * @param key The key to set.
52 * @param val The value to set.
55 MRB_API void mrb_hash_set(mrb_state *mrb, mrb_value hash, mrb_value key, mrb_value val);
58 * Gets a value from a key. If the key is not found, the default of the
65 * @param mrb The mruby state reference.
66 * @param hash The target hash.
67 * @param key The key to get.
68 * @return The found value.
70 MRB_API mrb_value mrb_hash_get(mrb_state *mrb, mrb_value hash, mrb_value key);
73 * Gets a value from a key. If the key is not found, the default parameter is
78 * hash.key?(key) ? hash[key] : def
80 * @param mrb The mruby state reference.
81 * @param hash The target hash.
82 * @param key The key to get.
83 * @param def The default value.
84 * @return The found value.
86 MRB_API mrb_value mrb_hash_fetch(mrb_state *mrb, mrb_value hash, mrb_value key, mrb_value def);
89 * Deletes hash key and value pair.
95 * @param mrb The mruby state reference.
96 * @param hash The target hash.
97 * @param key The key to delete.
98 * @return The deleted value.
100 MRB_API mrb_value mrb_hash_delete_key(mrb_state *mrb, mrb_value hash, mrb_value key);
103 * Gets an array of keys.
109 * @param mrb The mruby state reference.
110 * @param hash The target hash.
111 * @return An array with the keys of the hash.
113 MRB_API mrb_value mrb_hash_keys(mrb_state *mrb, mrb_value hash);
115 * Check if the hash has the key.
121 * @param mrb The mruby state reference.
122 * @param hash The target hash.
123 * @param key The key to check existence.
124 * @return True if the hash has the key
126 MRB_API mrb_bool mrb_hash_key_p(mrb_state *mrb, mrb_value hash, mrb_value key);
129 * Check if the hash is empty
135 * @param mrb The mruby state reference.
136 * @param self The target hash.
137 * @return True if the hash is empty, false otherwise.
139 MRB_API mrb_bool mrb_hash_empty_p(mrb_state *mrb, mrb_value self);
142 * Gets an array of values.
148 * @param mrb The mruby state reference.
149 * @param hash The target hash.
150 * @return An array with the values of the hash.
152 MRB_API mrb_value mrb_hash_values(mrb_state *mrb, mrb_value hash);
161 * @param mrb The mruby state reference.
162 * @param hash The target hash.
165 MRB_API mrb_value mrb_hash_clear(mrb_state *mrb, mrb_value hash);
174 * @param mrb The mruby state reference.
175 * @param hash The target hash.
176 * @return The hash size.
178 MRB_API mrb_int mrb_hash_size(mrb_state *mrb, mrb_value hash);
184 * @param mrb The mruby state reference.
185 * @param hash The target hash.
186 * @return The copy of the hash
188 MRB_API mrb_value mrb_hash_dup(mrb_state *mrb, mrb_value hash);
191 * Merges two hashes. The first hash will be modified by the
194 * @param mrb The mruby state reference.
195 * @param hash1 The target hash.
196 * @param hash2 Updating hash
198 MRB_API void mrb_hash_merge(mrb_state *mrb, mrb_value hash1, mrb_value hash2);
200 /* declaration of struct mrb_hash_value */
201 /* be careful when you touch the internal */
207 /* RHASH_TBL allocates st_table if not available. */
208 #define RHASH(obj) ((struct RHash*)(mrb_ptr(obj)))
209 #define RHASH_TBL(h) (RHASH(h)->ht)
210 #define RHASH_IFNONE(h) mrb_iv_get(mrb, (h), mrb_intern_lit(mrb, "ifnone"))
211 #define RHASH_PROCDEFAULT(h) RHASH_IFNONE(h)
213 #define MRB_HASH_DEFAULT 1
214 #define MRB_HASH_PROC_DEFAULT 2
215 #define MRB_RHASH_DEFAULT_P(h) (RHASH(h)->flags & MRB_HASH_DEFAULT)
216 #define MRB_RHASH_PROCDEFAULT_P(h) (RHASH(h)->flags & MRB_HASH_PROC_DEFAULT)
219 void mrb_gc_mark_hash(mrb_state*, struct RHash*);
220 size_t mrb_gc_mark_hash_size(mrb_state*, struct RHash*);
221 void mrb_gc_free_hash(mrb_state*, struct RHash*);
223 /* return non zero to break the loop */
224 typedef int (mrb_hash_foreach_func)(mrb_state *mrb, mrb_value key, mrb_value val, void *data);
225 MRB_API void mrb_hash_foreach(mrb_state *mrb, struct RHash *hash, mrb_hash_foreach_func *func, void *p);
229 #endif /* MRUBY_HASH_H */