Misc #15925 ยป sorted-set-min-max-sum.patch
benchmark/set_min_max_sum.yml | ||
---|---|---|
prelude: |
|
||
require 'set'
|
||
set = SortedSet.new((0..1000).sort{rand})
|
||
benchmark:
|
||
min: set.min
|
||
max: set.max
|
||
minmax: set.minmax
|
||
sum: set.sum
|
lib/set.rb | ||
---|---|---|
super
|
||
end
|
||
def min
|
||
to_a.first
|
||
end
|
||
def max
|
||
to_a.last
|
||
end
|
||
def minmax
|
||
[min, max]
|
||
end
|
||
def sum
|
||
to_a.sum
|
||
end
|
||
def add(o)
|
||
o.respond_to?(:<=>) or raise ArgumentError, "value must respond to <=>"
|
||
@keys = nil
|
test/test_set.rb | ||
---|---|---|
s = SortedSet[4,5,3,1,2]
|
||
assert_equal([1,2,3,4,5], s.to_a)
|
||
assert_equal(1, s.min)
|
||
assert_equal(5, s.max)
|
||
assert_equal([1,5], s.minmax)
|
||
assert_equal(15, s.sum)
|
||
prev = nil
|
||
s.each { |o| assert(prev < o) if prev; prev = o }
|