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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
|
// license:BSD-3-Clause
// copyright-holders:Couriersud
#ifndef PLISTS_H_
#define PLISTS_H_
///
/// \file plists.h
///
#include "ptypes.h"
#include <algorithm>
#include <array>
#include <type_traits>
#include <utility>
namespace plib {
/// \brief Array holding uninitialized elements
///
/// Use with care. This template is provided to improve locality of storage
/// in high frequency applications. It should not be used for anything else.
///
template <class C, std::size_t N>
class uninitialised_array
{
public:
using value_type = C;
using pointer = value_type *;
using const_pointer = const value_type *;
using reference = value_type &;
using const_reference = const value_type &;
using iterator = value_type *;
using const_iterator = const value_type *;
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;
using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
constexpr uninitialised_array() noexcept = default;
constexpr uninitialised_array(const uninitialised_array &) = default;
constexpr uninitialised_array &operator=(const uninitialised_array &) = default;
constexpr uninitialised_array(uninitialised_array &&) noexcept = default;
constexpr uninitialised_array &operator=(uninitialised_array &&) noexcept = default;
~uninitialised_array() noexcept = default;
constexpr size_t size() const noexcept { return N; }
constexpr bool empty() const noexcept { return size() == 0; }
constexpr reference operator[](size_type index) noexcept
{
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
return reinterpret_cast<reference>(m_buf[index]);
}
constexpr const_reference operator[](size_type index) const noexcept
{
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
return reinterpret_cast<const_reference>(m_buf[index]);
}
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
constexpr iterator begin() const noexcept { return reinterpret_cast<iterator>(&m_buf[0]); }
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
constexpr iterator end() const noexcept { return reinterpret_cast<iterator>(&m_buf[0] + N); }
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
constexpr iterator begin() noexcept { return reinterpret_cast<iterator>(&m_buf[0]); }
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
constexpr iterator end() noexcept { return reinterpret_cast<iterator>(&m_buf[0] + N); }
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
constexpr const_iterator cbegin() const noexcept { return reinterpret_cast<const_iterator>(&m_buf[0]); }
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
constexpr const_iterator cend() const noexcept { return reinterpret_cast<const_iterator>(&m_buf[0] + N); }
private:
std::array<typename std::aligned_storage<sizeof(C), alignof(C)>::type, N> m_buf;
};
/// \brief fixed allocation vector
///
/// Currently only emplace_back and clear are supported.
///
/// Use with care. This template is provided to improve locality of storage
/// in high frequency applications. It should not be used for anything else.
///
template <class C, std::size_t N>
class static_vector
{
public:
using value_type = C;
using pointer = value_type *;
using const_pointer = const value_type *;
using reference = value_type &;
using const_reference = const value_type &;
using iterator = value_type *;
using const_iterator = const value_type *;
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;
using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
constexpr static_vector() noexcept
: m_pos(0)
{
}
constexpr static_vector(const static_vector &) = default;
constexpr static_vector &operator=(const static_vector &) = default;
constexpr static_vector(static_vector &&) noexcept = default;
constexpr static_vector &operator=(static_vector &&) noexcept = default;
~static_vector() noexcept
{
clear();
}
constexpr size_t size() const noexcept { return m_pos; }
constexpr bool empty() const noexcept { return size() == 0; }
constexpr void clear()
{
for (size_type i=0; i<m_pos; ++i)
(*this)[i].~C();
m_pos = 0;
}
template<typename... Args>
constexpr void emplace_back(Args&&... args)
{
// placement new on buffer
new (&m_buf[m_pos]) C(std::forward<Args>(args)...);
m_pos++;
}
constexpr reference operator[](size_type index) noexcept
{
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
return reinterpret_cast<reference>(m_buf[index]);
}
constexpr const_reference operator[](size_type index) const noexcept
{
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
return reinterpret_cast<const_reference>(m_buf[index]);
}
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
constexpr iterator begin() const noexcept { return reinterpret_cast<iterator>(&m_buf[0]); }
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
constexpr iterator end() const noexcept { return reinterpret_cast<iterator>(&m_buf[0] + m_pos); }
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
constexpr iterator begin() noexcept { return reinterpret_cast<iterator>(&m_buf[0]); }
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
constexpr iterator end() noexcept { return reinterpret_cast<iterator>(&m_buf[0] + m_pos); }
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
constexpr const_iterator cbegin() const noexcept { return reinterpret_cast<const_iterator>(&m_buf[0]); }
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
constexpr const_iterator cend() const noexcept { return reinterpret_cast<const_iterator>(&m_buf[0] + m_pos); }
private:
std::array<typename std::aligned_storage<sizeof(C), alignof(C)>::type, N> m_buf;
size_type m_pos;
};
/// \brief a simple linked list.
///
/// The list allows insertions and deletions whilst being processed.
///
template <class LC, int TAG>
class linked_list_t
{
public:
using ttag = std::integral_constant<int, TAG>;
struct element_t
{
public:
using tag = std::integral_constant<int, TAG>;
friend class linked_list_t<LC, TAG>;
constexpr element_t() noexcept : m_next(nullptr), m_prev(nullptr) {}
~element_t() noexcept = default;
constexpr element_t(const element_t &) noexcept = default;
constexpr element_t &operator=(const element_t &) noexcept = default;
constexpr element_t(element_t &&) noexcept = default;
constexpr element_t &operator=(element_t &&) noexcept = default;
private:
element_t * m_next;
element_t * m_prev;
};
struct iter_t final
{
private:
element_t * p;
public:
using tag = std::integral_constant<int, TAG>;
using iterator_category = std::forward_iterator_tag;
using value_type = LC;
using pointer = value_type *;
using reference = value_type &;
using difference_type = std::ptrdiff_t;
explicit constexpr iter_t(element_t * x) noexcept : p(x) { }
constexpr iter_t(const iter_t &rhs) noexcept = default;
constexpr iter_t(iter_t &&rhs) noexcept = default;
constexpr iter_t& operator=(const iter_t &rhs) noexcept = default;
constexpr iter_t& operator=(iter_t &&rhs) noexcept = default;
~iter_t() noexcept = default;
iter_t& operator++() noexcept { p = p->m_next; return *this; }
// NOLINTNEXTLINE(cert-dcl21-cpp)
iter_t operator++(int) & noexcept { const iter_t tmp(*this); operator++(); return tmp; }
constexpr bool operator==(const iter_t& rhs) const noexcept { return p == rhs.p; }
constexpr bool operator!=(const iter_t& rhs) const noexcept { return p != rhs.p; }
#if 0
constexpr LC& operator*() noexcept { return *static_cast<LC *>(p); }
constexpr LC* operator->() noexcept { return static_cast<LC *>(p); }
constexpr LC& operator*() const noexcept { return *static_cast<LC *>(p); }
constexpr LC* operator->() const noexcept { return static_cast<LC *>(p); }
#else
constexpr LC* operator*() noexcept { return static_cast<LC *>(p); }
constexpr LC* operator->() noexcept { return static_cast<LC *>(p); }
constexpr LC* operator*() const noexcept { return static_cast<LC *>(p); }
constexpr LC* operator->() const noexcept { return static_cast<LC *>(p); }
#endif
};
constexpr linked_list_t() noexcept : m_head(nullptr) {}
constexpr iter_t begin() const noexcept { return iter_t(m_head); }
constexpr iter_t end() const noexcept { return iter_t(nullptr); }
constexpr void push_front(element_t *elem) noexcept
{
elem->m_next = m_head;
elem->m_prev = nullptr;
if (m_head)
m_head->m_prev = elem;
m_head = elem;
}
constexpr void push_back(element_t *elem) noexcept
{
element_t ** p(&m_head);
element_t * prev(nullptr);
while (*p != nullptr)
{
prev = *p;
p = &((*p)->m_next);
}
*p = elem;
elem->m_prev = prev;
elem->m_next = nullptr;
}
constexpr void remove(const element_t *elem) noexcept
{
if (elem->m_prev)
elem->m_prev->m_next = elem->m_next;
else
m_head = elem->m_next;
if (elem->m_next)
elem->m_next->m_prev = elem->m_prev;
else
{
// update tail
}
}
constexpr LC *front() const noexcept { return m_head; }
constexpr bool empty() const noexcept { return (m_head == nullptr); }
constexpr std::size_t size() const noexcept
{
std::size_t ret = 0;
element_t *p = m_head;
while (p != nullptr)
{
ret++;
p = p->m_next;
}
return ret;
}
constexpr void clear() noexcept
{
element_t *p(m_head);
while (p != nullptr)
{
element_t *n(p->m_next);
p->m_next = nullptr;
p->m_prev = nullptr;
p = n;
}
m_head = nullptr;
}
private:
element_t *m_head;
};
} // namespace plib
#endif // PLISTS_H_
|