Project

General

Profile

Feature #15725 ยป 0001-Add-Array-reverse_sort-reverse_sort_by-methods.patch

sikachu (Prem Sichanugrist), 03/23/2019 10:01 AM

View differences:

array.c
2822 2822
    return ary;
2823 2823
}
2824 2824

  
2825
/*
2826
 *  call-seq:
2827
 *     ary.reverse_sort                   -> new_ary
2828
 *     ary.reverse_sort {|a, b| block}    -> new_ary
2829
 *
2830
 *  Returns a new array created by sorting +self+, then reverse it in place.
2831
 *
2832
 *     ary = [ "d", "a", "e", "c", "b" ]
2833
 *     ary.reverse_sort                     #=> ["e", "d", "c", "b", "a"]
2834
 *     ary.reverse_sort {|a, b| b <=> a}    #=> ["a", "b", "c", "d", "e"]
2835
 *
2836
 *  The result is not guaranteed to be stable.  When the comparison of two
2837
 *  elements returns +0+, the order of the elements is unpredictable.
2838
 *
2839
 *  See also Array#sort.
2840
 */
2841
static VALUE
2842
rb_ary_reverse_sort(VALUE ary)
2843
{
2844
    return rb_ary_reverse_bang(rb_ary_sort(ary));
2845
}
2846

  
2847
/*
2848
 *  call-seq:
2849
 *     ary.reverse_sort!                   -> ary
2850
 *     ary.reverse_sort! {|a, b| block}    -> ary
2851
 *
2852
 *  Sorts +self+ and reverse it in place.
2853
 *
2854
 *     ary = [ "d", "a", "e", "c", "b" ]
2855
 *     ary.reverse_sort!                     #=> ["e", "d", "c", "b", "a"]
2856
 *     ary.reverse_sort! {|a, b| b <=> a}    #=> ["a", "b", "c", "d", "e"]
2857
 *
2858
 *  The result is not guaranteed to be stable.  When the comparison of two
2859
 *  elements returns +0+, the order of the elements is unpredictable.
2860
 *
2861
 *  See also Array#sort!
2862
 */
2863
static VALUE
2864
rb_ary_reverse_sort_bang(VALUE ary)
2865
{
2866
    return rb_ary_reverse_bang(rb_ary_sort_bang(ary));
2867
}
2868

  
2825 2869
static VALUE rb_ary_bsearch_index(VALUE ary);
2826 2870

  
2827 2871
/*
......
2983 3027
    return ary;
2984 3028
}
2985 3029

  
3030
/*
3031
 *  call-seq:
3032
 *     ary.reverse_sort_by {|obj| block}      -> ary
3033
 *     ary.reverse_sort_by                    -> Enumerator
3034
 *
3035
 *  Returns a new array containing +self+'s elements using a set of keys
3036
 *  generated by mapping the values in +self+ through the given block, then
3037
 *  reverse it in place.
3038
 *
3039
 *  The result is not guaranteed to be stable.  When two keys are equal,
3040
 *  the order of the corresponding elements is unpredictable.
3041
 *
3042
 *  If no block is given, an Enumerator is returned instead.
3043
 *
3044
 *  See also Array#sort_by
3045
 */
3046
static VALUE
3047
rb_ary_reverse_sort_by(VALUE ary)
3048
{
3049
    ary = rb_ary_dup(ary);
3050
    return rb_ary_reverse_bang(rb_ary_sort_by_bang(ary));
3051
}
3052

  
3053
/*
3054
 *  call-seq:
3055
 *     ary.reverse_sort_by! {|obj| block}      -> ary
3056
 *     ary.reverse_sort_by!                    -> Enumerator
3057
 *
3058
 *  Sorts +self+ in place using a set of keys generated by mapping the
3059
 *  values in +self+ through the given block, then reverse it in-place.
3060
 *
3061
 *  The result is not guaranteed to be stable.  When two keys are equal,
3062
 *  the order of the corresponding elements is unpredictable.
3063
 *
3064
 *  If no block is given, an Enumerator is returned instead.
3065
 *
3066
 *  See also Array#sort_by!
3067
 */
