Debian Codegraph @

vendor/mruby-source/src/hash.c

raw
1 /*
2 ** hash.c - Hash class
3 **
4 ** See Copyright Notice in mruby.h
5 */
6
7 #include <string.h>
8 #include <mruby.h>
9 #include <mruby/array.h>
10 #include <mruby/class.h>
11 #include <mruby/hash.h>
12 #include <mruby/string.h>
13 #include <mruby/variable.h>
14 #include <mruby/internal.h>
15 #include <mruby/presym.h>
16
17 /*
18 * === Glossary
19 *
20 * [EA]
21 * Entry Array. Store `Hash' entries in insertion order.
22 *
23 * [AR]
24 * Array Table Implementation. The structure of `Hash` that doesn't have a
25 * hash table and linearly searches EA. It is used when `Hash` size <= 16.
26 *
27 * [IB]
28 * Index Buckets. The buckets of hash table, where the bucket value is EA
29 * index. The index is represented by variable length bits according to
30 * the capacity.
31 *
32 * [HT]
33 * Hash Table Implementation. The structure of `Hash` that has IB and is
34 * searched by hash table algorithm. It is used when `Hash` size > 16.
35 * Collision resolution strategy is open addressing method.
36 *
37 * [size]
38 * The number of `Hash` entries (value of `Hash#size`).
39 *
40 * [slot]
41 * The generic term for EA or IB elements.
42 *
43 * [active]
44 * The state in which a slot is recognized as a `Hash` entry.
45 *
46 * [deleted]
47 * The state in which a slot is marked as deleted.
48 *
49 * [used]
50 * The state in which a slot is active or deleted.
51 *
52 * [empty]
53 * The state in which a slot is not used. Capacity is equal to the sum of
54 * the number of used slots and the number of empty slots.
55 */
56
57 #define EA_N_RESERVED_INDICES 2 /* empty and deleted */
58 #define EA_INCREASE_RATIO 6 / 5 + 6
59 #define EA_MAX_INCREASE UINT16_MAX
60 #define EA_MAX_CAPA U32(lesser(IB_MAX_CAPA - EA_N_RESERVED_INDICES, MRB_INT_MAX))
61 #define IB_MAX_CAPA (U32(1) << IB_MAX_BIT)
62 #define IB_TYPE_BIT 32
63 #define IB_INIT_BIT ( \
64 ib_upper_bound_for(32) <= AR_MAX_SIZE ? 6 : \
65 ib_upper_bound_for(16) <= AR_MAX_SIZE ? 5 : \
66 4 \
67 )
68 #define IB_MAX_BIT (IB_TYPE_BIT - 1)
69 #define AR_DEFAULT_CAPA 4
70 #define AR_MAX_SIZE 16
71 #define H_MAX_SIZE EA_MAX_CAPA
72
73 mrb_static_assert(offsetof(struct RHash, iv) == offsetof(struct RObject, iv));
74 mrb_static_assert(AR_MAX_SIZE < (1 << MRB_HASH_AR_EA_CAPA_BIT));
75
76 typedef struct hash_entry {
77 mrb_value key;
78 mrb_value val;
79 } hash_entry;
80
81 typedef struct hash_table {
82 hash_entry *ea;
83 #ifdef MRB_32BIT
84 uint32_t ea_capa;
85 uint32_t ea_n_used;
86 #endif
87 uint32_t ib[];
88 } hash_table;
89
90 typedef struct index_buckets_iter {
91 struct RHash *h;
92 uint32_t bit;
93 uint32_t mask;
94 uint32_t pos;
95 uint32_t ary_index;
96 uint32_t ea_index;
97 uint32_t shift1;
98 uint32_t shift2;
99 uint32_t step;
100 } index_buckets_iter;
101
102 /*
103 * `c_` :: receiver class (category)
104 * `n_` :: attribute name
105 * `t_` :: attribute type
106 * `p_` :: struct member path
107 * `k_` :: macro key
108 */
109 #define DEFINE_GETTER(c_, n_, t_, p_) \
110 MRB_INLINE t_ c_##_##n_(const struct RHash *h) {return h->p_;}
111 #define DEFINE_SETTER(c_, n_, t_, p_) \
112 MRB_INLINE void c_##_set_##n_(struct RHash *h, t_ v) {h->p_ = v;}
113 #define DEFINE_ACCESSOR(c_, n_, t_, p_) \
114 DEFINE_GETTER(c_, n_, t_, p_) \
115 DEFINE_SETTER(c_, n_, t_, p_)
116 #define DEFINE_FLAG_GETTER(c_, n_, t_, k_) \
117 MRB_INLINE t_ c_##_##n_(const struct RHash *h) { \
118 return (t_)((h->flags & MRB_HASH_##k_##_MASK) >> MRB_HASH_##k_##_SHIFT); \
119 }
120 #define DEFINE_FLAG_SETTER(c_, n_, t_, k_) \
121 MRB_INLINE void c_##_set_##n_(struct RHash *h, t_ v) { \
122 h->flags &= ~MRB_HASH_##k_##_MASK; \
123 h->flags |= v << MRB_HASH_##k_##_SHIFT; \
124 }
125 #define DEFINE_FLAG_ACCESSOR(c_, n_, t_, k_) \
126 DEFINE_FLAG_GETTER(c_, n_, t_, k_) \
127 DEFINE_FLAG_SETTER(c_, n_, t_, k_)
128 #define DEFINE_INCREMENTER(c_, n_) \
129 MRB_INLINE void c_##_inc_##n_(struct RHash *h) { \
130 c_##_set_##n_(h, c_##_##n_(h) + 1); \
131 }
132 #define DEFINE_DECREMENTER(c_, n_) \
133 MRB_INLINE void c_##_dec_##n_(struct RHash *h) { \
134 c_##_set_##n_(h, c_##_##n_(h) - 1); \
135 }
136 #define DEFINE_SWITCHER(n_, k_) \
137 MRB_INLINE void h_##n_##_on(struct RHash *h) { \
138 h->flags |= MRB_HASH_##k_; \
139 } \
140 MRB_INLINE void h_##n_##_off(struct RHash *h) { \
141 h->flags &= ~MRB_HASH_##k_; \
142 } \
143 MRB_INLINE mrb_bool h_##n_##_p(const struct RHash *h) { \
144 return (h->flags & MRB_HASH_##k_) == MRB_HASH_##k_; \
145 }
146
147 #ifdef MRB_64BIT
148 DEFINE_ACCESSOR(ar, ea_capa, uint32_t, ea_capa)
149 DEFINE_ACCESSOR(ar, ea_n_used, uint32_t, ea_n_used)
150 DEFINE_ACCESSOR(ht, ea_capa, uint32_t, ea_capa)
151 DEFINE_ACCESSOR(ht, ea_n_used, uint32_t, ea_n_used)
152 #else
153 DEFINE_FLAG_ACCESSOR(ar, ea_capa, uint32_t, AR_EA_CAPA)
154 DEFINE_FLAG_ACCESSOR(ar, ea_n_used, uint32_t, AR_EA_N_USED)
155 DEFINE_ACCESSOR(ht, ea_capa, uint32_t, hsh.ht->ea_capa)
156 DEFINE_ACCESSOR(ht, ea_n_used, uint32_t, hsh.ht->ea_n_used)
157 #endif
158 DEFINE_FLAG_ACCESSOR(ib, bit, uint32_t, IB_BIT)
159 DEFINE_ACCESSOR(ar, size, uint32_t, size)
160 DEFINE_ACCESSOR(ar, ea, hash_entry*, hsh.ea)
161 DEFINE_DECREMENTER(ar, size)
162 DEFINE_ACCESSOR(ht, size, uint32_t, size)
163 DEFINE_ACCESSOR(ht, ea, hash_entry*, hsh.ht->ea)
164 DEFINE_GETTER(ht, ib, uint32_t*, hsh.ht->ib)
165 DEFINE_INCREMENTER(ht, size)
166 DEFINE_DECREMENTER(ht, size)
167 DEFINE_GETTER(h, size, uint32_t, size)
168 DEFINE_ACCESSOR(h, ht, hash_table*, hsh.ht)
169 DEFINE_SWITCHER(ht, HT)
170
171 #define ea_each_used(ea, n_used, entry_var, code) do { \
172 hash_entry *entry_var = ea, *ea_end__ = entry_var + (n_used); \
173 for (; entry_var < ea_end__; entry_var++) { \
174 code; \
175 } \
176 } while (0)
177
178 #define ea_each(ea, size, entry_var, code) do { \
179 hash_entry *entry_var = ea; \
180 uint32_t size__ = size; \
181 for (; 0 < size__; entry_var++) { \
182 if (entry_deleted_p(entry_var)) continue; \
183 --size__; \
184 code; \
185 } \
186 } while (0)
187
188 #define ib_cycle_by_key(mrb, h, key, it_var, code) do { \
189 index_buckets_iter it_var[1]; \
190 ib_it_init(mrb, it_var, h, key); \
191 for (;;) { \
192 ib_it_next(it_var); \
193 code; \
194 } \
195 } while (0)
196
197 #define ib_find_by_key(mrb, h_, key_, it_var, code) do { \
198 mrb_value ib_fbk_key__ = key_; \
199 ib_cycle_by_key(mrb, h_, ib_fbk_key__, it_var, { \
200 if (ib_it_empty_p(it_var)) break; \
201 if (ib_it_deleted_p(it_var)) continue; \
202 if (obj_eql(mrb, ib_fbk_key__, ib_it_entry(it_var)->key, it_var->h)) { \
203 code; \
204 break; \
205 } \
206 }); \
207 } while (0)
208
209 #define h_each(h, entry_var, code) do { \
210 struct RHash *h__ = h; \
211 hash_entry *h_e_ea__; \
212 uint32_t h_e_size__; \
213 h_ar_p(h) ? (h_e_ea__ = ar_ea(h__), h_e_size__ = ar_size(h__)) : \
214 (h_e_ea__ = ht_ea(h__), h_e_size__ = ht_size(h__)); \
215 ea_each(h_e_ea__, h_e_size__, entry_var, code); \
216 } while (0)
217
218 /*
219 * In `h_check_modified()`, in the case of `MRB_NO_BOXING`, `ht_ea()` or
220 * `ht_ea_capa()` for AR may read uninitialized area (#5332). Therefore, do
221 * not use those macros for AR in `MRB_NO_BOXING` (but in the case of
222 * `MRB_64BIT`, `ht_ea_capa()` is the same as `ar_ea_capa()`, so use it).
223 */
224 #ifdef MRB_NO_BOXING
225 # define H_CHECK_MODIFIED_USE_HT_EA_FOR_AR FALSE
226 # ifdef MRB_64BIT
227 # define H_CHECK_MODIFIED_USE_HT_EA_CAPA_FOR_AR TRUE
228 # else
229 # define H_CHECK_MODIFIED_USE_HT_EA_CAPA_FOR_AR FALSE
230 # endif /* MRB_64BIT */
231 #else
232 # define H_CHECK_MODIFIED_USE_HT_EA_FOR_AR TRUE
233 # define H_CHECK_MODIFIED_USE_HT_EA_CAPA_FOR_AR TRUE
234 /*
235 * `h_check_modified` raises an exception when a dangerous modification is
236 * made to `h` by executing `code`.
237 *
238 * `h_check_modified` macro is not called if `h->hsh.ht` (`h->hsh.ea`) is `NULL`
239 * (`Hash` size is zero). And because the `hash_entry` is rather large,
240 * `h->hsh.ht->ea` and `h->hsh.ht->ea_capa` are able to be safely accessed even for
241 * AR. This nature is used to eliminate branch of AR or HT.
242 *
243 * `HT_ASSERT_SAFE_READ` checks if members can be accessed according to its
244 * assumptions.
245 */
246 # define HT_ASSERT_SAFE_READ(attr_name) \
247 mrb_static_assert( \
248 offsetof(hash_table, attr_name) + sizeof(((hash_table*)0)->attr_name) <= \
249 sizeof(hash_entry))
250 HT_ASSERT_SAFE_READ(ea);
251 # ifdef MRB_32BIT
252 HT_ASSERT_SAFE_READ(ea_capa);
253 # endif
254 # undef HT_ASSERT_SAFE_READ
255 #endif /* MRB_NO_BOXING */
256
257 /*
258 * `h_check_modified` raises an exception when a dangerous modification is
259 * made to `h` by executing `code`.
260 */
261 #define h_check_modified(mrb, h, code) do { \
262 struct RHash *h__ = h; \
263 uint32_t mask__ = MRB_HASH_HT|MRB_HASH_IB_BIT_MASK|MRB_HASH_AR_EA_CAPA_MASK; \
264 uint32_t flags__ = h__->flags & mask__; \
265 void* tbl__ = (mrb_assert(h__->hsh.ht), h__->hsh.ht); \
266 uint32_t ht_ea_capa__ = 0; \
267 hash_entry *ht_ea__ = NULL; \
268 if (H_CHECK_MODIFIED_USE_HT_EA_CAPA_FOR_AR || h_ht_p(h__)) { \
269 ht_ea_capa__ = ht_ea_capa(h__); \
270 } \
271 if (H_CHECK_MODIFIED_USE_HT_EA_FOR_AR || h_ht_p(h__)) { \
272 ht_ea__ = ht_ea(h__); \
273 } \
274 code; \
275 if (flags__ != (h__->flags & mask__) || \
276 tbl__ != h__->hsh.ht || \
277 ((H_CHECK_MODIFIED_USE_HT_EA_CAPA_FOR_AR || h_ht_p(h__)) && \
278 ht_ea_capa__ != ht_ea_capa(h__)) || \
279 ((H_CHECK_MODIFIED_USE_HT_EA_FOR_AR || h_ht_p(h__)) && \
280 ht_ea__ != ht_ea(h__))) { \
281 mrb_raise(mrb, E_RUNTIME_ERROR, "hash modified"); \
282 } \
283 } while (0)
284
285 #define U32(v) ((uint32_t)(v))
286 #define h_ar_p(h) (!h_ht_p(h))
287 #define h_ar_on(h) h_ht_off(h)
288 #define lesser(a, b) ((a) < (b) ? (a) : (b))
289 #define RHASH_IFNONE(hash) mrb_iv_get(mrb, (hash), MRB_SYM(ifnone))
290 #define RHASH_PROCDEFAULT(hash) RHASH_IFNONE(hash)
291
292 static uint32_t ib_upper_bound_for(uint32_t capa);
293 static uint32_t ib_bit_to_capa(uint32_t bit);
294 static void ht_init(
295 mrb_state *mrb, struct RHash *h, uint32_t size,
296 hash_entry *ea, uint32_t ea_capa, hash_table *ht, uint32_t ib_bit);
297 static void ht_set(mrb_state *mrb, struct RHash *h, mrb_value key, mrb_value val);
298 static void ht_set_without_ib_adjustment(
299 mrb_state *mrb, struct RHash *h, mrb_value key, mrb_value val);
300
301 static uint32_t
302 next_power2(uint32_t v)
303 {
304 mrb_assert(v != 0);
305 #ifdef __GNUC__
306 return U32(1) << ((sizeof(unsigned) * CHAR_BIT) - __builtin_clz(v));
307 #else
308 v |= v >> 1;
309 v |= v >> 2;
310 v |= v >> 4;
311 v |= v >> 8;
312 v |= v >> 16;
313 v++;
314 return v;
315 #endif
316 }
317
318 static uint32_t
319 obj_hash_code(mrb_state *mrb, mrb_value key, struct RHash *h)
320 {
321 enum mrb_vtype tt = mrb_type(key);
322 uint32_t hash_code;
323 mrb_value hash_code_obj;
324 switch (tt) {
325 case MRB_TT_STRING:
326 hash_code = mrb_str_hash(mrb, key);
327 break;
328 case MRB_TT_TRUE:
329 case MRB_TT_FALSE:
330 case MRB_TT_SYMBOL:
331 hash_code = U32(mrb_fixnum(key));
332 break;
333 case MRB_TT_INTEGER:
334 if (mrb_fixnum_p(key)) {
335 hash_code = U32(mrb_fixnum(key));
336 break;
337 }
338 #ifndef MRB_NO_FLOAT
339 /* fall through */
340 case MRB_TT_FLOAT:
341 #endif
342 hash_code = U32(mrb_obj_id(key));
343 break;
344 default:
345 h_check_modified(mrb, h, {
346 hash_code_obj = mrb_funcall_argv(mrb, key, MRB_SYM(hash), 0, NULL);
347 });
348
349 hash_code = U32(tt) ^ U32(mrb_integer(hash_code_obj));
350 break;
351 }
352 return hash_code ^ (hash_code << 2) ^ (hash_code >> 2);
353 }
354
355 static mrb_bool
356 obj_eql(mrb_state *mrb, mrb_value a, mrb_value b, struct RHash *h)
357 {
358 enum mrb_vtype tt = mrb_type(a);
359 mrb_bool eql;
360
361 switch (tt) {
362 case MRB_TT_STRING:
363 return mrb_str_equal(mrb, a, b);
364
365 case MRB_TT_SYMBOL:
366 if (!mrb_symbol_p(b)) return FALSE;
367 return mrb_symbol(a) == mrb_symbol(b);
368
369 case MRB_TT_INTEGER:
370 if (!mrb_integer_p(b)) return FALSE;
371 return mrb_integer(a) == mrb_integer(b);
372
373 #ifndef MRB_NO_FLOAT
374 case MRB_TT_FLOAT:
375 if (!mrb_float_p(b)) return FALSE;
376 return mrb_float(a) == mrb_float(b);
377 #endif
378
379 default:
380 h_check_modified(mrb, h, {eql = mrb_eql(mrb, a, b);});
381 return eql;
382 }
383 }
384
385 static inline mrb_bool
386 entry_deleted_p(const hash_entry* entry)
387 {
388 return mrb_undef_p(entry->key);
389 }
390
391 static void
392 entry_delete(hash_entry* entry)
393 {
394 entry->key = mrb_undef_value();
395 }
396
397 static uint32_t
398 ea_next_capa_for(uint32_t size, uint32_t max_capa)
399 {
400 if (size < AR_DEFAULT_CAPA) {
401 return AR_DEFAULT_CAPA;
402 }
403 else {
404 /*
405 * For 32-bit CPU, the theoretical value of maximum EA capacity is
406 * `UINT32_MAX / sizeof (hash_entry)`. At this time, if
407 * `EA_INCREASE_RATIO` is the current value, 32-bit range will not be
408 * exceeded during the calculation of `capa`, so `size_t` is used.
409 */
410 size_t capa = (size_t)size * EA_INCREASE_RATIO, inc = capa - size;
411 if (EA_MAX_INCREASE < inc) capa = size + EA_MAX_INCREASE;
412 return capa <= max_capa ? U32(capa) : max_capa;
413 }
414 }
415
416 static hash_entry*
417 ea_resize(mrb_state *mrb, hash_entry *ea, uint32_t capa)
418 {
419 return (hash_entry*)mrb_realloc(mrb, ea, sizeof(hash_entry) * capa);
420 }
421
422 static void
423 ea_compress(hash_entry *ea, uint32_t n_used)
424 {
425 hash_entry *w_entry = ea;
426 ea_each_used(ea, n_used, r_entry, {
427 if (entry_deleted_p(r_entry)) continue;
428 if (r_entry != w_entry) *w_entry = *r_entry;
429 w_entry++;
430 });
431 }
432
433 /*
434 * Increase or decrease capacity of `ea` to a standard size that can
435 * accommodate `*capap + 1` entries (but, not exceed `max_capa`). Set the
436 * changed capacity to `*capap` and return a pointer to `mrb_realloc`ed EA.
437 */
438 static hash_entry*
439 ea_adjust(mrb_state *mrb, hash_entry *ea, uint32_t *capap, uint32_t max_capa)
440 {
441 *capap = ea_next_capa_for(*capap, max_capa);
442 return ea_resize(mrb, ea, *capap);
443 }
444
445 static hash_entry*
446 ea_dup(mrb_state *mrb, const hash_entry *ea, uint32_t capa)
447 {
448 size_t byte_size = sizeof(hash_entry) * capa;
449 hash_entry *new_ea = (hash_entry*)mrb_malloc(mrb, byte_size);
450 return (hash_entry*)memcpy(new_ea, ea, byte_size);
451 }
452
453 static hash_entry*
454 ea_get_by_key(mrb_state *mrb, hash_entry *ea, uint32_t size, mrb_value key,
455 struct RHash *h)
456 {
457 ea_each(ea, size, entry, {
458 if (obj_eql(mrb, key, entry->key, h)) return entry;
459 });
460 return NULL;
461 }
462
463 static hash_entry*
464 ea_get(hash_entry *ea, uint32_t index)
465 {
466 return &ea[index];
467 }
468
469 static void
470 ea_set(hash_entry *ea, uint32_t index, mrb_value key, mrb_value val)
471 {
472 ea[index].key = key;
473 ea[index].val = val;
474 }
475
476 static void
477 ar_init(struct RHash *h, uint32_t size,
478 hash_entry *ea, uint32_t ea_capa, uint32_t ea_n_used)
479 {
480 h_ar_on(h);
481 ar_set_size(h, size);
482 ar_set_ea(h, ea);
483 ar_set_ea_capa(h, ea_capa);
484 ar_set_ea_n_used(h, ea_n_used);
485 }
486
487 static void
488 ar_free(mrb_state *mrb, struct RHash *h)
489 {
490 mrb_free(mrb, ar_ea(h));
491 }
492
493 static void
494 ar_adjust_ea(mrb_state *mrb, struct RHash *h, uint32_t size, uint32_t max_ea_capa)
495 {
496 uint32_t ea_capa = size;
497 hash_entry *ea = ea_adjust(mrb, ar_ea(h), &ea_capa, max_ea_capa);
498 ar_set_ea(h, ea);
499 ar_set_ea_capa(h, ea_capa);
500 }
501
502 static void
503 ar_compress(mrb_state *mrb, struct RHash *h)
504 {
505 uint32_t size = ar_size(h);
506 ea_compress(ar_ea(h), ar_ea_n_used(h));
507 ar_set_ea_n_used(h, size);
508 ar_adjust_ea(mrb, h, size, lesser(ar_ea_capa(h), AR_MAX_SIZE));
509 }
510
511 static mrb_bool
512 ar_get(mrb_state *mrb, struct RHash *h, mrb_value key, mrb_value *valp)
513 {
514 ea_each(ar_ea(h), ar_size(h), entry, {
515 if (!obj_eql(mrb, key, entry->key, h)) continue;
516 *valp = entry->val;
517 return TRUE;
518 });
519 return FALSE;
520 }
521
522 static void
523 ar_set(mrb_state *mrb, struct RHash *h, mrb_value key, mrb_value val)
524 {
525 uint32_t size = ar_size(h);
526 hash_entry *entry;
527 if ((entry = ea_get_by_key(mrb, ar_ea(h), size, key, h))) {
528 entry->val = val;
529 }
530 else {
531 uint32_t ea_capa = ar_ea_capa(h), ea_n_used = ar_ea_n_used(h);
532 if (ea_capa == ea_n_used) {
533 if (size == ea_n_used) {
534 if (size == AR_MAX_SIZE) {
535 ht_init(mrb, h, size, ar_ea(h), ea_capa, NULL, IB_INIT_BIT);
536 ht_set(mrb, h, key, val);
537 return;
538 }
539 else {
540 ar_adjust_ea(mrb, h, size, AR_MAX_SIZE);
541 }
542 }
543 else {
544 ar_compress(mrb, h);
545 ea_n_used = size;
546 }
547 }
548 ea_set(ar_ea(h), ea_n_used, key, val);
549 ar_set_size(h, ++size);
550 ar_set_ea_n_used(h, ++ea_n_used);
551 }
552 }
553
554 static mrb_bool
555 ar_delete(mrb_state *mrb, struct RHash *h, mrb_value key, mrb_value *valp)
556 {
557 hash_entry *entry = ea_get_by_key(mrb, ar_ea(h), ar_size(h), key, h);
558 if (!entry) return FALSE;
559 *valp = entry->val;
560 entry_delete(entry);
561 ar_dec_size(h);
562 return TRUE;
563 }
564
565 static void
566 ar_shift(mrb_state *mrb, struct RHash *h, mrb_value *keyp, mrb_value *valp)
567 {
568 uint32_t size = ar_size(h);
569 ea_each(ar_ea(h), size, entry, {
570 *keyp = entry->key;
571 *valp = entry->val;
572 entry_delete(entry);
573 ar_set_size(h, --size);
574 return;
575 });
576 }
577
578 static void
579 ar_rehash(mrb_state *mrb, struct RHash *h)
580 {
581 /* see comments in `h_rehash` */
582 uint32_t size = ar_size(h), w_size = 0, ea_capa = ar_ea_capa(h);
583 hash_entry *ea = ar_ea(h), *w_entry;
584 ea_each(ea, size, r_entry, {
585 if ((w_entry = ea_get_by_key(mrb, ea, w_size, r_entry->key, h))) {
586 w_entry->val = r_entry->val;
587 ar_set_size(h, --size);
588 entry_delete(r_entry);
589 }
590 else {
591 if (w_size != U32(r_entry - ea)) {
592 ea_set(ea, w_size, r_entry->key, r_entry->val);
593 entry_delete(r_entry);
594 }
595 w_size++;
596 }
597 });
598 mrb_assert(size == w_size);
599 ar_set_ea_n_used(h, size);
600 ar_adjust_ea(mrb, h, size, ea_capa);
601 }
602
603 static uint32_t
604 ib_it_pos_for(index_buckets_iter *it, uint32_t v)
605 {
606 return v & it->mask;
607 }
608
609 static uint32_t
610 ib_it_empty_value(const index_buckets_iter *it)
611 {
612 return it->mask;
613 }
614
615 static uint32_t
616 ib_it_deleted_value(const index_buckets_iter *it)
617 {
618 return it->mask - 1;
619 }
620
621 static mrb_bool
622 ib_it_empty_p(const index_buckets_iter *it)
623 {
624 return it->ea_index == ib_it_empty_value(it);
625 }
626
627 static mrb_bool
628 ib_it_deleted_p(const index_buckets_iter *it)
629 {
630 return it->ea_index == ib_it_deleted_value(it);
631 }
632
633 static mrb_bool
634 ib_it_active_p(const index_buckets_iter *it)
635 {
636 return it->ea_index < ib_it_deleted_value(it);
637 }
638
639 static void
640 ib_it_init(mrb_state *mrb, index_buckets_iter *it, struct RHash *h, mrb_value key)
641 {
642 it->h = h;
643 it->bit = ib_bit(h);
644 it->mask = ib_bit_to_capa(it->bit) - 1;
645 it->pos = ib_it_pos_for(it, obj_hash_code(mrb, key, h));
646 it->step = 0;
647 }
648
649 static void
650 ib_it_next(index_buckets_iter *it)
651 {
652 /*
653 * [IB image]
654 *
655 * ary_index(1) --.
656 * \ .-- shift1(3) .-- shift2(29)
657 * pos(6) --. \ / /
658 * View | \ \ <-o-> <----------o---------->
659 * -------- +---------------------\----\--+-----------------------------+-----
660 * array | 0 `--. `-|--- o 1 | ...
661 * +---------+---------+-----+\--+-----+---------+---------+---+-----
662 * buckets | 0 | 1 | ... | o 6 | 7 | 8 | ...
663 * +---------+---------+-----+=========+---------+---------+---------
664 * bit set |1 1 1 0 0|0 0 0 1 1| ... |0 1 0 1 1|0 1 1 1 0|0 1 0 1 0| ...
665 * +---------+---------+-----+========*+---------+---------+---------
666 * <---o---> \
667 * \ `-- bit_pos(34)
668 * `-- bit(5)
669 */
670
671 /* Slide to handle as `capa == 32` to avoid 64-bit operations */
672 uint32_t slid_pos = it->pos & (IB_TYPE_BIT - 1);
673 uint32_t slid_bit_pos = it->bit * (slid_pos + 1) - 1;
674 uint32_t slid_ary_index = slid_bit_pos / IB_TYPE_BIT;
675 it->ary_index = slid_ary_index + it->pos / IB_TYPE_BIT * it->bit;
676 it->shift2 = (slid_ary_index + 1) * IB_TYPE_BIT - slid_bit_pos - 1;
677 it->ea_index = (ht_ib(it->h)[it->ary_index] >> it->shift2) & it->mask;
678 if (IB_TYPE_BIT - it->bit < it->shift2) {
679 it->shift1 = IB_TYPE_BIT - it->shift2;
680 it->ea_index |= (ht_ib(it->h)[it->ary_index - 1] << it->shift1) & it->mask;
681 }
682 else {
683 it->shift1 = 0;
684 }
685 it->pos = ib_it_pos_for(it, it->pos + (++it->step));
686 }
687
688 static uint32_t
689 ib_it_get(const index_buckets_iter *it)
690 {
691 return it->ea_index;
692 }
693
694 static void
695 ib_it_set(index_buckets_iter *it, uint32_t ea_index)
696 {
697 uint32_t mask, i;
698 it->ea_index = ea_index;
699 if (it->shift1) {
700 i = it->ary_index - 1;
701 mask = it->mask >> it->shift1;
702 ht_ib(it->h)[i] = (ht_ib(it->h)[i] & ~mask) | (ea_index >> it->shift1);
703 }
704 i = it->ary_index;
705 mask = it->mask << it->shift2;
706 ht_ib(it->h)[i] = (ht_ib(it->h)[i] & ~mask) | (ea_index << it->shift2);
707 }
708
709 static void
710 ib_it_delete(index_buckets_iter *it)
711 {
712 ib_it_set(it, ib_it_deleted_value(it));
713 }
714
715 static hash_entry*
716 ib_it_entry(index_buckets_iter *it)
717 {
718 return ea_get(ht_ea(it->h), it->ea_index);
719 }
720
721 static uint32_t
722 ib_capa_to_bit(uint32_t capa)
723 {
724 #ifdef __GNUC__
725 return U32(__builtin_ctz(capa));
726 #else
727 /* http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogDeBruijn */
728 static const uint32_t MultiplyDeBruijnBitPosition2[] = {
729 0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
730 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
731 };
732 return MultiplyDeBruijnBitPosition2[U32(capa * 0x077CB531U) >> 27];
733 #endif
734 }
735
736 static uint32_t
737 ib_bit_to_capa(uint32_t bit)
738 {
739 return U32(1) << bit;
740 }
741
742 static uint32_t
743 ib_upper_bound_for(uint32_t capa)
744 {
745 return (capa >> 2) | (capa >> 1); /* 3/4 */
746 }
747
748 static uint32_t
749 ib_bit_for(uint32_t size)
750 {
751 uint32_t capa = next_power2(size);
752 if (capa != IB_MAX_CAPA && ib_upper_bound_for(capa) < size) capa *= 2;
753 return ib_capa_to_bit(capa);
754 }
755
756 static uint32_t
757 ib_byte_size_for(uint32_t ib_bit)
758 {
759 mrb_assert(IB_INIT_BIT <= ib_bit);
760 uint32_t ary_size = IB_INIT_BIT == 4 ?
761 ib_bit_to_capa(ib_bit) * 2 / IB_TYPE_BIT * ib_bit / 2 :
762 ib_bit_to_capa(ib_bit) / IB_TYPE_BIT * ib_bit;
763 return U32(sizeof(uint32_t) * ary_size);
764 }
765
766 static void
767 ib_init(mrb_state *mrb, struct RHash *h, uint32_t ib_bit, size_t ib_byte_size)
768 {
769 hash_entry *ea = ht_ea(h);
770 memset(ht_ib(h), 0xff, ib_byte_size);
771 ib_set_bit(h, ib_bit);
772 ea_each_used(ea, ht_ea_n_used(h), entry, {
773 ib_cycle_by_key(mrb, h, entry->key, it, {
774 if (!ib_it_empty_p(it)) continue;
775 ib_it_set(it, U32(entry - ea));
776 break;
777 });
778 });
779 }
780
781 static void
782 ht_init(mrb_state *mrb, struct RHash *h, uint32_t size,
783 hash_entry *ea, uint32_t ea_capa, hash_table *ht, uint32_t ib_bit)
784 {
785 size_t ib_byte_size = ib_byte_size_for(ib_bit);
786 size_t ht_byte_size = sizeof(hash_table) + ib_byte_size;
787 ht = (hash_table*)mrb_realloc(mrb, ht, ht_byte_size);
788 h_ht_on(h);
789 h_set_ht(h, ht);
790 ht_set_size(h, size);
791 ht_set_ea(h, ea);
792 ht_set_ea_capa(h, ea_capa);
793 ht_set_ea_n_used(h, size);
794 ib_init(mrb, h, ib_bit, ib_byte_size);
795 }
796
797 static void
798 ht_free(mrb_state *mrb, struct RHash *h)
799 {
800 mrb_free(mrb, ht_ea(h));
801 mrb_free(mrb, h_ht(h));
802 }
803
804 static hash_table*
805 ht_dup(mrb_state *mrb, const struct RHash *h)
806 {
807 size_t ib_byte_size = ib_byte_size_for(ib_bit(h));
808 size_t ht_byte_size = sizeof(hash_table) + ib_byte_size;
809 hash_table *new_ht = (hash_table*)mrb_malloc(mrb, ht_byte_size);
810 return (hash_table*)memcpy(new_ht, h_ht(h), ht_byte_size);
811 }
812
813 static void
814 ht_adjust_ea(mrb_state *mrb, struct RHash *h, uint32_t size, uint32_t max_ea_capa)
815 {
816 uint32_t ea_capa = size;
817 hash_entry *ea = ea_adjust(mrb, ht_ea(h), &ea_capa, max_ea_capa);
818 ht_set_ea(h, ea);
819 ht_set_ea_capa(h, ea_capa);
820 }
821
822 static void
823 ht_to_ar(mrb_state *mrb, struct RHash *h)
824 {
825 uint32_t size = ht_size(h), ea_capa = size;
826 hash_entry *ea = ht_ea(h);
827 ea_compress(ea, ht_ea_n_used(h));
828 ea = ea_adjust(mrb, ea, &ea_capa, AR_MAX_SIZE);
829 mrb_free(mrb, h_ht(h));
830 ar_init(h, size, ea, ea_capa, size);
831 }
832
833 static mrb_bool
834 ht_get(mrb_state *mrb, struct RHash *h, mrb_value key, mrb_value *valp)
835 {
836 ib_find_by_key(mrb, h, key, it, {
837 *valp = ib_it_entry(it)->val;
838 return TRUE;
839 });
840 return FALSE;
841 }
842
843 static void
844 ht_set_as_ar(mrb_state *mrb, struct RHash *h, mrb_value key, mrb_value val)
845 {
846 ht_to_ar(mrb, h);
847 ar_set(mrb, h, key, val);
848 }
849
850 static void
851 ht_set_without_ib_adjustment(mrb_state *mrb, struct RHash *h,
852 mrb_value key, mrb_value val)
853 {
854 mrb_assert(ht_size(h) < ib_bit_to_capa(ib_bit(h)));
855 ib_cycle_by_key(mrb, h, key, it, {
856 if (ib_it_active_p(it)) {
857 if (!obj_eql(mrb, key, ib_it_entry(it)->key, h)) continue;
858 ib_it_entry(it)->val = val;
859 }
860 else {
861 uint32_t ea_n_used = ht_ea_n_used(h);
862 if (ea_n_used == H_MAX_SIZE) {
863 mrb_assert(ht_size(h) == ea_n_used);
864 mrb_raise(mrb, E_ARGUMENT_ERROR, "hash too big");
865 }
866 if (ea_n_used == ht_ea_capa(h)) ht_adjust_ea(mrb, h, ea_n_used, EA_MAX_CAPA);
867 ib_it_set(it, ea_n_used);
868 ea_set(ht_ea(h), ea_n_used, key, val);
869 ht_inc_size(h);
870 ht_set_ea_n_used(h, ++ea_n_used);
871 }
872 return;
873 });
874 }
875
876 static void
877 ht_set(mrb_state *mrb, struct RHash *h, mrb_value key, mrb_value val)
878 {
879 uint32_t size = ht_size(h);
880 uint32_t ib_bit_width = ib_bit(h), ib_capa = ib_bit_to_capa(ib_bit_width);
881 if (ib_upper_bound_for(ib_capa) <= size) {
882 if (size != ht_ea_n_used(h)) ea_compress(ht_ea(h), ht_ea_n_used(h));
883 ht_init(mrb, h, size, ht_ea(h), ht_ea_capa(h), h_ht(h), ++ib_bit_width);
884 }
885 else if (size != ht_ea_n_used(h)) {
886 if (ib_capa - EA_N_RESERVED_INDICES <= ht_ea_n_used(h)) goto compress;
887 if (ht_ea_capa(h) == ht_ea_n_used(h)) {
888 if (size <= AR_MAX_SIZE) {ht_set_as_ar(mrb, h, key, val); return;}
889 if (ea_next_capa_for(size, EA_MAX_CAPA) <= ht_ea_capa(h)) {
890 compress:
891 ea_compress(ht_ea(h), ht_ea_n_used(h));
892 ht_adjust_ea(mrb, h, size, ht_ea_capa(h));
893 ht_init(mrb, h, size, ht_ea(h), ht_ea_capa(h), h_ht(h), ib_bit_width);
894 }
895 }
896 }
897 ht_set_without_ib_adjustment(mrb, h, key, val);
898 }
899
900 static mrb_bool
901 ht_delete(mrb_state *mrb, struct RHash *h, mrb_value key, mrb_value *valp)
902 {
903 ib_find_by_key(mrb, h, key, it, {
904 hash_entry *entry = ib_it_entry(it);
905 *valp = entry->val;
906 ib_it_delete(it);
907 entry_delete(entry);
908 ht_dec_size(h);
909 return TRUE;
910 });
911 return FALSE;
912 }
913
914 static void
915 ht_shift(mrb_state *mrb, struct RHash *h, mrb_value *keyp, mrb_value *valp)
916 {
917 hash_entry *ea = ht_ea(h);
918 ea_each(ea, ht_size(h), entry, {
919 ib_cycle_by_key(mrb, h, entry->key, it, {
920 if (ib_it_get(it) != U32(entry - ea)) continue;
921 *keyp = entry->key;
922 *valp = entry->val;
923 ib_it_delete(it);
924 entry_delete(entry);
925 ht_dec_size(h);
926 return;
927 });
928 });
929 }
930
931 static void
932 ht_rehash(mrb_state *mrb, struct RHash *h)
933 {
934 /* see comments in `h_rehash` */
935 uint32_t size = ht_size(h);
936 if (size <= AR_MAX_SIZE) {
937 ht_to_ar(mrb, h);
938 ar_rehash(mrb, h);
939 return;
940 }
941 uint32_t w_size = 0, ea_capa = ht_ea_capa(h);
942 hash_entry *ea = ht_ea(h);
943 ht_init(mrb, h, 0, ea, ea_capa, h_ht(h), ib_bit_for(size));
944 ht_set_size(h, size);
945 ht_set_ea_n_used(h, ht_ea_n_used(h));
946 ea_each(ea, size, r_entry, {
947 ib_cycle_by_key(mrb, h, r_entry->key, it, {
948 if (ib_it_active_p(it)) {
949 if (!obj_eql(mrb, r_entry->key, ib_it_entry(it)->key, h)) continue;
950 ib_it_entry(it)->val = r_entry->val;
951 ht_set_size(h, --size);
952 entry_delete(r_entry);
953 }
954 else {
955 if (w_size != U32(r_entry - ea)) {
956 ea_set(ea, w_size, r_entry->key, r_entry->val);
957 entry_delete(r_entry);
958 }
959 ib_it_set(it, w_size++);
960 }
961 break;
962 });
963 });
964 mrb_assert(size == w_size);
965 ht_set_ea_n_used(h, size);
966 size <= AR_MAX_SIZE ? ht_to_ar(mrb, h) : ht_adjust_ea(mrb, h, size, ea_capa);
967 }
968
969 static mrb_value
970 h_key_for(mrb_state *mrb, mrb_value key)
971 {
972 if (mrb_string_p(key) && !MRB_FROZEN_P(mrb_str_ptr(key))) {
973 key = mrb_str_dup(mrb, key);
974 MRB_SET_FROZEN_FLAG(mrb_str_ptr(key));
975 }
976 return key;
977 }
978
979 static struct RHash*
980 h_alloc(mrb_state *mrb)
981 {
982 return MRB_OBJ_ALLOC(mrb, MRB_TT_HASH, mrb->hash_class);
983 }
984
985 static void
986 h_init(struct RHash *h)
987 {
988 ar_init(h, 0, NULL, 0, 0);
989 }
990
991 static void
992 h_free_table(mrb_state *mrb, struct RHash *h)
993 {
994 (h_ar_p(h) ? ar_free : ht_free)(mrb, h);
995 }
996
997 static void
998 h_clear(mrb_state *mrb, struct RHash *h)
999 {
1000 h_free_table(mrb, h);
1001 h_init(h);
1002 }
1003
1004 static mrb_bool
1005 h_get(mrb_state *mrb, struct RHash *h, mrb_value key, mrb_value *valp)
1006 {
1007 return (h_ar_p(h) ? ar_get : ht_get)(mrb, h, key, valp);
1008 }
1009
1010 static void
1011 h_set(mrb_state *mrb, struct RHash *h, mrb_value key, mrb_value val)
1012 {
1013 (h_ar_p(h) ? ar_set : ht_set)(mrb, h, key, val);
1014 }
1015
1016 static mrb_bool
1017 h_delete(mrb_state *mrb, struct RHash *h, mrb_value key, mrb_value *valp)
1018 {
1019 return (h_ar_p(h) ? ar_delete : ht_delete)(mrb, h, key, valp);
1020 }
1021
1022 /* find first element in the table, and remove it. */
1023 static void
1024 h_shift(mrb_state *mrb, struct RHash *h, mrb_value *keyp, mrb_value *valp)
1025 {
1026 (h_ar_p(h) ? ar_shift : ht_shift)(mrb, h, keyp, valp);
1027 }
1028
1029 static void
1030 h_rehash(mrb_state *mrb, struct RHash *h)
1031 {
1032 /*
1033 * ==== Comments common to `ar_rehash` and `ht_rehash`
1034 *
1035 * - Because reindex (such as elimination of duplicate keys) must be
1036 * guaranteed, it is necessary to set one by one.
1037 *
1038 * - To prevent EA from breaking if an exception occurs in the middle,
1039 * delete the slot before moving when moving the entry, and update size
1040 * at any time when overwriting.
1041 */
1042 (h_size(h) == 0 ? h_clear : h_ar_p(h) ? ar_rehash : ht_rehash)(mrb, h);
1043 }
1044
1045 static void
1046 h_replace(mrb_state *mrb, struct RHash *h, struct RHash *orig_h)
1047 {
1048 uint32_t size = h_size(orig_h);
1049 if (size == 0) {
1050 h_clear(mrb, h);
1051 }
1052 else if (h_ar_p(orig_h)) {
1053 uint32_t ea_capa = ar_ea_capa(orig_h);
1054 hash_entry *ea = ea_dup(mrb, ar_ea(orig_h), ea_capa);
1055 h_free_table(mrb, h);
1056 ar_init(h, size, ea, ea_capa, ar_ea_n_used(orig_h));
1057 }
1058 else { /* HT */
1059 uint32_t ea_capa = ht_ea_capa(orig_h);
1060 hash_entry *ea = ea_dup(mrb, ht_ea(orig_h), ea_capa);
1061 hash_table *ht = ht_dup(mrb, orig_h);
1062 h_free_table(mrb, h);
1063 h_ht_on(h);
1064 h_set_ht(h, ht);
1065 ht_set_size(h, size);
1066 ht_set_ea(h, ea);
1067 #ifdef MRB_64BIT
1068 ht_set_ea_capa(h, ea_capa);
1069 ht_set_ea_n_used(h, ht_ea_n_used(orig_h));
1070 #endif
1071 ib_set_bit(h, ib_bit(orig_h));
1072 }
1073 }
1074
1075 void
1076 mrb_gc_mark_hash(mrb_state *mrb, struct RHash *h)
1077 {
1078 h_each(h, entry, {
1079 mrb_gc_mark_value(mrb, entry->key);
1080 mrb_gc_mark_value(mrb, entry->val);
1081 });
1082 }
1083
1084 size_t
1085 mrb_gc_mark_hash_size(mrb_state *mrb, struct RHash *h)
1086 {
1087 return h_size(h) * 2;
1088 }
1089
1090 void
1091 mrb_gc_free_hash(mrb_state *mrb, struct RHash *h)
1092 {
1093 h_free_table(mrb, h);
1094 }
1095
1096 size_t
1097 mrb_hash_memsize(mrb_value self)
1098 {
1099 struct RHash *h = mrb_hash_ptr(self);
1100 return mrb_obj_iv_tbl_memsize(self) +
1101 (h_ar_p(h) ? (ar_ea_capa(h) * sizeof(hash_entry)) :
1102 (ht_ea_capa(h) * sizeof(hash_entry) +
1103 sizeof(hash_table) +
1104 ib_byte_size_for(ib_bit(h))));
1105 }
1106
1107 /* Iterates over the key/value pairs. */
1108 MRB_API void
1109 mrb_hash_foreach(mrb_state *mrb, struct RHash *h, mrb_hash_foreach_func *func, void *data)
1110 {
1111 h_each(h, entry, {
1112 int n;
1113 h_check_modified(mrb, h, {
1114 n = func(mrb, entry->key, entry->val, data);
1115 });
1116 if (n != 0) return;
1117 });
1118 }
1119
1120 MRB_API mrb_value
1121 mrb_hash_new(mrb_state *mrb)
1122 {
1123 struct RHash *h = h_alloc(mrb);
1124 return mrb_obj_value(h);
1125 }
1126
1127 /*
1128 * Set the capacity of EA and IB to minimum capacity (and appropriate load
1129 * factor) that does not cause expansion when inserting `capa` elements.
1130 */
1131 MRB_API mrb_value
1132 mrb_hash_new_capa(mrb_state *mrb, mrb_int capa)
1133 {
1134 if (capa < 0 || EA_MAX_CAPA < capa) {
1135 mrb_raise(mrb, E_ARGUMENT_ERROR, "hash too big");
1136 return mrb_nil_value(); /* not reached */
1137 }
1138 else if (capa == 0) {
1139 return mrb_hash_new(mrb);
1140 }
1141 else {
1142 uint32_t size = U32(capa);
1143 struct RHash *h = h_alloc(mrb);
1144 hash_entry *ea = ea_resize(mrb, NULL, size);
1145 if (size <= AR_MAX_SIZE) {
1146 ar_init(h, 0, ea, size, 0);
1147 }
1148 else {
1149 ht_init(mrb, h, 0, ea, size, NULL, ib_bit_for(size));
1150 }
1151 return mrb_obj_value(h);
1152 }
1153 }
1154
1155 static mrb_value mrb_hash_default(mrb_state *mrb, mrb_value hash);
1156
1157 static void
1158 hash_modify(mrb_state *mrb, mrb_value hash)
1159 {
1160 mrb_check_frozen(mrb, mrb_hash_ptr(hash));
1161 }
1162
1163 static mrb_value
1164 hash_default(mrb_state *mrb, mrb_value hash, mrb_value key)
1165 {
1166 if (MRB_RHASH_DEFAULT_P(hash)) {
1167 if (MRB_RHASH_PROCDEFAULT_P(hash)) {
1168 return mrb_funcall_id(mrb, RHASH_PROCDEFAULT(hash), MRB_SYM(call), 2, hash, key);
1169 }
1170 else {
1171 return RHASH_IFNONE(hash);
1172 }
1173 }
1174 return mrb_nil_value();
1175 }
1176
1177 static void
1178 hash_replace(mrb_state *mrb, mrb_value self, mrb_value orig)
1179 {
1180 struct RHash *h = mrb_hash_ptr(self), *orig_h = mrb_hash_ptr(orig);
1181 uint32_t mask = MRB_HASH_DEFAULT | MRB_HASH_PROC_DEFAULT;
1182 mrb_sym name;
1183 h_replace(mrb, h, orig_h);
1184 name = MRB_SYM(ifnone);
1185 if (orig_h->flags & MRB_HASH_DEFAULT) {
1186 mrb_iv_set(mrb, self, name, mrb_iv_get(mrb, orig, name));
1187 }
1188 else {
1189 mrb_iv_remove(mrb, self, name);
1190 }
1191 h->flags &= ~mask;
1192 h->flags |= orig_h->flags & mask;
1193 }
1194
1195 static mrb_value
1196 mrb_hash_init_copy(mrb_state *mrb, mrb_value self)
1197 {
1198 mrb_value orig;
1199 mrb_get_args(mrb, "H", &orig);
1200 hash_modify(mrb, self);
1201 if (mrb_hash_ptr(self) != mrb_hash_ptr(orig)) hash_replace(mrb, self, orig);
1202 return self;
1203 }
1204
1205 MRB_API mrb_value
1206 mrb_hash_dup(mrb_state *mrb, mrb_value self)
1207 {
1208 struct RHash* copy_h = h_alloc(mrb);
1209 mrb_value copy = mrb_obj_value(copy_h);
1210 copy_h->c = mrb_hash_ptr(self)->c;
1211 hash_replace(mrb, copy, self);
1212 return copy;
1213 }
1214
1215 MRB_API mrb_value
1216 mrb_hash_get(mrb_state *mrb, mrb_value hash, mrb_value key)
1217 {
1218 mrb_value val;
1219 mrb_sym mid;
1220
1221 if (h_get(mrb, mrb_hash_ptr(hash), key, &val)) {
1222 return val;
1223 }
1224
1225 mid = MRB_SYM(default);
1226 if (mrb_func_basic_p(mrb, hash, mid, mrb_hash_default)) {
1227 return hash_default(mrb, hash, key);
1228 }
1229 /* xxx mrb_funcall_tailcall(mrb, hash, "default", 1, key); */
1230 return mrb_funcall_argv(mrb, hash, mid, 1, &key);
1231 }
1232
1233 MRB_API mrb_value
1234 mrb_hash_fetch(mrb_state *mrb, mrb_value hash, mrb_value key, mrb_value def)
1235 {
1236 mrb_value val;
1237
1238 if (h_get(mrb, mrb_hash_ptr(hash), key, &val)) {
1239 return val;
1240 }
1241 /* not found */
1242 return def;
1243 }
1244
1245 MRB_API void
1246 mrb_hash_set(mrb_state *mrb, mrb_value hash, mrb_value key, mrb_value val)
1247 {
1248 hash_modify(mrb, hash);
1249 key = h_key_for(mrb, key);
1250 h_set(mrb, mrb_hash_ptr(hash), key, val);
1251 mrb_field_write_barrier_value(mrb, mrb_basic_ptr(hash), key);
1252 mrb_field_write_barrier_value(mrb, mrb_basic_ptr(hash), val);
1253 }
1254
1255 /* 15.2.13.4.16 */
1256 /*
1257 * call-seq:
1258 * Hash.new -> new_hash
1259 * Hash.new(obj) -> new_hash
1260 * Hash.new {|hash, key| block } -> new_hash
1261 *
1262 * Returns a new, empty hash. If this hash is subsequently accessed by
1263 * a key that doesn't correspond to a hash entry, the value returned
1264 * depends on the style of <code>new</code> used to create the hash. In
1265 * the first form, the access returns <code>nil</code>. If
1266 * <i>obj</i> is specified, this single object will be used for
1267 * all <em>default values</em>. If a block is specified, it will be
1268 * called with the hash object and the key, and should return the
1269 * default value. It is the block's responsibility to store the value
1270 * in the hash if required.
1271 *
1272 * h = Hash.new("Go Fish")
1273 * h["a"] = 100
1274 * h["b"] = 200
1275 * h["a"] #=> 100
1276 * h["c"] #=> "Go Fish"
1277 * # The following alters the single default object
1278 * h["c"].upcase! #=> "GO FISH"
1279 * h["d"] #=> "GO FISH"
1280 * h.keys #=> ["a", "b"]
1281 *
1282 * # While this creates a new default object each time
1283 * h = Hash.new { |hash, key| hash[key] = "Go Fish: #{key}" }
1284 * h["c"] #=> "Go Fish: c"
1285 * h["c"].upcase! #=> "GO FISH: C"
1286 * h["d"] #=> "Go Fish: d"
1287 * h.keys #=> ["c", "d"]
1288 *
1289 */
1290
1291 static mrb_value
1292 mrb_hash_init(mrb_state *mrb, mrb_value hash)
1293 {
1294 mrb_value block, ifnone;
1295 mrb_bool ifnone_p;
1296
1297 ifnone = mrb_nil_value();
1298 mrb_get_args(mrb, "&|o?", &block, &ifnone, &ifnone_p);
1299 hash_modify(mrb, hash);
1300 if (!mrb_nil_p(block)) {
1301 if (ifnone_p) {
1302 mrb_argnum_error(mrb, 1, 0, 0);
1303 }
1304 RHASH(hash)->flags |= MRB_HASH_PROC_DEFAULT;
1305 ifnone = block;
1306 }
1307 if (!mrb_nil_p(ifnone)) {
1308 RHASH(hash)->flags |= MRB_HASH_DEFAULT;
1309 mrb_iv_set(mrb, hash, MRB_SYM(ifnone), ifnone);
1310 }
1311 return hash;
1312 }
1313
1314 /* 15.2.13.4.2 */
1315 /*
1316 * call-seq:
1317 * hsh[key] -> value
1318 *
1319 * Element Reference---Retrieves the <i>value</i> object corresponding
1320 * to the <i>key</i> object. If not found, returns the default value (see
1321 * <code>Hash::new</code> for details).
1322 *
1323 * h = { "a" => 100, "b" => 200 }
1324 * h["a"] #=> 100
1325 * h["c"] #=> nil
1326 *
1327 */
1328 static mrb_value
1329 mrb_hash_aget(mrb_state *mrb, mrb_value self)
1330 {
1331 mrb_value key = mrb_get_arg1(mrb);
1332
1333 return mrb_hash_get(mrb, self, key);
1334 }
1335
1336 /* 15.2.13.4.5 */
1337 /*
1338 * call-seq:
1339 * hsh.default(key=nil) -> obj
1340 *
1341 * Returns the default value, the value that would be returned by
1342 * <i>hsh</i>[<i>key</i>] if <i>key</i> did not exist in <i>hsh</i>.
1343 * See also <code>Hash::new</code> and <code>Hash#default=</code>.
1344 *
1345 * h = Hash.new #=> {}
1346 * h.default #=> nil
1347 * h.default(2) #=> nil
1348 *
1349 * h = Hash.new("cat") #=> {}
1350 * h.default #=> "cat"
1351 * h.default(2) #=> "cat"
1352 *
1353 * h = Hash.new {|h,k| h[k] = k.to_i*10} #=> {}
1354 * h.default #=> nil
1355 * h.default(2) #=> 20
1356 */
1357
1358 static mrb_value
1359 mrb_hash_default(mrb_state *mrb, mrb_value hash)
1360 {
1361 mrb_value key;
1362 mrb_bool given;
1363
1364 mrb_get_args(mrb, "|o?", &key, &given);
1365 if (MRB_RHASH_DEFAULT_P(hash)) {
1366 if (MRB_RHASH_PROCDEFAULT_P(hash)) {
1367 if (!given) return mrb_nil_value();
1368 return mrb_funcall_id(mrb, RHASH_PROCDEFAULT(hash), MRB_SYM(call), 2, hash, key);
1369 }
1370 else {
1371 return RHASH_IFNONE(hash);
1372 }
1373 }
1374 return mrb_nil_value();
1375 }
1376
1377 /* 15.2.13.4.6 */
1378 /*
1379 * call-seq:
1380 * hsh.default = obj -> obj
1381 *
1382 * Sets the default value, the value returned for a key that does not
1383 * exist in the hash. It is not possible to set the default to a
1384 * <code>Proc</code> that will be executed on each key lookup.
1385 *
1386 * h = { "a" => 100, "b" => 200 }
1387 * h.default = "Go fish"
1388 * h["a"] #=> 100
1389 * h["z"] #=> "Go fish"
1390 * # This doesn't do what you might hope...
1391 * h.default = proc do |hash, key|
1392 * hash[key] = key + key
1393 * end
1394 * h[2] #=> #<Proc:0x401b3948@-:6>
1395 * h["cat"] #=> #<Proc:0x401b3948@-:6>
1396 */
1397
1398 static mrb_value
1399 mrb_hash_set_default(mrb_state *mrb, mrb_value hash)
1400 {
1401 mrb_value ifnone = mrb_get_arg1(mrb);
1402
1403 hash_modify(mrb, hash);
1404 mrb_iv_set(mrb, hash, MRB_SYM(ifnone), ifnone);
1405 RHASH(hash)->flags &= ~MRB_HASH_PROC_DEFAULT;
1406 if (!mrb_nil_p(ifnone)) {
1407 RHASH(hash)->flags |= MRB_HASH_DEFAULT;
1408 }
1409 else {
1410 RHASH(hash)->flags &= ~MRB_HASH_DEFAULT;
1411 }
1412 return ifnone;
1413 }
1414
1415 /* 15.2.13.4.7 */
1416 /*
1417 * call-seq:
1418 * hsh.default_proc -> anObject
1419 *
1420 * If <code>Hash::new</code> was invoked with a block, return that
1421 * block, otherwise return <code>nil</code>.
1422 *
1423 * h = Hash.new {|h,k| h[k] = k*k } #=> {}
1424 * p = h.default_proc #=> #<Proc:0x401b3d08@-:1>
1425 * a = [] #=> []
1426 * p.call(a, 2)
1427 * a #=> [nil, nil, 4]
1428 */
1429
1430 static mrb_value
1431 mrb_hash_default_proc(mrb_state *mrb, mrb_value hash)
1432 {
1433 if (MRB_RHASH_PROCDEFAULT_P(hash)) {
1434 return RHASH_PROCDEFAULT(hash);
1435 }
1436 return mrb_nil_value();
1437 }
1438
1439 /*
1440 * call-seq:
1441 * hsh.default_proc = proc_obj -> proc_obj
1442 *
1443 * Sets the default proc to be executed on each key lookup.
1444 *
1445 * h.default_proc = proc do |hash, key|
1446 * hash[key] = key + key
1447 * end
1448 * h[2] #=> 4
1449 * h["cat"] #=> "catcat"
1450 */
1451
1452 static mrb_value
1453 mrb_hash_set_default_proc(mrb_state *mrb, mrb_value hash)
1454 {
1455 mrb_value ifnone = mrb_get_arg1(mrb);
1456
1457 hash_modify(mrb, hash);
1458 mrb_iv_set(mrb, hash, MRB_SYM(ifnone), ifnone);
1459 if (!mrb_nil_p(ifnone)) {
1460 RHASH(hash)->flags |= MRB_HASH_PROC_DEFAULT;
1461 RHASH(hash)->flags |= MRB_HASH_DEFAULT;
1462 }
1463 else {
1464 RHASH(hash)->flags &= ~MRB_HASH_DEFAULT;
1465 RHASH(hash)->flags &= ~MRB_HASH_PROC_DEFAULT;
1466 }
1467
1468 return ifnone;
1469 }
1470
1471 MRB_API mrb_value
1472 mrb_hash_delete_key(mrb_state *mrb, mrb_value hash, mrb_value key)
1473 {
1474 mrb_value del_val;
1475
1476 hash_modify(mrb, hash);
1477 if (h_delete(mrb, mrb_hash_ptr(hash), key, &del_val)) {
1478 return del_val;
1479 }
1480
1481 /* not found */
1482 return mrb_nil_value();
1483 }
1484
1485 static mrb_value
1486 mrb_hash_delete(mrb_state *mrb, mrb_value self)
1487 {
1488 mrb_value key = mrb_get_arg1(mrb);
1489 mrb->c->ci->mid = 0;
1490 return mrb_hash_delete_key(mrb, self, key);
1491 }
1492
1493 /* 15.2.13.4.24 */
1494 /*
1495 * call-seq:
1496 * hsh.shift -> anArray or obj
1497 *
1498 * Removes a key-value pair from <i>hsh</i> and returns it as the
1499 * two-item array <code>[</code> <i>key, value</i> <code>]</code>, or
1500 * the hash's default value if the hash is empty.
1501 *
1502 * h = { 1 => "a", 2 => "b", 3 => "c" }
1503 * h.shift #=> [1, "a"]
1504 * h #=> {2=>"b", 3=>"c"}
1505 */
1506
1507 static mrb_value
1508 mrb_hash_shift(mrb_state *mrb, mrb_value hash)
1509 {
1510 struct RHash *h = mrb_hash_ptr(hash);
1511
1512 hash_modify(mrb, hash);
1513 if (h_size(h) == 0) {
1514 return mrb_nil_value();
1515 }
1516 else {
1517 mrb_value del_key, del_val;
1518 h_shift(mrb, h, &del_key, &del_val);
1519 mrb_gc_protect(mrb, del_key);
1520 mrb_gc_protect(mrb, del_val);
1521 return mrb_assoc_new(mrb, del_key, del_val);
1522 }
1523 }
1524
1525 /* 15.2.13.4.4 */
1526 /*
1527 * call-seq:
1528 * hsh.clear -> hsh
1529 *
1530 * Removes all key-value pairs from `hsh`.
1531 *
1532 * h = { "a" => 100, "b" => 200 } #=> {"a"=>100, "b"=>200}
1533 * h.clear #=> {}
1534 *
1535 */
1536
1537 MRB_API mrb_value
1538 mrb_hash_clear(mrb_state *mrb, mrb_value hash)
1539 {
1540 hash_modify(mrb, hash);
1541 h_clear(mrb, mrb_hash_ptr(hash));
1542 return hash;
1543 }
1544
1545 /* 15.2.13.4.3 */
1546 /* 15.2.13.4.26 */
1547 /*
1548 * call-seq:
1549 * hsh[key] = value -> value
1550 * hsh.store(key, value) -> value
1551 *
1552 * Element Assignment---Associates the value given by
1553 * <i>value</i> with the key given by <i>key</i>.
1554 * <i>key</i> should not have its value changed while it is in
1555 * use as a key (a <code>String</code> passed as a key will be
1556 * duplicated and frozen).
1557 *
1558 * h = { "a" => 100, "b" => 200 }
1559 * h["a"] = 9
1560 * h["c"] = 4
1561 * h #=> {"a"=>9, "b"=>200, "c"=>4}
1562 *
1563 */
1564 static mrb_value
1565 mrb_hash_aset(mrb_state *mrb, mrb_value self)
1566 {
1567 mrb_int argc = mrb_get_argc(mrb);
1568
1569 if (argc != 2) {
1570 mrb_argnum_error(mrb, argc, 2, 2);
1571 }
1572
1573 const mrb_value *argv = mrb_get_argv(mrb);
1574 mrb_value val = argv[1];
1575
1576 mrb_hash_set(mrb, self, argv[0], argv[1]);
1577 return val;
1578 }
1579
1580 MRB_API mrb_int
1581 mrb_hash_size(mrb_state *mrb, mrb_value hash)
1582 {
1583 return (mrb_int)h_size(mrb_hash_ptr(hash));
1584 }
1585
1586 /* 15.2.13.4.20 */
1587 /* 15.2.13.4.25 */
1588 /*
1589 * call-seq:
1590 * hsh.length -> integer
1591 * hsh.size -> integer
1592 *
1593 * Returns the number of key-value pairs in the hash.
1594 *
1595 * h = { "d" => 100, "a" => 200, "v" => 300, "e" => 400 }
1596 * h.length #=> 4
1597 * h.delete("a") #=> 200
1598 * h.length #=> 3
1599 */
1600 static mrb_value
1601 mrb_hash_size_m(mrb_state *mrb, mrb_value self)
1602 {
1603 mrb_int size = mrb_hash_size(mrb, self);
1604 return mrb_int_value(mrb, size);
1605 }
1606
1607 MRB_API mrb_bool
1608 mrb_hash_empty_p(mrb_state *mrb, mrb_value self)
1609 {
1610 return h_size(mrb_hash_ptr(self)) == 0;
1611 }
1612
1613 /* 15.2.13.4.12 */
1614 /*
1615 * call-seq:
1616 * hsh.empty? -> true or false
1617 *
1618 * Returns <code>true</code> if <i>hsh</i> contains no key-value pairs.
1619 *
1620 * {}.empty? #=> true
1621 *
1622 */
1623 static mrb_value
1624 mrb_hash_empty_m(mrb_state *mrb, mrb_value self)
1625 {
1626 return mrb_bool_value(mrb_hash_empty_p(mrb, self));
1627 }
1628
1629 /* 15.2.13.4.19 */
1630 /*
1631 * call-seq:
1632 * hsh.keys -> array
1633 *
1634 * Returns a new array populated with the keys from this hash. See also
1635 * <code>Hash#values</code>.
1636 *
1637 * h = { "a" => 100, "b" => 200, "c" => 300, "d" => 400 }
1638 * h.keys #=> ["a", "b", "c", "d"]
1639 *
1640 */
1641
1642 MRB_API mrb_value
1643 mrb_hash_keys(mrb_state *mrb, mrb_value hash)
1644 {
1645 struct RHash *h = mrb_hash_ptr(hash);
1646 mrb_value ary = mrb_ary_new_capa(mrb, (mrb_int)h_size(h));
1647 h_each(h, entry, {
1648 mrb_ary_push(mrb, ary, entry->key);
1649 });
1650 return ary;
1651 }
1652
1653 /* 15.2.13.4.28 */
1654 /*
1655 * call-seq:
1656 * hsh.values -> array
1657 *
1658 * Returns a new array populated with the values from <i>hsh</i>. See
1659 * also <code>Hash#keys</code>.
1660 *
1661 * h = { "a" => 100, "b" => 200, "c" => 300 }
1662 * h.values #=> [100, 200, 300]
1663 *
1664 */
1665
1666 MRB_API mrb_value
1667 mrb_hash_values(mrb_state *mrb, mrb_value hash)
1668 {
1669 struct RHash *h = mrb_hash_ptr(hash);
1670 mrb_value ary = mrb_ary_new_capa(mrb, (mrb_int)h_size(h));
1671 h_each(h, entry, {
1672 mrb_ary_push(mrb, ary, entry->val);
1673 });
1674 return ary;
1675 }
1676
1677 /* 15.2.13.4.13 */
1678 /* 15.2.13.4.15 */
1679 /* 15.2.13.4.18 */
1680 /* 15.2.13.4.21 */
1681 /*
1682 * call-seq:
1683 * hsh.has_key?(key) -> true or false
1684 * hsh.include?(key) -> true or false
1685 * hsh.key?(key) -> true or false
1686 * hsh.member?(key) -> true or false
1687 *
1688 * Returns <code>true</code> if the given key is present in <i>hsh</i>.
1689 *
1690 * h = { "a" => 100, "b" => 200 }
1691 * h.has_key?("a") #=> true
1692 * h.has_key?("z") #=> false
1693 *
1694 */
1695
1696 MRB_API mrb_bool
1697 mrb_hash_key_p(mrb_state *mrb, mrb_value hash, mrb_value key)
1698 {
1699 mrb_value val;
1700 return h_get(mrb, mrb_hash_ptr(hash), key, &val);
1701 }
1702
1703 static mrb_value
1704 mrb_hash_has_key(mrb_state *mrb, mrb_value hash)
1705 {
1706 mrb_value key = mrb_get_arg1(mrb);
1707 mrb_bool key_p;
1708
1709 key_p = mrb_hash_key_p(mrb, hash, key);
1710 return mrb_bool_value(key_p);
1711 }
1712
1713 /* 15.2.13.4.14 */
1714 /* 15.2.13.4.27 */
1715 /*
1716 * call-seq:
1717 * hsh.has_value?(value) -> true or false
1718 * hsh.value?(value) -> true or false
1719 *
1720 * Returns <code>true</code> if the given value is present for some key
1721 * in <i>hsh</i>.
1722 *
1723 * h = { "a" => 100, "b" => 200 }
1724 * h.has_value?(100) #=> true
1725 * h.has_value?(999) #=> false
1726 */
1727
1728 static mrb_value
1729 mrb_hash_has_value(mrb_state *mrb, mrb_value hash)
1730 {
1731 mrb_value val = mrb_get_arg1(mrb);
1732 struct RHash *h = mrb_hash_ptr(hash);
1733 h_each(h, entry, {
1734 h_check_modified(mrb, h, {
1735 if (mrb_equal(mrb, val, entry->val)) return mrb_true_value();
1736 });
1737 });
1738 return mrb_false_value();
1739 }
1740
1741 MRB_API void
1742 mrb_hash_merge(mrb_state *mrb, mrb_value hash1, mrb_value hash2)
1743 {
1744 struct RHash *h1, *h2;
1745
1746 hash_modify(mrb, hash1);
1747 mrb_ensure_hash_type(mrb, hash2);
1748 h1 = mrb_hash_ptr(hash1);
1749 h2 = mrb_hash_ptr(hash2);
1750
1751 if (h1 == h2) return;
1752 if (h_size(h2) == 0) return;
1753 h_each(h2, entry, {
1754 h_check_modified(mrb, h2, {h_set(mrb, h1, entry->key, entry->val);});
1755 mrb_field_write_barrier_value(mrb, (struct RBasic*)h1, entry->key);
1756 mrb_field_write_barrier_value(mrb, (struct RBasic*)h1, entry->val);
1757 });
1758 }
1759
1760 static mrb_value
1761 mrb_hash_merge_m(mrb_state *mrb, mrb_value hash)
1762 {
1763 mrb_int argc;
1764 mrb_value *argv;
1765
1766 mrb_get_args(mrb, "*", &argv, &argc);
1767 while (argc--) {
1768 mrb_hash_merge(mrb, hash, *argv++);
1769 }
1770 return hash;
1771 }
1772
1773 /*
1774 * call-seq:
1775 * hsh.rehash -> hsh
1776 *
1777 * Rebuilds the hash based on the current hash values for each key. If
1778 * values of key objects have changed since they were inserted, this
1779 * method will reindex <i>hsh</i>.
1780 *
1781 * keys = (1..17).map{|n| [n]}
1782 * k = keys[0]
1783 * h = {}
1784 * keys.each{|key| h[key] = key[0]}
1785 * h #=> { [1]=>1, [2]=>2, ... [16]=>16, [17]=>17}
1786 * h[k] #=> 1
1787 * k[0] = keys.size + 1
1788 * h #=> {[18]=>1, [2]=>2, ... [16]=>16, [17]=>17}
1789 * h[k] #=> nil
1790 * h.rehash
1791 * h[k] #=> 1
1792 */
1793 static mrb_value
1794 mrb_hash_rehash(mrb_state *mrb, mrb_value self)
1795 {
1796 h_rehash(mrb, mrb_hash_ptr(self));
1797 return self;
1798 }
1799
1800 static mrb_value
1801 mrb_hash_compact(mrb_state *mrb, mrb_value hash)
1802 {
1803 struct RHash *h = mrb_hash_ptr(hash);
1804 mrb_bool ht_p = h_ht_p(h);
1805 uint32_t size = ht_p ? ht_size(h) : ar_size(h);
1806 uint32_t dec = 0;
1807
1808 mrb_check_frozen(mrb, h);
1809 h_each(h, entry, {
1810 if (mrb_nil_p(entry->val)) {
1811 entry_delete(entry);
1812 dec++;
1813 }
1814 });
1815 if (dec == 0) return mrb_nil_value();
1816 size -= dec;
1817 if (ht_p) {
1818 ht_set_size(h, size);
1819 }
1820 else {
1821 ar_set_size(h, size);
1822 }
1823 return hash;
1824 }
1825
1826 /*
1827 * call-seq:
1828 * hash.to_s -> string
1829 * hash.inspect -> string
1830 *
1831 * Return the contents of this hash as a string.
1832 */
1833 static mrb_value
1834 mrb_hash_to_s(mrb_state *mrb, mrb_value self)
1835 {
1836 mrb->c->ci->mid = MRB_SYM(inspect);
1837 mrb_value ret = mrb_str_new_lit(mrb, "{");
1838 int ai = mrb_gc_arena_save(mrb);
1839 if (mrb_inspect_recursive_p(mrb, self)) {
1840 mrb_str_cat_lit(mrb, ret, "...}");
1841 return ret;
1842 }
1843
1844 mrb_int i = 0;
1845 struct RHash *h = mrb_hash_ptr(self);
1846 h_each(h, entry, {
1847 if (i++ > 0) mrb_str_cat_lit(mrb, ret, ", ");
1848 h_check_modified(mrb, h, {
1849 mrb_str_cat_str(mrb, ret, mrb_inspect(mrb, entry->key));
1850 });
1851 mrb_gc_arena_restore(mrb, ai);
1852 mrb_str_cat_lit(mrb, ret, "=>");
1853 h_check_modified(mrb, h, {
1854 mrb_str_cat_str(mrb, ret, mrb_inspect(mrb, entry->val));
1855 });
1856 mrb_gc_arena_restore(mrb, ai);
1857 });
1858 mrb_str_cat_lit(mrb, ret, "}");
1859
1860 return ret;
1861 }
1862
1863 void
1864 mrb_init_hash(mrb_state *mrb)
1865 {
1866 struct RClass *h;
1867
1868 mrb->hash_class = h = mrb_define_class(mrb, "Hash", mrb->object_class); /* 15.2.13 */
1869 MRB_SET_INSTANCE_TT(h, MRB_TT_HASH);
1870
1871 mrb_define_method(mrb, h, "[]", mrb_hash_aget, MRB_ARGS_REQ(1)); /* 15.2.13.4.2 */
1872 mrb_define_method(mrb, h, "[]=", mrb_hash_aset, MRB_ARGS_REQ(2)); /* 15.2.13.4.3 */
1873 mrb_define_method(mrb, h, "clear", mrb_hash_clear, MRB_ARGS_NONE()); /* 15.2.13.4.4 */
1874 mrb_define_method(mrb, h, "default", mrb_hash_default, MRB_ARGS_OPT(1)); /* 15.2.13.4.5 */
1875 mrb_define_method(mrb, h, "default=", mrb_hash_set_default, MRB_ARGS_REQ(1)); /* 15.2.13.4.6 */
1876 mrb_define_method(mrb, h, "default_proc", mrb_hash_default_proc,MRB_ARGS_NONE()); /* 15.2.13.4.7 */
1877 mrb_define_method(mrb, h, "default_proc=", mrb_hash_set_default_proc,MRB_ARGS_REQ(1)); /* 15.2.13.4.7 */
1878 mrb_define_method(mrb, h, "__delete", mrb_hash_delete, MRB_ARGS_REQ(1)); /* core of 15.2.13.4.8 */
1879 mrb_define_method(mrb, h, "empty?", mrb_hash_empty_m, MRB_ARGS_NONE()); /* 15.2.13.4.12 */
1880 mrb_define_method(mrb, h, "has_key?", mrb_hash_has_key, MRB_ARGS_REQ(1)); /* 15.2.13.4.13 */
1881 mrb_define_method(mrb, h, "has_value?", mrb_hash_has_value, MRB_ARGS_REQ(1)); /* 15.2.13.4.14 */
1882 mrb_define_method(mrb, h, "include?", mrb_hash_has_key, MRB_ARGS_REQ(1)); /* 15.2.13.4.15 */
1883 mrb_define_method(mrb, h, "initialize", mrb_hash_init, MRB_ARGS_OPT(1)|MRB_ARGS_BLOCK()); /* 15.2.13.4.16 */
1884 mrb_define_method(mrb, h, "initialize_copy", mrb_hash_init_copy, MRB_ARGS_REQ(1)); /* 15.2.13.4.17 */
1885 mrb_define_method(mrb, h, "key?", mrb_hash_has_key, MRB_ARGS_REQ(1)); /* 15.2.13.4.18 */
1886 mrb_define_method(mrb, h, "keys", mrb_hash_keys, MRB_ARGS_NONE()); /* 15.2.13.4.19 */
1887 mrb_define_method(mrb, h, "length", mrb_hash_size_m, MRB_ARGS_NONE()); /* 15.2.13.4.20 */
1888 mrb_define_method(mrb, h, "member?", mrb_hash_has_key, MRB_ARGS_REQ(1)); /* 15.2.13.4.21 */
1889 mrb_define_method(mrb, h, "replace", mrb_hash_init_copy, MRB_ARGS_REQ(1)); /* 15.2.13.4.23 */
1890 mrb_define_method(mrb, h, "shift", mrb_hash_shift, MRB_ARGS_NONE()); /* 15.2.13.4.24 */
1891 mrb_define_method(mrb, h, "size", mrb_hash_size_m, MRB_ARGS_NONE()); /* 15.2.13.4.25 */
1892 mrb_define_method(mrb, h, "store", mrb_hash_aset, MRB_ARGS_REQ(2)); /* 15.2.13.4.26 */
1893 mrb_define_method(mrb, h, "value?", mrb_hash_has_value, MRB_ARGS_REQ(1)); /* 15.2.13.4.27 */
1894 mrb_define_method(mrb, h, "values", mrb_hash_values, MRB_ARGS_NONE()); /* 15.2.13.4.28 */
1895 mrb_define_method(mrb, h, "to_s", mrb_hash_to_s, MRB_ARGS_NONE());
1896 mrb_define_method(mrb, h, "inspect", mrb_hash_to_s, MRB_ARGS_NONE());
1897 mrb_define_method(mrb, h, "rehash", mrb_hash_rehash, MRB_ARGS_NONE());
1898 mrb_define_method(mrb, h, "__merge", mrb_hash_merge_m, MRB_ARGS_REQ(1));
1899 mrb_define_method(mrb, h, "__compact", mrb_hash_compact, MRB_ARGS_NONE()); /* implementation of Hash#compact! */
1900 }