Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_dynDlist.H
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
43#ifndef TPL_DYNDLIST_H
44#define TPL_DYNDLIST_H
45
46#include <ahFunctional.H>
47#include <ahDry.H>
48#include <ah-dry.H>
49#include <ah-args-ctor.H>
50#include <ahIterator.H>
51#include <ah-iterator.H>
52#include <tpl_dnode.H>
53#include <stdexcept>
54#include <type_traits>
55#include <utility>
56#include <ah-errors.H>
57
58namespace Aleph {
59
116template <typename T = int>
117class DynDlist : public Dnode<T>,
118 public GenericTraverse<DynDlist<T>>,
119 public SpecialCtors<DynDlist<T>, T>,
120 public LocateFunctions<DynDlist<T>, T>,
121 public FunctionalMethods<DynDlist<T>, T>,
122 public GenericItems<DynDlist<T>, T>,
123 public EqualSequenceMethod<DynDlist<T>>,
124 public StlAlephIterator<DynDlist<T>>
125{
126 size_t num_elem = 0;
127
128public:
131
133 using Item_Type = T;
134
136 using Key_Type = T;
137
139 [[nodiscard]] const size_t &size() const noexcept
140 {
141 return num_elem;
142 }
143
144 DynDlist() = default;
145
146 // using CtorBase = SpecialCtors<DynDlist<T>, T>;
147 // using CtorBase::CtorBase;
148
149 // /** Construct a new list with copies of elements of list `l`
150
151 // \param[in] l list to be copied
152 // \throw bad_alloc if there is no enough memory
153 // */
154 // DynDlist(const DynList<T> & l) : Dnode<T>(), CtorBase(l) {}
155
157
166 void empty() noexcept
167 {
168 while (not this->is_empty())
169 delete this->Dnode<T>::remove_next();
170
171 num_elem = 0;
172 }
173
181 void clear() noexcept
182 {
183 empty();
184 }
185
187 {
188 empty();
189 }
190
191private:
192 T &_insert(Dnode<T> *p) noexcept
193 {
195 ++num_elem;
196 return p->get_data();
197 }
198
199 T &_append(Dnode<T> *p) noexcept
200 {
202 ++num_elem;
203 return p->get_data();
204 }
205
206public:
213 T &insert(const T &item)
214 {
215 return _insert(new Dnode<T>(item));
216 }
217
224 T &insert(T &&item)
225 {
226 return _insert(new Dnode<T>(std::forward<T>(item)));
227 }
228
235 T &append(const T &item)
236 {
237 return _append(new Dnode<T>(item));
238 }
239
246 T &append(T &&item)
247 {
248 return _append(new Dnode<T>(std::forward<T>(item)));
249 }
250
251private:
252 void _insert(DynDlist<T> &list) noexcept
253 {
254 Dlink::insert_list(&list);
255 num_elem += list.num_elem;
256 list.num_elem = 0;
257
258 assert(list.is_empty());
259 }
260
261 void _append(DynDlist<T> &list) noexcept
262 {
263 Dlink::append_list(&list);
264 num_elem += list.num_elem;
265 list.num_elem = 0;
266
267 assert(list.is_empty());
268 }
269
270public:
275 void insert(const DynDlist &list)
276 {
277 auto l = list; // perform a copy of list
278 _insert(l);
279 }
280
282 void insert(DynDlist &&list) noexcept
283 {
284 _insert(list);
285 }
286
291 void append(const DynDlist &list)
292 {
293 auto l = list; // perform a copy of list
294 _append(l);
295 }
296
298 void append(DynDlist &&list) noexcept
299 {
300 _append(list);
301 }
302
304 T &get_first_ne() noexcept
305 {
306 return this->get_next()->get_data();
307 }
308
311 const T &get_first_ne() const noexcept
312 {
313 return this->get_next()->get_data();
314 }
315
317 T &get_last_ne() noexcept
318 {
319 return this->get_prev()->get_data();
320 }
321
324 const T &get_last_ne() const noexcept
325 {
326 return this->get_prev()->get_data();
327 }
328
331 {
332 ah_underflow_error_if(this->is_empty()) << "DynDlist is empty";
333 return get_first_ne();
334 }
335
338 const T &get_first() const
339 {
340 ah_underflow_error_if(this->is_empty()) << "DynDlist is empty";
341 return get_first_ne();
342 }
343
346 {
347 ah_underflow_error_if(this->is_empty()) << "DynDlist is empty";
348 return get_last_ne();
349 }
350
353 const T &get_last() const
354 {
355 ah_underflow_error_if(this->is_empty()) << "DynDlist is empty";
356 return get_last_ne();
357 }
358
359private:
360 // Frees the first node, or the last one
361 template <bool First>
362 void delete_end() noexcept
363 {
364 if constexpr (First)
365 delete this->remove_next();
366 else
367 delete this->remove_prev();
368 --num_elem;
369 }
370
371 // Removes the first item, or the last one. A move that can throw goes
372 // straight into the result, which a named object could not guarantee,
373 // and the node leaves when leave ends, after that move; if the move
374 // throws, the node stays, and with it the item.
375 template <bool First>
376 T remove_end_ne() noexcept(std::is_nothrow_move_constructible_v<T>)
377 {
378 T &item = First ? get_first_ne() : get_last_ne();
379 if constexpr (std::is_nothrow_move_constructible_v<T>)
380 {
381 T ret_val = std::move(item);
382 delete_end<First>();
383 return ret_val;
384 }
385 else
386 {
387 struct Leave
388 {
389 DynDlist &list;
390 bool keep = false;
391
392 ~Leave()
393 {
394 if (not keep)
395 list.template delete_end<First>();
396 }
397 } leave{*this};
398
399 try
400 {
401 return std::move(item);
402 }
403 catch (...)
404 {
405 leave.keep = true;
406 throw;
407 }
408 }
409 }
410
411public:
419 T remove_first_ne() noexcept(std::is_nothrow_move_constructible_v<T>)
420 {
421 return remove_end_ne<true>();
422 }
423
431 T remove_last_ne() noexcept(std::is_nothrow_move_constructible_v<T>)
432 {
433 return remove_end_ne<false>();
434 }
435
442 {
443 ah_underflow_error_if(this->is_empty()) << "DynDlist is empty";
444 return remove_first_ne();
445 }
446
453 {
454 ah_underflow_error_if(this->is_empty()) << "DynDlist is empty";
455 return remove_last_ne();
456 }
457
459 T &put(const T &item)
460 {
461 return append(item);
462 }
463
465 T &put(T &&item)
466 {
467 return append(std::forward<T>(item));
468 }
469
472 {
473 return remove_first();
474 }
475
479 {
480 return get_last();
481 }
482
486 {
487 return get_first();
488 }
489
491 T &push(const T &item)
492 {
493 return insert(item);
494 }
495
497 T &push(T &&item)
498 {
499 return insert(std::forward<T>(item));
500 }
501
504 {
505 return remove_first();
506 }
507
510 {
511 return get_first();
512 }
513
516 const T &top() const
517 {
518 return get_first();
519 }
520
534 void remove(T &data) noexcept
535 {
537 p->del();
538 delete p;
539 --num_elem;
540 }
541
543 void erase(T &data) noexcept
544 {
545 remove(data);
546 }
547
550 void swap(DynDlist &l) noexcept
551 {
552 std::swap(num_elem, l.num_elem);
553 this->Dlink::swap(&l);
554 }
555
569 {
570 const size_t n = Dlink::split_list(l, r); // l takes the first n - n / 2
571 l.num_elem += n - n / 2;
572 r.num_elem += n / 2;
573 num_elem = 0;
574 }
575
589 {
590 ah_domain_error_if((not l.is_empty()) or (not r.is_empty())) << "lists are not empty";
591 split_list_ne(l, r);
592 }
593
597 {
598 split_list(l, r);
599 }
600
605 class Iterator : public Dnode<T>::Iterator
606 {
607 DynDlist *list_ptr; // Pointer to list
608 long pos; // Position of the current element in the sequence
609
610 using Base = typename Dnode<T>::Iterator;
611
612 public:
615
617 using Item_Type = T;
618
620 [[nodiscard]] long get_pos() const noexcept
621 {
622 return pos;
623 }
624
627 void next_ne() noexcept
628 {
630 pos++;
631 }
632
635 void next()
636 {
638 pos++;
639 }
640
643 void prev()
644 {
646 pos--;
647 }
648
650 void reset_first() noexcept
651 {
653 pos = 0;
654 }
655
657 void reset_last() noexcept
658 {
660 pos = list_ptr->num_elem - 1;
661 }
662
664 void end() noexcept
665 {
666 put_itor_at_the_end(*this);
667 }
668
670 Iterator(DynDlist<T> &list) noexcept : Base(list), list_ptr(&list), pos(0)
671 {
672 // empty
673 }
674
676 Iterator(const DynDlist<T> &) = delete;
677
678 Iterator() noexcept : list_ptr(nullptr)
679 { /* empty */
680 }
681
682 Iterator(const Iterator &) = default;
683
684 Iterator &operator = (const Iterator &it) noexcept
685 {
686 if (this == &it)
687 return *this;
689 list_ptr = it.list_ptr;
690 pos = it.pos;
691
692 return *this;
693 }
694
697 T &get_curr() const
698 {
699 return Dnode<T>::Iterator::get_curr()->get_data();
700 }
701
702 T &get_curr_ne() const noexcept
703 {
704 return Dnode<T>::Iterator::get_curr_ne()->get_data();
705 }
706
716 void insert(const T &item)
717 {
718 ah_overflow_error_if(not this->has_curr()) << "DynDlist Iterator has not current";
719
720 Dnode<T>::Iterator::get_curr()->insert(new Dnode<T>(item));
722 }
723
734 void insert(T &&item)
735 {
736 ah_overflow_error_if(not this->has_curr()) << "DynDlist Iterator has not current";
737
738 Dnode<T>::Iterator::get_curr()->insert(new Dnode<T>(std::forward<T>(item)));
740 }
741
751 void append(const T &item)
752 {
753 ah_overflow_error_if(not this->has_curr()) << "DynDlist Iterator has not current";
754
755 Dnode<T>::Iterator::get_curr()->append(new Dnode<T>(item));
757 }
758
769 void append(T &&item)
770 {
771 ah_overflow_error_if(not this->has_curr()) << "DynDlist Iterator has not current";
772
773 Dnode<T>::Iterator::get_curr()->append(new Dnode<T>(std::forward<T>(item)));
775 }
776
793 {
794 ah_overflow_error_if(not this->has_curr()) << "DynDlist Iterator has not current";
795
796 Dnode<T>::Iterator::get_curr()->insert_list(&list);
797 list_ptr->num_elem += list.num_elem;
798 list.num_elem = 0;
799
800 assert(list.is_empty());
801 }
802
819 {
820 ah_overflow_error_if(not this->has_curr()) << "DynDlist Iterator has not current";
821
822 Dnode<T>::Iterator::get_curr()->append_list(&list);
823 list_ptr->num_elem += list.num_elem;
824 list.num_elem = 0;
825
826 assert(list.is_empty());
827 }
828
836 {
837 ah_overflow_error_if(not this->has_curr()) << "DynDlist Iterator has not current";
838
840 T ret_val = ptr->get_data();
842 ptr->del();
843 delete ptr;
845 return ret_val;
846 }
847 };
848
851
862 {
863 if (this == &list)
864 return *this;
865
866 empty();
867
868 for (Iterator itor(const_cast<DynDlist &>(list)); itor.has_curr(); itor.next_ne())
869 this->append(itor.get_curr());
870
871 return *this;
872 }
873
881 DynDlist(const DynDlist &list) : DynDlist()
882 {
883 assert(this->is_empty());
884
885 for (Iterator itor(const_cast<DynDlist &>(list)); itor.has_curr(); itor.next_ne())
886 this->append(itor.get_curr());
887 }
888
898 DynDlist(DynDlist &&list) noexcept
899 requires std::is_nothrow_default_constructible_v<T>
900 : DynDlist()
901 {
902 swap(list);
903 }
904
917 DynDlist(DynDlist &&list) noexcept(std::is_nothrow_move_constructible_v<T>)
918 requires (not std::is_nothrow_default_constructible_v<T> and
919 std::is_move_constructible_v<T>)
920 : Dnode<T>(std::move(static_cast<Dnode<T> &>(list))), num_elem(list.num_elem)
921 {
922 list.num_elem = 0;
923 }
924
938 requires (not std::is_nothrow_default_constructible_v<T> and
939 not std::is_move_constructible_v<T>)
940 : DynDlist()
941 {
942 swap(list);
943 }
944
954 {
955 swap(list);
956 return *this;
957 }
958
961 T &operator [] (const size_t n)
962 {
963 auto it = this->get_it();
964 for (size_t i = 0; i < n && it.has_curr(); ++i)
965 it.next_ne();
966
967 ah_out_of_range_error_if(not it.has_curr()) << "DynDlist index out of range";
968
969 return it.get_curr();
970 }
971
974 const T &operator [] (const size_t n) const
975 {
976 auto it = this->get_it();
977 for (size_t i = 0; i < n && it.has_curr(); ++i)
978 it.next_ne();
979
980 ah_out_of_range_error_if(not it.has_curr()) << "DynDlist index out of range";
981
982 return it.get_curr();
983 }
984
985 DynDlist &reverse() noexcept
986 {
988 return *this;
989 }
990
991 DynDlist &rev() noexcept
992 {
993 return reverse();
994 }
995
1001 {
1002 DynDlist<T> ret;
1003 for (auto it = this->get_it(); it.has_curr(); it.next_ne())
1004 ret.insert(it.get_curr());
1005 return ret;
1006 }
1007
1009 {
1010 return reverse();
1011 }
1012};
1013
1014} // end namespace Aleph
1015
1016#endif /* TPL_DYNDLIST_H */
Variadic constructor macros for containers.
Container traversal and functional operation mixins.
Exception handling system with formatted messages for Aleph-w.
#define ah_out_of_range_error_if(C)
Throws std::out_of_range if condition holds.
Definition ah-errors.H:727
#define ah_underflow_error_if(C)
Throws std::underflow_error if condition holds.
Definition ah-errors.H:517
#define ah_overflow_error_if(C)
Throws std::overflow_error if condition holds.
Definition ah-errors.H:611
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
Definition ah-errors.H:670
STL-compatible iterator adapters for Aleph containers.
DRY (Don't Repeat Yourself) utilities and macros.
#define Special_Ctors(Set_Type, Type)
Generates special constructors for containers.
Definition ahDry.H:123
Functional programming utilities for Aleph-w containers.
Iterator traits and STL-compatible iterator wrappers.
void put_itor_at_the_end(Itor &it) noexcept
Definition aleph.H:54
Read-only view of an Aleph iterator: the iterator of a constant container.
Iterator on a list of Dnode objects.
Definition tpl_dnode.H:294
Dnode< T > * get_curr_ne() const noexcept
Return the current link guaranteeing no exception. Be careful.
Definition tpl_dnode.H:308
Dnode< T > * get_curr() const
Return a pointer to the current node.
Definition tpl_dnode.H:318
Node belonging to a double circular linked list with header node.
Definition tpl_dnode.H:106
Dnode< T > *& get_next() const noexcept
Return the next node to this
Definition tpl_dnode.H:114
Dnode< T > * remove_next() noexcept
Remove the next node to this; return its address.
Definition tpl_dnode.H:123
Dnode< T > *& get_prev() const noexcept
Return the previous node to this
Definition tpl_dnode.H:117
static Dnode * data_to_node(T &data) noexcept
Given an reference to the data in the node, returns a pointer to the Dnode object that contains it.
Definition tpl_dnode.H:282
T & get_data() noexcept
Return a modifiable reference to the data contained in the node.
Definition tpl_dnode.H:266
Dnode< T > * remove_prev() noexcept
Remove the previous node to this; return its address.
Definition tpl_dnode.H:120
Iterator dynamic list.
Iterator(const DynDlist< T > &)=delete
A constant list is iterated through Const_Iterator.
void append_list(DynDlist &list)
Move and append all elements of list before the current item of iterator.
T Item_Type
The type of element stored in the container.
typename Dnode< T >::Iterator Base
void next_ne() noexcept
Move the iterator one position forward guaranteeing no exception.
void insert(const T &item)
Insert by copy an item after the current item.
Iterator(const Iterator &)=default
T & get_curr() const
Return the current item; throw overflow_error if there is no current item.
void reset_last() noexcept
Reset the iterator to the last item.
Iterator & operator=(const Iterator &it) noexcept
void end() noexcept
Put the iterator at the end state (where there is no current item)
void prev()
Move the iterator one item backward.
long get_pos() const noexcept
Return the ordinal position of the current item.
void insert(T &&item)
Insert by movement an item after the current item.
Iterator(DynDlist< T > &list) noexcept
Iterator on the first item of the modifiable list list.
T del()
Remove from the list the current node and move the iterator one position forward.
void append(T &&item)
Append by movement an item after the current item.
void insert_list(DynDlist &list)
Move and insert all elements of list after the current item of iterator.
void reset_first() noexcept
Reset the iterator to the first item.
void append(const T &item)
Append by copy an item before the current item.
void next()
Move the iterator one item forward.
T & get_curr_ne() const noexcept
Dynamic doubly linked list with O(1) size and bidirectional access.
T & get_last_ne() noexcept
Return a modifiable reference to last item in the list.
const T & get_last_ne() const noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
T & operator[](const size_t n)
Return a modifiable reference to the i-th item of the list.
void insert(DynDlist &&list) noexcept
See insert(const DynDlist & list).
const size_t & size() const noexcept
Return the number of elements (constant time)
T & get_first()
Return a modifiable reference to first item in the list.
void _insert(DynDlist< T > &list) noexcept
DynDlist & reverse() noexcept
T Key_Type
The type of element that stores the container.
T & front()
If this was treated as a queue, the it returns the most oldlest inserted item.
DynDlist< T > reverse() const
Return a reversed copy of this
const T & get_first_ne() const noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
T pop()
See remove_first().
T & push(T &&item)
See insert(T && item).
T remove_first_ne() noexcept(std::is_nothrow_move_constructible_v< T >)
Remove the first item of the list, which must not be empty.
T remove_first()
Remove the first item of the list; return a copy of removed item.
T & put(T &&item)
See append(T && item).
const T & top() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
DynDlist(DynDlist &&list)
Move constructor for a T whose default construction may throw and that cannot be moved,...
void split_list(DynDlist &l, DynDlist &r)
Split the list in two.
void swap(DynDlist &l) noexcept
Swap in constant time all the items of this with all the items of l (very fast!)
void remove(T &data) noexcept
Assuming that data is a reference to the item in the list, it removes the item.
T remove_last_ne() noexcept(std::is_nothrow_move_constructible_v< T >)
Remove the last item of the list, which must not be empty.
T & append(const T &item)
Append a copied item at the end of the list.
void delete_end() noexcept
void append(const DynDlist &list)
Append all the elements of list after this.
T & get_first_ne() noexcept
Return a modifiable reference to first item in the list.
T & get_last()
Return a modifiable reference to last item in the list.
DynDlist(DynDlist &&list) noexcept(std::is_nothrow_move_constructible_v< T >)
Move constructor for a T whose default construction may throw, as one that allocates,...
T Item_Type
The type of element that stores the container.
T & push(const T &item)
See insert(const T & item).
T remove_last()
Remove the last item of the list; return a copy of removed item.
DynDlist()=default
T & put(const T &item)
See append(const T & item).
void empty() noexcept
@brief Empties the container.
const T & get_first() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
DynDlist & rev() noexcept
T & rear()
If this was treated as a queue, the it returns the most recently inserted item.
void split(DynDlist &l, DynDlist &r)
This is an overloaded member function, provided for convenience. It differs from the above function o...
DynDlist< T > rev() const
T & top()
See get_first().
void _append(DynDlist< T > &list) noexcept
T & _append(Dnode< T > *p) noexcept
void clear() noexcept
@brief Empties the container.
void erase(T &data) noexcept
See remove(T & data).
void append(DynDlist &&list) noexcept
See append(const DynDlist & list).
T & insert(const T &item)
Insert a copy of item at the beginning of the list.
T & _insert(Dnode< T > *p) noexcept
const T & get_last() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
void insert(const DynDlist &list)
Insert all the elements of list before this.
~DynDlist()
Destructor.
T get()
See remove_first().
T remove_end_ne() noexcept(std::is_nothrow_move_constructible_v< T >)
DynDlist< T > & operator=(const DynDlist &list)
Assignment by copy.
T & insert(T &&item)
Insert a moved item at the beginning of the list.
DynDlist(DynDlist &&list) noexcept
Move constructor: takes in constant time the items of list, which is left empty.
void split_list_ne(DynDlist &l, DynDlist &r) noexcept
Split the list in two.
T & append(T &&item)
Append a moved item at the end of the list.
DynDlist(const DynDlist &list)
Copy constructor; all items of list are copied.
constexpr bool is_empty() const noexcept
Definition htlist.H:434
Mixin providing equality comparison for sequence containers.
Definition ah-dry.H:2945
Common methods to the Aleph-w ( ) containers.
Definition ah-dry.H:1235
and
Insert a new element into the container by constructing it from the given args.
Definition ah-dry.H:1273
Common sequential searching methods on containers.
Definition ah-dry.H:555
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
Definition ah-dry.H:663
Mixin that adds STL begin()/end() and cbegin()/cend() to Aleph containers.
Main namespace for Aleph-w library functions.
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:152
STL namespace.
Generic list of items stored in a container.
Definition ah-dry.H:2896
Generic traversal of the container through its iterator.
Definition ah-dry.H:363
Special constructors common to Aleph-w ( ) containers.
Definition ah-dry.H:1100
Gsl_Compatible_Rng r
Doubly linked list node with typed data.
DynList< int > l