blob: 99cf21e62809f6a8a3765c4bc13287ac35bddee0 (
plain) (
blame)
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "featureset.h"
namespace vespalib {
FeatureSet::FeatureSet()
: _names(),
_docIds(),
_values()
{
}
FeatureSet::~FeatureSet() {}
FeatureSet::FeatureSet(const StringVector &names, uint32_t expectDocs)
: _names(names),
_docIds(),
_values()
{
_docIds.reserve(expectDocs);
_values.reserve(expectDocs * names.size());
}
bool
FeatureSet::equals(const FeatureSet &rhs) const
{
return ((_docIds == rhs._docIds) &&
(_values == rhs._values) &&
(_names == rhs._names)); // do names last, as they are most likely to match
}
uint32_t
FeatureSet::addDocId(uint32_t docId)
{
_docIds.push_back(docId);
_values.resize(_names.size() * _docIds.size());
return (_docIds.size() - 1);
}
bool
FeatureSet::contains(const std::vector<uint32_t> &docIds) const
{
using ITR = std::vector<uint32_t>::const_iterator;
ITR myPos = _docIds.begin();
ITR myEnd = _docIds.end();
ITR pos = docIds.begin();
ITR end = docIds.end();
for (; pos != end; ++pos) {
while (myPos != myEnd && *myPos < *pos) {
++myPos;
}
if (myPos == myEnd || *myPos != *pos) {
return false;
}
++myPos;
}
return true;
}
FeatureSet::Value *
FeatureSet::getFeaturesByIndex(uint32_t idx)
{
if (idx >= _docIds.size()) {
return 0;
}
return &(_values[idx * _names.size()]);
}
const FeatureSet::Value *
FeatureSet::getFeaturesByDocId(uint32_t docId) const
{
uint32_t low = 0;
uint32_t hi = _docIds.size();
while (low < hi) {
uint32_t pos = (low + hi) >> 1;
uint32_t val = _docIds[pos];
if (val < docId) {
low = pos + 1;
} else if (val > docId) {
hi = pos;
} else {
return &(_values[pos * _names.size()]);
}
}
return 0;
}
FeatureValues::FeatureValues() noexcept = default;
FeatureValues::FeatureValues(const FeatureValues& rhs) = default;
FeatureValues::FeatureValues(FeatureValues&& rhs) noexcept = default;
FeatureValues::~FeatureValues() noexcept = default;
FeatureValues& FeatureValues::operator=(const FeatureValues& rhs) = default;;
FeatureValues& FeatureValues::operator=(FeatureValues&& rhs) noexcept = default;
}
|