Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
dlink.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
32#ifndef DLINK_H
33#define DLINK_H
34
50#include <aleph.H>
51#include <ah-errors.H>
52
53namespace Aleph {
54
55template <typename T>
56class Dnode; // Forward declaration of derived class
57
99class Dlink
100{
101protected:
102 mutable Dlink *prev;
103 mutable Dlink *next;
104
105public:
107 template <typename T>
108 Dnode<T> *to_dnode() noexcept;
110 template <typename T>
111 const Dnode<T> *to_dnode() const noexcept;
113 template <typename T>
114 T &to_data() noexcept;
116 template <typename T>
117 const T &to_data() const noexcept;
118
120 Dlink() noexcept : prev(this), next(this)
121 {
122 assert(is_empty());
123 }
124
127 Dlink(const Dlink &l) noexcept : prev(l.prev), next(l.next)
128 {
129 if (l.is_empty())
130 reset();
131 }
132
141 void swap(Dlink *link) noexcept
142 {
143 if (is_empty() and link->is_empty())
144 return;
145
146 if (is_empty())
147 {
148 link->next->prev = this;
149 link->prev->next = this;
150 next = link->next;
151 prev = link->prev;
152 link->reset();
153 return;
154 }
155
156 if (link->is_empty())
157 {
158 next->prev = link;
159 prev->next = link;
160 link->next = next;
161 link->prev = prev;
162 reset();
163 return;
164 }
165
166 std::swap(prev->next, link->prev->next);
167 std::swap(next->prev, link->next->prev);
168 std::swap(prev, link->prev);
169 std::swap(next, link->next);
170 }
171
180 void swap(Dlink &l) noexcept
181 {
182 swap(&l);
183 }
184
192 Dlink(Dlink &&l) noexcept : Dlink()
193 {
194 swap(l);
195 }
196
204 Dlink &operator = (const Dlink &l) noexcept
205 {
206 if (l.is_empty())
207 return *this;
208
209 assert(is_empty());
210 prev = l.prev;
211 next = l.next;
212 return *this;
213 }
214
227 {
228 swap(l);
229 return *this;
230 }
231
238 void reset() noexcept
239 {
240 next = prev = this;
241 }
242
244 void init() noexcept
245 {
246 reset();
247 }
248
250 [[nodiscard]] constexpr bool is_empty() const noexcept
251 {
252 return this == next and this == prev;
253 }
254
256 [[nodiscard]] constexpr bool is_unitarian() const noexcept
257 {
258 return this != next and next == prev;
259 }
260
262 [[nodiscard]] constexpr bool is_unitarian_or_empty() const noexcept
263 {
264 return next == prev;
265 }
266
272 void insert(Dlink *node) noexcept
273 {
274 assert(next != nullptr and prev != nullptr);
275 assert(node != nullptr);
276 assert(node->is_empty());
277
278 node->prev = this;
279 node->next = next;
280 next->prev = node;
281 next = node;
282 }
283
285 void push(Dlink *node) noexcept
286 {
287 insert(node);
288 }
289
295 void append(Dlink *node) noexcept
296 {
297 assert(next != nullptr and prev != nullptr);
298 assert(node != nullptr);
299 assert(node->is_empty());
300
301 node->next = this;
302 node->prev = prev;
303 prev->next = node;
304 prev = node;
305 }
306
308 [[nodiscard]] Dlink *&get_next() const noexcept
309 {
310 assert(next != nullptr and prev != nullptr);
311 return next;
312 }
313
315 [[nodiscard]] Dlink *&get_prev() const noexcept
316 {
317 assert(next != nullptr and prev != nullptr);
318 return prev;
319 }
320
322 [[nodiscard]] constexpr Dlink *&get_first_ne() const noexcept
323 {
324 return next;
325 }
326
328 [[nodiscard]] constexpr Dlink *&get_last_ne() const noexcept
329 {
330 return prev;
331 }
332
334 [[nodiscard]] constexpr Dlink *&get_first() const noexcept
335 {
336 return next;
337 }
338
340 [[nodiscard]] constexpr Dlink *&get_last() const noexcept
341 {
342 return prev;
343 }
344
370 void wrap_header(Dlink *l) noexcept
371 {
372 assert(is_empty());
373 l->append(this);
374 }
375
386 void insert_list(Dlink *head) noexcept
387 {
388 if (head->is_empty())
389 return;
390
391 head->prev->next = next;
392 head->next->prev = this;
393 next->prev = head->prev;
394 next = head->next;
395 head->reset();
396 }
397
408 void append_list(Dlink *head) noexcept
409 {
410 if (head->is_empty())
411 return;
412
413 head->next->prev = prev;
414 head->prev->next = this;
415 prev->next = head->next;
416 prev = head->prev;
417 head->reset();
418 }
419
424 void splice(Dlink *l) noexcept
425 {
426 Dlink head;
427 head.wrap_header(l);
428 insert_list(&head);
429 assert(head.is_empty());
430 }
431
441 void concat_list(Dlink *head) noexcept
442 {
443 assert(head != nullptr);
444
445 if (head->is_empty())
446 return;
447
448 if (this->is_empty())
449 {
450 swap(head);
451 return;
452 }
453
454 prev->next = head->next;
455 head->next->prev = prev;
456 prev = head->prev;
457 head->prev->next = this;
458 head->reset();
459 }
460
462 void concat_list(Dlink &head) noexcept
463 {
464 concat_list(&head);
465 }
466
468 Dlink *del() noexcept
469 {
470 assert(next != nullptr and prev != nullptr);
471
472 prev->next = next;
473 next->prev = prev;
474 reset();
475 return this;
476 }
477
479 void erase() noexcept
480 {
481 del();
482 }
483
495 Dlink *remove_prev() noexcept
496 {
497 assert(not is_empty());
498
499 Dlink *retValue = prev;
500 retValue->del();
501
502 return retValue;
503 }
504
516 Dlink *remove_next() noexcept
517 {
518 assert(not is_empty());
519
520 Dlink *retValue = next;
521 retValue->del();
522
523 return retValue;
524 }
525
528 {
529 return remove_prev();
530 }
531
534 {
535 return remove_next();
536 }
537
539 Dlink *remove_last() noexcept
540 {
541 return remove_prev();
542 }
543
545 Dlink *remove_first() noexcept
546 {
547 return remove_next();
548 }
549
551 Dlink *top() const
552 {
553 ah_underflow_error_if(is_empty()) << "Dlink as stack is empty";
554 return get_next();
555 }
556
559 {
560 ah_underflow_error_if(is_empty()) << "Dlink as stack is empty";
561 return remove_next();
562 }
563
568 size_t reverse_list() noexcept
569 {
570 if (is_empty())
571 return 0;
572
573 Dlink tmp;
574 size_t counter = 0;
575 for (; not is_empty(); counter++)
576 tmp.insert(remove_next());
577
578 swap(&tmp);
579
580 return counter;
581 }
582
584 size_t reverse() noexcept
585 {
586 return reverse_list();
587 }
588
604 size_t split_list_ne(Dlink &l, Dlink &r) noexcept
605 {
606 assert(l.is_empty() and r.is_empty()); // l and r must be empty
607
608 if (is_empty())
609 return 0;
610
611 // p from the first node and q from the last meet in the middle; l
612 // takes the nodes up to p, the first n - n / 2, after its own, and r
613 // the others before its own, as if they had moved one by one
614 Dlink *p = next;
615 Dlink *q = prev;
616 size_t half = 0;
617 while (p != q and p->next != q)
618 {
619 p = p->next;
620 q = q->prev;
621 ++half;
622 }
623
624 Dlink *const first = next;
625 Dlink *const last = prev;
626 if (p != last)
627 {
628 Dlink *const r_first = p->next;
629 last->next = r.next;
630 r.next->prev = last;
631 r.next = r_first;
632 r_first->prev = &r;
633 }
634
635 l.prev->next = first;
636 first->prev = l.prev;
637 p->next = &l;
638 l.prev = p;
639
640 reset();
641
642 return 2 * half + (p == q ? 1 : 2);
643 }
644
645 size_t split_list(Dlink &l, Dlink &r) noexcept
646 {
647 return split_list_ne(l, r);
648 }
649
669 Dlink cut_list(Dlink *link) noexcept
670 {
671 assert(not is_empty() and not link->is_empty() and link != this);
672
673 Dlink list;
674 if (link == this->prev) // is link the last item?
675 {
676 link->del();
677 list.append(link);
678 return list;
679 }
680
681 if (link == this->next) // is link the first item?
682 {
683 list.swap(this);
684 assert(this->is_empty());
685 return list;
686 }
687
688 list.prev = this->prev; // link list to link (cutting point)
689 list.next = link;
690 this->prev = link->prev; // remove from this everything from link
691 link->prev->next = this;
692 link->prev = &list; // place the cut in list
693 list.prev->next = &list;
694
695 return list;
696 }
697
703 {
704 private:
705 mutable Dlink *head = nullptr;
706 mutable Dlink *curr = nullptr;
707
708 public:
711
713 using Item_Type = Dlink *;
714
720 Iterator(Dlink *head_ptr) noexcept : head(head_ptr), curr(head->get_next())
721 { /* */
722 }
723
728 Iterator(const Dlink &list) noexcept : head(&const_cast<Dlink &>(list)), curr(head->get_next())
729 {
730 // Empty
731 }
732
740 void set(Dlink *new_curr) noexcept
741 {
742 assert(curr != nullptr and head != nullptr);
743 curr = new_curr;
744 }
745
746 Iterator() noexcept : head(nullptr), curr(nullptr)
747 { /* Empty */
748 }
749
751 void reset_first() noexcept
752 {
753 assert(curr != nullptr and head != nullptr);
754 curr = head->get_next();
755 }
756
758 void reset_last() noexcept
759 {
760 assert(curr != nullptr and head != nullptr);
761 curr = head->get_prev();
762 }
763
765 void end() noexcept
766 {
767 put_itor_at_the_end(*this);
768 }
769
771 [[nodiscard]] bool has_curr() const noexcept
772 {
773 assert(curr != nullptr and head != nullptr);
774 return curr != head;
775 }
776
777 [[nodiscard]] bool is_last() const noexcept
778 {
779 return head->is_empty() ? false : curr == head->prev;
780 }
781
783 [[nodiscard]] Dlink *get_curr_ne() const noexcept
784 {
785 assert(curr != nullptr and head != nullptr);
786 return curr;
787 }
788
794 [[nodiscard]] Dlink *get_curr() const
795 {
796 ah_overflow_error_if(not has_curr()) << "Not element in list";
797
798 return get_curr_ne();
799 }
800
802 [[nodiscard]] constexpr bool is_in_first() const noexcept
803 {
804 return head->is_empty() ? false : curr == head->next;
805 }
806
808 [[nodiscard]] bool is_in_last() const noexcept
809 {
810 return is_last();
811 }
812
815 void prev_ne() noexcept
816 {
817 curr = curr->get_prev();
818 }
819
824 void prev()
825 {
826 ah_underflow_error_if(not has_curr()) << "Not previous element in list";
827 prev_ne();
828 }
829
832 void next_ne() noexcept
833 {
834 curr = curr->get_next();
835 }
836
841 void next()
842 {
843 ah_overflow_error_if(not has_curr()) << "Not next element in list";
844 next_ne();
845 }
846
848 [[nodiscard]] constexpr bool operator == (const Iterator &it) const noexcept
849 {
850 return curr == it.curr;
851 }
852
854 [[nodiscard]] constexpr bool operator != (const Iterator &it) const noexcept
855 {
856 return curr != it.curr;
857 }
858
865 {
866 assert(curr != nullptr and head != nullptr);
867 assert(has_curr());
868
869 Dlink *current = get_curr(); // the current node
870 next(); // move to the next node
871 current->del(); // unlink the node that was current
872 return current;
873 }
874
886 Dlink *del_ne() noexcept
887 {
888 assert(curr != nullptr and head != nullptr);
889 assert(has_curr());
890
891 Dlink *current = get_curr_ne(); // the current node
892 next_ne(); // move to the next node
893 current->del(); // unlink the node that was current
894 return current;
895 }
896
898 [[nodiscard]] constexpr bool verify(Dlink *l) const noexcept
899 {
900 return head == l;
901 }
902
904 [[nodiscard]] constexpr bool verify(const Iterator &it) const noexcept
905 {
906 return head == it.head;
907 }
908 };
909
921 void remove_all_and_delete() noexcept
922 {
923 while (not is_empty())
924 delete remove_next();
925 }
926
941 void rotate_left(size_t n)
942 {
943 if (is_empty())
944 {
945 if (n == 0)
946 return;
947 ah_domain_error() << "List is empty";
948 }
949
950 for (size_t i = 0; i < n; ++i)
952 }
953
955 void rotate_right(size_t n)
956 {
957 if (is_empty())
958 {
959 if (n == 0)
960 return;
961 ah_domain_error() << "List is empty";
962 }
963
964 for (size_t i = 0; i < n; ++i)
966 }
967
969 bool check()
970 {
971 Iterator itor(this);
972 Dlink *node;
973
974 for (/* nothing */; itor.has_curr(); itor.next_ne())
975 {
976 node = itor.get_curr();
977
978 if (not (node->get_next()->get_prev() == node))
979 return false;
980
981 if (not (node->get_prev()->get_next() == node))
982 return false;
983 }
984
985 for (itor.reset_last(); itor.has_curr(); itor.prev())
986 {
987 node = itor.get_curr();
988
989 if (not (node->get_next()->get_prev() == node))
990 return false;
991
992 if (not (node->get_prev()->get_next() == node))
993 return false;
994 }
995
996 return true;
997 }
998};
999
1029#define DLINK_TO_TYPE(type_name, link_name) \
1030 inline static type_name *dlink_to_##type_name(Dlink *link) noexcept \
1031 { \
1032 const size_t offset_link = __builtin_offsetof(type_name, link_name); \
1033 char *address_type = reinterpret_cast<char *>(link) - offset_link; \
1034 return reinterpret_cast<type_name *>(address_type); \
1035 }
1036
1078#define LINKNAME_TO_TYPE(type_name, link_name) \
1079 inline static type_name *link_name##_to_##type_name(Dlink *link) noexcept \
1080 { \
1081 const size_t offset_link = __builtin_offsetof(type_name, link_name); \
1082 char *address_type = reinterpret_cast<char *>(link) - offset_link; \
1083 return reinterpret_cast<type_name *>(address_type); \
1084 }
1085
1112#define DLINK_TO_BASE(type_name, link_name) \
1113 inline static type_name *dlink_to_base(Dlink *link) noexcept \
1114 { \
1115 const size_t offset_link = __builtin_offsetof(type_name, link_name); \
1116 char *address_type = reinterpret_cast<char *>(link) - offset_link; \
1117 return reinterpret_cast<type_name *>(address_type); \
1118 }
1119
1120} // namespace Aleph
1121
1122#endif /* DLINK_H */
Exception handling system with formatted messages for Aleph-w.
#define ah_domain_error()
Throws std::domain_error unconditionally.
Definition ah-errors.H:702
#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
Core header for the Aleph-w library.
void put_itor_at_the_end(Itor &it) noexcept
Definition aleph.H:54
Node belonging to a double circular linked list with header node.
Definition tpl_dnode.H:106
T & append(const T &item)
Definition htlist.H:1362
constexpr bool is_empty() const noexcept
Definition htlist.H:434
Main namespace for Aleph-w library functions.
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:152
and
Check uniqueness with explicit hash + equality functors.
static long counter
Definition test-splice.C:40
Gsl_Compatible_Rng r
DynList< int > l