Recti 1.2.4
Loading...
Searching...
No Matches
rdllist.hpp
Go to the documentation of this file.
1
5#pragma once
6
7#include <cstddef> // for size_t
8#include <vector>
9
10#include "dllink.hpp"
11
20 private:
21 Dllink<size_t>* cur;
22 Dllink<size_t>* stop;
23
24 public:
25 constexpr explicit RDllIterator(Dllink<size_t>* node) noexcept : cur{node}, stop{node} {}
26
27 constexpr auto operator++() noexcept -> RDllIterator& {
28 cur = cur->next;
29 return *this;
30 }
31
32 constexpr auto operator*() const noexcept -> const Dllink<size_t>& { return *cur; }
33
34 constexpr auto operator!=(const RDllIterator& other) const noexcept -> bool {
35 return cur != other.cur;
36 }
37
38 constexpr auto operator==(const RDllIterator& other) const noexcept -> bool {
39 return cur == other.cur;
40 }
41
42 // For range-based for loop support
43 constexpr auto begin() const noexcept -> RDllIterator { return RDllIterator{cur->next}; }
44
45 constexpr auto end() const noexcept -> RDllIterator { return RDllIterator{stop}; }
46};
47
55class RDllist {
56 public:
57 std::vector<Dllink<size_t>> cycle;
58
59 public:
70 explicit RDllist(size_t num_nodes, bool reverse = false) {
71 cycle.reserve(3 * num_nodes); // must reserve enough space for expansion!
72 for (size_t k = 0; k < num_nodes; ++k) {
73 cycle.emplace_back(Dllink<size_t>(k));
74 }
75
76 Dllink<size_t>* dl2 = &cycle.back();
77
78 if (!reverse) {
79 for (auto& dl1 : cycle) {
80 dl2->next = &dl1;
81 dl1.prev = dl2;
82 dl2 = &dl1;
83 }
84 } else {
85 for (auto& dl1 : cycle) {
86 dl2->prev = &dl1;
87 dl1.next = dl2;
88 dl2 = &dl1;
89 }
90 }
91 }
92
99 auto operator[](size_t k) -> Dllink<size_t>& { return cycle.at(k); }
100
107 auto operator[](size_t k) const -> const Dllink<size_t>& { return cycle.at(k); }
108
115 auto from_node(size_t k) -> RDllIterator { return RDllIterator{&cycle.at(k)}; }
116
123 auto from_node(size_t k) const -> RDllIterator {
124 return RDllIterator{const_cast<Dllink<size_t>*>(&cycle.at(k))};
125 }
126
132 auto begin() -> RDllIterator { return from_node(0); }
133
139 auto begin() const -> RDllIterator { return from_node(0); }
140
141 // auto end() -> RDllIterator { return RDllIterator{nullptr}; }
142 // auto end() const -> RDllIterator { return RDllIterator{nullptr}; }
143 // end() is unused for circular iteration — from_node(k) provides the stop sentinel
144};
Iterator for RDllist circular doubly-linked list.
Definition rdllist.hpp:19
constexpr auto end() const noexcept -> RDllIterator
Definition rdllist.hpp:45
constexpr auto operator++() noexcept -> RDllIterator &
Definition rdllist.hpp:27
constexpr auto operator==(const RDllIterator &other) const noexcept -> bool
Definition rdllist.hpp:38
constexpr auto operator*() const noexcept -> const Dllink< size_t > &
Definition rdllist.hpp:32
constexpr auto operator!=(const RDllIterator &other) const noexcept -> bool
Definition rdllist.hpp:34
constexpr auto begin() const noexcept -> RDllIterator
Definition rdllist.hpp:43
constexpr RDllIterator(Dllink< size_t > *node) noexcept
Definition rdllist.hpp:25
Circular doubly-linked list implementation.
Definition rdllist.hpp:55
auto begin() const -> RDllIterator
Get an iterator to the beginning of the list (const).
Definition rdllist.hpp:139
std::vector< Dllink< size_t > > cycle
Storage for all list nodes.
Definition rdllist.hpp:57
auto operator[](size_t k) -> Dllink< size_t > &
Access a node by index (mutable).
Definition rdllist.hpp:99
auto from_node(size_t k) const -> RDllIterator
Get an iterator starting from a given node (const).
Definition rdllist.hpp:123
auto begin() -> RDllIterator
Get an iterator to the beginning of the list.
Definition rdllist.hpp:132
RDllist(size_t num_nodes, bool reverse=false)
Construct a new RDllist object.
Definition rdllist.hpp:70
auto operator[](size_t k) const -> const Dllink< size_t > &
Access a node by index (const).
Definition rdllist.hpp:107
auto from_node(size_t k) -> RDllIterator
Get an iterator starting from a given node (mutable).
Definition rdllist.hpp:115