Project

General

Profile

patch4.diff

Glass_saga (Masaki Matsushita), 09/19/2017 05:41 AM

View differences:

hash.c
1336 1336
}
1337 1337

  
1338 1338
/*
1339
 *  call-seq:
1340
 *     hsh.slice! -> a_hash
1341
 *
1342
 *  Replaces the hash with only the given keys.
1343
 *  Returns a hash containing the removed key/value pairs.
1344
 *
1345
 *     h = { "a" => 100, "b" => 200, "c" => 300 }
1346
 *     h.slice!("a")  #=> {"b" => 200, "c" => 300}
1347
 *     h #=> {"a" => 100}
1348
 */
1349

  
1350
static VALUE
1351
rb_hash_slice_bang(int argc, VALUE *argv, VALUE hash)
1352
{
1353
    int i;
1354
    VALUE key, value, result;
1355

  
1356
    rb_hash_modify_check(hash);
1357
    if (RHASH_EMPTY_P(hash)) {
1358
	return rb_hash_new();
1359
    }
1360

  
1361
    result = rb_hash_dup(hash);
1362
    rb_hash_clear(hash);
1363

  
1364
    for (i = 0; i < argc; i++) {
1365
	key = argv[i];
1366
	value = rb_hash_delete_entry(result, key);
1367
	if (value != Qundef)
1368
	    rb_hash_aset(hash, key, value);
1369
    }
1370

  
1371
    return result;
1372
}
1373

  
1374
/*
1375
 *  call-seq:
1376
 *     hsh.slice -> a_hash
1377
 *
1378
 *  Slices a hash to include only the given keys.
1379
 *  Returns a hash containing the given keys.
1380
 *
1381
 *     h = { "a" => 100, "b" => 200, "c" => 300 }
1382
 *     h.slice("a")  #=> {"a" => 100}
1383
 */
1384

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

  
1391
    if (argc == 0 || RHASH_EMPTY_P(hash)) {
1392
	return rb_hash_new();
1393
    }
1394
    result = rb_hash_new_with_size(argc);
1395

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

  
1403
    return result;
1404
}
1405

  
1406
/*
1407
 *  call-seq:
1408
 *     hsh.except! -> a_hash
1409
 *
1410
 *  Removes the given keys from hash and returns it.
1411
 *
1412
 *     h = { "a" => 100, "b" => 200, "c" => 300 }
1413
 *     h.except!("a")  #=> {"b" => 200, "c" => 300}
1414
 *     h #=> {"b" => 200, "c" => 300}
1415
 */
1416

  
1417
static VALUE
1418
rb_hash_except_bang(int argc, VALUE *argv, VALUE hash)
1419
{
1420
    int i;
1421
    VALUE key;
1422

  
1423
    rb_hash_modify_check(hash);
1424

  
1425
    for (i = 0; i < argc; i++) {
1426
	key = argv[i];
1427
	rb_hash_delete_entry(hash, key);
1428
    }
1429

  
1430
    return hash;
1431
}
1432

  
1433
/*
1434
 *  call-seq:
1435
 *     hsh.except -> a_hash
1436
 *
1437
 *  Returns a hash that includes everything except given keys.
1438
 *
1439
 *     h = { "a" => 100, "b" => 200, "c" => 300 }
1440
 *     h.except("a")  #=> {"b" => 200, "c" => 300}
1441
 *     h #=> { "a" => 100, "b" => 200, "c" => 300 }
1442
 */
1443

  
1444
static VALUE
1445
rb_hash_except(int argc, VALUE *argv, VALUE hash)
1446
{
1447
    return rb_hash_except_bang(argc, argv, rb_hash_dup(hash));
1448
}
1449

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

  
1009
  def test_slice
1010
    h = @cls[1=>2,3=>4,5=>6]
1011
    assert_equal({1=>2, 3=>4}, h.slice(1, 3))
1012
    assert_equal({}, h.slice(7))
1013
    assert_equal({}, h.slice)
1014
    assert_equal({}, {}.slice)
1015
  end
1016

  
1017
  def test_slice!
1018
    h = @cls[1=>2,3=>4,5=>6]
1019
    assert_equal({5=>6}, h.slice!(1, 3))
1020
    assert_equal({1=>2,3=>4}, h)
1021
    assert_equal({1=>2,3=>4}, h.slice!)
1022
    assert_equal({}, h)
1023
    h = @cls[1=>2,3=>4,5=>6]
1024
    assert_equal({1=>2,3=>4,5=>6}, h.slice!(7))
1025
    assert_equal({}, h)
1026
    assert_equal({}, {}.slice!)
1027
  end
1028

  
1029
  def test_excpet
1030
    h = @cls[1=>2,3=>4,5=>6]
1031
    assert_equal({1=>2,3=>4,5=>6}, h.except(7))
1032
    assert_equal({5=>6}, h.except(1, 3))
1033
    assert_equal({}, {}.except)
1034
  end
1035

  
1036
  def test_except!
1037
    h = @cls[1=>2,3=>4,5=>6]
1038
    assert_equal({1=>2,3=>4,5=>6}, h.except!(7))
1039
    assert_equal({5=>6}, h.except!(1, 3))
1040
    assert_equal({5=>6}, h)
1041
    assert_equal({5=>6}, h.except!)
1042
    assert_equal({5=>6}, h)
1043
    assert_equal({}, {}.except!)
1044
  end
1045

  
1009 1046
  def test_clear2
1010 1047
    assert_equal({}, @cls[1=>2,3=>4,5=>6].clear)
1011 1048
    h = @cls[1=>2,3=>4,5=>6]