Misc #15925 ยป sortedsetminmaxsum.patch
benchmark/set_min_max_sum.yml  

1 
prelude:  

2 
require 'set' 

3 
set = SortedSet.new((0..1000).sort{rand}) 

4 
benchmark: 

5 
min: set.min 

6 
max: set.max 

7 
minmax: set.minmax 

8 
sum: set.sum 
lib/set.rb  

738  738 
super 
739  739 
end 
740  740  
741 
def min 

742 
to_a.first 

743 
end 

744  
745 
def max 

746 
to_a.last 

747 
end 

748  
749 
def minmax 

750 
[min, max] 

751 
end 

752  
753 
def sum 

754 
to_a.sum 

755 
end 

756  
741  757 
def add(o) 
742  758 
o.respond_to?(:<=>) or raise ArgumentError, "value must respond to <=>" 
743  759 
@keys = nil 
test/test_set.rb  

799  799 
s = SortedSet[4,5,3,1,2] 
800  800  
801  801 
assert_equal([1,2,3,4,5], s.to_a) 
802 
assert_equal(1, s.min) 

803 
assert_equal(5, s.max) 

804 
assert_equal([1,5], s.minmax) 

805 
assert_equal(15, s.sum) 

802  806  
803  807 
prev = nil 
804  808 
s.each { o assert(prev < o) if prev; prev = o } 
805 
 