diff options
| author | Yukihiro "Matz" Matsumoto <[email protected]> | 2018-09-26 13:04:13 +0900 |
|---|---|---|
| committer | Yukihiro "Matz" Matsumoto <[email protected]> | 2018-09-26 23:09:48 +0900 |
| commit | 54ec08c7f19e7c46de3dcb63e4f30cd65e4a56e7 (patch) | |
| tree | d4d8c125af24a62d03692bfc073acf2c46f79061 /src/hash.c | |
| parent | d78acc7afed35813f25e3091150dab668c373f05 (diff) | |
| download | mruby-54ec08c7f19e7c46de3dcb63e4f30cd65e4a56e7.tar.gz mruby-54ec08c7f19e7c46de3dcb63e4f30cd65e4a56e7.zip | |
Implement `Hash#rehash` in C using `sg_compact()`.
Diffstat (limited to 'src/hash.c')
| -rw-r--r-- | src/hash.c | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/src/hash.c b/src/hash.c index 07a12be68..21dc846af 100644 --- a/src/hash.c +++ b/src/hash.c @@ -1367,6 +1367,26 @@ mrb_hash_merge(mrb_state *mrb, mrb_value hash1, mrb_value hash2) return; } +/* + * call-seq: + * hsh.rehash -> hsh + * + * Rebuilds the hash based on the current hash values for each key. If + * values of key objects have changed since they were inserted, this + * method will reindex <i>hsh</i>. + * + * h = {"AAA" => "b"} + * h.keys[0].chop! + * h.rehash #=> {"AA"=>"b"} + * h["AA"] #=> "b" + */ +static mrb_value +mrb_hash_rehash(mrb_state *mrb, mrb_value self) +{ + sg_compact(mrb, RHASH_TBL(self)); + return self; +} + void mrb_init_hash(mrb_state *mrb) { @@ -1398,6 +1418,7 @@ mrb_init_hash(mrb_state *mrb) mrb_define_method(mrb, h, "store", mrb_hash_aset, MRB_ARGS_REQ(2)); /* 15.2.13.4.26 */ mrb_define_method(mrb, h, "value?", mrb_hash_has_value, MRB_ARGS_REQ(1)); /* 15.2.13.4.27 */ mrb_define_method(mrb, h, "values", mrb_hash_values, MRB_ARGS_NONE()); /* 15.2.13.4.28 */ + mrb_define_method(mrb, h, "rehash", mrb_hash_rehash, MRB_ARGS_NONE()); mrb_define_method(mrb, h, "to_hash", mrb_hash_to_hash, MRB_ARGS_NONE()); /* 15.2.13.4.29 (x)*/ } |
