emlabcpp
modern opinionated embedded C++ library
linked_list.h
Go to the documentation of this file.
1 
24 #pragma once
25 
26 #include <utility>
27 
28 namespace emlabcpp
29 {
30 
31 template < typename Base >
33 {
34 public:
35  linked_list_node_base() = default;
36 
39 
41  {
42  *this = std::move( other );
43  }
44 
46  {
47  if ( &other == this )
48  return *this;
49 
50  prev_ = other.prev_;
51  if ( prev_ != nullptr ) {
52  prev_->next_ = this;
53  other.prev_ = nullptr;
54  }
55 
56  next_ = other.next_;
57  if ( next_ != nullptr ) {
58  next_->prev_ = this;
59  other.next_ = nullptr;
60  }
61 
62  return *this;
63  }
64 
66  {
67  node.next_ = next_;
68  if ( next_ != nullptr )
69  next_->prev_ = &node;
70 
71  node.prev_ = this;
72  next_ = &node;
73  }
74 
75  virtual Base& operator*() = 0;
76  virtual Base const& operator*() const = 0;
77 
78  virtual Base* operator->() = 0;
79  virtual Base const* operator->() const = 0;
80 
82  {
83  return next_;
84  }
85 
86  [[nodiscard]] linked_list_node_base* get_next( std::size_t id )
87  {
88  auto* n = this;
89  while ( n != nullptr ) {
90  if ( id == 0 )
91  return n;
92  n = n->next_;
93  id -= 1;
94  }
95  return nullptr;
96  }
97 
98  [[nodiscard]] std::size_t count_next() const
99  {
100  std::size_t i = 0;
101  auto* n = next_;
102  while ( n != nullptr ) {
103  n = n->next_;
104  i += 1;
105  }
106  return i;
107  }
108 
110  {
111  return prev_;
112  }
113 
114  [[nodiscard]] linked_list_node_base* get_prev( std::size_t const id )
115  {
116  if ( id == 0 )
117  return this;
118  if ( prev_ == nullptr )
119  return nullptr;
120  return prev_->get_prev( id - 1 );
121  }
122 
124  {
125  if ( prev_ != nullptr )
126  prev_->next_ = next_;
127  if ( next_ != nullptr )
128  next_->prev_ = prev_;
129  }
130 
131 private:
132  linked_list_node_base* next_ = nullptr;
133  linked_list_node_base* prev_ = nullptr;
134 };
135 
136 template < typename T, typename Base >
138 {
139 public:
140  linked_list_node() = default;
141 
142  template < typename... Args >
143  explicit linked_list_node( Args&&... args )
144  : item_( std::forward< Args >( args )... )
145  {
146  }
147 
148  [[nodiscard]] T& get()
149  {
150  return item_;
151  }
152 
153  [[nodiscard]] T const& get() const
154  {
155  return item_;
156  }
157 
158  Base& operator*() override
159  {
160  return item_;
161  }
162 
163  Base const& operator*() const override
164  {
165  return item_;
166  }
167 
168  Base* operator->() override
169  {
170  return &item_;
171  }
172 
173  Base const* operator->() const override
174  {
175  return &item_;
176  }
177 
178  linked_list_node( linked_list_node&& other ) noexcept = default;
179 
180  linked_list_node& operator=( linked_list_node&& other ) noexcept = default;
181 
182 private:
183  T item_;
184 };
185 
186 } // namespace emlabcpp
Definition: linked_list.h:33
void link_as_next(linked_list_node_base< Base > &node)
Definition: linked_list.h:65
linked_list_node_base & operator=(linked_list_node_base &&other) noexcept
Definition: linked_list.h:45
virtual Base & operator*()=0
linked_list_node_base(linked_list_node_base &&other) noexcept
Definition: linked_list.h:40
linked_list_node_base * get_next()
Definition: linked_list.h:81
virtual Base * operator->()=0
std::size_t count_next() const
Definition: linked_list.h:98
linked_list_node_base * get_next(std::size_t id)
Definition: linked_list.h:86
virtual ~linked_list_node_base()
Definition: linked_list.h:123
linked_list_node_base & operator=(linked_list_node_base const &)=delete
linked_list_node_base(linked_list_node_base const &)=delete
virtual Base const & operator*() const =0
linked_list_node_base * get_prev(std::size_t const id)
Definition: linked_list.h:114
linked_list_node_base * get_prev()
Definition: linked_list.h:109
virtual Base const * operator->() const =0
Definition: linked_list.h:138
Base * operator->() override
Definition: linked_list.h:168
T const & get() const
Definition: linked_list.h:153
linked_list_node(Args &&... args)
Definition: linked_list.h:143
Base const & operator*() const override
Definition: linked_list.h:163
Base & operator*() override
Definition: linked_list.h:158
Base const * operator->() const override
Definition: linked_list.h:173
linked_list_node(linked_list_node &&other) noexcept=default
T & get()
Definition: linked_list.h:148
linked_list_node & operator=(linked_list_node &&other) noexcept=default
MIT License.
Definition: impl.h:31
Args const & args
Definition: min_max.h:83