xtd 1.0.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 "../../string.hpp"
10#include "../../usize.hpp"
11#include <list>
12
14namespace xtd {
16 namespace collections {
18 namespace generic {
50 template<typename type_t, typename allocator_t>
52 public:
54
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 template<xtd::iterable iterable_t>
100 linked_list(iterable_t&& items) {
101 for (const auto& item : items)
102 add(item);
103 }
104
106 linked_list(std::initializer_list < type_t > items) {
107 for (const auto& item : items)
108 add(item);
109 }
110
113 template < std::input_iterator input_iterator_t >
114 linked_list(input_iterator_t first, input_iterator_t last) {
115 for (auto iterator = first; iterator != last; ++iterator)
116 add(*iterator);
117 }
118
119
121
126 [[nodiscard]] auto count() const noexcept -> size_type override {return data_->items.size();}
127
133 [[nodiscard]] 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;}
139 [[nodiscard]] 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;}
140
144 [[nodiscard]] auto items() const noexcept -> const base_type& {return data_->items;}
148 [[nodiscard]] auto items() noexcept -> base_type& {return data_->items;}
149
155 [[nodiscard]] 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;}
161 [[nodiscard]] 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;}
163
165
174 auto add_after(const linked_list_node<type_t>& node, const type_t& value) -> linked_list_node<type_t> {
175 auto new_node = linked_list_node {value};
176 add_after(node, new_node);
177 return new_node;
178 }
179
185 auto add_after(const linked_list_node<type_t>& node, linked_list_node<type_t>& new_node) -> void {
187 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.");
188 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.");
189 auto iterator = node.data_->iterator;
190 if (iterator != data_->items.end()) ++iterator;
191 auto result = data_->items.insert(iterator, std::move(*new_node.data_->value));
192 ++data_->version;
193 new_node = {self_, result, data_->version};
194 }
195
203 auto add_before(const linked_list_node<type_t>& node, const type_t& value) -> linked_list_node<type_t> {
204 auto new_node = linked_list_node {value};
205 add_before(node, new_node);
206 return new_node;
207 }
208
214 auto add_before(const linked_list_node<type_t>& node, linked_list_node<type_t>& new_node) -> void {
216 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.");
217 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.");
218 auto iterator = node.data_->iterator;
219 auto result = data_->items.insert(iterator, std::move(*new_node.data_->value));
220 ++data_->version;
221 new_node = {self_, result, data_->version};
222 }
223
228 auto add_first(const type_t& value) -> linked_list_node<type_t> {
229 auto new_node = linked_list_node {value};
230 add_first(new_node);
231 return new_node;
232 }
233
239 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.");
240 data_->items.push_front(node.data_->value.value());
241 ++data_->version;
242 node = {self_, data_->items.begin(), data_->version};
243 }
244
249 auto add_last(const type_t& value) -> linked_list_node<type_t> {
250 auto new_node = linked_list_node {value};
251 add_last(new_node);
252 return new_node;
253 }
254
259 auto add_last(linked_list_node<type_t>& node) -> void {
260 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.");
261 data_->items.push_back(node.data_->value.value());
262 ++data_->version;
263 auto tmp = data_->items.end();
264 node = {self_, --tmp, data_->version};
265 }
266
271 auto clear() -> void override {
272 data_->items.clear();
273 ++data_->version;
274 }
275
279 [[nodiscard]] auto contains(const type_t& value) const noexcept -> bool override {
280 for (const auto& item : data_->items)
281 if (xtd::collections::generic::helpers::equator<type_t> {}(item, value)) return true;
282 return false;
283 }
284
292 auto copy_to(xtd::array < type_t >& array, size_type array_index) const -> void override {
294 for (const auto& item : data_->items)
295 array[array_index++] = item;
296 }
297
303 [[nodiscard]] auto find(const type_t value) const noexcept -> xtd::optional<linked_list_node<type_t>> {
304 for (auto node = first(); node; node = node->next())
305 if (xtd::collections::generic::helpers::equator<type_t> {}(node->value(), value)) return node;
306 return xtd::nullopt;
307 }
308
314 [[nodiscard]] auto find_last(const type_t value) const noexcept -> xtd::optional<linked_list_node<type_t>> {
315 for (auto node = last(); node; node = node->previous())
316 if (xtd::collections::generic::helpers::equator<type_t> {}(node->value(), value)) return node;
317 return xtd::nullopt;
318 }
319
322 [[nodiscard]] enumerator<value_type> get_enumerator() const noexcept override {
323 struct linked_list_enumerator : public ienumerator < value_type > {
324 explicit linked_list_enumerator(const linked_list & items, xtd::usize version) : items_(items), version_(version) {}
325
326 [[nodiscard]] const value_type& current() const override {
328 if (version_ != items_.data_->version) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::invalid_operation, "Collection was modified; enumeration operation may not execute.");
329 return *iterator_;
330 }
331
332 [[nodiscard]] bool move_next() override {
333 if (version_ != items_.data_->version) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::invalid_operation, "Collection was modified; enumeration operation may not execute.");
334 if (index_++ && iterator_ != items_.data_->items.cend()) ++iterator_;
335 else iterator_ = items_.data_->items.cbegin();
336 return iterator_ != items_.data_->items.cend();
337 }
338
339 void reset() override {
340 index_ = 0;
341 version_ = items_.data_->version;
342 iterator_ = items_.data_->items.cend();
343 }
344
345 private:
346 size_type index_ = 0;
347 const linked_list& items_;
348 typename base_type::const_iterator iterator_ = items_.data_->items.cend();
349 size_type version_ = 0;
350 };
351 return {new_ptr < linked_list_enumerator > (self_, data_->version)};
352 }
353
359 auto remove(const type_t& item) noexcept -> bool override {
360 auto node = find(item);
361 if (node == nullopt) return false;
362 remove(*node);
363 return true;
364 }
365
369 auto remove(linked_list_node<type_t>& node) -> void {
372 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.");
373 node.data_->value = *node.data_->iterator;
374 data_->items.erase(node.data_->iterator);
375 ++data_->version;
376 node.data_->list = null;
377 }
378
382 auto remove_first() -> void {
384 data_->items.erase(data_->items.begin());
385 ++data_->version;
386 }
387
391 auto remove_last() -> void {
393 data_->items.erase(--data_->items.end());
394 ++data_->version;
395 }
396
399 [[nodiscard]] auto to_string() const noexcept -> xtd::string override {return xtd::string::format("[{}]", xtd::string::join(", ", self_));}
401
403
408 auto operator =(const linked_list & other) -> linked_list& = default;
413 data_->items = std::move(other.data_->items);
414 return self_;
415 }
416
419 auto operator =(const std::initializer_list < type_t >& items) -> linked_list& {
420 data_->items = items;
421 return self_;
422 }
423
426 operator const base_type& () const noexcept {return data_->items;}
429 operator base_type& () noexcept {return data_->items;}
431
432 private:
433 friend class linked_list_node<type_t>;
434 auto add(const type_t& item) -> void override {add_last(item);}
435 auto is_read_only() const noexcept -> bool override {return false;}
436 auto is_synchronized() const noexcept -> bool override {return false;}
437 const xtd::object& sync_root() const noexcept override {return data_->sync_root;}
438
439 struct linked_list_data {
440 base_type items;
441 xtd::object sync_root;
442 xtd::usize version = 0;
443 };
444
446 };
447
449 // Deduction guides for xtd::collections::generic::linked_list
450 // {
451 template < class type_t, typename allocator_t = xtd::collections::generic::helpers::allocator < type_t>>
452 linked_list(linked_list < type_t, allocator_t >&&) -> linked_list < type_t, allocator_t >;
453
454 template < class type_t, typename allocator_t = xtd::collections::generic::helpers::allocator < type_t>>
455 linked_list(const list < type_t, allocator_t >&) -> linked_list < type_t, allocator_t >;
456
457 template < class type_t>
458 linked_list(const std::list < type_t >&) -> linked_list < type_t >;
459
460 template < class type_t>
461 linked_list(std::list < type_t >&&) -> linked_list < type_t >;
462
463 template < class type_t>
464 linked_list(const ienumerable < type_t >&) -> linked_list < type_t >;
465
466 template <xtd::iterable iterable_t>
468
469 template < class type_t>
470 linked_list(std::initializer_list < type_t >) -> linked_list < type_t >;
471
472 template<typename input_iterator_t>
473 linked_list(input_iterator_t, input_iterator_t) -> linked_list<std::iter_value_t<input_iterator_t>>;
474 // }
476 }
477 }
478}
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition array.hpp:64
virtual auto length() const noexcept -> size_type
Gets a size that represents the total number of elements in all the dimensions of the array.
Definition basic_array.hpp:122
static auto join(const basic_string &separator, const collection_t &values) noexcept -> basic_string
Definition basic_string.hpp:1259
Defines methods to manipulate generic collections.
Definition icollection.hpp:45
virtual auto sync_root() const noexcept -> const xtd::object &=0
Gets an object that can be used to synchronize access to the the xtd::collections::generic::icollecti...
virtual auto is_synchronized() const noexcept -> bool=0
Gets a value indicating whether access to the xtd::collections::generic::icollection <type_t> is sync...
virtual auto count() const noexcept -> xtd::usize=0
Gets the number of elements contained in the xtd::collections::generic::icollection <type_t>.
typename xtd::collections::generic::ienumerable< xtd::any_object >::value_type value_type
Definition icollection.hpp:51
virtual auto is_read_only() const noexcept -> bool=0
Gets a value indicating whether the xtd::collections::generic::icollection <type_t> is read-only.
typename xtd::collections::generic::extensions::enumerable_iterators< value_type, xtd::collections::generic::ienumerable< value_type > >::iterator iterator
Definition ienumerable.hpp:48
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:106
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:139
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:314
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:249
linked_list()=default
Initializes a new instance of the xtd::collections::generic::linked_list <type_t> class that is empty...
linked_list(iterable_t &&items)
Initializes a new instance of the xtd::iterable that contains elements copied from the specified coll...
Definition linked_list.hpp:100
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:382
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:185
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:161
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:214
auto clear() -> void override
Removes all elements from the xtd::collections::generic::linked_list <type_t>.
Definition linked_list.hpp:271
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:259
auto to_string() const noexcept -> xtd::string override
Returns a xtd::string that represents the current object.
Definition linked_list.hpp:399
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:359
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:114
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:155
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:292
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:369
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:133
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:279
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:238
auto items() noexcept -> base_type &
Returns the underlying base type items.
Definition linked_list.hpp:148
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:228
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:126
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:174
auto items() const noexcept -> const base_type &
Definition linked_list.hpp:144
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:322
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:203
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:303
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:391
xtd::usize size_type
Represents the list size type (usually xtd::usize).
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:82
static auto throws(xtd::helpers::exception_case exception_case, const source_location &location=source_location::current()) -> void
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:40
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
static auto format(const basic_string< char > &fmt, args_t &&... args) -> basic_string
@ 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
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:26
std::size_t usize
Represents an unsigned size of any object in bytes.
Definition usize.hpp:22
constexpr null_opt nullopt
Represents a nullopt value. Used to indicate that an std::optional does not contain a value.
Definition nullopt.hpp:26
auto new_ptr(args_t &&... args) -> xtd::ptr< type_t >
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
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:39
Implements a function object for performing comparisons. Unless specialised, invokes operator== on ty...
Definition equator.hpp:39
Contains xtd::usize type.