libbmb
Modern implementation of STL
Loading...
Searching...
No Matches
llist_iterator.h
Go to the documentation of this file.
1#pragma once
8#include <cstddef>
9
10#include "details/llist_nodes.h"
11#include "utils/iterators.h"
12#include "utils/type_traits.h"
13
14namespace bmb {
15
16template <typename, bool, bool, typename>
17class LinkedList;
18
19namespace detail {
20
26template <typename T, bool IsConst>
27class LListIter {
28 using Self = LListIter;
29
30 // NOTE: Nodes are never const. We provide
31 // const-correctness through 'reference' and 'pointer' usings
32 using Node = llist::Node<T>;
34
35 template <typename, bool, bool, typename>
36 friend class bmb::LinkedList;
37
38public:
44
46 : node_(nullptr) {};
47
49 : node_(node) {};
50
51 // Want a conversion non-const -> const underlying type
53
54 pointer operator->() const noexcept { return &static_cast<Node*>(node_)->val; }
55 reference operator*() const noexcept { return static_cast<Node*>(node_)->val; }
56
58 node_ = node_->next;
59 return *this;
60 }
61
62 Self operator++(int) noexcept {
63 Self copy(*this);
64 node_ = node_->next;
65 return copy;
66 }
67
68 bool operator==(const Self&) const = default;
69
70private:
71 // Sometimes want to return given const_iterator,
72 // but non-const
74 return LListIter<T, false>(node_);
75 }
76
77 BaseNode* node_;
78};
79
80} // namespace detail
81} // namespace bmb
Singly linked list.
Definition linked_list.h:53
Forward iterator for linked list.
Definition llist_iterator.h:27
pointer operator->() const noexcept
Definition llist_iterator.h:54
ptrdiff_t difference_type
Definition llist_iterator.h:42
bool operator==(const Self &) const =default
Self & operator++() noexcept
Definition llist_iterator.h:57
Self operator++(int) noexcept
Definition llist_iterator.h:62
LListIter() noexcept
Definition llist_iterator.h:45
reference operator*() const noexcept
Definition llist_iterator.h:55
conditional_t< IsConst, const T, T > * pointer
Definition llist_iterator.h:41
remove_const_t< T > value_type
Definition llist_iterator.h:39
LListIter(BaseNode *node) noexcept
Definition llist_iterator.h:48
conditional_t< IsConst, const T, T > & reference
Definition llist_iterator.h:40
Definition algo_base.h:14
conditional< Cond, IfTrue, IfFalse >::type conditional_t
conditional_t
Definition type_traits.h:197
remove_const< T >::type remove_const_t
remove_const_t
Definition type_traits.h:162
LinkedList(Iter, Iter, Allocator=Allocator()) -> LinkedList< typename IteratorTraits< Iter >::value_type, true, true, PrimitiveAllocator >
constexpr T && forward(remove_ref_t< T > &value) noexcept
Forward a lvalue.
Definition move.h:33
forward_iter_tag
Definition iterators.h:22
Definition llist_nodes.h:13
BaseNode * next
Definition llist_nodes.h:14
Definition llist_nodes.h:18