-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcount.cpp
35 lines (27 loc) · 893 Bytes
/
count.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include "count.h"
#include "general_reduce.h"
#include "built_in.h"
bool TRTC_Count(const DVVectorLike& vec, const DeviceViewable& value, size_t& ret)
{
Functor src({ {"vec_in", &vec}, {"eq_value", &value } }, { "idx" },
" return (vec_in[idx] == (decltype(vec_in)::value_t)eq_value)?1:0;\n");
Functor op("Plus");
ret = 0;
if (vec.size() < 1) return true;
ViewBuf buf;
if (!general_reduce(vec.size(), "size_t", src, op, buf)) return false;
ret = *(size_t*)buf.data();
return true;
}
bool TRTC_Count_If(const DVVectorLike& vec, const Functor& pred, size_t& ret)
{
Functor src({ {"vec_in", &vec}, {"pred", &pred } }, { "idx" },
" return pred(vec_in[idx])?1:0;\n");
Functor op("Plus");
ret = 0;
if (vec.size() < 1) return true;
ViewBuf buf;
if (!general_reduce(vec.size(), "size_t", src, op, buf)) return false;
ret = *(size_t*)buf.data();
return true;
}