Project

General

Profile

patch3.diff

Glass_saga (Masaki Matsushita), 09/07/2017 10:09 AM

View differences:

hash.c
1335 1335
    return result;
1336 1336
}
1337 1337

  
1338
struct slice_i_arg {
1339
    int argc;
1340
    VALUE *argv;
1341
    VALUE result;
1342
};
1343

  
1344
static int
1345
slice_i(VALUE key, VALUE value, VALUE arg)
1346
{
1347
    int i;
1348
    struct slice_i_arg *p = (struct slice_i_arg *)arg;
1349
    VALUE key_to_slice;
1350

  
1351
    for (i = 0; i < p->argc; i++) {
1352
	key_to_slice = p->argv[i];
1353
	if (rb_equal(key, key_to_slice))
1354
	    return ST_CONTINUE;
1355
    }
1356

  
1357
    rb_hash_aset(p->result, key, value);
1358
    return ST_DELETE;
1359
}
1360

  
1361
static VALUE
1362
rb_hash_slice_bang(int argc, VALUE *argv, VALUE hash)
1363
{
1364
    st_index_t n;
1365
    VALUE result;
1366

  
1367
    rb_hash_modify(hash);
1368
    n = RHASH_SIZE(hash);
1369
    if (!n) return hash;
1370
    if (argc) {
1371
	struct slice_i_arg arg;
1372
	result = rb_hash_new();
1373
	arg.argc = argc;
1374
	arg.argv = argv;
1375
	arg.result = result;
1376
	rb_hash_foreach(hash, slice_i, (VALUE)&arg);
1377
    }
1378
    else {
1379
	result = rb_obj_dup(hash);
1380
	rb_hash_clear(hash);
1381
    }
1382

  
1383
    return result;
1384
}
1385

  
1386
static VALUE
1387
rb_hash_slice(int argc, VALUE *argv, VALUE hash)
1388
{
1389
    int i;
1390
    VALUE key, value, result = rb_hash_new();
1391

  
1392
    if (!argc || RHASH_EMPTY_P(hash)) return result;
1393

  
1394
    for (i = 0; i < argc; i++) {
1395
	key = argv[i];
1396
	value = rb_hash_lookup2(hash, key, Qundef);
1397
	if (value != Qundef)
1398
	    rb_hash_aset(result, key, value);
1399
    }
1400

  
1401
    return result;
1402
}
1403

  
1404
struct except_i_arg {
1405
    int argc;
1406
    VALUE *argv;
1407
};
1408

  
1409
static int
1410
except_i(VALUE key, VALUE value, VALUE arg)
1411
{
1412
    int i;
1413
    struct except_i_arg *p = (struct except_i_arg *)arg;
1414
    VALUE key_to_except;
1415

  
1416
    for (i = 0; i < p->argc; i++) {
1417
	key_to_except = p->argv[i];
1418
	if (rb_equal(key, key_to_except))
1419
	    return ST_DELETE;
1420
    }
1421

  
1422
    return ST_CONTINUE;
1423
}
1424

  
1425
static VALUE
1426
rb_hash_except_bang(int argc, VALUE *argv, VALUE hash)
1427
{
1428
    st_index_t n;
1429
    struct except_i_arg arg;
1430

  
1431
    rb_hash_modify(hash);
1432
    n = RHASH_SIZE(hash);
1433

  
1434
    if (n && argc) {
1435
	arg.argc = argc;
1436
	arg.argv = argv;
1437
	rb_hash_foreach(hash, except_i, (VALUE)&arg);
1438
    }
1439

  
1440
    return hash;
1441
}
1442

  
1443
static VALUE
1444
rb_hash_except(int argc, VALUE *argv, VALUE hash)
1445
{
1446
    VALUE result = rb_obj_dup(hash);
1447
    rb_hash_except_bang(argc, argv, result);
1448
    return result;
1449
}
1450

  
1338 1451
/*
1339 1452
 * call-seq:
1340 1453
 *   hsh.values_at(key, ...)   -> array
......
4596 4709
    rb_define_method(rb_cHash, "select!", rb_hash_select_bang, 0);
4597 4710
    rb_define_method(rb_cHash, "reject", rb_hash_reject, 0);
4598 4711
    rb_define_method(rb_cHash, "reject!", rb_hash_reject_bang, 0);
4712
    rb_define_method(rb_cHash,"slice", rb_hash_slice, -1);
4713
    rb_define_method(rb_cHash,"slice!", rb_hash_slice_bang, -1);
4714
    rb_define_method(rb_cHash,"except", rb_hash_except, -1);
4715
    rb_define_method(rb_cHash,"except!", rb_hash_except_bang, -1);
4599 4716
    rb_define_method(rb_cHash, "clear", rb_hash_clear, 0);
4600 4717
    rb_define_method(rb_cHash, "invert", rb_hash_invert, 0);
4601 4718
    rb_define_method(rb_cHash, "update", rb_hash_update, 1);
test/ruby/test_hash.rb
1002 1002
    assert_equal(nil, h.select!{true})
1003 1003
  end
1004 1004

  
1005
  def test_slice
1006
    h = {1=>2,3=>4,5=>6}
1007
    assert_equal({1=>2, 3=>4}, h.slice(1, 3))
1008
    assert_equal({}, h.slice)
1009
  end
1010

  
1011
  def test_slice!
1012
    h = {1=>2,3=>4,5=>6}
1013
    assert_equal({5=>6}, h.slice!(1, 3))
1014
    assert_equal({1=>2,3=>4}, h)
1015
    assert_equal({1=>2,3=>4}, h.slice!(7))
1016
    assert_equal({}, h)
1017
    assert_equal({}, {}.slice!)
1018
  end
1019

  
1020
  def test_except
1021
    h = {1=>2,3=>4,5=>6}
1022
    assert_equal({5=>6}, h.except(1, 3))
1023
    assert_equal(h, h.except)
1024
  end
1025

  
1026
  def test_except!
1027
    h = {1=>2,3=>4,5=>6}
1028
    assert_equal({5=>6}, h.except!(1, 3))
1029
    assert_equal({5=>6}, h)
1030
    assert_equal({5=>6}, h.except!(1,3))
1031
    assert_equal({5=>6}, h.except!)
1032
    assert_equal({5=>6}, h)
1033
  end
1034

  
1005 1035
  def test_clear2
1006 1036
    assert_equal({}, @cls[1=>2,3=>4,5=>6].clear)
1007 1037
    h = @cls[1=>2,3=>4,5=>6]