3068
static VALUE
3069
rb_ary_reverse_sort_by_bang(VALUE ary)
3070
{
3071
    return rb_ary_reverse_bang(rb_ary_sort_by_bang(ary));
3072
}
2986 3073

  
2987 3074
/*
2988 3075
 *  call-seq:
......
6836 6923
    rb_define_method(rb_cArray, "join", rb_ary_join_m, -1);
6837 6924
    rb_define_method(rb_cArray, "reverse", rb_ary_reverse_m, 0);
6838 6925
    rb_define_method(rb_cArray, "reverse!", rb_ary_reverse_bang, 0);
6926
    rb_define_method(rb_cArray, "reverse_sort", rb_ary_reverse_sort, 0);
6927
    rb_define_method(rb_cArray, "reverse_sort!", rb_ary_reverse_sort_bang, 0);
6928
    rb_define_method(rb_cArray, "reverse_sort_by", rb_ary_reverse_sort_by, 0);
6929
    rb_define_method(rb_cArray, "reverse_sort_by!", rb_ary_reverse_sort_by_bang, 0);
6839 6930
    rb_define_method(rb_cArray, "rotate", rb_ary_rotate_m, -1);
6840 6931
    rb_define_method(rb_cArray, "rotate!", rb_ary_rotate_bang, -1);
6841 6932
    rb_define_method(rb_cArray, "sort", rb_ary_sort, 0);
test/ruby/test_array.rb
1415 1415
    assert_equal(0, i)
1416 1416
  end
1417 1417

  
1418
  def test_reverse_sort
1419
    a = @cls[ 4, 1, 2, 3 ]
1420
    assert_equal(@cls[4, 3, 2, 1], a.reverse_sort)
1421
    assert_equal(@cls[4, 1, 2, 3], a)
1422

  
1423
    assert_equal(@cls[4, 3, 2, 1], a.reverse_sort { |x, y| y <=> x} )
1424
    assert_equal(@cls[4, 1, 2, 3], a)
1425

  
1426
    assert_equal(@cls[4, 3, 2, 1], a.reverse_sort { |x, y| (x - y) * (2**100) })
1427

  
1428
    a.fill(1)
1429
    assert_equal(@cls[1, 1, 1, 1], a.reverse_sort)
1430

  
1431
    assert_equal(@cls[], @cls[].reverse_sort)
1432
  end
1433

  
1434
  def test_reverse_sort!
1435
    a = @cls[ 4, 1, 2, 3 ]
1436
    assert_equal(@cls[4, 3, 2, 1], a.reverse_sort!)
1437
    assert_equal(@cls[4, 3, 2, 1], a)
1438

  
1439
    assert_equal(@cls[1, 2, 3, 4], a.reverse_sort! { |x, y| y <=> x} )
1440
    assert_equal(@cls[1, 2, 3, 4], a)
1441

  
1442
    a.fill(1)
1443
    assert_equal(@cls[1, 1, 1, 1], a.reverse_sort!)
1444

  
1445
    assert_equal(@cls[1], @cls[1].reverse_sort!)
1446
    assert_equal(@cls[], @cls[].reverse_sort!)
1447

  
1448
    a = @cls[4, 3, 2, 1]
1449
    a.reverse_sort! {|m, n| a.replace([9, 8, 7, 6]); m <=> n }
1450
    assert_equal([4, 3, 2, 1], a)
1451

  
1452
    a = @cls[4, 3, 2, 1]
1453
    a.reverse_sort! {|m, n| a.replace([9, 8, 7]); m <=> n }
1454
    assert_equal([4, 3, 2, 1], a)
1455
  end
1456

  
1457
  def test_reverse_sort_by
1458
    a = [1, 3, 5, 2, 4]
1459
    assert_equal([1, 2, 3, 4, 5], a.reverse_sort_by {|x| -x })
1460
    assert_equal([1, 3, 4, 2, 4], a)
1461
  end
1462

  
1463
  def test_reverse_sort_by!
1464
    a = [1, 3, 5, 2, 4]
1465
    a.reverse_sort_by! {|x| -x }
1466
    assert_equal([1, 2, 3, 4, 5], a)
1467
  end
1468

  
1418 1469
  def test_rindex
1419 1470
    a = @cls[ 'cat', 99, /a/, 99, [ 1, 2, 3] ]
1420 1471
    assert_equal(0, a.rindex('cat'))
1421
-