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
|
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "work.h"
#include <vespa/searchlib/util/bufferwriter.h>
namespace search
{
template <class T>
class WriteFunctor
{
BufferWriter &_writer;
public:
WriteFunctor(BufferWriter &writer)
: _writer(writer)
{
}
void operator()(const T &val) { _writer.write(&val, sizeof(val)); }
};
template <class T>
class WriteFunctor2
{
BufferWriter &_writer;
public:
WriteFunctor2(BufferWriter &writer)
: _writer(writer)
{
}
void operator()(const T &val) __attribute((noinline))
{ _writer.write(&val, sizeof(val)); }
};
template <class T, class Func>
void workLoop(const std::vector<T> &v, Func &&func)
{
for (const auto &val : v) {
func(val);
}
}
template <class T>
void work(const std::vector<T> &v, BufferWriter &writer)
{
for (const auto &val : v) {
writer.write(&val, sizeof(val));
}
writer.flush();
}
template <class T>
void workLambda(const std::vector<T> &v, BufferWriter &writer)
{
workLoop<T>(v,
[&writer](const T &val) { writer.write(&val, sizeof(val)); });
writer.flush();
}
template <class T>
void workFunctor(const std::vector<T> &v, BufferWriter &writer)
{
workLoop<T>(v, WriteFunctor<T>(writer));
writer.flush();
}
template <class T>
void workFunctor2(const std::vector<T> &v, BufferWriter &writer)
{
workLoop<T>(v, WriteFunctor2<T>(writer));
writer.flush();
}
template void work(const std::vector<char> &v, BufferWriter &writer);
template void work(const std::vector<short> &v, BufferWriter &writer);
template void work(const std::vector<int> &v, BufferWriter &writer);
template void work(const std::vector<long> &v, BufferWriter &writer);
template void workLambda(const std::vector<char> &v, BufferWriter &writer);
template void workLambda(const std::vector<short> &v, BufferWriter &writer);
template void workLambda(const std::vector<int> &v, BufferWriter &writer);
template void workLambda(const std::vector<long> &v, BufferWriter &writer);
template void workFunctor(const std::vector<char> &v, BufferWriter &writer);
template void workFunctor(const std::vector<short> &v, BufferWriter &writer);
template void workFunctor(const std::vector<int> &v, BufferWriter &writer);
template void workFunctor(const std::vector<long> &v, BufferWriter &writer);
template void workFunctor2(const std::vector<char> &v, BufferWriter &writer);
template void workFunctor2(const std::vector<short> &v, BufferWriter &writer);
template void workFunctor2(const std::vector<int> &v, BufferWriter &writer);
template void workFunctor2(const std::vector<long> &v, BufferWriter &writer);
} // namespace search
|