[270] | 1 | /*
|
---|
| 2 | ** hash.c - Hash class
|
---|
| 3 | **
|
---|
| 4 | ** See Copyright Notice in mruby.h
|
---|
| 5 | */
|
---|
| 6 |
|
---|
| 7 | #include "mruby.h"
|
---|
| 8 | #include "mruby/array.h"
|
---|
| 9 | #include "mruby/class.h"
|
---|
| 10 | #include "mruby/hash.h"
|
---|
| 11 | #include "mruby/khash.h"
|
---|
| 12 | #include "mruby/string.h"
|
---|
| 13 | #include "mruby/variable.h"
|
---|
| 14 |
|
---|
| 15 | /* a function to get hash value of a float number */
|
---|
| 16 | mrb_int mrb_float_id(mrb_float f);
|
---|
| 17 |
|
---|
| 18 | static inline khint_t
|
---|
| 19 | mrb_hash_ht_hash_func(mrb_state *mrb, mrb_value key)
|
---|
| 20 | {
|
---|
| 21 | enum mrb_vtype t = mrb_type(key);
|
---|
| 22 | mrb_value hv;
|
---|
| 23 | const char *p;
|
---|
| 24 | mrb_int i, len;
|
---|
| 25 | khint_t h;
|
---|
| 26 |
|
---|
| 27 | switch (t) {
|
---|
| 28 | case MRB_TT_STRING:
|
---|
| 29 | p = RSTRING_PTR(key);
|
---|
| 30 | len = RSTRING_LEN(key);
|
---|
| 31 | h = 0;
|
---|
| 32 | for (i=0; i<len; i++) {
|
---|
| 33 | h = (h << 5) - h + *p++;
|
---|
| 34 | }
|
---|
| 35 | return h;
|
---|
| 36 |
|
---|
| 37 | case MRB_TT_SYMBOL:
|
---|
| 38 | h = (khint_t)mrb_symbol(key);
|
---|
| 39 | return kh_int_hash_func(mrb, h);
|
---|
| 40 |
|
---|
| 41 | case MRB_TT_FIXNUM:
|
---|
| 42 | h = (khint_t)mrb_float_id((mrb_float)mrb_fixnum(key));
|
---|
| 43 | return kh_int_hash_func(mrb, h);
|
---|
| 44 |
|
---|
| 45 | case MRB_TT_FLOAT:
|
---|
| 46 | h = (khint_t)mrb_float_id(mrb_float(key));
|
---|
| 47 | return kh_int_hash_func(mrb, h);
|
---|
| 48 |
|
---|
| 49 | default:
|
---|
| 50 | hv = mrb_funcall(mrb, key, "hash", 0);
|
---|
| 51 | h = (khint_t)t ^ mrb_fixnum(hv);
|
---|
| 52 | return kh_int_hash_func(mrb, h);
|
---|
| 53 | }
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | static inline khint_t
|
---|
| 57 | mrb_hash_ht_hash_equal(mrb_state *mrb, mrb_value a, mrb_value b)
|
---|
| 58 | {
|
---|
| 59 | enum mrb_vtype t = mrb_type(a);
|
---|
| 60 |
|
---|
| 61 | switch (t) {
|
---|
| 62 | case MRB_TT_STRING:
|
---|
| 63 | return mrb_str_equal(mrb, a, b);
|
---|
| 64 |
|
---|
| 65 | case MRB_TT_SYMBOL:
|
---|
| 66 | if (mrb_type(b) != MRB_TT_SYMBOL) return FALSE;
|
---|
| 67 | return mrb_symbol(a) == mrb_symbol(b);
|
---|
| 68 |
|
---|
| 69 | case MRB_TT_FIXNUM:
|
---|
| 70 | switch (mrb_type(b)) {
|
---|
| 71 | case MRB_TT_FIXNUM:
|
---|
| 72 | return mrb_fixnum(a) == mrb_fixnum(b);
|
---|
| 73 | case MRB_TT_FLOAT:
|
---|
| 74 | return (mrb_float)mrb_fixnum(a) == mrb_float(b);
|
---|
| 75 | default:
|
---|
| 76 | return FALSE;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | case MRB_TT_FLOAT:
|
---|
| 80 | switch (mrb_type(b)) {
|
---|
| 81 | case MRB_TT_FIXNUM:
|
---|
| 82 | return mrb_float(a) == (mrb_float)mrb_fixnum(b);
|
---|
| 83 | case MRB_TT_FLOAT:
|
---|
| 84 | return mrb_float(a) == mrb_float(b);
|
---|
| 85 | default:
|
---|
| 86 | return FALSE;
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | default:
|
---|
| 90 | return mrb_eql(mrb, a, b);
|
---|
| 91 | }
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | typedef struct {
|
---|
| 95 | mrb_value v;
|
---|
| 96 | mrb_int n;
|
---|
| 97 | } mrb_hash_value;
|
---|
| 98 |
|
---|
| 99 | KHASH_DECLARE(ht, mrb_value, mrb_hash_value, TRUE)
|
---|
| 100 | KHASH_DEFINE (ht, mrb_value, mrb_hash_value, TRUE, mrb_hash_ht_hash_func, mrb_hash_ht_hash_equal)
|
---|
| 101 |
|
---|
| 102 | static void mrb_hash_modify(mrb_state *mrb, mrb_value hash);
|
---|
| 103 |
|
---|
| 104 | static inline mrb_value
|
---|
| 105 | mrb_hash_ht_key(mrb_state *mrb, mrb_value key)
|
---|
| 106 | {
|
---|
| 107 | if (mrb_string_p(key) && !RSTR_FROZEN_P(mrb_str_ptr(key))) {
|
---|
| 108 | key = mrb_str_dup(mrb, key);
|
---|
| 109 | RSTR_SET_FROZEN_FLAG(mrb_str_ptr(key));
|
---|
| 110 | }
|
---|
| 111 | return key;
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | #define KEY(key) mrb_hash_ht_key(mrb, key)
|
---|
| 115 |
|
---|
| 116 | void
|
---|
| 117 | mrb_gc_mark_hash(mrb_state *mrb, struct RHash *hash)
|
---|
| 118 | {
|
---|
| 119 | khiter_t k;
|
---|
| 120 | khash_t(ht) *h = hash->ht;
|
---|
| 121 |
|
---|
| 122 | if (!h) return;
|
---|
| 123 | for (k = kh_begin(h); k != kh_end(h); k++) {
|
---|
| 124 | if (kh_exist(h, k)) {
|
---|
| 125 | mrb_value key = kh_key(h, k);
|
---|
| 126 | mrb_value val = kh_value(h, k).v;
|
---|
| 127 |
|
---|
| 128 | mrb_gc_mark_value(mrb, key);
|
---|
| 129 | mrb_gc_mark_value(mrb, val);
|
---|
| 130 | }
|
---|
| 131 | }
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | size_t
|
---|
| 135 | mrb_gc_mark_hash_size(mrb_state *mrb, struct RHash *hash)
|
---|
| 136 | {
|
---|
| 137 | if (!hash->ht) return 0;
|
---|
| 138 | return kh_size(hash->ht)*2;
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | void
|
---|
| 142 | mrb_gc_free_hash(mrb_state *mrb, struct RHash *hash)
|
---|
| 143 | {
|
---|
| 144 | if (hash->ht) kh_destroy(ht, mrb, hash->ht);
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 |
|
---|
| 148 | MRB_API mrb_value
|
---|
| 149 | mrb_hash_new_capa(mrb_state *mrb, int capa)
|
---|
| 150 | {
|
---|
| 151 | struct RHash *h;
|
---|
| 152 |
|
---|
| 153 | h = (struct RHash*)mrb_obj_alloc(mrb, MRB_TT_HASH, mrb->hash_class);
|
---|
| 154 | h->ht = kh_init(ht, mrb);
|
---|
| 155 | if (capa > 0) {
|
---|
| 156 | kh_resize(ht, mrb, h->ht, capa);
|
---|
| 157 | }
|
---|
| 158 | h->iv = 0;
|
---|
| 159 | return mrb_obj_value(h);
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | MRB_API mrb_value
|
---|
| 163 | mrb_hash_new(mrb_state *mrb)
|
---|
| 164 | {
|
---|
| 165 | return mrb_hash_new_capa(mrb, 0);
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | MRB_API mrb_value
|
---|
| 169 | mrb_hash_get(mrb_state *mrb, mrb_value hash, mrb_value key)
|
---|
| 170 | {
|
---|
| 171 | khash_t(ht) *h = RHASH_TBL(hash);
|
---|
| 172 | khiter_t k;
|
---|
| 173 |
|
---|
| 174 | if (h) {
|
---|
| 175 | k = kh_get(ht, mrb, h, key);
|
---|
| 176 | if (k != kh_end(h))
|
---|
| 177 | return kh_value(h, k).v;
|
---|
| 178 | }
|
---|
| 179 |
|
---|
| 180 | /* not found */
|
---|
| 181 | if (MRB_RHASH_PROCDEFAULT_P(hash)) {
|
---|
| 182 | return mrb_funcall(mrb, RHASH_PROCDEFAULT(hash), "call", 2, hash, key);
|
---|
| 183 | }
|
---|
| 184 | return RHASH_IFNONE(hash);
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | MRB_API mrb_value
|
---|
| 188 | mrb_hash_fetch(mrb_state *mrb, mrb_value hash, mrb_value key, mrb_value def)
|
---|
| 189 | {
|
---|
| 190 | khash_t(ht) *h = RHASH_TBL(hash);
|
---|
| 191 | khiter_t k;
|
---|
| 192 |
|
---|
| 193 | if (h) {
|
---|
| 194 | k = kh_get(ht, mrb, h, key);
|
---|
| 195 | if (k != kh_end(h))
|
---|
| 196 | return kh_value(h, k).v;
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 | /* not found */
|
---|
| 200 | return def;
|
---|
| 201 | }
|
---|
| 202 |
|
---|
| 203 | MRB_API void
|
---|
| 204 | mrb_hash_set(mrb_state *mrb, mrb_value hash, mrb_value key, mrb_value val)
|
---|
| 205 | {
|
---|
| 206 | khash_t(ht) *h;
|
---|
| 207 | khiter_t k;
|
---|
| 208 | int r;
|
---|
| 209 |
|
---|
| 210 | mrb_hash_modify(mrb, hash);
|
---|
| 211 | h = RHASH_TBL(hash);
|
---|
| 212 |
|
---|
| 213 | if (!h) h = RHASH_TBL(hash) = kh_init(ht, mrb);
|
---|
| 214 | k = kh_put2(ht, mrb, h, key, &r);
|
---|
| 215 | kh_value(h, k).v = val;
|
---|
| 216 |
|
---|
| 217 | if (r != 0) {
|
---|
| 218 | /* expand */
|
---|
| 219 | int ai = mrb_gc_arena_save(mrb);
|
---|
| 220 | key = kh_key(h, k) = KEY(key);
|
---|
| 221 | mrb_gc_arena_restore(mrb, ai);
|
---|
| 222 | kh_value(h, k).n = kh_size(h)-1;
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 | mrb_field_write_barrier_value(mrb, (struct RBasic*)RHASH(hash), key);
|
---|
| 226 | mrb_field_write_barrier_value(mrb, (struct RBasic*)RHASH(hash), val);
|
---|
| 227 | return;
|
---|
| 228 | }
|
---|
| 229 |
|
---|
| 230 | static mrb_value
|
---|
| 231 | mrb_hash_dup(mrb_state *mrb, mrb_value hash)
|
---|
| 232 | {
|
---|
| 233 | struct RHash* ret;
|
---|
| 234 | khash_t(ht) *h, *ret_h;
|
---|
| 235 | khiter_t k, ret_k;
|
---|
| 236 |
|
---|
| 237 | h = RHASH_TBL(hash);
|
---|
| 238 | ret = (struct RHash*)mrb_obj_alloc(mrb, MRB_TT_HASH, mrb->hash_class);
|
---|
| 239 | ret->ht = kh_init(ht, mrb);
|
---|
| 240 |
|
---|
| 241 | if (kh_size(h) > 0) {
|
---|
| 242 | ret_h = ret->ht;
|
---|
| 243 |
|
---|
| 244 | for (k = kh_begin(h); k != kh_end(h); k++) {
|
---|
| 245 | if (kh_exist(h, k)) {
|
---|
| 246 | int ai = mrb_gc_arena_save(mrb);
|
---|
| 247 | ret_k = kh_put(ht, mrb, ret_h, KEY(kh_key(h, k)));
|
---|
| 248 | mrb_gc_arena_restore(mrb, ai);
|
---|
| 249 | kh_val(ret_h, ret_k) = kh_val(h, k);
|
---|
| 250 | }
|
---|
| 251 | }
|
---|
| 252 | }
|
---|
| 253 |
|
---|
| 254 | return mrb_obj_value(ret);
|
---|
| 255 | }
|
---|
| 256 |
|
---|
| 257 | MRB_API mrb_value
|
---|
| 258 | mrb_check_hash_type(mrb_state *mrb, mrb_value hash)
|
---|
| 259 | {
|
---|
| 260 | return mrb_check_convert_type(mrb, hash, MRB_TT_HASH, "Hash", "to_hash");
|
---|
| 261 | }
|
---|
| 262 |
|
---|
| 263 | MRB_API khash_t(ht)*
|
---|
| 264 | mrb_hash_tbl(mrb_state *mrb, mrb_value hash)
|
---|
| 265 | {
|
---|
| 266 | khash_t(ht) *h = RHASH_TBL(hash);
|
---|
| 267 |
|
---|
| 268 | if (!h) {
|
---|
| 269 | return RHASH_TBL(hash) = kh_init(ht, mrb);
|
---|
| 270 | }
|
---|
| 271 | return h;
|
---|
| 272 | }
|
---|
| 273 |
|
---|
| 274 | static void
|
---|
| 275 | mrb_hash_modify(mrb_state *mrb, mrb_value hash)
|
---|
| 276 | {
|
---|
| 277 | mrb_hash_tbl(mrb, hash);
|
---|
| 278 | }
|
---|
| 279 |
|
---|
| 280 | /* 15.2.13.4.16 */
|
---|
| 281 | /*
|
---|
| 282 | * call-seq:
|
---|
| 283 | * Hash.new -> new_hash
|
---|
| 284 | * Hash.new(obj) -> new_hash
|
---|
| 285 | * Hash.new {|hash, key| block } -> new_hash
|
---|
| 286 | *
|
---|
| 287 | * Returns a new, empty hash. If this hash is subsequently accessed by
|
---|
| 288 | * a key that doesn't correspond to a hash entry, the value returned
|
---|
| 289 | * depends on the style of <code>new</code> used to create the hash. In
|
---|
| 290 | * the first form, the access returns <code>nil</code>. If
|
---|
| 291 | * <i>obj</i> is specified, this single object will be used for
|
---|
| 292 | * all <em>default values</em>. If a block is specified, it will be
|
---|
| 293 | * called with the hash object and the key, and should return the
|
---|
| 294 | * default value. It is the block's responsibility to store the value
|
---|
| 295 | * in the hash if required.
|
---|
| 296 | *
|
---|
| 297 | * h = Hash.new("Go Fish")
|
---|
| 298 | * h["a"] = 100
|
---|
| 299 | * h["b"] = 200
|
---|
| 300 | * h["a"] #=> 100
|
---|
| 301 | * h["c"] #=> "Go Fish"
|
---|
| 302 | * # The following alters the single default object
|
---|
| 303 | * h["c"].upcase! #=> "GO FISH"
|
---|
| 304 | * h["d"] #=> "GO FISH"
|
---|
| 305 | * h.keys #=> ["a", "b"]
|
---|
| 306 | *
|
---|
| 307 | * # While this creates a new default object each time
|
---|
| 308 | * h = Hash.new { |hash, key| hash[key] = "Go Fish: #{key}" }
|
---|
| 309 | * h["c"] #=> "Go Fish: c"
|
---|
| 310 | * h["c"].upcase! #=> "GO FISH: C"
|
---|
| 311 | * h["d"] #=> "Go Fish: d"
|
---|
| 312 | * h.keys #=> ["c", "d"]
|
---|
| 313 | *
|
---|
| 314 | */
|
---|
| 315 |
|
---|
| 316 | static mrb_value
|
---|
| 317 | mrb_hash_init(mrb_state *mrb, mrb_value hash)
|
---|
| 318 | {
|
---|
| 319 | mrb_value block, ifnone;
|
---|
| 320 | mrb_bool ifnone_p;
|
---|
| 321 |
|
---|
| 322 | ifnone = mrb_nil_value();
|
---|
| 323 | mrb_get_args(mrb, "&|o?", &block, &ifnone, &ifnone_p);
|
---|
| 324 | mrb_hash_modify(mrb, hash);
|
---|
| 325 | if (!mrb_nil_p(block)) {
|
---|
| 326 | if (ifnone_p) {
|
---|
| 327 | mrb_raise(mrb, E_ARGUMENT_ERROR, "wrong number of arguments");
|
---|
| 328 | }
|
---|
| 329 | RHASH(hash)->flags |= MRB_HASH_PROC_DEFAULT;
|
---|
| 330 | ifnone = block;
|
---|
| 331 | }
|
---|
| 332 | mrb_iv_set(mrb, hash, mrb_intern_lit(mrb, "ifnone"), ifnone);
|
---|
| 333 | return hash;
|
---|
| 334 | }
|
---|
| 335 |
|
---|
| 336 | /* 15.2.13.4.2 */
|
---|
| 337 | /*
|
---|
| 338 | * call-seq:
|
---|
| 339 | * hsh[key] -> value
|
---|
| 340 | *
|
---|
| 341 | * Element Reference---Retrieves the <i>value</i> object corresponding
|
---|
| 342 | * to the <i>key</i> object. If not found, returns the default value (see
|
---|
| 343 | * <code>Hash::new</code> for details).
|
---|
| 344 | *
|
---|
| 345 | * h = { "a" => 100, "b" => 200 }
|
---|
| 346 | * h["a"] #=> 100
|
---|
| 347 | * h["c"] #=> nil
|
---|
| 348 | *
|
---|
| 349 | */
|
---|
| 350 | static mrb_value
|
---|
| 351 | mrb_hash_aget(mrb_state *mrb, mrb_value self)
|
---|
| 352 | {
|
---|
| 353 | mrb_value key;
|
---|
| 354 |
|
---|
| 355 | mrb_get_args(mrb, "o", &key);
|
---|
| 356 | return mrb_hash_get(mrb, self, key);
|
---|
| 357 | }
|
---|
| 358 |
|
---|
| 359 | /* 15.2.13.4.5 */
|
---|
| 360 | /*
|
---|
| 361 | * call-seq:
|
---|
| 362 | * hsh.default(key=nil) -> obj
|
---|
| 363 | *
|
---|
| 364 | * Returns the default value, the value that would be returned by
|
---|
| 365 | * <i>hsh</i>[<i>key</i>] if <i>key</i> did not exist in <i>hsh</i>.
|
---|
| 366 | * See also <code>Hash::new</code> and <code>Hash#default=</code>.
|
---|
| 367 | *
|
---|
| 368 | * h = Hash.new #=> {}
|
---|
| 369 | * h.default #=> nil
|
---|
| 370 | * h.default(2) #=> nil
|
---|
| 371 | *
|
---|
| 372 | * h = Hash.new("cat") #=> {}
|
---|
| 373 | * h.default #=> "cat"
|
---|
| 374 | * h.default(2) #=> "cat"
|
---|
| 375 | *
|
---|
| 376 | * h = Hash.new {|h,k| h[k] = k.to_i*10} #=> {}
|
---|
| 377 | * h.default #=> nil
|
---|
| 378 | * h.default(2) #=> 20
|
---|
| 379 | */
|
---|
| 380 |
|
---|
| 381 | static mrb_value
|
---|
| 382 | mrb_hash_default(mrb_state *mrb, mrb_value hash)
|
---|
| 383 | {
|
---|
| 384 | mrb_value key;
|
---|
| 385 | mrb_bool given;
|
---|
| 386 |
|
---|
| 387 | mrb_get_args(mrb, "|o?", &key, &given);
|
---|
| 388 | if (MRB_RHASH_PROCDEFAULT_P(hash)) {
|
---|
| 389 | if (!given) return mrb_nil_value();
|
---|
| 390 | return mrb_funcall(mrb, RHASH_PROCDEFAULT(hash), "call", 2, hash, key);
|
---|
| 391 | }
|
---|
| 392 | else {
|
---|
| 393 | return RHASH_IFNONE(hash);
|
---|
| 394 | }
|
---|
| 395 | }
|
---|
| 396 |
|
---|
| 397 | /* 15.2.13.4.6 */
|
---|
| 398 | /*
|
---|
| 399 | * call-seq:
|
---|
| 400 | * hsh.default = obj -> obj
|
---|
| 401 | *
|
---|
| 402 | * Sets the default value, the value returned for a key that does not
|
---|
| 403 | * exist in the hash. It is not possible to set the default to a
|
---|
| 404 | * <code>Proc</code> that will be executed on each key lookup.
|
---|
| 405 | *
|
---|
| 406 | * h = { "a" => 100, "b" => 200 }
|
---|
| 407 | * h.default = "Go fish"
|
---|
| 408 | * h["a"] #=> 100
|
---|
| 409 | * h["z"] #=> "Go fish"
|
---|
| 410 | * # This doesn't do what you might hope...
|
---|
| 411 | * h.default = proc do |hash, key|
|
---|
| 412 | * hash[key] = key + key
|
---|
| 413 | * end
|
---|
| 414 | * h[2] #=> #<Proc:0x401b3948@-:6>
|
---|
| 415 | * h["cat"] #=> #<Proc:0x401b3948@-:6>
|
---|
| 416 | */
|
---|
| 417 |
|
---|
| 418 | static mrb_value
|
---|
| 419 | mrb_hash_set_default(mrb_state *mrb, mrb_value hash)
|
---|
| 420 | {
|
---|
| 421 | mrb_value ifnone;
|
---|
| 422 |
|
---|
| 423 | mrb_get_args(mrb, "o", &ifnone);
|
---|
| 424 | mrb_hash_modify(mrb, hash);
|
---|
| 425 | mrb_iv_set(mrb, hash, mrb_intern_lit(mrb, "ifnone"), ifnone);
|
---|
| 426 | RHASH(hash)->flags &= ~(MRB_HASH_PROC_DEFAULT);
|
---|
| 427 |
|
---|
| 428 | return ifnone;
|
---|
| 429 | }
|
---|
| 430 |
|
---|
| 431 | /* 15.2.13.4.7 */
|
---|
| 432 | /*
|
---|
| 433 | * call-seq:
|
---|
| 434 | * hsh.default_proc -> anObject
|
---|
| 435 | *
|
---|
| 436 | * If <code>Hash::new</code> was invoked with a block, return that
|
---|
| 437 | * block, otherwise return <code>nil</code>.
|
---|
| 438 | *
|
---|
| 439 | * h = Hash.new {|h,k| h[k] = k*k } #=> {}
|
---|
| 440 | * p = h.default_proc #=> #<Proc:0x401b3d08@-:1>
|
---|
| 441 | * a = [] #=> []
|
---|
| 442 | * p.call(a, 2)
|
---|
| 443 | * a #=> [nil, nil, 4]
|
---|
| 444 | */
|
---|
| 445 |
|
---|
| 446 |
|
---|
| 447 | static mrb_value
|
---|
| 448 | mrb_hash_default_proc(mrb_state *mrb, mrb_value hash)
|
---|
| 449 | {
|
---|
| 450 | if (MRB_RHASH_PROCDEFAULT_P(hash)) {
|
---|
| 451 | return RHASH_PROCDEFAULT(hash);
|
---|
| 452 | }
|
---|
| 453 | return mrb_nil_value();
|
---|
| 454 | }
|
---|
| 455 |
|
---|
| 456 | /*
|
---|
| 457 | * call-seq:
|
---|
| 458 | * hsh.default_proc = proc_obj -> proc_obj
|
---|
| 459 | *
|
---|
| 460 | * Sets the default proc to be executed on each key lookup.
|
---|
| 461 | *
|
---|
| 462 | * h.default_proc = proc do |hash, key|
|
---|
| 463 | * hash[key] = key + key
|
---|
| 464 | * end
|
---|
| 465 | * h[2] #=> 4
|
---|
| 466 | * h["cat"] #=> "catcat"
|
---|
| 467 | */
|
---|
| 468 |
|
---|
| 469 | static mrb_value
|
---|
| 470 | mrb_hash_set_default_proc(mrb_state *mrb, mrb_value hash)
|
---|
| 471 | {
|
---|
| 472 | mrb_value ifnone;
|
---|
| 473 |
|
---|
| 474 | mrb_get_args(mrb, "o", &ifnone);
|
---|
| 475 | mrb_hash_modify(mrb, hash);
|
---|
| 476 | mrb_iv_set(mrb, hash, mrb_intern_lit(mrb, "ifnone"), ifnone);
|
---|
| 477 | RHASH(hash)->flags |= MRB_HASH_PROC_DEFAULT;
|
---|
| 478 |
|
---|
| 479 | return ifnone;
|
---|
| 480 | }
|
---|
| 481 |
|
---|
| 482 | MRB_API mrb_value
|
---|
| 483 | mrb_hash_delete_key(mrb_state *mrb, mrb_value hash, mrb_value key)
|
---|
| 484 | {
|
---|
| 485 | khash_t(ht) *h = RHASH_TBL(hash);
|
---|
| 486 | khiter_t k;
|
---|
| 487 | mrb_value delVal;
|
---|
| 488 | mrb_int n;
|
---|
| 489 |
|
---|
| 490 | if (h) {
|
---|
| 491 | k = kh_get(ht, mrb, h, key);
|
---|
| 492 | if (k != kh_end(h)) {
|
---|
| 493 | delVal = kh_value(h, k).v;
|
---|
| 494 | n = kh_value(h, k).n;
|
---|
| 495 | kh_del(ht, mrb, h, k);
|
---|
| 496 | for (k = kh_begin(h); k != kh_end(h); k++) {
|
---|
| 497 | if (!kh_exist(h, k)) continue;
|
---|
| 498 | if (kh_value(h, k).n > n) kh_value(h, k).n--;
|
---|
| 499 | }
|
---|
| 500 | return delVal;
|
---|
| 501 | }
|
---|
| 502 | }
|
---|
| 503 |
|
---|
| 504 | /* not found */
|
---|
| 505 | return mrb_nil_value();
|
---|
| 506 | }
|
---|
| 507 |
|
---|
| 508 | /* 15.2.13.4.8 */
|
---|
| 509 | /*
|
---|
| 510 | * call-seq:
|
---|
| 511 | * hsh.delete(key) -> value
|
---|
| 512 | * hsh.delete(key) {| key | block } -> value
|
---|
| 513 | *
|
---|
| 514 | * Deletes and returns a key-value pair from <i>hsh</i> whose key is
|
---|
| 515 | * equal to <i>key</i>. If the key is not found, returns the
|
---|
| 516 | * <em>default value</em>. If the optional code block is given and the
|
---|
| 517 | * key is not found, pass in the key and return the result of
|
---|
| 518 | * <i>block</i>.
|
---|
| 519 | *
|
---|
| 520 | * h = { "a" => 100, "b" => 200 }
|
---|
| 521 | * h.delete("a") #=> 100
|
---|
| 522 | * h.delete("z") #=> nil
|
---|
| 523 | * h.delete("z") { |el| "#{el} not found" } #=> "z not found"
|
---|
| 524 | *
|
---|
| 525 | */
|
---|
| 526 | static mrb_value
|
---|
| 527 | mrb_hash_delete(mrb_state *mrb, mrb_value self)
|
---|
| 528 | {
|
---|
| 529 | mrb_value key;
|
---|
| 530 |
|
---|
| 531 | mrb_get_args(mrb, "o", &key);
|
---|
| 532 | return mrb_hash_delete_key(mrb, self, key);
|
---|
| 533 | }
|
---|
| 534 |
|
---|
| 535 | /* 15.2.13.4.24 */
|
---|
| 536 | /*
|
---|
| 537 | * call-seq:
|
---|
| 538 | * hsh.shift -> anArray or obj
|
---|
| 539 | *
|
---|
| 540 | * Removes a key-value pair from <i>hsh</i> and returns it as the
|
---|
| 541 | * two-item array <code>[</code> <i>key, value</i> <code>]</code>, or
|
---|
| 542 | * the hash's default value if the hash is empty.
|
---|
| 543 | *
|
---|
| 544 | * h = { 1 => "a", 2 => "b", 3 => "c" }
|
---|
| 545 | * h.shift #=> [1, "a"]
|
---|
| 546 | * h #=> {2=>"b", 3=>"c"}
|
---|
| 547 | */
|
---|
| 548 |
|
---|
| 549 | static mrb_value
|
---|
| 550 | mrb_hash_shift(mrb_state *mrb, mrb_value hash)
|
---|
| 551 | {
|
---|
| 552 | khash_t(ht) *h = RHASH_TBL(hash);
|
---|
| 553 | khiter_t k;
|
---|
| 554 | mrb_value delKey, delVal;
|
---|
| 555 |
|
---|
| 556 | mrb_hash_modify(mrb, hash);
|
---|
| 557 | if (h && kh_size(h) > 0) {
|
---|
| 558 | for (k = kh_begin(h); k != kh_end(h); k++) {
|
---|
| 559 | if (!kh_exist(h, k)) continue;
|
---|
| 560 |
|
---|
| 561 | delKey = kh_key(h, k);
|
---|
| 562 | mrb_gc_protect(mrb, delKey);
|
---|
| 563 | delVal = mrb_hash_delete_key(mrb, hash, delKey);
|
---|
| 564 | mrb_gc_protect(mrb, delVal);
|
---|
| 565 |
|
---|
| 566 | return mrb_assoc_new(mrb, delKey, delVal);
|
---|
| 567 | }
|
---|
| 568 | }
|
---|
| 569 |
|
---|
| 570 | if (MRB_RHASH_PROCDEFAULT_P(hash)) {
|
---|
| 571 | return mrb_funcall(mrb, RHASH_PROCDEFAULT(hash), "call", 2, hash, mrb_nil_value());
|
---|
| 572 | }
|
---|
| 573 | else {
|
---|
| 574 | return RHASH_IFNONE(hash);
|
---|
| 575 | }
|
---|
| 576 | }
|
---|
| 577 |
|
---|
| 578 | /* 15.2.13.4.4 */
|
---|
| 579 | /*
|
---|
| 580 | * call-seq:
|
---|
| 581 | * hsh.clear -> hsh
|
---|
| 582 | *
|
---|
| 583 | * Removes all key-value pairs from `hsh`.
|
---|
| 584 | *
|
---|
| 585 | * h = { "a" => 100, "b" => 200 } #=> {"a"=>100, "b"=>200}
|
---|
| 586 | * h.clear #=> {}
|
---|
| 587 | *
|
---|
| 588 | */
|
---|
| 589 |
|
---|
| 590 | MRB_API mrb_value
|
---|
| 591 | mrb_hash_clear(mrb_state *mrb, mrb_value hash)
|
---|
| 592 | {
|
---|
| 593 | khash_t(ht) *h = RHASH_TBL(hash);
|
---|
| 594 |
|
---|
| 595 | if (h) kh_clear(ht, mrb, h);
|
---|
| 596 | return hash;
|
---|
| 597 | }
|
---|
| 598 |
|
---|
| 599 | /* 15.2.13.4.3 */
|
---|
| 600 | /* 15.2.13.4.26 */
|
---|
| 601 | /*
|
---|
| 602 | * call-seq:
|
---|
| 603 | * hsh[key] = value -> value
|
---|
| 604 | * hsh.store(key, value) -> value
|
---|
| 605 | *
|
---|
| 606 | * Element Assignment---Associates the value given by
|
---|
| 607 | * <i>value</i> with the key given by <i>key</i>.
|
---|
| 608 | * <i>key</i> should not have its value changed while it is in
|
---|
| 609 | * use as a key (a <code>String</code> passed as a key will be
|
---|
| 610 | * duplicated and frozen).
|
---|
| 611 | *
|
---|
| 612 | * h = { "a" => 100, "b" => 200 }
|
---|
| 613 | * h["a"] = 9
|
---|
| 614 | * h["c"] = 4
|
---|
| 615 | * h #=> {"a"=>9, "b"=>200, "c"=>4}
|
---|
| 616 | *
|
---|
| 617 | */
|
---|
| 618 | static mrb_value
|
---|
| 619 | mrb_hash_aset(mrb_state *mrb, mrb_value self)
|
---|
| 620 | {
|
---|
| 621 | mrb_value key, val;
|
---|
| 622 |
|
---|
| 623 | mrb_get_args(mrb, "oo", &key, &val);
|
---|
| 624 | mrb_hash_set(mrb, self, key, val);
|
---|
| 625 | return val;
|
---|
| 626 | }
|
---|
| 627 |
|
---|
| 628 | /* 15.2.13.4.20 */
|
---|
| 629 | /* 15.2.13.4.25 */
|
---|
| 630 | /*
|
---|
| 631 | * call-seq:
|
---|
| 632 | * hsh.length -> fixnum
|
---|
| 633 | * hsh.size -> fixnum
|
---|
| 634 | *
|
---|
| 635 | * Returns the number of key-value pairs in the hash.
|
---|
| 636 | *
|
---|
| 637 | * h = { "d" => 100, "a" => 200, "v" => 300, "e" => 400 }
|
---|
| 638 | * h.length #=> 4
|
---|
| 639 | * h.delete("a") #=> 200
|
---|
| 640 | * h.length #=> 3
|
---|
| 641 | */
|
---|
| 642 | static mrb_value
|
---|
| 643 | mrb_hash_size_m(mrb_state *mrb, mrb_value self)
|
---|
| 644 | {
|
---|
| 645 | khash_t(ht) *h = RHASH_TBL(self);
|
---|
| 646 |
|
---|
| 647 | if (!h) return mrb_fixnum_value(0);
|
---|
| 648 | return mrb_fixnum_value(kh_size(h));
|
---|
| 649 | }
|
---|
| 650 |
|
---|
| 651 | /* 15.2.13.4.12 */
|
---|
| 652 | /*
|
---|
| 653 | * call-seq:
|
---|
| 654 | * hsh.empty? -> true or false
|
---|
| 655 | *
|
---|
| 656 | * Returns <code>true</code> if <i>hsh</i> contains no key-value pairs.
|
---|
| 657 | *
|
---|
| 658 | * {}.empty? #=> true
|
---|
| 659 | *
|
---|
| 660 | */
|
---|
| 661 | MRB_API mrb_value
|
---|
| 662 | mrb_hash_empty_p(mrb_state *mrb, mrb_value self)
|
---|
| 663 | {
|
---|
| 664 | khash_t(ht) *h = RHASH_TBL(self);
|
---|
| 665 |
|
---|
| 666 | if (h) return mrb_bool_value(kh_size(h) == 0);
|
---|
| 667 | return mrb_true_value();
|
---|
| 668 | }
|
---|
| 669 |
|
---|
| 670 | /* 15.2.13.4.29 (x)*/
|
---|
| 671 | /*
|
---|
| 672 | * call-seq:
|
---|
| 673 | * hsh.to_hash => hsh
|
---|
| 674 | *
|
---|
| 675 | * Returns +self+.
|
---|
| 676 | */
|
---|
| 677 |
|
---|
| 678 | static mrb_value
|
---|
| 679 | mrb_hash_to_hash(mrb_state *mrb, mrb_value hash)
|
---|
| 680 | {
|
---|
| 681 | return hash;
|
---|
| 682 | }
|
---|
| 683 |
|
---|
| 684 | /* 15.2.13.4.19 */
|
---|
| 685 | /*
|
---|
| 686 | * call-seq:
|
---|
| 687 | * hsh.keys -> array
|
---|
| 688 | *
|
---|
| 689 | * Returns a new array populated with the keys from this hash. See also
|
---|
| 690 | * <code>Hash#values</code>.
|
---|
| 691 | *
|
---|
| 692 | * h = { "a" => 100, "b" => 200, "c" => 300, "d" => 400 }
|
---|
| 693 | * h.keys #=> ["a", "b", "c", "d"]
|
---|
| 694 | *
|
---|
| 695 | */
|
---|
| 696 |
|
---|
| 697 | MRB_API mrb_value
|
---|
| 698 | mrb_hash_keys(mrb_state *mrb, mrb_value hash)
|
---|
| 699 | {
|
---|
| 700 | khash_t(ht) *h = RHASH_TBL(hash);
|
---|
| 701 | khiter_t k;
|
---|
| 702 | mrb_value ary;
|
---|
| 703 | mrb_value *p;
|
---|
| 704 |
|
---|
| 705 | if (!h || kh_size(h) == 0) return mrb_ary_new(mrb);
|
---|
| 706 | ary = mrb_ary_new_capa(mrb, kh_size(h));
|
---|
| 707 | mrb_ary_set(mrb, ary, kh_size(h)-1, mrb_nil_value());
|
---|
| 708 | p = mrb_ary_ptr(ary)->ptr;
|
---|
| 709 | for (k = kh_begin(h); k != kh_end(h); k++) {
|
---|
| 710 | if (kh_exist(h, k)) {
|
---|
| 711 | mrb_value kv = kh_key(h, k);
|
---|
| 712 | mrb_hash_value hv = kh_value(h, k);
|
---|
| 713 |
|
---|
| 714 | p[hv.n] = kv;
|
---|
| 715 | }
|
---|
| 716 | }
|
---|
| 717 | return ary;
|
---|
| 718 | }
|
---|
| 719 |
|
---|
| 720 | /* 15.2.13.4.28 */
|
---|
| 721 | /*
|
---|
| 722 | * call-seq:
|
---|
| 723 | * hsh.values -> array
|
---|
| 724 | *
|
---|
| 725 | * Returns a new array populated with the values from <i>hsh</i>. See
|
---|
| 726 | * also <code>Hash#keys</code>.
|
---|
| 727 | *
|
---|
| 728 | * h = { "a" => 100, "b" => 200, "c" => 300 }
|
---|
| 729 | * h.values #=> [100, 200, 300]
|
---|
| 730 | *
|
---|
| 731 | */
|
---|
| 732 |
|
---|
| 733 | static mrb_value
|
---|
| 734 | mrb_hash_values(mrb_state *mrb, mrb_value hash)
|
---|
| 735 | {
|
---|
| 736 | khash_t(ht) *h = RHASH_TBL(hash);
|
---|
| 737 | khiter_t k;
|
---|
| 738 | mrb_value ary;
|
---|
| 739 |
|
---|
| 740 | if (!h) return mrb_ary_new(mrb);
|
---|
| 741 | ary = mrb_ary_new_capa(mrb, kh_size(h));
|
---|
| 742 | for (k = kh_begin(h); k != kh_end(h); k++) {
|
---|
| 743 | if (kh_exist(h, k)) {
|
---|
| 744 | mrb_hash_value hv = kh_value(h, k);
|
---|
| 745 |
|
---|
| 746 | mrb_ary_set(mrb, ary, hv.n, hv.v);
|
---|
| 747 | }
|
---|
| 748 | }
|
---|
| 749 | return ary;
|
---|
| 750 | }
|
---|
| 751 |
|
---|
| 752 | /* 15.2.13.4.13 */
|
---|
| 753 | /* 15.2.13.4.15 */
|
---|
| 754 | /* 15.2.13.4.18 */
|
---|
| 755 | /* 15.2.13.4.21 */
|
---|
| 756 | /*
|
---|
| 757 | * call-seq:
|
---|
| 758 | * hsh.has_key?(key) -> true or false
|
---|
| 759 | * hsh.include?(key) -> true or false
|
---|
| 760 | * hsh.key?(key) -> true or false
|
---|
| 761 | * hsh.member?(key) -> true or false
|
---|
| 762 | *
|
---|
| 763 | * Returns <code>true</code> if the given key is present in <i>hsh</i>.
|
---|
| 764 | *
|
---|
| 765 | * h = { "a" => 100, "b" => 200 }
|
---|
| 766 | * h.has_key?("a") #=> true
|
---|
| 767 | * h.has_key?("z") #=> false
|
---|
| 768 | *
|
---|
| 769 | */
|
---|
| 770 |
|
---|
| 771 | static mrb_value
|
---|
| 772 | mrb_hash_has_key(mrb_state *mrb, mrb_value hash)
|
---|
| 773 | {
|
---|
| 774 | mrb_value key;
|
---|
| 775 | khash_t(ht) *h;
|
---|
| 776 | khiter_t k;
|
---|
| 777 |
|
---|
| 778 | mrb_get_args(mrb, "o", &key);
|
---|
| 779 |
|
---|
| 780 | h = RHASH_TBL(hash);
|
---|
| 781 | if (h) {
|
---|
| 782 | k = kh_get(ht, mrb, h, key);
|
---|
| 783 | return mrb_bool_value(k != kh_end(h));
|
---|
| 784 | }
|
---|
| 785 | return mrb_false_value();
|
---|
| 786 | }
|
---|
| 787 |
|
---|
| 788 | /* 15.2.13.4.14 */
|
---|
| 789 | /* 15.2.13.4.27 */
|
---|
| 790 | /*
|
---|
| 791 | * call-seq:
|
---|
| 792 | * hsh.has_value?(value) -> true or false
|
---|
| 793 | * hsh.value?(value) -> true or false
|
---|
| 794 | *
|
---|
| 795 | * Returns <code>true</code> if the given value is present for some key
|
---|
| 796 | * in <i>hsh</i>.
|
---|
| 797 | *
|
---|
| 798 | * h = { "a" => 100, "b" => 200 }
|
---|
| 799 | * h.has_value?(100) #=> true
|
---|
| 800 | * h.has_value?(999) #=> false
|
---|
| 801 | */
|
---|
| 802 |
|
---|
| 803 | static mrb_value
|
---|
| 804 | mrb_hash_has_value(mrb_state *mrb, mrb_value hash)
|
---|
| 805 | {
|
---|
| 806 | mrb_value val;
|
---|
| 807 | khash_t(ht) *h;
|
---|
| 808 | khiter_t k;
|
---|
| 809 |
|
---|
| 810 | mrb_get_args(mrb, "o", &val);
|
---|
| 811 | h = RHASH_TBL(hash);
|
---|
| 812 |
|
---|
| 813 | if (h) {
|
---|
| 814 | for (k = kh_begin(h); k != kh_end(h); k++) {
|
---|
| 815 | if (!kh_exist(h, k)) continue;
|
---|
| 816 |
|
---|
| 817 | if (mrb_equal(mrb, kh_value(h, k).v, val)) {
|
---|
| 818 | return mrb_true_value();
|
---|
| 819 | }
|
---|
| 820 | }
|
---|
| 821 | }
|
---|
| 822 | return mrb_false_value();
|
---|
| 823 | }
|
---|
| 824 |
|
---|
| 825 | void
|
---|
| 826 | mrb_init_hash(mrb_state *mrb)
|
---|
| 827 | {
|
---|
| 828 | struct RClass *h;
|
---|
| 829 |
|
---|
| 830 | mrb->hash_class = h = mrb_define_class(mrb, "Hash", mrb->object_class); /* 15.2.13 */
|
---|
| 831 | MRB_SET_INSTANCE_TT(h, MRB_TT_HASH);
|
---|
| 832 |
|
---|
| 833 | mrb_define_method(mrb, h, "[]", mrb_hash_aget, MRB_ARGS_REQ(1)); /* 15.2.13.4.2 */
|
---|
| 834 | mrb_define_method(mrb, h, "[]=", mrb_hash_aset, MRB_ARGS_REQ(2)); /* 15.2.13.4.3 */
|
---|
| 835 | mrb_define_method(mrb, h, "clear", mrb_hash_clear, MRB_ARGS_NONE()); /* 15.2.13.4.4 */
|
---|
| 836 | mrb_define_method(mrb, h, "default", mrb_hash_default, MRB_ARGS_ANY()); /* 15.2.13.4.5 */
|
---|
| 837 | mrb_define_method(mrb, h, "default=", mrb_hash_set_default, MRB_ARGS_REQ(1)); /* 15.2.13.4.6 */
|
---|
| 838 | mrb_define_method(mrb, h, "default_proc", mrb_hash_default_proc,MRB_ARGS_NONE()); /* 15.2.13.4.7 */
|
---|
| 839 | mrb_define_method(mrb, h, "default_proc=", mrb_hash_set_default_proc,MRB_ARGS_REQ(1)); /* 15.2.13.4.7 */
|
---|
| 840 | mrb_define_method(mrb, h, "__delete", mrb_hash_delete, MRB_ARGS_REQ(1)); /* core of 15.2.13.4.8 */
|
---|
| 841 | mrb_define_method(mrb, h, "empty?", mrb_hash_empty_p, MRB_ARGS_NONE()); /* 15.2.13.4.12 */
|
---|
| 842 | mrb_define_method(mrb, h, "has_key?", mrb_hash_has_key, MRB_ARGS_REQ(1)); /* 15.2.13.4.13 */
|
---|
| 843 | mrb_define_method(mrb, h, "has_value?", mrb_hash_has_value, MRB_ARGS_REQ(1)); /* 15.2.13.4.14 */
|
---|
| 844 | mrb_define_method(mrb, h, "include?", mrb_hash_has_key, MRB_ARGS_REQ(1)); /* 15.2.13.4.15 */
|
---|
| 845 | mrb_define_method(mrb, h, "initialize", mrb_hash_init, MRB_ARGS_OPT(1)); /* 15.2.13.4.16 */
|
---|
| 846 | mrb_define_method(mrb, h, "key?", mrb_hash_has_key, MRB_ARGS_REQ(1)); /* 15.2.13.4.18 */
|
---|
| 847 | mrb_define_method(mrb, h, "keys", mrb_hash_keys, MRB_ARGS_NONE()); /* 15.2.13.4.19 */
|
---|
| 848 | mrb_define_method(mrb, h, "length", mrb_hash_size_m, MRB_ARGS_NONE()); /* 15.2.13.4.20 */
|
---|
| 849 | mrb_define_method(mrb, h, "member?", mrb_hash_has_key, MRB_ARGS_REQ(1)); /* 15.2.13.4.21 */
|
---|
| 850 | mrb_define_method(mrb, h, "shift", mrb_hash_shift, MRB_ARGS_NONE()); /* 15.2.13.4.24 */
|
---|
| 851 | mrb_define_method(mrb, h, "dup", mrb_hash_dup, MRB_ARGS_NONE());
|
---|
| 852 | mrb_define_method(mrb, h, "size", mrb_hash_size_m, MRB_ARGS_NONE()); /* 15.2.13.4.25 */
|
---|
| 853 | mrb_define_method(mrb, h, "store", mrb_hash_aset, MRB_ARGS_REQ(2)); /* 15.2.13.4.26 */
|
---|
| 854 | mrb_define_method(mrb, h, "value?", mrb_hash_has_value, MRB_ARGS_REQ(1)); /* 15.2.13.4.27 */
|
---|
| 855 | mrb_define_method(mrb, h, "values", mrb_hash_values, MRB_ARGS_NONE()); /* 15.2.13.4.28 */
|
---|
| 856 |
|
---|
| 857 | mrb_define_method(mrb, h, "to_hash", mrb_hash_to_hash, MRB_ARGS_NONE()); /* 15.2.13.4.29 (x)*/
|
---|
| 858 | }
|
---|