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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572 | /**
* @file memory_zone.hpp
* @author Robin Passama
* @brief include file for memory zone utilisty class
* @date 2022-06-10
*
* @ingroup containers
*
*/
#pragma once
#include <cstdint>
#include <array>
#include <iostream>
#include <functional>
namespace pid {
template <typename T>
struct MemoryZoneElement {
using reference_t = T&;
using const_reference_t = const T&;
using pointer_t = T*;
using const_pointer_t = const T*;
using memory_pointer =
MemoryZoneElement<T>*; // points somewhere in the containing heap
private:
uint8_t used_;
T val_;
memory_pointer next_, previous_;
uint32_t index_;
public:
MemoryZoneElement()
: used_{0},
val_{}, // T must be default constructible
next_{nullptr},
previous_{nullptr},
index_{0} {
}
MemoryZoneElement(const MemoryZoneElement&) = delete;
MemoryZoneElement& operator=(const MemoryZoneElement&) = delete;
MemoryZoneElement(MemoryZoneElement&&) = delete;
MemoryZoneElement& operator=(MemoryZoneElement&&) = delete;
virtual ~MemoryZoneElement() = default;
uint32_t& index() {
return (index_);
}
const uint32_t& index() const {
return (index_);
}
const_pointer_t pointer() const {
return (&val_);
}
pointer_t pointer() {<--- Technically the member function 'pid::MemoryZoneElement::pointer' can be const. [+]The member function 'pid::MemoryZoneElement::pointer' can be made a const function. Making this function 'const' should not cause compiler errors. Even though the function can be made const function technically it may not make sense conceptually. Think about your design and the task of the function first - is it a function that must not change object internal state?
return (&val_);
}
reference_t reference() {<--- Technically the member function 'pid::MemoryZoneElement::reference' can be const. [+]The member function 'pid::MemoryZoneElement::reference' can be made a const function. Making this function 'const' should not cause compiler errors. Even though the function can be made const function technically it may not make sense conceptually. Think about your design and the task of the function first - is it a function that must not change object internal state?
return (val_);
}
const_reference_t reference() const {
return (val_);
}
memory_pointer next() const {
return (next_);
}
memory_pointer& next() {
return (next_);
}
memory_pointer previous() const {
return (previous_);
}
memory_pointer& previous() {
return (previous_);
}
void set_used() {
used_ = true;
}
bool used() const {
return (used_);
}
void reset() {
used_ = false;
next_ = previous_ = nullptr;
}
};
template <typename T>
struct MemoryZoneIterator {
using memory_t = MemoryZoneElement<T>;
using element_type = memory_t*;
private:
element_type element_; // the pointed type is always non const
public:
MemoryZoneIterator() : element_(nullptr) {
}
~MemoryZoneIterator() = default;
MemoryZoneIterator(const MemoryZoneIterator& it) = default;
MemoryZoneIterator& operator=(const MemoryZoneIterator& it) = default;
void set(element_type pointed) {
element_ = pointed;
}
element_type get() const {
return (element_);
}
typename memory_t::pointer_t operator->() const {
return (element_->pointer());
}
typename memory_t::reference_t operator*() {
return (element_->reference());
}
bool operator==(const MemoryZoneIterator& other) const {
return (element_ == other.element_);
}
bool operator!=(const MemoryZoneIterator& other) const {
return (element_ != other.element_);
}
operator bool() const {
return (element_ != nullptr);
}
MemoryZoneIterator& operator++() {
element_ = element_->next();
return (*this);
}
MemoryZoneIterator& operator--() {
element_ = element_->previous();
return (*this);
}
MemoryZoneIterator operator++(int) {
auto temp(*this); // need a copy in this case
element_ = element_->next();
return (temp);
}
MemoryZoneIterator operator--(int) {
auto temp(*this); // need a copy in this case
element_ = element_->previous();
return (temp);
}
};
template <typename T>
struct MemoryZoneConstIterator {
using memory_t = MemoryZoneElement<T>;
using element_type = memory_t*;
protected:
element_type element_; // the pointed type is always non const
public:
MemoryZoneConstIterator() : element_(nullptr) {
}
~MemoryZoneConstIterator() = default;
MemoryZoneConstIterator(const MemoryZoneConstIterator& it) = default;
MemoryZoneConstIterator&
operator=(const MemoryZoneConstIterator& it) = default;
MemoryZoneConstIterator(const MemoryZoneIterator<T>& it)<--- Struct 'MemoryZoneConstIterator' has a constructor with 1 argument that is not explicit. [+]Struct 'MemoryZoneConstIterator' has a constructor with 1 argument that is not explicit. Such constructors should in general be explicit for type safety reasons. Using the explicit keyword in the constructor means some mistakes when using the class can be avoided.
: element_(it.get()) { // this constructor is usefull to create a
// const_iterator from an iterator
}
element_type get() const {
return (element_);
}
typename memory_t::const_pointer_t operator->() const {
return (element_->pointer());
}
typename memory_t::const_reference_t operator*() const {
return (element_->reference());
}
bool operator==(const MemoryZoneConstIterator& other) const {
return (element_ == other.element_);
}
bool operator!=(const MemoryZoneConstIterator& other) const {
return (element_ != other.element_);
}
operator bool() const {
return (element_ != nullptr);
}
MemoryZoneConstIterator& operator++() {
element_ = element_->next();
return (*this);
}
MemoryZoneConstIterator& operator--() {
element_ = element_->previous();
return (*this);
}
MemoryZoneConstIterator operator++(int) {
auto temp(*this); // need a copy in this case
element_ = element_->next();
return (temp);
}
MemoryZoneConstIterator operator--(int) {
auto temp(*this); // need a copy in this case
element_ = element_->previous();
return (temp);
}
};
template <typename T, unsigned int Limit>
class MemoryZone {
public:
using size_type = uint32_t;
using contained_type = T;
using value_type = T;
using reference = T&;
using const_reference = const T&;
using pointer = T*;
using const_pointer = const T*;
using iterator = MemoryZoneIterator<T>;
using const_iterator = MemoryZoneConstIterator<T>;
using element_type = MemoryZoneElement<T>*;
private:
std::array<MemoryZoneElement<T>, Limit> data_buffer_;
iterator first_element_, last_element_;
uint32_t next_element_slot_;
size_type size_;
static const size_type capacity_ = Limit;
void reset() {
for (size_type i = 0; i < capacity_; ++i) {
data_buffer_[i].index() = i;
data_buffer_[i].reset();
}
first_element_.set(nullptr);
last_element_ = first_element_;
next_element_slot_ = 0;
size_ = 0;
}
void increment_next_slot_index() {
if (size_ == capacity_) { // now full
next_element_slot_ = capacity_;
return;
}
++next_element_slot_;
if (next_element_slot_ == capacity_) {
next_element_slot_ = 0;
}
while (data_buffer_[next_element_slot_].used()) {
++next_element_slot_;
if (next_element_slot_ == capacity_) {
next_element_slot_ = 0;
}
}
}
element_type use_free_slot() {
auto curr = at_index(next_element_slot_); // get the first free slot
++size_;
data_buffer_[next_element_slot_].set_used(); // this slot is used
increment_next_slot_index();
return (curr);
}
void copy(const MemoryZone& other) {
reset();
size_ = other.size_;
next_element_slot_ = other.next_element_slot_;
// now need to correctly assin pointers
for (unsigned int i = 0; i < capacity_; ++i) {
if (other.data_buffer_[i].used()) { // data_buffer_[i] used as well
data_buffer_[i].set_used();
if (other.data_buffer_[i].next() != nullptr) {
data_buffer_[i].next() =
&data_buffer_[other.data_buffer_[i].next()->index()];
} else {
data_buffer_[i].next() = nullptr;
}
if (other.data_buffer_[i].previous() != nullptr) {
data_buffer_[i].previous() =
&data_buffer_
[other.data_buffer_[i].previous()->index()];
} else {
data_buffer_[i].previous() = nullptr;
}
data_buffer_[i].reference() =
*other.data_buffer_[i].pointer(); // copy the data
} else {
data_buffer_[i].reset();
}
}
if (other.empty()) {
first_element_.set(nullptr);
last_element_.set(nullptr); // if no first then no last element
} else {
first_element_.set(
&data_buffer_[other.first_element_.get()->index()]);
last_element_.set(&data_buffer_[other.last_element_.get()
->index()]); // if no first then
// no last element
}
}
protected:
iterator add_element(iterator iter) {
if (size_ == capacity_) { // cannot add anything to a full zone
return end();
}
iterator ret;
if (static_cast<bool>(first_element_)) { // not empty
if (iter == first_element_) { // insert at first place BEFORE the
// current first element
// the first element will change (BUT NOT the last one)
auto temp_first = first_element_;
// always adding at the beginning
first_element_.set(
use_free_slot()); // get the next free element from the zone
first_element_.get()->previous() =
nullptr; // first has no previous
first_element_.get()->next() =
temp_first.get(); // next is old first element
temp_first.get()->previous() =
first_element_
.get(); // previous of old first element is new element
ret = first_element_;
} else if (iter == end()) { // insert just AFTER last element
auto temp_first = last_element_;
last_element_.set(
use_free_slot()); // get the next free element from the zone
last_element_.get()->previous() = temp_first.get();
last_element_.get()->next() =
nullptr; // last element has no next
temp_first.get()->next() = last_element_.get();
ret = last_element_;
} else { // insert just BEFORE the target element
auto temp_iter = iter;
// always adding at the beginning
iter.set(
use_free_slot()); // get the next free element from the zone
// manage double linked list
iter.get()->previous() =
temp_iter.get()
->previous(); // new element has same previous as the
// previous element
temp_iter.get()->previous() =
iter.get(); // previous of target is new element
iter.get()->next() =
temp_iter.get(); // next is old first element
iter.get()->previous()->next() =
iter.get(); // nect element of previous is the new one
ret = iter;
}
} else { // empty zone, iterator is not useful
first_element_.set(use_free_slot());
// manage double linked list
first_element_.get()->previous() = nullptr;
first_element_.get()->next() = nullptr;
last_element_ = first_element_;
ret = first_element_;
}
return ret;
}
iterator remove_element(iterator iter) {
if (empty()) { // canot remove anything from an empty zone
return end();
}
if (iter == first_element_) { // need to change the begin iterator
// first element becomes the next element
if (first_element_.get()->next() !=
nullptr) { // the removed element has a follower
++first_element_;
first_element_.get()->previous() =
nullptr; // new first element has no previous (by
// definition)
} else { // only one element
first_element_.set(nullptr);
last_element_ = first_element_;
}
} else if (iter == last_element_) { // need to change the last iterator
// if code pass here it means the last element is not the first one
--last_element_;
last_element_.get()->next() =
nullptr; // new first element has no next (by definition)
} else { // any element in the memory except the first and last one
iter.get()->previous()->next() = iter.get()->next();
// if code pass here then it means this is not the last iterator so
// iter.get()->next() is never nullptr
iter.get()->next()->previous() = iter.get()->previous();
}
iter.get()->reset(); // reset the removed element (no more used), but
// its value is still accessible
next_element_slot_ =
iter.get()->index(); // always take the last removed slot as next
// slot to use
--size_;
return (iter);
}
public:
MemoryZone()
: data_buffer_(),
first_element_(),
last_element_(),
next_element_slot_(0),
size_(0) {
reset();
}
MemoryZone(const MemoryZone& copied) : MemoryZone() {
copy(copied); // deep copy
}
MemoryZone(MemoryZone&& moved) : MemoryZone() { // swapping memory
copy(moved);
}
MemoryZone& operator=(const MemoryZone& copied) {
if (&copied != this) {
copy(copied);
}
return (*this);
}
MemoryZone& operator=(MemoryZone&& moved) noexcept {
copy(moved);
return (*this);
}
virtual ~MemoryZone() = default;
// iterating over the circular buffer
size_type size() const {
return (size_);
}
element_type at_index(uint32_t idx) {<--- Technically the member function 'pid::MemoryZone::at_index' can be const. [+]The member function 'pid::MemoryZone::at_index' can be made a const function. Making this function 'const' should not cause compiler errors. Even though the function can be made const function technically it may not make sense conceptually. Think about your design and the task of the function first - is it a function that must not change object internal state?
return (&data_buffer_[idx]);
}
element_type at_index(uint32_t idx) const {
return (&data_buffer_[idx]);
}
// iterating over the circular buffer
size_type capacity() const {<--- Technically the member function 'pid::MemoryZone::capacity' can be static (but you may consider moving to unnamed namespace). [+]The member function 'pid::MemoryZone::capacity' can be made a static function. Making a function static can bring a performance benefit since no 'this' instance is passed to the function. This change should not cause compiler errors but it does not necessarily make sense conceptually. Think about your design and the task of the function first - is it a function that must not access members of class instances? And maybe it is more appropriate to move this function to a unnamed namespace.
return (capacity_);
}
iterator begin() {<--- Technically the member function 'pid::MemoryZone::begin' can be const. [+]The member function 'pid::MemoryZone::begin' can be made a const function. Making this function 'const' should not cause compiler errors. Even though the function can be made const function technically it may not make sense conceptually. Think about your design and the task of the function first - is it a function that must not change object internal state?
return (first_element_);
}
const_iterator begin() const {
return (const_iterator(first_element_));
}
iterator last() {<--- Technically the member function 'pid::MemoryZone::last' can be const. [+]The member function 'pid::MemoryZone::last' can be made a const function. Making this function 'const' should not cause compiler errors. Even though the function can be made const function technically it may not make sense conceptually. Think about your design and the task of the function first - is it a function that must not change object internal state?
return (last_element_);
}
const_iterator last() const {
return (last_element_);
}
iterator end() {<--- Technically the member function 'pid::MemoryZone::end' can be static (but you may consider moving to unnamed namespace). [+]The member function 'pid::MemoryZone::end' can be made a static function. Making a function static can bring a performance benefit since no 'this' instance is passed to the function. This change should not cause compiler errors but it does not necessarily make sense conceptually. Think about your design and the task of the function first - is it a function that must not access members of class instances? And maybe it is more appropriate to move this function to a unnamed namespace.
return (iterator());
}
const_iterator end() const {<--- Technically the member function 'pid::MemoryZone::end' can be static (but you may consider moving to unnamed namespace). [+]The member function 'pid::MemoryZone::end' can be made a static function. Making a function static can bring a performance benefit since no 'this' instance is passed to the function. This change should not cause compiler errors but it does not necessarily make sense conceptually. Think about your design and the task of the function first - is it a function that must not access members of class instances? And maybe it is more appropriate to move this function to a unnamed namespace.
return (const_iterator());
}
iterator start() {<--- Technically the member function 'pid::MemoryZone::start' can be static (but you may consider moving to unnamed namespace). [+]The member function 'pid::MemoryZone::start' can be made a static function. Making a function static can bring a performance benefit since no 'this' instance is passed to the function. This change should not cause compiler errors but it does not necessarily make sense conceptually. Think about your design and the task of the function first - is it a function that must not access members of class instances? And maybe it is more appropriate to move this function to a unnamed namespace.
return (iterator());
}
const_iterator start() const {<--- Technically the member function 'pid::MemoryZone::start' can be static (but you may consider moving to unnamed namespace). [+]The member function 'pid::MemoryZone::start' can be made a static function. Making a function static can bring a performance benefit since no 'this' instance is passed to the function. This change should not cause compiler errors but it does not necessarily make sense conceptually. Think about your design and the task of the function first - is it a function that must not access members of class instances? And maybe it is more appropriate to move this function to a unnamed namespace.
return (const_iterator());
}
// using the circular buffer as a FIFO queue
bool full() const {
return (capacity() == size());
}
bool empty() const {
return (size_ == 0);
}
void clear() {
for (unsigned int i = 0; i < capacity_; ++i) {
data_buffer_[i].reset();
}
first_element_.set(nullptr);
last_element_.set(nullptr);
next_element_slot_ = size_ = 0;
}
const_iterator
find(const std::function<bool(const_reference)>& condition) const {
for (auto iter = begin(); iter != end(); ++iter) {
if (condition(*iter)) {
return (iter);
}
}
return (end());
}
iterator find(const std::function<bool(const_reference)>& condition) {
for (auto iter = begin(); iter != end(); ++iter) {
if (condition(*iter)) {
return (iter);
}
}
return (end());
}
void print_memory(std::ostream& os) {
for (unsigned int i = 0; i < Limit; ++i) {
os << std::to_string(i) << ": "
<< reinterpret_cast<void*>(&data_buffer_[i])
<< " used: " << (data_buffer_[i].used() ? "YES" : "NO")
<< " pre: "
<< (data_buffer_[i].previous() == nullptr
? "NULL"
: std::to_string(data_buffer_[i].previous()->index()))
<< " next: "
<< (data_buffer_[i].next() == nullptr
? "NULL"
: std::to_string(data_buffer_[i].next()->index()))
<< std::endl;
}
}
};
} // namespace pid
|