-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfilter.h
73 lines (63 loc) · 1.84 KB
/
filter.h
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#ifndef FILTER_H
#define FILTER_H
#include "tiny_id_func.h"
template<class Pred>
int count_true(const Pred&p){
int sum = 0;
for(int i=0; i<p.preimage_count(); ++i)
if(p(i))
++sum;
return sum;
}
template<class Pred, class IDFunc>
typename std::enable_if<
is_only_id_func<IDFunc>::value,
ArrayIDFunc<typename id_func_image_type<IDFunc>::type>
>::type keep_if(const Pred&p, int new_preimage_count, const IDFunc&f){
assert(p.preimage_count() == f.preimage_count());
assert(new_preimage_count == count_true(p));
ArrayIDFunc<typename id_func_image_type<IDFunc>::type>result(new_preimage_count);
int out = 0;
for(int in=0; in<f.preimage_count(); ++in)
if(p(in))
result[out++] = f(in);
assert(out == new_preimage_count);
return result; // NVRO
}
template<class Pred, class IDFunc>
typename std::enable_if<
is_id_id_func<IDFunc>::value,
ArrayIDIDFunc
>::type keep_if(const Pred&p, int new_preimage_count, const IDFunc&f){
assert(p.preimage_count() == f.preimage_count());
assert(new_preimage_count == count_true(p));
ArrayIDIDFunc result(new_preimage_count, f.image_count());
int out = 0;
for(int in=0; in<f.preimage_count(); ++in)
if(p(in))
result[out++] = f(in);
assert(out == new_preimage_count);
return result; // NRVO
}
template<class Pred>
ArrayIDIDFunc compute_keep_function(const Pred&pred, int new_image_count){
ArrayIDIDFunc f(pred.preimage_count(), new_image_count);
int next_id = 0;
for(int i=0; i<pred.preimage_count(); ++i)
if(pred(i))
f[i] = next_id++;
else
f[i] = -1;
return f; // NRVO
}
template<class Pred>
ArrayIDIDFunc compute_inverse_keep_function(const Pred&pred, int new_image_count){
ArrayIDIDFunc f(new_image_count, pred.preimage_count());
int next_id = 0;
for(int i=0; i<pred.preimage_count(); ++i)
if(pred(i))
f[next_id++] = i;
assert(new_image_count == next_id);
return f; // NRVO
}
#endif