xtd 0.2.0
Loading...
Searching...
No Matches
linked_list.hpp
Go to the documentation of this file.
1
4#pragma once
5#include "helpers/equator.hpp"
7#include "icollection.hpp"
9#include "../../size.hpp"
10#include "../../string.hpp"
11#include <list>
12
14namespace xtd {
16 namespace collections {
18 namespace generic {
50 template<class type_t, class allocator_t>
51 class linked_list : public xtd::object, public xtd::collections::generic::icollection<type_t> {
52 public:
54
57 using value_type = typename icollection<type_t>::value_type;
59 using base_type = std::list<value_type, allocator_t>;
67
69
75 linked_list() = default;
81 linked_list(const linked_list & list) {*data_ = *list.data_;}
84 linked_list(base_type&& list) {data_->items = std::move(list);}
87 linked_list(const base_type& list) {data_->items = list;}
93 linked_list(const xtd::collections::generic::ienumerable < type_t >& collection) {
94 for (const auto& item : collection)
95 add(item);
96 }
97
99 linked_list(std::initializer_list < type_t > items) {
100 for (const auto& item : items)
101 add(item);
102 }
103
106 template < std::input_iterator input_iterator_t >
107 linked_list(input_iterator_t first, input_iterator_t last) {
108 for (auto iterator = first; iterator != last; ++iterator)
109 add(*iterator);
110 }
111
112
114
119 auto count() const noexcept -> size_type override {return data_->items.size();}
120
126 auto first() const noexcept -> xtd::optional<linked_list_node<type_t>> {return count() ? xtd::optional<linked_list_node<type_t>> {linked_list_node<type_t> {const_cast<linked_list&>(self_), data_->items.begin(), data_->version}} : xtd::nullopt;}
132 auto first() noexcept -> xtd::optional<linked_list_node<type_t>> {return count() ? xtd::optional<linked_list_node<type_t>> {linked_list_node<type_t> {self_, data_->items.begin(), data_->version}} : xtd::nullopt;}
133
137 auto items() const noexcept -> const base_type& {return data_->items;}
141 auto items() noexcept -> base_type& {return data_->items;}
142
148 auto last() const noexcept -> xtd::optional<linked_list_node<type_t>> {return count() ? xtd::optional<linked_list_node<type_t>> {linked_list_node<type_t> {const_cast<linked_list&>(self_), --data_->items.end(), data_->version}} : xtd::nullopt;}
154 auto last() noexcept -> xtd::optional<linked_list_node<type_t>> {return count() ? xtd::optional<linked_list_node<type_t>> {linked_list_node<type_t> {self_, --data_->items.end(), data_->version}} : xtd::nullopt;}
156
158
167 auto add_after(const linked_list_node<type_t>& node, const type_t& value) -> linked_list_node<type_t> {
168 auto new_node = linked_list_node {value};
169 add_after(node, new_node);
170 return new_node;
171 }
172
178 auto add_after(const linked_list_node<type_t>& node, linked_list_node<type_t>& new_node) -> void {
180 if (new_node.data_->list || !new_node.data_->value.has_value()) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::invalid_operation, "The linked_list node belongs to a linked_list.");
181 if (node.data_->version != data_->version) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::invalid_operation, "Collection was modified; enumeration operation may not execute.");
182 auto iterator = node.data_->iterator;
183 if (iterator != data_->items.end()) ++iterator;
184 auto result = data_->items.insert(iterator, std::move(*new_node.data_->value));
185 ++data_->version;
186 new_node = {self_, result, data_->version};
187 }
188
196 auto add_before(const linked_list_node<type_t>& node, const type_t& value) -> linked_list_node<type_t> {
197 auto new_node = linked_list_node {value};
198 add_before(node, new_node);
199 return new_node;
200 }
201
207 auto add_before(const linked_list_node<type_t>& node, linked_list_node<type_t>& new_node) -> void {
209 if (new_node.data_->list || !new_node.data_->value.has_value()) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::invalid_operation, "The linked_list node belongs to a linked_list.");
210 if (node.data_->version != data_->version) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::invalid_operation, "Collection was modified; enumeration operation may not execute.");
211 auto iterator = node.data_->iterator;
212 auto result = data_->items.insert(iterator, std::move(*new_node.data_->value));
213 ++data_->version;
214 new_node = {self_, result, data_->version};
215 }
216
221 auto add_first(const type_t& value) -> linked_list_node<type_t> {
222 auto new_node = linked_list_node {value};
223 add_first(new_node);
224 return new_node;
225 }
226
232 if (node.data_->list || !node.data_->value.has_value()) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::invalid_operation, "The linked_list node belongs to a linked_list.");
233 data_->items.push_front(node.data_->value.value());
234 ++data_->version;
235 node = {self_, data_->items.begin(), data_->version};
236 }
237
242 auto add_last(const type_t& value) -> linked_list_node<type_t> {
243 auto new_node = linked_list_node {value};
244 add_last(new_node);
245 return new_node;
246 }
247
252 auto add_last(linked_list_node<type_t>& node) -> void {
253 if (node.data_->list || !node.data_->value.has_value()) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::invalid_operation, "The linked_list node belongs to a linked_list.");
254 data_->items.push_back(node.data_->value.value());
255 ++data_->version;
256 auto tmp = data_->items.end();
257 node = {self_, --tmp, data_->version};
258 }
259
264 auto clear() -> void override {
265 data_->items.clear();
266 ++data_->version;
267 }
268
272 auto contains(const type_t& value) const noexcept -> bool override {
273 for (const auto& item : data_->items)
274 if (xtd::collections::generic::helpers::equator<type_t> {}(item, value)) return true;
275 return false;
276 }
277
285 auto copy_to(xtd::array < type_t >& array, size_type array_index) const -> void override {
287 for (const auto& item : data_->items)
288 array[array_index++] = item;
289 }
290
296 auto find(const type_t value) const noexcept -> xtd::optional<linked_list_node<type_t>> {
297 for (auto node = first(); node; node = node->next())
298 if (xtd::collections::generic::helpers::equator<type_t> {}(node->value(), value)) return node;
299 return xtd::nullopt;
300 }
301
307 auto find_last(const type_t value) const noexcept -> xtd::optional<linked_list_node<type_t>> {
308 for (auto node = last(); node; node = node->previous())
309 if (xtd::collections::generic::helpers::equator<type_t> {}(node->value(), value)) return node;
310 return xtd::nullopt;
311 }
312
315 enumerator<value_type> get_enumerator() const noexcept override {
316 struct linked_list_enumerator : public ienumerator < value_type > {
317 explicit linked_list_enumerator(const linked_list & items, xtd::size version) : items_(items), version_(version) {}
318
319 const value_type& current() const override {
321 if (version_ != items_.data_->version) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::invalid_operation, "Collection was modified; enumeration operation may not execute.");
322 return *iterator_;
323 }
324
325 bool move_next() override {
326 if (version_ != items_.data_->version) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::invalid_operation, "Collection was modified; enumeration operation may not execute.");
327 if (index_++ && iterator_ != items_.data_->items.cend()) ++iterator_;
328 else iterator_ = items_.data_->items.cbegin();
329 return iterator_ != items_.data_->items.cend();
330 }
331
332 void reset() override {
333 index_ = 0;
334 version_ = items_.data_->version;
335 iterator_ = items_.data_->items.cend();
336 }
337
338 private:
339 size_type index_ = 0;
340 const linked_list& items_;
341 typename base_type::const_iterator iterator_ = items_.data_->items.cend();
342 size_type version_ = 0;
343 };
344 return {new_ptr < linked_list_enumerator > (self_, data_->version)};
345 }
346
352 auto remove(const type_t& item) noexcept -> bool override {
353 auto node = find(item);
354 if (node == nullopt) return false;
355 remove(*node);
356 return true;
357 }
358
362 auto remove(linked_list_node<type_t>& node) -> void {
365 if (node.data_->version != data_->version) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::invalid_operation, "Collection was modified; enumeration operation may not execute.");
366 node.data_->value = *node.data_->iterator;
367 data_->items.erase(node.data_->iterator);
368 ++data_->version;
369 node.data_->list = null;
370 }
371
375 auto remove_first() -> void {
377 data_->items.erase(data_->items.begin());
378 ++data_->version;
379 }
380
384 auto remove_last() -> void {
386 data_->items.erase(--data_->items.end());
387 ++data_->version;
388 }
389
392 auto to_string() const noexcept -> xtd::string override {return xtd::string::format("[{}]", xtd::string::join(", ", self_));}
394
396
401 auto operator =(const linked_list & other) -> linked_list& = default;
406 data_->items = std::move(other.data_->items);
407 return self_;
408 }
409
412 auto operator =(const std::initializer_list < type_t >& items) -> linked_list& {
413 data_->items = items;
414 return self_;
415 }
416
419 operator const base_type& () const noexcept {return data_->items;}
422 operator base_type& () noexcept {return data_->items;}
424
425 private:
426 friend class linked_list_node<type_t>;
427 auto add(const type_t& item) -> void override {add_last(item);}
428 auto is_read_only() const noexcept -> bool override {return false;}
429 auto is_synchronized() const noexcept -> bool override {return false;}
430 const xtd::object& sync_root() const noexcept override {return data_->sync_root;}
431
432 struct linked_list_data {
433 base_type items;
434 xtd::object sync_root;
435 xtd::size version = 0;
436 };
437
439 };
440
442 // Deduction guides for xtd::collections::generic::linked_list
443 // {
444 template < class type_t, class allocator_t = xtd::collections::generic::helpers::allocator < type_t>>
445 linked_list(linked_list < type_t, allocator_t >&&) -> linked_list < type_t, allocator_t >;
446
447 template < class type_t, class allocator_t = xtd::collections::generic::helpers::allocator < type_t>>
448 linked_list(const list < type_t, allocator_t >&) -> linked_list < type_t, allocator_t >;
449
450 template < class type_t>
451 linked_list(const std::list < type_t >&) -> linked_list < type_t >;
452
453 template < class type_t>
454 linked_list(std::list < type_t >&&) -> linked_list < type_t >;
455
456 template < class type_t>
457 linked_list(const ienumerable < type_t >&) -> linked_list < type_t >;
458
459 template < class type_t>
460 linked_list(std::initializer_list < type_t >) -> linked_list < type_t >;
461
462 template <class input_iterator_t>
463 linked_list(input_iterator_t, input_iterator_t) -> linked_list<typename input_iterator_t::value_type>;
464 // }
466 }
467 }
468}
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition array.hpp:64
virtual size_type length() const noexcept
Gets a size that represents the total number of elements in all the dimensions of the array.
Definition basic_array.hpp:124
Supports a simple iteration over a generic collection.
Definition ienumerator.hpp:58
Represents a node in a LinkedList<T>. This class cannot be inherited.
Definition linked_list_node.hpp:43
Represents a doubly linked list.
Definition linked_list.hpp:51
linked_list(std::initializer_list< type_t > items)
Constructs the container with the contents of the specified initializer list, and allocator.
Definition linked_list.hpp:99
auto first() noexcept -> xtd::optional< linked_list_node< type_t > >
Gets the first node of the xtd::collections::generic::linked_list <type_t>.
Definition linked_list.hpp:132
linked_list(const xtd::collections::generic::ienumerable< type_t > &collection)
Initializes a new instance of the xtd::collections::generic::linked_list <type_t> class that contains...
Definition linked_list.hpp:93
auto find_last(const type_t value) const noexcept -> xtd::optional< linked_list_node< type_t > >
Finds the last node that contains the specified value.
Definition linked_list.hpp:307
auto add_last(const type_t &value) -> linked_list_node< type_t >
Adds a new node containing the specified value at the end of the xtd::collections::generic::linked_li...
Definition linked_list.hpp:242
linked_list()=default
Initializes a new instance of the xtd::collections::generic::linked_list <type_t> class that is empty...
std::list< value_type, allocator_t > base_type
Represents the list base type.
Definition linked_list.hpp:59
linked_list(const linked_list &list)
Default copy constructor with specified list.
Definition linked_list.hpp:81
auto remove_first() -> void
Removes the node at the start of the xtd::collections::generic::linked_list <type_t>.
Definition linked_list.hpp:375
auto add_after(const linked_list_node< type_t > &node, linked_list_node< type_t > &new_node) -> void
Adds the specified new node after the specified existing node in the xtd::collections::generic::linke...
Definition linked_list.hpp:178
auto last() noexcept -> xtd::optional< linked_list_node< type_t > >
Gets the last node of the xtd::collections::generic::linked_list <type_t>.
Definition linked_list.hpp:154
auto add_before(const linked_list_node< type_t > &node, linked_list_node< type_t > &new_node) -> void
Adds the specified new node before the specified existing node in the xtd::collections::generic::link...
Definition linked_list.hpp:207
auto clear() -> void override
Removes all elements from the xtd::collections::generic::linked_list <type_t>.
Definition linked_list.hpp:264
auto add_last(linked_list_node< type_t > &node) -> void
Adds the specified new node at the end of the xtd::collections::generic::linked_list <type_t>.
Definition linked_list.hpp:252
auto to_string() const noexcept -> xtd::string override
Returns a xtd::string that represents the current object.
Definition linked_list.hpp:392
auto operator=(const linked_list &other) -> linked_list &=default
Copy assignment operator. Replaces the contents with a copy of the contents of other.
auto remove(const type_t &item) noexcept -> bool override
Removes the first occurrence of a specific object from the xtd::collections::generic::linked_list <ty...
Definition linked_list.hpp:352
linked_list(input_iterator_t first, input_iterator_t last)
Constructs the container with the contents of the range [first, last).
Definition linked_list.hpp:107
linked_list(linked_list &&list)=default
Move constructor with specified list.
auto last() const noexcept -> xtd::optional< linked_list_node< value_type > >
Definition linked_list.hpp:148
xtd::size size_type
Represents the list size type (usually xtd::size).
Definition linked_list.hpp:61
auto copy_to(xtd::array< type_t > &array, size_type array_index) const -> void override
Copies the entire xtd::colllections::generic::linked_list <type_t> to a compatible one-dimensional ar...
Definition linked_list.hpp:285
auto remove(linked_list_node< type_t > &node) -> void
Removes the specified node from the xtd::collections::generic::linked_list <type_t>.
Definition linked_list.hpp:362
linked_list(base_type &&list)
Move constructor with specified base type list.
Definition linked_list.hpp:84
auto first() const noexcept -> xtd::optional< linked_list_node< value_type > >
Definition linked_list.hpp:126
typename icollection< type_t >::value_type value_type
Represents the list value type.
Definition linked_list.hpp:57
auto contains(const type_t &value) const noexcept -> bool override
Determines whether an element is in the xtd::colllections::generic::linked_list <type_t>.
Definition linked_list.hpp:272
auto add_first(linked_list_node< type_t > &node) -> void
Adds the specified new node at the start of the xtd::collections::generic::linked_list <type_t>.
Definition linked_list.hpp:231
auto items() noexcept -> base_type &
Returns the underlying base type items.
Definition linked_list.hpp:141
auto add_first(const type_t &value) -> linked_list_node< type_t >
Adds a new node containing the specified value at the start of the xtd::collections::generic::linked_...
Definition linked_list.hpp:221
auto count() const noexcept -> size_type override
Gets the number of nodes actually contained in the xtd::collections::generic::linked_list <type_t>.
Definition linked_list.hpp:119
const value_type & const_reference
Represents the const reference of list value type.
Definition linked_list.hpp:65
auto add_after(const linked_list_node< type_t > &node, const type_t &value) -> linked_list_node< type_t >
Adds a new node containing the specified value after the specified existing node in the xtd::collecti...
Definition linked_list.hpp:167
auto items() const noexcept -> const base_type &
Definition linked_list.hpp:137
enumerator< value_type > get_enumerator() const noexcept override
Returns an enumerator that iterates through the xtd::collections::generic::linked_list <type_t>.
Definition linked_list.hpp:315
linked_list(const base_type &list)
Copy constructor with specified base type list.
Definition linked_list.hpp:87
auto add_before(const linked_list_node< type_t > &node, const type_t &value) -> linked_list_node< type_t >
Adds a new node containing the specified value before the specified existing node in the xtd::collect...
Definition linked_list.hpp:196
auto find(const type_t value) const noexcept -> xtd::optional< linked_list_node< type_t > >
Finds the first node that contains the specified value.
Definition linked_list.hpp:296
value_type & reference
Represents the reference of list value type.
Definition linked_list.hpp:63
auto remove_last() -> void
Removes the node at the end of the xtd::collections::generic::linked_list <type_t>.
Definition linked_list.hpp:384
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
Contains xtd::collections::generic::helpers::equator struct.
Contains xtd::collections::generic::icollection <type_t> interface.
generic::ienumerable< xtd::any_object > ienumerable
Exposes an enumerator, which supports a simple iteration over a non-generic collection.
Definition ienumerable.hpp:32
@ argument_out_of_range
The argument is out of range.
Definition exception_case.hpp:35
@ invalid_operation
The operation is not valid.
Definition exception_case.hpp:65
#define self_
The self_ expression is a reference value expression whose value is the reference of the implicit obj...
Definition self.hpp:20
size_t size
Represents a size of any object in bytes.
Definition size.hpp:23
null_ptr null
Represents a null pointer value.
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
@ other
The operating system is other.
Definition platform_id.hpp:60
@ add
The Add key.
Definition console_key.hpp:170
Contains xtd::collections::generic::linked_list_node <type_t> class.
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
Contains xtd::collections::object_model::read_only_collection class.
Contains xtd::string alias.
Supports a simple iteration over a generic collection.
Definition enumerator.hpp:38
Implements a function object for performing comparisons. Unless specialised, invokes operator== on ty...
Definition equator.hpp:39
Contains xtd::size type.