xtd 0.2.0
Loading...
Searching...
No Matches
linked_list_node.hpp
Go to the documentation of this file.
1
4#pragma once
6#include "../../null.hpp"
7#include "../../nullopt.hpp"
8#include "../../object.hpp"
9#include "../../optional.hpp"
10#include "../../ref.hpp"
11
13namespace xtd {
15 namespace collections {
17 namespace generic {
19 template<class type_t, class allocator_t = xtd::collections::generic::helpers::allocator<type_t>>
20 class linked_list;
22
42 template<class type_t>
43 class linked_list_node final : public xtd::object {
44 public:
46
49 using value_type = type_t;
57
59
67 linked_list_node(const value_type & value) {data_->value = value;}
69
71 linked_list_node(value_type&& value) {data_->value = std::move(value);}
73 linked_list_node(const linked_list_node&) = default;
75
77
84 auto list() const noexcept -> xtd::ref<const linked_list<type_t>> {return data_->list ? ref {*data_->list} : xtd::ref<linked_list<type_t>> {};}
85
91 auto next() const -> xtd::optional<linked_list_node> {
92 check_stale();
93 if (!data_->list || !data_->list->count() || data_->iterator == end()) return xtd::nullopt;
94 auto tmp = data_->iterator;
95 if (++tmp == end()) return xtd::nullopt;
96 return linked_list_node {*data_->list, tmp, data_->version};
97 }
98
105 check_stale();
106 if (!data_->list || !data_->list->count() || data_->iterator == begin()) return xtd::nullopt;
107 auto tmp = data_->iterator;
108 return linked_list_node {*data_->list, --tmp, data_->version};
109 }
110
117 auto value() const -> const value_type& {
118 check_stale();
119 return data_->value.has_value() ? data_->value.value() : *data_->iterator;
120 }
121
127 auto value() -> value_type& {
128 check_stale();
129 return data_->value.has_value() ? data_->value.value() : *data_->iterator;
130 }
131
132
134 auto operator =(linked_list_node&&) -> linked_list_node& = default;
135 auto operator =(const linked_list_node&) -> linked_list_node& = default;
137
138 private:
139 friend class linked_list<type_t>;
140 using iterator_type = typename base_type::iterator;
141
142 linked_list_node(linked_list_type & list, iterator_type iterator, size_type version) {
143 data_->list = &list;
144 data_->iterator = iterator;
145 data_->version = version;
146 }
147
148 auto begin() const -> iterator_type {return data_->list->data_->items.begin();}
149 auto check_stale() const -> void {if (data_->list && data_->list->data_->version != data_->version) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::invalid_operation, "Collection was modified; node operation may not execute.");}
150 auto end() const -> iterator_type {return data_->list->data_->items.end();}
151
152 struct node_data {
153 linked_list<type_t>* list = null;
154 iterator_type iterator;
156 size_type version;
157 };
158
160 };
161 }
162 }
163}
Contains xtd::collections::generic::helpers::allocator alias.
Represents a node in a LinkedList<T>. This class cannot be inherited.
Definition linked_list_node.hpp:43
type_t value_type
Represents the linked list node value type.
Definition linked_list_node.hpp:49
auto value() -> value_type &
Gets the value contained in the node.
Definition linked_list_node.hpp:127
auto previous() const -> xtd::optional< linked_list_node >
Gets the previous node in the LinkedList<T>.
Definition linked_list_node.hpp:104
auto next() const -> xtd::optional< linked_list_node >
Gets the next node in the LinkedList<T>.
Definition linked_list_node.hpp:91
auto list() const noexcept -> xtd::ref< const linked_list< type_t > >
Gets the xtd::collections::generic::linked_list <type_t> that the xtd::collections::generic::linked_l...
Definition linked_list_node.hpp:84
typename linked_list_type::base_type base_type
Represents the linked list base type.
Definition linked_list_node.hpp:53
auto value() const -> const value_type &
Gets the value contained in the node.
Definition linked_list_node.hpp:117
linked_list< value_type > linked_list_type
Represents the linked list type.
Definition linked_list_node.hpp:51
linked_list_node(const value_type &value)
Initializes a new instance of the xtd::collections::generic::linked_list_node <type_t> class,...
Definition linked_list_node.hpp:67
typename linked_list_type::size_type size_type
Represents the size type.
Definition linked_list_node.hpp:55
Represents a doubly linked list.
Definition linked_list.hpp:51
std::list< value_type, allocator_t > base_type
Definition linked_list.hpp:59
xtd::size size_type
Definition linked_list.hpp:61
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
Definition list.hpp:80
static void throws(xtd::helpers::exception_case exception_case, const source_location &location=source_location::current())
Throws an exption with specified exception case.
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.hpp:44
Represents the version number of an assembly, operating system, or the xtd. This class cannot be inhe...
Definition version.hpp:115
@ invalid_operation
The operation is not valid.
Definition exception_case.hpp:65
null_ptr null
Represents a null pointer value.
xtd::reference_wrapper_object< type_t > ref
The xtd::ref object is a reference wrapper.
Definition ref.hpp:25
xtd::sptr< type_t > ptr
The xtd::ptr object is a shared pointer.
Definition ptr.hpp:27
std::optional< type_t > optional
Represents the optional alias on std::optional.
Definition optional.hpp:25
constexpr null_opt nullopt
Represents a nullopt value. Used to indicate that an std::optional does not contain a value.
Definition nullopt.hpp:26
ptr< type_t > new_ptr(args_t &&... args)
The xtd::new_ptr operator creates a xtd::ptr object.
Definition new_ptr.hpp:24
The xtd::collections::generic namespace contains interfaces and classes that define generic collectio...
Definition comparer.hpp:16
The xtd::collections namespace contains interfaces and classes that define various collections of obj...
Definition any_pair.hpp:10
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8
const_iterator begin() const
Returns an iterator to the beginning.
Definition read_only_span.hpp:183
Contains xtd::null pointer valiue.
Contains xtd::nullopt valiue.
Contains xtd::object class.
Contains xtd::optional type.
Contains xtd::ref type.