Core: Count, CountIf algorithms

git-svn-id: svn://ultimatepp.org/upp/trunk@10483 f0d560ea-af0d-0410-9eb7-867de7ffcac7
This commit is contained in:
cxl 2016-11-22 18:38:35 +00:00
parent 61f984116e
commit 9fd64ee55d
2 changed files with 34 additions and 0 deletions

View file

@ -50,6 +50,26 @@ ValueTypeOf<T> Sum(const T& c)
return Sum(c, (ValueTypeOf<T>)0);
}
template <class Range, class V>
int Count(const Range& r, const V& val)
{
int count = 0;
for(const auto& x : r)
if(x == val)
count++;
return count;
}
template <class Range, class Predicate>
int CountIf(const Range& r, Predicate p)
{
int count = 0;
for(const auto& x : r)
if(p(x))
count++;
return count;
}
template <class Range, class Pred>
int FindBest(const Range& r, const Pred& pred)
{

View file

@ -62,6 +62,20 @@ ename]_[_^Upp`:`:ValueTypeOf^ ValueTypeOf]<[*@4 Range]>_[* Sum]([@(0.0.255) cons
[s2;%% Same as Sum(c, 0).&]
[s3;%% &]
[s4; &]
[s5;:Upp`:`:Count`(const Range`&`,const V`&`): [@(0.0.255) template]_<[@(0.0.255) class]_
[*@4 Range], [@(0.0.255) class]_[*@4 V]>_[@(0.0.255) int]_[* Count]([@(0.0.255) const]_[*@4 Ran
ge][@(0.0.255) `&]_[*@3 r], [@(0.0.255) const]_[*@4 V][@(0.0.255) `&]_[*@3 val])&]
[s2;%% Counts the number of elements in the Range [%-*@3 r] that are
equal to [%-*@3 val] .&]
[s3;%% &]
[s4; &]
[s5;:Upp`:`:CountIf`(const Range`&`,Predicate`): [@(0.0.255) template]_<[@(0.0.255) class
]_[*@4 Range], [@(0.0.255) class]_[*@4 Predicate]>_[@(0.0.255) int]_[* CountIf]([@(0.0.255) c
onst]_[*@4 Range][@(0.0.255) `&]_[*@3 r], [*@4 Predicate]_[*@3 p])&]
[s2;%% Counts the number of elements in the Range [%-*@3 r] that satisfy
condition [%-*@3 p].&]
[s3;%% &]
[s4; &]
[s5;:Upp`:`:FindBest`(const Range`&`,const Pred`&`): [@(0.0.255) template]_<[@(0.0.255) c
lass]_[*@4 Range], [@(0.0.255) class]_[*@4 Pred]>_[@(0.0.255) int]_[* FindBest]([@(0.0.255) c
onst]_[*@4 Range][@(0.0.255) `&]_[*@3 r], [@(0.0.255) const]_[*@4 Pred][@(0.0.255) `&]_[*@3 pre