-
Notifications
You must be signed in to change notification settings - Fork 399
Expand file tree
/
Copy pathfixed_queue.hpp
More file actions
94 lines (76 loc) · 2.19 KB
/
Copy pathfixed_queue.hpp
File metadata and controls
94 lines (76 loc) · 2.19 KB
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
// This file is a part of the IncludeOS unikernel - www.includeos.org
//
// Copyright 2015 Oslo and Akershus University College of Applied Sciences
// and Alfred Bratterud
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef FIXEDQUEUE_H_INCLUDED
#define FIXEDQUEUE_H_INCLUDED
#include <array>
#include <type_traits>
namespace util
{
template<typename T, size_t N> class fixed_queue
{
public:
using buffer_t = std::array<T, N>;
explicit fixed_queue() : index_(0)
{
static_assert(N > 0, "fixed_queue size should be larger than zero!");
};
~fixed_queue() {};
fixed_queue(const fixed_queue&) = default;
fixed_queue(fixed_queue&&) = default;
fixed_queue& operator= (const fixed_queue&) = default;
fixed_queue& operator= (fixed_queue&&) = default;
void push_back(const T& val)
noexcept(std::is_trivially_copy_assignable<T>::value)
{
++index_;
buff_[index_ % N] = val;
}
void push_back(T&& val)
noexcept(std::is_trivially_move_assignable<T>::value)
{
++index_;
buff_[index_ % N] = std::move(val);
}
T& front() noexcept { return buff_[index_ % N]; }
T& back() noexcept { return buff_[(index_ + 1) % N]; }
template<typename F> void fold(F&& func)
{
for (size_t i = 0, max = index_ < N ? index_ : N ; i < max; ++i)
std::forward<F>(func)(buff_[(index_ - i) % N]);
}
private:
size_t index_;
buffer_t buff_;
};
template<typename T, size_t N> T merge_ring_range(
fixed_queue<T, N>& client_agents
)
{
T ret;
size_t capacity{};
client_agents.fold(
[&capacity](const auto& s) noexcept -> void
{ capacity += s.capacity(); }
);
ret.reserve(capacity);
client_agents.fold(
[&ret](const auto& val) -> void { ret += val; }
);
return ret;
}
}
#endif // FIXEDQUEUE_H_INCLUDED