blob: 2e9b802c97ab79eacb66469fb86c2160c6a0fecd (
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
|
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once
#include <vespa/eval/eval/value.h>
#include <vespa/vespalib/objects/nbostream.h>
#include <cassert>
namespace vespalib::eval {
/**
* Reads a stream of serialized labels.
* Reading more labels than available will trigger an assert.
**/
struct LabelStream {
const StringIdVector &source;
size_t pos;
LabelStream(const StringIdVector &data) : source(data), pos(0) {}
string_id next_label() {
assert(pos < source.size());
return source[pos++];
}
void reset() { pos = 0; }
};
/**
* Represents an address (set of labels) mapping to a subspace index
**/
struct LabelBlock {
static constexpr size_t npos = -1;
size_t subspace_index;
ConstArrayRef<string_id> address;
operator bool() const { return subspace_index != npos; }
};
/**
* Utility for reading a buffer with serialized labels
* as a stream of LabelBlock objects.
**/
class LabelBlockStream {
private:
size_t _num_subspaces;
LabelStream _labels;
size_t _subspace_index;
StringIdVector _current_address;
public:
LabelBlock next_block() {
if (_subspace_index < _num_subspaces) {
for (auto & label : _current_address) {
label = _labels.next_label();
}
return LabelBlock{_subspace_index++, _current_address};
} else {
return LabelBlock{LabelBlock::npos, {}};
}
}
void reset() {
_subspace_index = 0;
_labels.reset();
}
LabelBlockStream(uint32_t num_subspaces,
const StringIdVector &labels,
uint32_t num_mapped_dims)
: _num_subspaces(num_subspaces),
_labels(labels),
_subspace_index(num_subspaces),
_current_address(num_mapped_dims)
{}
~LabelBlockStream();
};
} // namespace
|