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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
|
// license:BSD-3-Clause
// copyright-holders:Couriersud
#ifndef PTIMED_QUEUE_H_
#define PTIMED_QUEUE_H_
///
/// \file ptimed_queue.h
///
#include "palloc.h" // FIXME: for aligned_vector
#include "pchrono.h"
#include "pmulti_threading.h"
#include "ptypes.h"
#include <algorithm>
#include <mutex>
#include <type_traits>
#include <utility>
#include <vector>
namespace plib {
// ----------------------------------------------------------------------------------------
// timed queue
// ----------------------------------------------------------------------------------------
template <typename Time, typename Element>
struct queue_entry_t final
{
using element_type = Element;
constexpr queue_entry_t() noexcept : m_exec_time(), m_object(nullptr) { }
constexpr queue_entry_t(const Time &t, const Element &o) noexcept : m_exec_time(t), m_object(o) { }
queue_entry_t(const queue_entry_t &) = default;
queue_entry_t &operator=(const queue_entry_t &) = default;
queue_entry_t(queue_entry_t &&) noexcept = default;
queue_entry_t &operator=(queue_entry_t &&) noexcept = default;
~queue_entry_t() = default;
constexpr bool operator ==(const queue_entry_t &rhs) const noexcept
{
return m_object == rhs.m_object;
}
constexpr bool operator ==(const Element &rhs) const noexcept
{
return m_object == rhs;
}
constexpr bool operator <=(const queue_entry_t &rhs) const noexcept
{
return (m_exec_time <= rhs.m_exec_time);
}
constexpr bool operator <(const queue_entry_t &rhs) const noexcept
{
return (m_exec_time < rhs.m_exec_time);
}
static constexpr queue_entry_t never() noexcept { return queue_entry_t(Time::never(), nullptr); }
constexpr const Time &exec_time() const noexcept { return m_exec_time; }
constexpr const Element &object() const noexcept { return m_object; }
private:
Time m_exec_time;
Element m_object;
};
template <class A, class T>
class timed_queue_linear
{
public:
explicit timed_queue_linear(A &arena, const std::size_t list_size)
: m_list(arena, list_size)
{
clear();
}
~timed_queue_linear() = default;
timed_queue_linear(const timed_queue_linear &) = delete;
timed_queue_linear &operator=(const timed_queue_linear &) = delete;
timed_queue_linear(timed_queue_linear &&) noexcept = delete;
timed_queue_linear &operator=(timed_queue_linear &&) noexcept = delete;
constexpr std::size_t capacity() const noexcept { return m_list.capacity() - 1; }
constexpr bool empty() const noexcept { return (m_end == &m_list[1]); }
template <bool KEEPSTAT, typename... Args>
constexpr void emplace (Args&&... args) noexcept;
template <bool KEEPSTAT>
constexpr void push(T && e) noexcept;
constexpr void pop() noexcept { --m_end; }
constexpr const T &top() const noexcept { return *(m_end-1); }
constexpr bool exists(const typename T::element_type &elem) const noexcept;
template <bool KEEPSTAT>
constexpr void remove(const T &elem) noexcept;
template <bool KEEPSTAT>
constexpr void remove(const typename T::element_type &elem) noexcept;
constexpr void clear() noexcept
{
m_end = &m_list[0];
// put an empty element with maximum time into the queue.
// the insert algo above will run into this element and doesn't
// need a comparison with queue start.
//
m_list[0] = T::never();
m_end++;
}
// save state support & mame disassembler
constexpr const T *list_pointer() const noexcept { return &m_list[1]; }
constexpr std::size_t size() const noexcept { return narrow_cast<std::size_t>(m_end - &m_list[1]); }
constexpr const T & operator[](std::size_t index) const noexcept { return m_list[ 1 + index]; }
private:
PALIGNAS(PALIGN_CACHELINE)
T * m_end;
plib::arena_vector<A, T, PALIGN_CACHELINE> m_list;
public:
// profiling
// FIXME: Make those private
pperfcount_t<true> m_prof_sort_move; // NOLINT
pperfcount_t<true> m_prof_call; // NOLINT
pperfcount_t<true> m_prof_remove; // NOLINT
};
template <class A, class T>
template <bool KEEPSTAT, typename... Args>
inline constexpr void timed_queue_linear<A, T>::emplace (Args&&... args) noexcept
{
T * i(m_end);
*i = T(std::forward<Args>(args)...);
//if (i->object() != nullptr && exists(i->object()))
// printf("Object exists %s\n", i->object()->name().c_str());
m_end++;
if constexpr (!KEEPSTAT)
{
for (; *(i-1) < *i; --i)
{
std::swap(*(i-1), *(i));
}
}
else
{
for (; *(i-1) < *i; --i)
{
std::swap(*(i-1), *(i));
m_prof_sort_move.inc();
}
m_prof_call.inc();
}
}
template <class A, class T>
template <bool KEEPSTAT>
inline constexpr void timed_queue_linear<A, T>::push(T && e) noexcept
{
#if 0
// The code is not as fast as the code path which is enabled.
// It is left here in case on a platform different to x64 it may
// be faster.
T * i(m_end-1);
for (; *i < e; --i)
{
*(i+1) = *(i);
if (KEEPSTAT)
m_prof_sort_move.inc();
}
*(i+1) = std::move(e);
++m_end;
#else
//if (e.object() != nullptr && exists(e.object()))
// printf("Object exists %s\n", e.object()->name().c_str());
T * i(m_end++);
*i = std::move(e);
for (; *(i-1) < *i; --i)
{
std::swap(*(i-1), *(i));
if constexpr (KEEPSTAT)
m_prof_sort_move.inc();
}
#endif
if constexpr (KEEPSTAT)
m_prof_call.inc();
}
template <class A, class T>
template <bool KEEPSTAT>
inline constexpr void timed_queue_linear<A, T>::remove(const T &elem) noexcept
{
if constexpr (KEEPSTAT)
m_prof_remove.inc();
for (T * i = m_end - 1; i > &m_list[0]; --i)
{
// == operator ignores time!
if (*i == elem)
{
std::copy(i+1, m_end--, i);
return;
}
}
//printf("Element not found in delete %s\n", elem->name().c_str());
}
template <class A, class T>
template <bool KEEPSTAT>
inline constexpr void timed_queue_linear<A, T>::remove(const typename T::element_type &elem) noexcept
{
if constexpr (KEEPSTAT)
m_prof_remove.inc();
for (T * i = m_end - 1; i > &m_list[0]; --i)
{
// == operator ignores time!
if (*i == elem)
{
std::copy(i+1, m_end--, i);
return;
}
}
//printf("Element not found in delete %s\n", elem->name().c_str());
}
template <class A, class T>
inline constexpr bool timed_queue_linear<A, T>::exists(const typename T::element_type &elem) const noexcept
{
for (T * i = &m_list[1]; i < m_end; ++i)
{
if (*i == elem)
{
return true;
}
}
return false;
}
template <class A, class T>
class timed_queue_heap
{
public:
struct compare
{
constexpr bool operator()(const T &a, const T &b) const noexcept { return b <= a; }
};
explicit timed_queue_heap(A &arena, const std::size_t list_size)
: m_list(arena, list_size)
{
clear();
}
~timed_queue_heap() = default;
PCOPYASSIGNMOVE(timed_queue_heap, delete)
std::size_t capacity() const noexcept { return m_list.capacity(); }
bool empty() const noexcept { return &m_list[0] == m_end; }
template <bool KEEPSTAT, typename... Args>
void emplace(Args&&... args) noexcept
{
*m_end++ = T(std::forward<Args>(args)...);
std::push_heap(&m_list[0], m_end, compare());
if (KEEPSTAT)
m_prof_call.inc();
}
template <bool KEEPSTAT>
void push(T &&e) noexcept
{
*m_end++ = e;
std::push_heap(&m_list[0], m_end, compare());
if (KEEPSTAT)
m_prof_call.inc();
}
void pop() noexcept
{
std::pop_heap(&m_list[0], m_end, compare());
m_end--;
}
const T &top() const noexcept { return m_list[0]; }
template <bool KEEPSTAT>
void remove(const T &elem) noexcept
{
if (KEEPSTAT)
m_prof_remove.inc();
for (T * i = m_end - 1; i >= &m_list[0]; i--)
{
if (*i == elem)
{
m_end--;
*i = *m_end;
std::make_heap(&m_list[0], m_end, compare());
return;
}
}
}
template <bool KEEPSTAT>
void remove(const typename T::element_type &elem) noexcept
{
if (KEEPSTAT)
m_prof_remove.inc();
for (T * i = m_end - 1; i >= &m_list[0]; i--)
{
if (*i == elem)
{
m_end--;
*i = *m_end;
std::make_heap(&m_list[0], m_end, compare());
return;
}
}
}
void clear()
{
m_list.clear();
m_end = &m_list[0];
}
// save state support & mame disassembler
constexpr const T *list_pointer() const { return &m_list[0]; }
constexpr std::size_t size() const noexcept { return m_list.size(); }
constexpr const T & operator[](const std::size_t index) const { return m_list[ 0 + index]; }
private:
T * m_end;
plib::arena_vector<A, T> m_list;
public:
// profiling
pperfcount_t<true> m_prof_sort_move; // NOLINT
pperfcount_t<true> m_prof_call; // NOLINT
pperfcount_t<true> m_prof_remove; // NOLINT
};
} // namespace plib
#endif // PTIMED_QUEUE_H_
|