|
1 | 1 | #pragma once |
2 | 2 |
|
| 3 | +#include <cassert> |
| 4 | +#include <iterator> |
| 5 | +#include <type_traits> |
| 6 | + |
3 | 7 | namespace cinder::arena { |
4 | 8 |
|
5 | 9 | template <typename T> |
6 | 10 | class ArenaList; |
7 | 11 |
|
8 | 12 | template <typename T> |
9 | 13 | struct ListHook { |
10 | | - T* previous; |
11 | | - T* next; |
| 14 | + T* previous = nullptr; |
| 15 | + T* next = nullptr; |
12 | 16 |
|
13 | 17 | template <typename> |
14 | 18 | friend class ArenaList; |
15 | 19 | }; |
16 | 20 |
|
17 | 21 | template <typename T> |
18 | 22 | class ArenaList { |
| 23 | + static_assert(std::is_base_of_v<ListHook<T>, T>, "T must inherit from ListHook<T>"); |
| 24 | + |
19 | 25 | public: |
20 | 26 | class iterator { |
21 | 27 | public: |
| 28 | + using iterator_category = std::forward_iterator_tag; |
| 29 | + using value_type = T; |
| 30 | + using difference_type = std::ptrdiff_t; |
| 31 | + using pointer = T*; |
| 32 | + using reference = T&; |
| 33 | + |
22 | 34 | explicit iterator(T* node) : node_(node) {} |
23 | 35 |
|
24 | | - T& operator*() const { |
25 | | - return *node_; |
26 | | - } |
27 | | - T* operator->() const { |
28 | | - return node_; |
29 | | - } |
| 36 | + T& operator*() const { return *node_; } |
| 37 | + T* operator->() const { return node_; } |
30 | 38 |
|
31 | 39 | iterator& operator++() { |
32 | | - node_ = node_->next; |
| 40 | + node_ = hook(node_).next; |
33 | 41 | return *this; |
34 | 42 | } |
35 | 43 |
|
36 | | - bool operator==(const iterator& other) const { |
37 | | - return node_ == other.node_; |
38 | | - } |
39 | | - bool operator!=(const iterator& other) const { |
40 | | - return node_ != other.node_; |
| 44 | + iterator operator++(int) { |
| 45 | + iterator old = *this; |
| 46 | + ++(*this); |
| 47 | + return old; |
41 | 48 | } |
42 | 49 |
|
| 50 | + bool operator==(const iterator& other) const { return node_ == other.node_; } |
| 51 | + |
| 52 | + bool operator!=(const iterator& other) const { return node_ != other.node_; } |
| 53 | + |
43 | 54 | private: |
44 | 55 | T* node_ = nullptr; |
45 | 56 | }; |
46 | 57 |
|
47 | | - iterator begin() { |
48 | | - return iterator(head_); |
49 | | - } |
50 | | - iterator end() { |
51 | | - return iterator(nullptr); |
52 | | - } |
| 58 | + ArenaList() = default; |
| 59 | + |
| 60 | + ArenaList(const ArenaList&) = delete; |
| 61 | + ArenaList& operator=(const ArenaList&) = delete; |
| 62 | + |
| 63 | + iterator begin() { return iterator(head_); } |
| 64 | + iterator end() { return iterator(nullptr); } |
| 65 | + |
| 66 | + [[nodiscard]] bool empty() const { return head_ == nullptr; } |
| 67 | + |
| 68 | + [[nodiscard]] T* front() const { return head_; } |
| 69 | + [[nodiscard]] T* back() const { return tail_; } |
| 70 | + |
| 71 | + void push_back(T* node) { |
| 72 | + assert(node != nullptr); |
| 73 | + |
| 74 | + auto& node_hook = hook(node); |
| 75 | + |
| 76 | + node_hook.previous = tail_; |
| 77 | + node_hook.next = nullptr; |
| 78 | + |
| 79 | + if (tail_) { |
| 80 | + hook(tail_).next = node; |
| 81 | + } else { |
| 82 | + head_ = node; |
| 83 | + } |
53 | 84 |
|
54 | | - [[nodiscard]] bool empty() const { |
55 | | - return head_ == nullptr; |
| 85 | + tail_ = node; |
56 | 86 | } |
57 | | - T* front() const { |
58 | | - return head_; |
| 87 | + |
| 88 | + void push_front(T* node) { |
| 89 | + assert(node != nullptr); |
| 90 | + |
| 91 | + auto& node_hook = hook(node); |
| 92 | + |
| 93 | + node_hook.previous = nullptr; |
| 94 | + node_hook.next = head_; |
| 95 | + |
| 96 | + if (head_) { |
| 97 | + hook(head_).previous = node; |
| 98 | + } else { |
| 99 | + tail_ = node; |
| 100 | + } |
| 101 | + |
| 102 | + head_ = node; |
59 | 103 | } |
60 | | - T* back() const { |
61 | | - return tail_; |
| 104 | + |
| 105 | + void remove(T* node) { |
| 106 | + assert(node != nullptr); |
| 107 | + |
| 108 | + auto& node_hook = hook(node); |
| 109 | + |
| 110 | + T* previous = node_hook.previous; |
| 111 | + T* next = node_hook.next; |
| 112 | + |
| 113 | + if (previous) { |
| 114 | + hook(previous).next = next; |
| 115 | + } else { |
| 116 | + head_ = next; |
| 117 | + } |
| 118 | + |
| 119 | + if (next) { |
| 120 | + hook(next).previous = previous; |
| 121 | + } else { |
| 122 | + tail_ = previous; |
| 123 | + } |
| 124 | + |
| 125 | + node_hook.previous = nullptr; |
| 126 | + node_hook.next = nullptr; |
62 | 127 | } |
63 | 128 |
|
| 129 | + private: |
| 130 | + static ListHook<T>& hook(T* node) { return static_cast<ListHook<T>&>(*node); } |
| 131 | + |
| 132 | + static const ListHook<T>& hook(const T* node) { return static_cast<const ListHook<T>&>(*node); } |
| 133 | + |
64 | 134 | private: |
65 | 135 | T* head_ = nullptr; |
66 | 136 | T* tail_ = nullptr; |
67 | 137 | }; |
| 138 | + |
68 | 139 | } // namespace cinder::arena |
0 commit comments