Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
htlist.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
44#ifndef HTLIST_H
45#define HTLIST_H
46
47#include <type_traits>
48#include <cassert>
49#include <stdexcept>
50#include <utility>
51#include <ahFunction.H>
52#include <ahFunctional.H>
53#include <ah-args-ctor.H>
54#include <ah-iterator.H>
55#include <ah-errors.H>
56#include <ah-dry.H>
57
58#define NEXT(p) (p->get_next())
59
60namespace Aleph {
61
62template <typename T>
63class Snodenc; // forward declaration
64
95{
96 Slinknc *next = nullptr;
97
98public:
99 virtual ~Slinknc() = default;
100
102 [[nodiscard]] constexpr bool is_empty() const noexcept
103 {
104 return next == nullptr;
105 }
106
109 Slinknc() noexcept : next(nullptr)
110 { /* empty */
111 }
112
115 Slinknc(const Slinknc &) noexcept : next(nullptr)
116 { /* empty */
117 }
118
121 void reset() noexcept
122 {
123 next = nullptr;
124 }
125
128 Slinknc &operator = (const Slinknc &) noexcept
129 {
130 next = nullptr;
131 return *this;
132 }
133
134 Slinknc *&get_next() noexcept
135 {
136 return next;
137 }
138
146 void insert(Slinknc *p) noexcept
147 {
148 assert(p != nullptr);
149 p->next = next;
150 next = p;
151 }
152
160 {
161 Slinknc *ret_val = next;
162 next = ret_val->next;
163 ret_val->reset();
164 return ret_val;
165 }
166
172 template <typename T>
173 inline Snodenc<T> *to_snodenc() noexcept;
174
176 template <typename T>
177 inline T &to_data() noexcept;
178
180 template <typename T>
181 inline const Snodenc<T> *to_snodenc() const noexcept;
182
184 template <typename T>
185 inline const T &to_data() const noexcept;
186
194 {
197
198 void init() noexcept
199 {
200 curr = head->is_empty() ? nullptr : head->next;
201 }
202
203 public:
204 Iterator() noexcept : head(nullptr), curr(nullptr)
205 {
206 // Empty
207 }
208
211 Iterator(Slinknc &list) noexcept : head(&list)
212 {
213 init();
214 }
215
222 Iterator(Slinknc *_head, Slinknc *_curr) noexcept : head(_head), curr(_curr)
223 {
224 // Empty
225 }
226
231 bool has_curr() const noexcept
232 {
233 return curr != nullptr;
234 }
235
242 {
243 ah_overflow_error_if(not has_curr()) << "Iterator is at the end of the list";
244 return curr;
245 }
246
248 Slinknc *get_curr_ne() const noexcept
249 {
250 return curr;
251 }
252
255 void next_ne() noexcept
256 {
257 curr = curr->next;
258 }
259
265 void next()
266 {
267 ah_overflow_error_if(not has_curr()) << "Iterator is at the end of the list";
268 curr = curr->next;
269 }
270
273 void reset_first() noexcept
274 {
275 init();
276 }
277 };
278};
279
287template <typename T>
288class Snodenc : public Slinknc
289{
291
292public:
295 T &get_data() noexcept
296 {
297 return data;
298 }
299
302 const T &get_data() const noexcept
303 {
304 return data;
305 }
306
308 {
309 static_assert(std::is_default_constructible<T>::value, "No default constructor for T");
310 }
311
314 Snodenc(const T &item) : data(item)
315 {
316 static_assert(std::is_copy_constructible<T>::value, "No copy constructor for T");
317 }
318
321 Snodenc(T &&item) : data(std::forward<T>(item))
322 {
323 static_assert(std::is_move_constructible<T>::value, "No move constructor for T");
324 }
325
336 {
337 return static_cast<Snodenc *>(Slinknc::remove_next());
338 }
339
342 Snodenc *&get_next() noexcept
343 {
344 // STRICT ALIASING WARNING: This reinterpret_cast violates C++ strict
345 // aliasing rules. Requires -fno-strict-aliasing compiler flag.
346 return reinterpret_cast<Snodenc *&>(Slinknc::get_next());
347 }
348
351 {
352 return Snodenc::remove_next();
353 }
354
363 Snodenc *&get_first() noexcept
364 {
365 return Snodenc::get_next();
366 }
367};
368
369template <typename T>
371{
372 return static_cast<Snodenc<T> *>(this);
373}
374
375template <typename T>
376inline const Snodenc<T> *Slinknc::to_snodenc() const noexcept
377{
378 return static_cast<const Snodenc<T> *>(this);
379}
380
381template <typename T>
383{
384 return this->to_snodenc<T>()->get_data();
385}
386
387template <typename T>
388const T &Slinknc::to_data() const noexcept
389{
390 return this->to_snodenc<T>()->get_data();
391}
392
420{
423
424public:
425 HTList() noexcept : head(nullptr), tail(nullptr)
426 { /* empty */
427 }
428
429 void reset() noexcept
430 {
431 head = tail = nullptr;
432 }
433
434 [[nodiscard]] constexpr bool is_empty() const noexcept
435 {
436 return head == nullptr;
437 }
438
440
441 bool is_unitarian() const noexcept
442 {
443 return head != nullptr and head == tail;
444 }
445
447
448 bool is_unitarian_or_empty() const noexcept
449 {
450 return head == tail;
451 }
452
453 Slinknc *get_head() const noexcept
454 {
455 return head;
456 }
457
458 Slinknc *get_tail() const noexcept
459 {
460 return tail;
461 }
462
463 Slinknc *get_first() const noexcept
464 {
465 return get_head();
466 }
467
469 Slinknc *get_first_ne() const noexcept
470 {
471 return head;
472 }
473
475 Slinknc *get_last() const noexcept
476 {
477 return get_tail();
478 }
479
488 HTList &swap(HTList &l) noexcept
489 {
490 std::swap(head, l.head);
491 std::swap(tail, l.tail);
492 return *this;
493 }
494
499 void insert(Slinknc *link) noexcept
500 {
501 assert(NEXT(link) == nullptr);
502
503 if (head == nullptr)
504 {
505 assert(tail == nullptr);
506 head = tail = link;
507 return;
508 }
509
510 NEXT(link) = head;
511 head = link;
512 }
513
518 void append(Slinknc *link) noexcept
519 {
520 assert(link != nullptr and NEXT(link) == nullptr);
521
522 if (head == nullptr)
523 {
524 assert(tail == nullptr);
525 head = tail = link;
526 return;
527 }
528
529 NEXT(tail) = link;
530 tail = link;
531 }
532
537 void append(HTList &l) noexcept
538 {
539 if (l.is_empty())
540 return;
541
542 if (this->is_empty())
543 {
544 this->swap(l);
545 return;
546 }
547
548 NEXT(tail) = l.head;
549 tail = l.tail;
550 l.head = l.tail = nullptr;
551 }
552
553 void put(Slinknc *link) noexcept
554 {
555 append(link);
556 }
557
559
560 void concat(HTList &l) noexcept
561 {
562 append(l);
563 }
564
566 void concat_list(HTList &l) noexcept
567 {
568 append(l);
569 }
570
572
580 void insert(HTList &l) noexcept
581 {
582 l.append(*this);
583 this->swap(l);
584 }
585
613 void insert(Slinknc *link, HTList &list) noexcept
614 {
615 if (list.is_empty())
616 return;
617
618 Slinknc *link_next = NEXT(link); // save what was after link
619 NEXT(link) = list.head; // link now points to list's head
620 NEXT(list.tail) = link_next; // list's tail points to what was after link
621
622 if (link == tail) // if link was the tail, update tail
623 tail = list.tail;
624
625 list.head = list.tail = nullptr;
626 }
627
629
631 {
632 Slinknc *ret_val = head;
633 if (head == tail)
634 head = tail = nullptr;
635 else
636 {
637 head = NEXT(head);
638 if (head == nullptr)
639 tail = nullptr;
640 }
641
642 ret_val->reset();
643
644 return ret_val;
645 }
646
653 {
654 ah_underflow_error_if(is_empty()) << "HTList is empty";
655 return remove_head_ne();
656 }
657
668 {
669 return remove_head_ne();
670 }
671
673 {
674 return remove_head();
675 }
676
691 bool remove(Slinknc *link)
692 {
693 ah_underflow_error_if(is_empty()) << "Removing from a empty list";
694
695 if (link == head)
696 {
697 head = NEXT(head);
698 if (head == nullptr)
699 tail = nullptr;
700 link->reset();
701 return true;
702 }
703
704 for (Slinknc *prev = head, *p = NEXT(head); p != nullptr; prev = p, p = NEXT(p))
705 if (p == link)
706 {
707 NEXT(prev) = NEXT(p);
708 if (link == tail)
709 tail = prev;
710 link->reset();
711 return true;
712 }
713
714 return false;
715 }
716
717 void push(Slinknc *link) noexcept
718 {
719 insert(link);
720 }
721
734 {
735 ah_underflow_error_if(is_empty()) << "HTList as stack is empty";
736 return remove_head();
737 }
738
739 Slinknc *top() const
740 {
741 ah_underflow_error_if(is_empty()) << "HTList as stack is empty";
742 return get_first();
743 }
744
746
760 size_t split_list(HTList &l, HTList &r) noexcept
761 {
762 assert(l.is_empty() and r.is_empty()); // l y r deben estar vacías
763
764 if (is_empty())
765 return 0;
766
767 if (is_unitarian())
768 {
769 swap(l);
770 return 1;
771 }
772
773 size_t count = 1; // q, which goes from the first item to the last
774 Slinknc *p = head;
775 Slinknc *q = head;
776 while (q != tail and q != nullptr)
777 {
778 q = NEXT(q);
779 ++count;
780 if (q == tail or q == nullptr)
781 break;
782
783 p = NEXT(p);
784 q = NEXT(q);
785 ++count;
786 }
787
788 Slinknc *r_head = NEXT(p);
789
790 l.head = head;
791 l.tail = p;
792
793 r.head = r_head;
794 r.tail = r_head == nullptr ? nullptr : tail;
795
796 NEXT(l.tail) = nullptr;
797
798 head = tail = nullptr;
799
800 return count;
801 }
802
803 size_t split_list_ne(HTList &l, HTList &r) noexcept
804 {
805 return split_list(l, r);
806 }
807
809 size_t split(HTList &l, HTList &r) noexcept
810 {
811 return split_list(l, r);
812 }
813
815
816 size_t reverse() noexcept
817 {
818 HTList tmp;
819
820 size_t count = 0;
821 while (not is_empty())
822 {
823 tmp.insert(remove_first_ne());
824 ++count;
825 }
826
827 swap(tmp);
828
829 return count;
830 }
831
833 size_t reverse_list() noexcept
834 {
835 return reverse();
836 }
837
839
841
849 void cut(Slinknc *link, HTList &list) noexcept
850 {
851 assert(list.is_empty());
852
853 list.head = NEXT(link);
854 list.tail = list.head == nullptr ? nullptr : tail;
855
856 tail = link;
857 NEXT(link) = nullptr;
858 }
859
861 void cut_list(Slinknc *link, HTList &list) noexcept
862 {
863 cut(link, list);
864 }
865
879 void remove_all_and_delete() noexcept
880 {
881 while (not is_empty())
882 delete remove_head_ne();
883 }
884
892 {
893 HTList *lptr = nullptr;
894 Slinknc *curr = nullptr;
895 Slinknc *prev = nullptr;
896
897 long pos = 0;
898
899 protected:
901 HTList *get_list_ptr() const noexcept
902 {
903 return lptr;
904 }
905
906 public:
907 Iterator() noexcept = default;
908
910
912 Iterator(const HTList &list) noexcept
913 : lptr(&const_cast<HTList &>(list)), curr(lptr->head), prev(curr)
914 { /* empty */
915 }
916
918
919 void reset() noexcept
920 {
921 prev = curr = lptr->head;
922 pos = 0;
923 }
924
925 long get_pos() const noexcept
926 {
927 return pos;
928 }
929
931 void reset_first() noexcept
932 {
933 reset();
934 }
935
938 {
939 reset();
940 if (lptr->is_empty())
941 return;
942
943 while (NEXT(curr))
944 {
945 prev = curr;
946 curr = NEXT(curr);
947 ++pos;
948 }
949 }
950
952
953 void end() noexcept
954 {
955 curr = nullptr;
956 }
957
958 Iterator(const Iterator &) = default;
959
960 Iterator &operator = (const Iterator &it) noexcept
961 {
962 lptr = it.lptr;
963 curr = it.curr;
964 prev = it.prev;
965 pos = it.pos;
966 return *this;
967 }
968
969 bool has_curr() const noexcept
970 {
971 return curr != nullptr;
972 }
973
974 bool is_last() const noexcept
975 {
976 return lptr->is_empty() ? false : curr == lptr->tail;
977 }
978
980 bool is_in_first() const noexcept
981 {
982 return lptr->is_empty() ? false : curr == lptr->head;
983 }
984
986 bool is_in_last() const noexcept
987 {
988 return is_last();
989 }
990
998 {
999 ah_overflow_error_if(curr == nullptr) << "Iterator overflow";
1000
1001 return curr;
1002 }
1003
1005 Slinknc *get_curr_ne() const noexcept
1006 {
1007 return curr;
1008 }
1009
1012 void next_ne() noexcept
1013 {
1014 if (curr == lptr->head) // on the first item?
1015 {
1016 assert(prev == lptr->head);
1017 curr = NEXT(curr);
1018 }
1019 else if (curr == lptr->tail) // on the last item?
1020 {
1021 assert(NEXT(prev) == curr);
1022 curr = nullptr;
1023 }
1024 else
1025 {
1026 assert(NEXT(prev) == curr);
1027 prev = curr;
1028 curr = NEXT(curr);
1029 assert(NEXT(prev) == curr);
1030 }
1031 pos++;
1032 }
1033
1040 void next()
1041 {
1042 ah_overflow_error_if(not has_curr()) << "Iterator overflow";
1043 next_ne();
1044 }
1045
1057 Slinknc *del_ne() noexcept
1058 {
1059 assert(has_curr());
1060
1061 Slinknc *ret_val = nullptr;
1062 if (curr == lptr->head) // first item removal
1063 {
1064 ret_val = lptr->remove_first_ne();
1065 prev = curr = lptr->head;
1066 }
1067 else if (curr == lptr->tail) // last item removal
1068 {
1069 assert(NEXT(prev) == curr);
1070 ret_val = curr;
1071 NEXT(prev) = NEXT(curr);
1072 lptr->tail = prev;
1073 curr = nullptr; // put in overflow
1074 }
1075 else
1076 {
1077 assert(NEXT(prev) == curr);
1078 ret_val = curr;
1079 NEXT(prev) = NEXT(curr);
1080 curr = NEXT(curr);
1081 }
1082
1083 ret_val->reset();
1084 return ret_val;
1085 }
1086
1098 {
1099 ah_overflow_error_if(not has_curr()) << "Iterator overflow";
1100
1101 return del_ne();
1102 }
1103 };
1104
1119 size_t size() const noexcept
1120 {
1121 size_t count = 0;
1122 for (Iterator it(*this); it.has_curr(); it.next_ne())
1123 ++count;
1124
1125 return count;
1126 }
1127
1144 void rotate_left(size_t n)
1145 {
1146 if (is_empty())
1147 {
1148 if (n == 0)
1149 return;
1150 ah_domain_error() << "List is empty";
1151 return; // unreachable, silences -Wreturn-type
1152 }
1153
1154 for (size_t i = 0; i < n; ++i)
1156 }
1157};
1158
1159namespace htlist_detail {
1167{
1168 size_t num_items = 0;
1169};
1170} // namespace htlist_detail
1171
1221template <typename T = int>
1222class DynList : public HTList,
1224 public GenericTraverse<DynList<T>>,
1225 public LocateFunctions<DynList<T>, T>,
1226 public SpecialCtors<DynList<T>, T>,
1227 public FunctionalMethods<DynList<T>, T>,
1228 public GenericItems<DynList<T>, T>,
1229 public EqualSequenceMethod<DynList<T>>,
1230 public StlAlephIterator<DynList<T>>
1231{
1233
1234 using CtorBase::CtorBase;
1235
1236 // in_place_transpose() moves the nodes of the rows between lists
1237 template <typename U>
1239
1240public:
1241 using Item_Type = T;
1242
1243 using Key_Type = T;
1244
1245 Slinknc *get_head() const noexcept = delete;
1246 Slinknc *get_tail() const noexcept = delete;
1247
1253 DynList &swap(DynList &l) noexcept
1254 {
1255 std::swap(num_items, l.num_items);
1256 return static_cast<DynList &>(HTList::swap(l));
1257 }
1258
1264 [[nodiscard]] size_t size() const noexcept
1265 {
1266 assert((num_items == 0) == this->is_empty());
1267 return num_items;
1268 }
1269
1271 DynList() noexcept
1272 { /* empty */
1273 }
1274
1277
1278private:
1279 T &__insert(Snodenc<T> *p) noexcept
1280 {
1281 static_assert(std::is_copy_constructible_v<T> or std::is_move_constructible_v<T>,
1282 "No copy assign for T");
1283 HTList::insert(p);
1284 ++num_items;
1285 return p->get_data();
1286 }
1287
1288 T &__append(Snodenc<T> *p) noexcept
1289 {
1290 static_assert(std::is_copy_constructible_v<T> or std::is_move_constructible_v<T>,
1291 "No copy assign for T");
1292 HTList::append(p);
1293 ++num_items;
1294 return p->get_data();
1295 }
1296
1297public:
1308 T &insert(const T &item)
1309 {
1310 return __insert(new Snodenc<T>(item));
1311 }
1312
1323 T &insert(T &&item)
1324 {
1325 return __insert(new Snodenc<T>(std::forward<T>(item)));
1326 }
1327
1329 T &push(const T &item)
1330 {
1331 return insert(item);
1332 }
1333
1335 T &push(T &&item)
1336 {
1337 return insert(std::forward<T>(item));
1338 }
1339
1341 T &put(const T &item)
1342 {
1343 return push(item);
1344 }
1345
1347 T &put(T &&item)
1348 {
1349 return push(std::forward<T>(item));
1350 }
1351
1362 T &append(const T &item)
1363 {
1364 return __append(new Snodenc<T>(item));
1365 }
1366
1377 T &append(T &&item)
1378 {
1379 return __append(new Snodenc<T>(std::forward<T>(item)));
1380 }
1381
1382private:
1383 // Removes the first item, which the list must have, when its move can
1384 // throw. The move goes straight into the result, which a named object
1385 // could not guarantee, and the node leaves when leave ends, after that
1386 // move; if the move throws, the node stays, and with it the item.
1388 {
1389 struct Leave
1390 {
1391 DynList &list;
1392 bool keep = false;
1393
1394 ~Leave()
1395 {
1396 if (not keep)
1397 {
1398 delete static_cast<Snodenc<T> *>(list.HTList::remove_head_ne());
1399 --list.num_items;
1400 }
1401 }
1402 } leave{*this};
1403
1404 try
1405 {
1406 return std::move(get_first_ne());
1407 }
1408 catch (...)
1409 {
1410 leave.keep = true;
1411 throw;
1412 }
1413 }
1414
1415public:
1423 T remove_ne() noexcept(std::is_nothrow_move_constructible_v<T>)
1424 {
1425 if constexpr (std::is_nothrow_move_constructible_v<T>)
1426 {
1427 Slinknc *l = this->HTList::remove_head_ne();
1428 --num_items;
1429 auto *p = static_cast<Snodenc<T> *>(l);
1430 T ret_val = std::move(p->get_data());
1431 delete p;
1432
1433 return ret_val;
1434 }
1435 else
1437 }
1438
1448 {
1449 if constexpr (std::is_nothrow_move_constructible_v<T>)
1450 {
1451 Slinknc *l = this->HTList::remove_head();
1452 --num_items;
1453 auto *p = static_cast<Snodenc<T> *>(l);
1454 T ret_val = std::move(p->get_data());
1455 delete p;
1456
1457 return ret_val;
1458 }
1459 else
1460 {
1461 ah_underflow_error_if(this->is_empty()) << "HTList is empty";
1463 }
1464 }
1465
1467 T remove_first_ne() noexcept(std::is_nothrow_move_constructible_v<T>)
1468 {
1469 return remove_ne();
1470 }
1471
1474 {
1475 return remove();
1476 }
1477
1480 {
1481 return remove_first();
1482 }
1483
1486 {
1487 return remove();
1488 }
1489
1494 T &get_last_ne() noexcept
1495 {
1496 auto *p = static_cast<Snodenc<T> *>(this->HTList::get_last());
1497 return p->get_data();
1498 }
1499
1502 const T &get_last_ne() const noexcept
1503 {
1504 auto *p = static_cast<Snodenc<T> *>(this->HTList::get_last());
1505 return p->get_data();
1506 }
1507
1512 T &get_first_ne() noexcept
1513 {
1514 return static_cast<Snodenc<T> *>(this->HTList::get_first())->get_data();
1515 }
1516
1519 const T &get_first_ne() const noexcept
1520 {
1521 return static_cast<Snodenc<T> *>(this->HTList::get_first())->get_data();
1522 }
1523
1530 {
1531 ah_underflow_error_if(is_empty()) << "List is empty";
1532 return get_last_ne();
1533 }
1534
1537 const T &get_last() const
1538 {
1539 ah_underflow_error_if(is_empty()) << "List is empty";
1540 return get_last_ne();
1541 }
1542
1549 {
1550 ah_underflow_error_if(is_empty()) << "List is empty";
1551 return get_first_ne();
1552 }
1553
1556 const T &get_first() const
1557 {
1558 ah_underflow_error_if(is_empty()) << "List is empty";
1559 return get_first_ne();
1560 }
1561
1564 {
1565 return get_first();
1566 }
1567
1570 const T &top() const
1571 {
1572 return get_first();
1573 }
1574
1576 void empty() noexcept
1577 {
1578 if (is_empty())
1579 return;
1580
1581 // a very fast deletion
1582 auto *last = static_cast<Snodenc<T> *>(this->HTList::get_last());
1583 auto *p = static_cast<Snodenc<T> *>(this->HTList::get_first());
1584 while (p != last)
1585 {
1586 Snodenc<T> *q = p;
1587 p = p->get_next();
1588 delete q;
1589 }
1590 delete p;
1591 HTList::reset();
1592 num_items = 0;
1593 }
1594
1599 void clear() noexcept
1600 {
1601 empty();
1602 }
1603
1605 {
1606 empty();
1607 }
1608
1614 {
1615 // The list, whose count the deletes lower: the one of the base
1616 // iterator, so that the iterator takes no more room
1617 DynList *list_ptr() const noexcept
1618 {
1619 return static_cast<DynList *>(this->get_list_ptr());
1620 }
1621
1622 // Removes the current item, which the iterator must have, when its
1623 // move can throw. As in remove_first_whose_move_throws(), the move
1624 // goes straight into the result and the node leaves after it; if the
1625 // move throws, the node stays, and the iterator on it.
1627 {
1628 struct Leave
1629 {
1630 Iterator &it;
1631 bool keep = false;
1632
1633 ~Leave()
1634 {
1635 if (not keep)
1636 delete static_cast<Snodenc<T> *>(it.del_ne());
1637 }
1638 } leave{*this};
1639
1640 try
1641 {
1642 return std::move(get_curr_ne());
1643 }
1644 catch (...)
1645 {
1646 leave.keep = true;
1647 throw;
1648 }
1649 }
1650
1651 public:
1652 using Item_Type = T;
1653
1655
1657
1658 Iterator() noexcept = default;
1659
1661 Iterator(DynList &list) noexcept : HTList::Iterator(list)
1662 { /* empty */
1663 }
1664
1666 Iterator(const DynList &) = delete;
1667
1669 T &get_curr_ne() const noexcept
1670 {
1671 return static_cast<Snodenc<T> *>(HTList::Iterator::get_curr_ne())->get_data();
1672 }
1673
1679 T &get_curr() const
1680 {
1681 return static_cast<Snodenc<T> *>(HTList::Iterator::get_curr())->get_data();
1682 }
1683
1696 {
1697 if constexpr (std::is_nothrow_move_constructible_v<T>)
1698 {
1699 auto *p = static_cast<Snodenc<T> *>(this->HTList::Iterator::del());
1700 --list_ptr()->num_items;
1701 T ret_val = std::move(p->get_data());
1702 delete p;
1703 return ret_val;
1704 }
1705 else
1706 {
1707 ah_overflow_error_if(not this->has_curr()) << "Iterator overflow";
1708 return del_whose_move_throws();
1709 }
1710 }
1711
1717 Slinknc *del_ne() noexcept
1718 {
1720 --list_ptr()->num_items;
1721 return p;
1722 }
1723 };
1724
1727
1728 DynList &reverse() noexcept
1729 {
1730 reverse_list();
1731 return *this;
1732 }
1733
1734 DynList &rev() noexcept
1735 {
1736 return reverse();
1737 }
1738
1742 {
1743 DynList ret;
1744 for (auto it = this->get_it(); it.has_curr(); it.next())
1745 ret.insert(it.get_curr());
1746
1747 return ret;
1748 }
1749
1750 DynList rev() const
1751 {
1752 return reverse();
1753 }
1754
1755 template <class Equal = std::equal_to<T>>
1756 T remove_ne(Equal eq) noexcept
1757 {
1758 for (Iterator it(*this); it.has_curr(); it.next_ne())
1759 {
1760 const T &item = it.get_curr_ne();
1761 if (not eq(item))
1762 continue;
1763 T ret = item; // performs a copy
1764 auto *removed = static_cast<Snodenc<T> *>(it.del_ne());
1765 delete removed;
1766 return ret;
1767 }
1768 return T(); // not found, return default constructed T
1769 }
1770
1794 template <class Equal = std::equal_to<T>>
1795 T remove(Equal eq)
1796 {
1797 for (Iterator it(*this); it.has_curr(); it.next_ne())
1798 if (const T &item = it.get_curr(); eq(item))
1799 {
1800 T ret = item; // performs a copy
1801 auto *removed = static_cast<Snodenc<T> *>(it.HTList::Iterator::del());
1802 --num_items;
1803 delete removed;
1804 return ret;
1805 }
1806
1807 ah_domain_error() << "DynList::remove(Equal &): item not found";
1808 return T(); // unreachable, silences -Wreturn-type
1809 }
1810
1825 DynList(DynList &&l) noexcept
1826 {
1827 swap(l);
1828 }
1829
1841 {
1842 if (&l == this)
1843 return *this;
1844
1845 empty();
1846
1847 for (auto it = l.get_it(); it.has_curr(); it.next_ne())
1848 append(it.get_curr());
1849
1850 return *this;
1851 }
1852
1868 {
1869 return static_cast<DynList &>(this->swap(l));
1870 }
1871
1886 DynList &append(DynList &&list) noexcept
1887 {
1888 HTList::append(list);
1889 num_items += list.num_items;
1890 list.num_items = 0;
1891 return *this;
1892 }
1893
1908 DynList &insert(DynList &&list) noexcept
1909 {
1910 HTList::insert(list);
1911 num_items += list.num_items;
1912 list.num_items = 0;
1913 return *this;
1914 }
1915
1924 DynList &append(const DynList &list)
1925 {
1926 DynList copy = list;
1927 return append(std::move(copy));
1928 }
1929
1938 DynList &insert(const DynList &list)
1939 {
1940 DynList tmp = list;
1941 return insert(std::move(tmp));
1942 }
1943
1947 void concat(DynList &l) noexcept
1948 {
1949 append(std::move(l));
1950 }
1951
1953 void concat_list(DynList &l) noexcept
1954 {
1955 append(std::move(l));
1956 }
1957
1967 void cut(Slinknc *link, DynList &list) noexcept
1968 {
1969 size_t k = 0;
1970 for (Slinknc *p = link->get_next(); p != nullptr; p = p->get_next())
1971 ++k;
1972
1973 HTList::cut(link, list);
1974 num_items -= k;
1975 list.num_items = k;
1976 }
1977
1979 void cut_list(Slinknc *link, DynList &list) noexcept
1980 {
1981 cut(link, list);
1982 }
1983
1990 {
1991 --num_items;
1992 return HTList::remove_head_ne();
1993 }
1994
2002 {
2004 --num_items;
2005 return p;
2006 }
2007
2010 {
2011 empty();
2012 }
2013
2017 void reset() noexcept
2018 {
2019 HTList::reset();
2020 num_items = 0;
2021 }
2022
2033 size_t split_list_ne(DynList &l, DynList &r) noexcept
2034 {
2035 assert(l.is_empty() and r.is_empty());
2036
2037 const size_t n = num_items;
2038 if (n == 0)
2039 return 0;
2040
2041 Slinknc *p = HTList::get_first(); // to be the last node of l
2042 for (size_t i = 1; i < n - n / 2; ++i)
2043 p = p->get_next();
2044
2045 HTList::cut(p, r);
2046 HTList::swap(l);
2047 l.num_items = n - n / 2;
2048 r.num_items = n / 2;
2049 num_items = 0;
2050
2051 return n;
2052 }
2053
2055 size_t split_list(DynList &l, DynList &r) noexcept
2056 {
2057 return split_list_ne(l, r);
2058 }
2059
2061 size_t split(DynList &l, DynList &r) noexcept
2062 {
2063 return split_list_ne(l, r);
2064 }
2065
2068 T &get(const size_t i)
2069 {
2070 Iterator it(*this);
2071
2072 for (size_t __i = 0; it.has_curr() and __i < i; it.next_ne(), ++__i)
2073 ;
2074
2075 return it.get_curr();
2076 }
2077
2079 T &operator [] (const size_t &i)
2080 {
2081 return get(i);
2082 }
2083};
2084
2085template <class Container>
2087{
2088 return c.template maps<typename Container::Item_Type>([](const auto &d) -> const auto &
2089 {
2090 return d;
2091 });
2092}
2093
2094template <typename T>
2096{
2097 DynList<T> ret;
2098 ret.append(item);
2099 return ret;
2100}
2101
2102template <typename T>
2103inline DynList<T> unitarian_DynList(const T &item)
2104{
2105 return DynList<T>({item});
2106}
2107
2108#undef NEXT
2109
2110} // end namespace Aleph
2111
2112#endif // HTLIST_H
Variadic constructor macros for containers.
Container traversal and functional operation mixins.
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
STL-compatible iterator adapters for Aleph containers.
Standard functor implementations and comparison objects.
Functional programming utilities for Aleph-w containers.
Read-only view of an Aleph iterator: the iterator of a constant container.
Iterator on the items of list.
Definition htlist.H:1614
Iterator() noexcept=default
The type of container.
DynList * list_ptr() const noexcept
Definition htlist.H:1617
T & get_curr() const
Return the current item.
Definition htlist.H:1679
T del()
Remove the current item of the iterator.
Definition htlist.H:1695
Iterator(const DynList &)=delete
A constant list is iterated through Const_Iterator.
Slinknc * del_ne() noexcept
Remove the current node, which the caller takes, and move the iterator forward; the iterator must hav...
Definition htlist.H:1717
T & get_curr_ne() const noexcept
Return the current link guaranteeing no exception. Be careful.
Definition htlist.H:1669
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1231
DynList rev() const
Definition htlist.H:1750
T & get_first()
Return the first item of the list.
Definition htlist.H:1548
void remove_all_and_delete() noexcept
Delete all the items, as empty() does.
Definition htlist.H:2009
T remove(Equal eq)
Remove the first element matching an equality criteria.
Definition htlist.H:1795
size_t split_list_ne(DynList &l, DynList &r) noexcept
Split the list in two.
Definition htlist.H:2033
void cut_list(Slinknc *link, DynList &list) noexcept
See cut(Slinknc * link, DynList & list).
Definition htlist.H:1979
T & append(T &&item)
Definition htlist.H:1377
DynList(DynList &&l) noexcept
Initialize the list with all the items of l moved to this.
Definition htlist.H:1825
T pop()
See remove().
Definition htlist.H:1479
T & put(T &&item)
See insert(T && item).
Definition htlist.H:1347
const T & get_first() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition htlist.H:1556
T remove_ne(Equal eq) noexcept
Definition htlist.H:1756
size_t split(DynList &l, DynList &r) noexcept
See split_list_ne(DynList & l, DynList & r).
Definition htlist.H:2061
T & append(const T &item)
Definition htlist.H:1362
const T & get_last() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition htlist.H:1537
T remove_ne() noexcept(std::is_nothrow_move_constructible_v< T >)
Remove the first item of the list, which must not be empty.
Definition htlist.H:1423
Slinknc * get_tail() const noexcept=delete
T & get_first_ne() noexcept
Return the first item of the list without exception.
Definition htlist.H:1512
size_t split_list(DynList &l, DynList &r) noexcept
See split_list_ne(DynList & l, DynList & r).
Definition htlist.H:2055
DynList & operator=(const DynList &l)
Assign to this a copy of l
Definition htlist.H:1840
DynList() noexcept
Initialize an empty list. It allocates nothing.
Definition htlist.H:1271
Slinknc * get_head() const noexcept=delete
T & insert(const T &item)
Definition htlist.H:1308
DynList & append(DynList &&list) noexcept
Append listat the end of this by movement.
Definition htlist.H:1886
void concat(DynList &l) noexcept
Append the items of l to this, in constant time; l becomes empty.
Definition htlist.H:1947
T & operator[](const size_t &i)
Array-style access to the i-th item. Equivalent to get(i).
Definition htlist.H:2079
T get()
See remove().
Definition htlist.H:1485
const T & get_first_ne() const noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition htlist.H:1519
friend void in_place_transpose(DynList< DynList< U > > &l)
T remove_first_whose_move_throws()
Definition htlist.H:1387
T & get_last()
Return the last item of the list.
Definition htlist.H:1529
DynList reverse() const
Return a reversed copy of this.
Definition htlist.H:1741
DynList & rev() noexcept
Definition htlist.H:1734
T & get_last_ne() noexcept
Return the last item of the list without exception.
Definition htlist.H:1494
T & __insert(Snodenc< T > *p) noexcept
Definition htlist.H:1279
T remove()
Remove the first item of the list.
Definition htlist.H:1447
DynList(const DynList &l)
Initialize a list with a copy of all the items of list l
Definition htlist.H:1276
void cut(Slinknc *link, DynList &list) noexcept
Move to list, which must be empty, the items that follow the node link of this.
Definition htlist.H:1967
Slinknc * remove_head_ne() noexcept
Remove the first node of the list, which must not be empty; the caller takes the node,...
Definition htlist.H:1989
T & get(const size_t i)
Obtains a modifiable reference to the i-th item of this.
Definition htlist.H:2068
void clear() noexcept
Empties the container.
Definition htlist.H:1599
DynList & insert(const DynList &list)
Insert to this a copy of list.
Definition htlist.H:1938
void concat_list(DynList &l) noexcept
See concat(DynList & l).
Definition htlist.H:1953
T remove_first()
See remove().
Definition htlist.H:1473
T remove_first_ne() noexcept(std::is_nothrow_move_constructible_v< T >)
See remove_ne().
Definition htlist.H:1467
T & push(T &&item)
See insert(T && item).
Definition htlist.H:1335
const T & top() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition htlist.H:1570
T & top()
See get_first().
Definition htlist.H:1563
void reset() noexcept
Forget all the nodes of the list, without deleting them, as HTList::reset() does; the list becomes em...
Definition htlist.H:2017
size_t size() const noexcept
Return the number of items of the list, in constant time.
Definition htlist.H:1264
DynList & insert(DynList &&list) noexcept
Insert listat the beginning of this by movement.
Definition htlist.H:1908
T & push(const T &item)
See insert(const T & item).
Definition htlist.H:1329
const T & get_last_ne() const noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition htlist.H:1502
T & insert(T &&item)
Insert a new item by movement.
Definition htlist.H:1323
T & __append(Snodenc< T > *p) noexcept
Definition htlist.H:1288
T & put(const T &item)
See insert(const T & item).
Definition htlist.H:1341
DynList & reverse() noexcept
Definition htlist.H:1728
void empty() noexcept
empty the list
Definition htlist.H:1576
DynList & swap(DynList &l) noexcept
Definition htlist.H:1253
Slinknc * remove_head()
Remove the first node of the list; the caller takes the node, and its item.
Definition htlist.H:2001
DynList & append(const DynList &list)
Append to this a copy of list.
Definition htlist.H:1924
HTList * get_list_ptr() const noexcept
The list of the iterator, for the iterators of the derived lists.
Definition htlist.H:901
Slinknc * get_curr_ne() const noexcept
Return the current link guaranteeing no exception. Be careful.
Definition htlist.H:1005
long get_pos() const noexcept
Definition htlist.H:925
void reset_last()
It has O(n) of performance.
Definition htlist.H:937
void next_ne() noexcept
Move the iterator one position forward guaranteeing no exception.
Definition htlist.H:1012
void reset_first() noexcept
See reset().
Definition htlist.H:931
bool is_in_last() const noexcept
Return true if the iterator is positioned on the last item.
Definition htlist.H:986
bool is_last() const noexcept
Definition htlist.H:974
Slinknc * get_curr() const
Definition htlist.H:997
Iterator & operator=(const Iterator &it) noexcept
Definition htlist.H:960
bool has_curr() const noexcept
Definition htlist.H:969
bool is_in_first() const noexcept
Return true if the iterator is positioned on the first item.
Definition htlist.H:980
void reset() noexcept
Reset the iterator at the first item.
Definition htlist.H:919
Slinknc * del_ne() noexcept
Remove the current item and move the iterator one position forward, without checking that there is a ...
Definition htlist.H:1057
Iterator() noexcept=default
Iterator(const Iterator &)=default
void end() noexcept
Set the iterator to its end position, which has not.
Definition htlist.H:953
Single linked list of nodes.
Definition htlist.H:420
Slinknc * head
Definition htlist.H:421
Slinknc * get_first() const noexcept
Definition htlist.H:463
void push(Slinknc *link) noexcept
Definition htlist.H:717
void put(Slinknc *link) noexcept
Definition htlist.H:553
size_t reverse_list() noexcept
See reverse().
Definition htlist.H:833
void remove_all_and_delete() noexcept
Definition htlist.H:879
Slinknc * top() const
Definition htlist.H:739
Slinknc * get_tail() const noexcept
Definition htlist.H:458
size_t reverse() noexcept
It inverts all list elements. It returns list size.
Definition htlist.H:816
void insert(HTList &l) noexcept
Insert starting in link (contained in 'this' list) the.
Definition htlist.H:580
void cut(Slinknc *link, HTList &list) noexcept
It cuts 'this' over 'link' element and it puts all.
Definition htlist.H:849
void cut_list(Slinknc *link, HTList &list) noexcept
See cut(Slinknc * link, HTList & list).
Definition htlist.H:861
constexpr bool is_empty() const noexcept
Definition htlist.H:434
Slinknc * remove_head()
Remove the header item from the list.
Definition htlist.H:652
Slinknc * tail
Definition htlist.H:422
void insert(Slinknc *link, HTList &list) noexcept
Insert a list into this after one of its items.
Definition htlist.H:613
Slinknc * get_head() const noexcept
Definition htlist.H:453
void rotate_left(size_t n)
Rotate to left the list n positions.
Definition htlist.H:1144
void concat_list(HTList &l) noexcept
See concat(HTList & l).
Definition htlist.H:566
HTList & swap(HTList &l) noexcept
Definition htlist.H:488
Slinknc * pop()
Remove the first item of this.
Definition htlist.H:733
void append(Slinknc *link) noexcept
Definition htlist.H:518
void append(HTList &l) noexcept
Definition htlist.H:537
size_t split_list(HTList &l, HTList &r) noexcept
It divides 'this' into two equal lists without modifying.
Definition htlist.H:760
HTList() noexcept
Definition htlist.H:425
Slinknc * remove_first()
Definition htlist.H:672
Slinknc * remove_first_ne() noexcept
Remove the header item from the list.
Definition htlist.H:667
size_t split(HTList &l, HTList &r) noexcept
See split_list(HTList & l, HTList & r).
Definition htlist.H:809
size_t size() const noexcept
Count the number of elements of the list.
Definition htlist.H:1119
bool remove(Slinknc *link)
Definition htlist.H:691
void insert(Slinknc *link) noexcept
Definition htlist.H:499
Slinknc * remove_head_ne() noexcept
It deletes head element (first one). Return deleted.
Definition htlist.H:630
bool is_unitarian() const noexcept
Return true if the list contains exactly just one.
Definition htlist.H:441
Slinknc * get_last() const noexcept
Return the last item of the list (nullptr if the list is empty)
Definition htlist.H:475
size_t split_list_ne(HTList &l, HTList &r) noexcept
Definition htlist.H:803
void concat(HTList &l) noexcept
Concat to 'this' all 'l' list; 'l' becomes empty.
Definition htlist.H:560
void reset() noexcept
Definition htlist.H:429
Slinknc * get_first_ne() const noexcept
Return the first item of the list; the list must not be empty.
Definition htlist.H:469
bool is_unitarian_or_empty() const noexcept
Return true if list contains one element or is empty.
Definition htlist.H:448
void reset_first() noexcept
Reset the iterator to the first link on the list.
Definition htlist.H:273
bool has_curr() const noexcept
Return true if the iterator is positioned on a valid link.
Definition htlist.H:231
Slinknc * get_curr() const
Definition htlist.H:241
Slinknc * get_curr_ne() const noexcept
Return the current link guaranteeing no exception. Be careful.
Definition htlist.H:248
void init() noexcept
Definition htlist.H:198
Iterator(Slinknc *_head, Slinknc *_curr) noexcept
Initialize an iterator on the list pointed by _head, but on a node pointed by curr
Definition htlist.H:222
void next_ne() noexcept
Move the iterator one position forward guaranteeing no exception.
Definition htlist.H:255
Iterator(Slinknc &list) noexcept
Initialize an iterator on the first node of list
Definition htlist.H:211
Link of a single linked list non-circular and without header node.
Definition htlist.H:95
virtual ~Slinknc()=default
Slinknc * next
Definition htlist.H:96
T & to_data() noexcept
Return the item of the Snodenc<T> that this is.
Definition htlist.H:382
Slinknc *& get_next() noexcept
Definition htlist.H:134
void reset() noexcept
Reset the link to nullptr.
Definition htlist.H:121
Slinknc() noexcept
Init to nullptr.
Definition htlist.H:109
constexpr bool is_empty() const noexcept
Return true if this is empty.
Definition htlist.H:102
Slinknc & operator=(const Slinknc &) noexcept
Dummy asignation; link is set to nullptr.
Definition htlist.H:128
Slinknc(const Slinknc &) noexcept
Dummy copy constructor.
Definition htlist.H:115
Snodenc< T > * to_snodenc() noexcept
Convert this to a Snodenc<T>.
Definition htlist.H:370
Slinknc * remove_next() noexcept
Definition htlist.H:159
void insert(Slinknc *p) noexcept
insert(p) inserts the node pointed by p after this.
Definition htlist.H:146
Snodenc(T &&item)
Construct by moving item
Definition htlist.H:321
Snodenc *& get_first() noexcept
Return the first node of the list whose header is this.
Definition htlist.H:363
Snodenc(const T &item)
Construct with copy of item
Definition htlist.H:314
const T & get_data() const noexcept
Return a constant reference to the data.
Definition htlist.H:302
Snodenc *& get_next() noexcept
Return the node following to this
Definition htlist.H:342
T & get_data() noexcept
Return a modifiable reference to the data.
Definition htlist.H:295
Snodenc * remove_first() noexcept
See remove_next().
Definition htlist.H:350
Snodenc * remove_next() noexcept
Definition htlist.H:335
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.
#define NEXT(p)
Definition htlist.H:58
Main namespace for Aleph-w library functions.
DynList< typename Container::Item_Type > to_dynlist(const Container &c)
Definition htlist.H:2086
bool eq(const C1 &c1, const C2 &c2, Eq e=Eq())
Check equality of two containers using a predicate.
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
Definition ahAlgo.H:584
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:152
DynList< T > unitarian_DynList(const T &item)
Definition htlist.H:2103
and
Check uniqueness with explicit hash + equality functors.
bool has_curr() const noexcept
Return true if all underlying iterators are positioned on a valid item.
Definition ah-zip.H:173
DynList< T > get_unitarian_DynList(const T &item)
Definition htlist.H:2095
static std::atomic< bool > init
Definition hash-fct.C:54
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Definition ahAlgo.H:127
STL namespace.
The number of items of a DynList, which its operations keep.
Definition htlist.H:1167
size_t num_items
The number of items of the list.
Definition htlist.H:1168
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
static int * k
Gsl_Compatible_Rng r
DynList< int > l