Core: CoCount, CoCountIf

git-svn-id: svn://ultimatepp.org/upp/trunk@10485 f0d560ea-af0d-0410-9eb7-867de7ffcac7
This commit is contained in:
cxl 2016-11-22 18:59:49 +00:00
parent 1045e6331a
commit e794e89f82
4 changed files with 48 additions and 6 deletions

View file

@ -61,7 +61,7 @@ int Count(const Range& r, const V& val)
}
template <class Range, class Predicate>
int CountIf(const Range& r, Predicate p)
int CountIf(const Range& r, const Predicate& p)
{
int count = 0;
for(const auto& x : r)

View file

@ -33,7 +33,8 @@ void CoPartition(Range& r, const Lambda& lambda)
while(begin < end) {
auto e = begin + min(chunk, size_t(end - begin));
co & [=] {
lambda(SubRange(begin, e));
auto sr = SubRange(begin, e); // we need l-value
lambda(sr);
};
begin = e;
}
@ -49,7 +50,8 @@ void CoPartition(const Range& r, const Lambda& lambda, int max_chunk = INT_MAX)
while(begin < end) {
auto e = begin + min(chunk, size_t(end - begin));
co & [=] {
lambda(SubRange(begin, e));
auto sr = SubRange(begin, e); // we need l-value
lambda(sr);
};
begin = e;
}
@ -94,6 +96,30 @@ ValueTypeOf<T> CoSum(const T& c)
return CoSum(c, (ValueTypeOf<T>)0);
}
template <class Range, class V>
int CoCount(const Range& r, const V& val)
{
int count = 0;
CoPartition(r, [=, &val, &count](const SubRangeOf<const Range>& r) {
int n = Count(r, val);
CoWork::FinLock();
count += n;
});
return count;
}
template <class Range, class Predicate>
int CoCountIf(const Range& r, const Predicate& p)
{
int count = 0;
CoPartition(r, [=, &p, &count](const SubRangeOf<const Range>& r) {
int n = CountIf(r, p);
CoWork::FinLock();
count += n;
});
return count;
}
template <class Range, class Better>
int CoFindBest(const Range& r, const Better& better)
{

View file

@ -52,6 +52,21 @@ alueTypeOf]<[*@4 T]>_[* CoSum]([@(0.0.255) const]_[*@4 T][@(0.0.255) `&]_[*@3 c]
[s2;%% Same as CoSum(c, 0).&]
[s3;%% &]
[s4; &]
[s5;:Upp`:`:CoCount`(const Range`&`,const V`&`): [@(0.0.255) template]_<[@(0.0.255) class
]_[*@4 Range], [@(0.0.255) class]_[*@4 V]>_[@(0.0.255) int]_[* CoCount]([@(0.0.255) const]_[*@4 R
ange][@(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]. Runs in parallel.&]
[s3;%% &]
[s4; &]
[s5;:Upp`:`:CoCountIf`(const Range`&`,const Predicate`&`): [@(0.0.255) template]_<[@(0.0.255) c
lass]_[*@4 Range], [@(0.0.255) class]_[*@4 Predicate]>_[@(0.0.255) int]_[* CoCountIf]([@(0.0.255) c
onst]_[*@4 Range][@(0.0.255) `&]_[*@3 r], [@(0.0.255) const]_[*@4 Predicate][@(0.0.255) `&]_[*@3 p
])&]
[s2; [%% Counts the number of elements in the Range ][*@3 r][%% that satisfy
condition ][*@3 p][%% . ]Runs in parallel, [*@3 p ]must be reentrant.&]
[s3;%% &]
[s4; &]
[s5;:Upp`:`:CoFindBest`(const Range`&`,const Better`&`): [@(0.0.255) template]_<[@(0.0.255) c
lass]_[*@4 Range], [@(0.0.255) class]_[*@4 Better]>_[@(0.0.255) int]_[* CoFindBest]([@(0.0.255) c
onst]_[*@4 Range][@(0.0.255) `&]_[*@3 r], [@(0.0.255) const]_[*@4 Better][@(0.0.255) `&]_[*@3 b

View file

@ -69,9 +69,10 @@ ge][@(0.0.255) `&]_[*@3 r], [@(0.0.255) const]_[*@4 V][@(0.0.255) `&]_[*@3 val])
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])&]
[s5;:Upp`:`:CountIf`(const Range`&`,const Predicate`&`): [@(0.0.255) template]_<[@(0.0.255) c
lass]_[*@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], [@(0.0.255) const]_[*@4 Predicate][@(0.0.255) `&]_[*@3 p
])&]
[s2;%% Counts the number of elements in the Range [%-*@3 r] that satisfy
condition [%-*@3 p].&]
[s3;%% &]