-
Notifications
You must be signed in to change notification settings - Fork 8
/
ArrayFilterer.h
79 lines (58 loc) · 2.45 KB
/
ArrayFilterer.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
74
75
76
77
78
79
#ifndef Zadeh_TreeFilterer_H
#define Zadeh_TreeFilterer_H
#include "common.h"
#include "options.h"
#include "filter.h"
namespace zadeh {
template<typename ArrayType, typename ElementType = CandidateString>
class ArrayFilterer {
private:
vector<std::vector<CandidateString>> partitioned_candidates{};
ArrayType candidates_view; // TODO use a reference or a raw pointer?
public:
ArrayFilterer() = default;
ArrayFilterer(ArrayType &&candidates) {
set_candidates(forward<ArrayType>(candidates));
}
auto set_candidates(const ArrayType &candidates) {
const auto N = get_size(candidates);
const auto num_chunks = get_num_chunks(N);
partitioned_candidates.clear();
partitioned_candidates.resize(num_chunks);
auto cur_start = 0u;
for (auto iChunk = 0u; iChunk < num_chunks; iChunk++) {
auto chunk_size = N / num_chunks;
// Distribute remainder among the chunks.
if (iChunk < N % num_chunks) {
chunk_size++;
}
for (auto iCandidate = cur_start; iCandidate < cur_start + chunk_size; iCandidate++) {
partitioned_candidates[iChunk].emplace_back(get_at<ArrayType, ElementType>(candidates, iCandidate));
}
cur_start += chunk_size;
}
// store a view of candidates in case filter was called
candidates_view = candidates;
}
auto filter_indices(const std::string &query, const size_t maxResults = 0, const bool usePathScoring = true, const bool useExtensionBonus = false) {
// optimization for no candidates
if (partitioned_candidates.empty()) {
return vector<size_t>();
}
const Options options(query, maxResults, usePathScoring, useExtensionBonus);
return zadeh::filter(partitioned_candidates, query, options);
}
auto filter(const std::string &query, const size_t maxResults = 0, const bool usePathScoring = true, const bool useExtensionBonus = false) {
auto res = vector<ArrayType>{};
if (candidates_view == nullptr) {
return res; // return an empty vector (should we throw?)
}
const auto filter_indices = filter(query, maxResults, usePathScoring, useExtensionBonus);
for (size_t i = 0, len = filter_indices.size(); i < len; i++) {
res[i] = candidates_view[filter_indices[i]];
}
return res;
}
};
} // namespace zadeh
#endif