Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
noexcept_contract_test.cc
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
69#include <atomic>
70#include <cstddef>
71#include <cstdint>
72#include <exception>
73#include <limits>
74#include <mutex>
75#include <optional>
76#include <stdexcept>
77#include <string>
78#include <type_traits>
79#include <utility>
80
81#include <gtest/gtest.h>
82
83#include <ah-errors.H>
84#include <Floyd_Warshall.H>
85#include <HLD.H>
86#include <LCA.H>
87#include <Tree_DP.H>
88#include <Tree_Decomposition.H>
89#include <thread_pool.H>
90#include <tpl_b_tree.H>
91#include <tpl_bplus_tree.H>
92#include <tpl_ca_async_engine.H>
93#include <tpl_ca_engine.H>
95#include <tpl_ca_lattice.H>
99#include <tpl_ca_neighborhood.H>
101#include <tpl_ca_rule.H>
102#include <tpl_ca_storage.H>
103#include <tpl_cartesian_tree.H>
105#include <tpl_dynArrayHeap.H>
106#include <tpl_dynMapTree.H>
107#include <tpl_dynSkipList.H>
108#include <tpl_fenwick_tree.H>
109#include <tpl_graph.H>
110#include <tpl_mo_on_trees.H>
112#include <tpl_rand_tree.H>
113#include <tpl_segment_tree.H>
114#include <tpl_sparse_table.H>
115#include <tpl_tree_node.H>
116
117using namespace Aleph;
118using namespace Aleph::CA;
119
120namespace
121{
122 // The operations of the adversarial types that may throw count their
123 // calls, and throw while a test arms them. Atomic: the parallel engine
124 // may copy its rule in its threads
125 std::atomic<bool> armed{false};
126 std::atomic<size_t> risky_calls{0};
127
128 constexpr const char * adversarial_what = "an adversarial type of the user threw";
129
130 void risky_operation()
131 {
132 ++risky_calls;
133 ah_runtime_error_if(armed.load()) << adversarial_what;
134 }
135
136 bool is_adversarial(const std::exception & e)
137 {
138 return std::string(e.what()).find(adversarial_what) != std::string::npos;
139 }
140
141 // (a) A noexcept swap of its own, which the arguments find, and a move
142 // constructor and a move assignment that throw
143 template <class Base>
144 struct Swap_Noexcept : Base
145 {
146 Swap_Noexcept() = default;
147 explicit Swap_Noexcept(const Base & b) noexcept : Base(b) {}
148 Swap_Noexcept(const Swap_Noexcept & o) noexcept : Base(o) {}
149 Swap_Noexcept(Swap_Noexcept && o) : Base(std::move(o)) { risky_operation(); }
150
151 Swap_Noexcept & operator = (const Swap_Noexcept & o) noexcept
152 {
153 Base::operator = (o);
154 return *this;
155 }
156
157 Swap_Noexcept & operator = (Swap_Noexcept && o)
158 {
159 risky_operation();
160 Base::operator = (std::move(o));
161 return *this;
162 }
163
164 friend void swap(Swap_Noexcept & a, Swap_Noexcept & b) noexcept
165 {
166 std::swap(static_cast<Base &>(a), static_cast<Base &>(b));
167 }
168 };
169
170 // (b) A move assignment that throws, and a move constructor that does not
171 template <class Base>
172 struct Assign_Throws : Base
173 {
174 Assign_Throws() = default;
175 explicit Assign_Throws(const Base & b) noexcept : Base(b) {}
176 Assign_Throws(const Assign_Throws & o) noexcept : Base(o) {}
177 Assign_Throws(Assign_Throws && o) noexcept : Base(std::move(o)) {}
178
179 Assign_Throws & operator = (const Assign_Throws & o) noexcept
180 {
181 Base::operator = (o);
182 return *this;
183 }
184
185 Assign_Throws & operator = (Assign_Throws && o)
186 {
187 risky_operation();
188 Base::operator = (std::move(o));
189 return *this;
190 }
191 };
192
193 // (c) A copy that throws, and moves that do not
194 template <class Base>
195 struct Copy_Throws : Base
196 {
197 Copy_Throws() = default;
198 explicit Copy_Throws(const Base & b) noexcept : Base(b) {}
199 Copy_Throws(const Copy_Throws & o) : Base(o) { risky_operation(); }
200 Copy_Throws(Copy_Throws && o) noexcept : Base(std::move(o)) {}
201
202 Copy_Throws & operator = (const Copy_Throws & o)
203 {
204 risky_operation();
205 Base::operator = (o);
206 return *this;
207 }
208
209 Copy_Throws & operator = (Copy_Throws && o) noexcept
210 {
211 Base::operator = (std::move(o));
212 return *this;
213 }
214 };
215
216 // The swaps of the kind (d) are numbered from one while armed: those
217 // whose numbers are throw_at and throw_also throw. Two of them make a swap
218 // that puts back what another exchanged throw too
219 std::atomic<long> throw_at{1};
220 std::atomic<long> throw_also{0};
221 std::atomic<long> swaps_armed{0};
222
223 void risky_swap()
224 {
225 ++risky_calls;
226 if (not armed.load())
227 return;
228 const long k = ++swaps_armed;
229 ah_runtime_error_if(k == throw_at or k == throw_also) << adversarial_what;
230 }
231
232 // Arms the adversarial types, with the swaps numbered first and second
233 // to throw (none second if it is 0)
234 void arm_swaps(const long first, const long second = 0)
235 {
236 swaps_armed = 0;
237 throw_at = first;
238 throw_also = second;
239 armed = true;
240 }
241
242 // The tag of each live object of the kind (d), by its address
243 std::mutex tags_mutex;
245 std::atomic<long> last_tag{0};
246
247 void set_tag(const void * p, const long tag)
248 {
249 const std::lock_guard<std::mutex> lock(tags_mutex);
250 const auto key = reinterpret_cast<std::uintptr_t>(p);
251 if (auto * pair = live_tags.search(key); pair != nullptr)
252 pair->second = tag;
253 else
254 (void) live_tags.insert(key, tag);
255 }
256
257 void drop_tag(const void * p)
258 {
259 const std::lock_guard<std::mutex> lock(tags_mutex);
260 const auto key = reinterpret_cast<std::uintptr_t>(p);
261 if (live_tags.search(key) != nullptr)
262 live_tags.remove_key(key);
263 }
264
265 // The tags of the objects of the kind (d) that x holds in its own bytes,
266 // in the order of their addresses: whose types of the user x has
267 template <class T>
268 std::string tags_of(const T & x)
269 {
270 const std::lock_guard<std::mutex> lock(tags_mutex);
271 const auto lo = reinterpret_cast<std::uintptr_t>(&x);
272 std::string tags;
273 live_tags.for_each([&tags, lo] (const auto & p)
274 {
275 if (p.first >= lo and p.first < lo + sizeof(T))
276 tags += std::to_string(p.second) + " ";
277 });
278 return tags;
279 }
280
281 // (d) A swap of its own that throws, while armed, when throw_at reaches
282 // it, before it changes anything, and copies and moves that do not. Each
283 // object has a tag, which its copies and moves take and its swap
284 // exchanges, so that tags_of() tells whose types of the user an object
285 // holds. Built from its base without explicit, for the values that an
286 // operation returns
287 template <class Base>
288 struct Swap_Throws : Base
289 {
290 long tag = ++last_tag;
291
292 Swap_Throws() noexcept { set_tag(this, tag); }
293 Swap_Throws(const Base & b) noexcept : Base(b) { set_tag(this, tag); }
294 Swap_Throws(const Swap_Throws & o) noexcept : Base(o), tag(o.tag) { set_tag(this, tag); }
295 Swap_Throws(Swap_Throws && o) noexcept : Base(std::move(o)), tag(o.tag) { set_tag(this, tag); }
296 ~Swap_Throws() { drop_tag(this); }
297
298 Swap_Throws & operator = (const Swap_Throws & o) noexcept
299 {
300 Base::operator = (o);
301 set_tag(this, tag = o.tag);
302 return *this;
303 }
304
305 Swap_Throws & operator = (Swap_Throws && o) noexcept
306 {
307 Base::operator = (std::move(o));
308 set_tag(this, tag = o.tag);
309 return *this;
310 }
311
312 friend void swap(Swap_Throws & a, Swap_Throws & b)
313 {
314 risky_swap();
315 std::swap(static_cast<Base &>(a), static_cast<Base &>(b));
316 std::swap(a.tag, b.tag);
317 set_tag(&a, a.tag);
318 set_tag(&b, b.tag);
319 }
320 };
321
322 // (e) A move constructor that throws before it moves anything, a move
323 // assignment that does not, and no swap of its own: std::swap() throws
324 // when it builds its temporary
325 template <class Base>
326 struct Move_Ctor_Throws : Base
327 {
328 Move_Ctor_Throws() = default;
329 explicit Move_Ctor_Throws(const Base & b) noexcept : Base(b) {}
330 Move_Ctor_Throws(const Move_Ctor_Throws & o) noexcept : Base(o) {}
331 Move_Ctor_Throws(Move_Ctor_Throws && o) : Base((risky_operation(), static_cast<Base &&>(o))) {}
332
333 Move_Ctor_Throws & operator = (const Move_Ctor_Throws & o) noexcept
334 {
335 Base::operator = (o);
336 return *this;
337 }
338
339 Move_Ctor_Throws & operator = (Move_Ctor_Throws && o) noexcept
340 {
341 Base::operator = (std::move(o));
342 return *this;
343 }
344 };
345
346 template <template <class> class A> constexpr const char * kind_name = "";
347 template <> constexpr const char * kind_name<Swap_Noexcept> = "Swap_Noexcept";
348 template <> constexpr const char * kind_name<Assign_Throws> = "Assign_Throws";
349 template <> constexpr const char * kind_name<Copy_Throws> = "Copy_Throws";
350 template <> constexpr const char * kind_name<Swap_Throws> = "Swap_Throws";
351 template <> constexpr const char * kind_name<Move_Ctor_Throws> = "Move_Ctor_Throws";
352
353 // A value of the user, for the classes that keep one of their own, as
354 // an identity, in a member
355 struct Int
356 {
357 int v = 0;
358
359 Int() = default;
360 Int(const int x) noexcept : v(x) {}
361
362 friend bool operator == (const Int & a, const Int & b) noexcept { return a.v == b.v; }
363 };
364
365 struct Int_Plus
366 {
367 Int operator () (const Int & a, const Int & b) const noexcept { return a.v + b.v; }
368 };
369
370 struct Int_Less
371 {
372 bool operator () (const Int & a, const Int & b) const noexcept { return a.v < b.v; }
373 };
374
375 int value_of(const int x) noexcept { return x; }
376 int value_of(const Int & x) noexcept { return x.v; }
377
378 // The value x of the type V of a case: an int, or one made from an Int
379 template <class V>
380 V value_from(const int x)
381 {
382 if constexpr (std::is_same_v<V, int>)
383 return x;
384 else
385 return V(Int(x));
386 }
387
388 // n values in [0, 100]: a fixed scramble, with repetitions
389 Array<int> values_of(const size_t n)
390 {
391 auto a = Array<int>::create(n);
392 for (size_t i = 0; i < n; ++i)
393 a(i) = static_cast<int>((i * 37 + 11) % 101);
394 return a;
395 }
396
398 using Gnode = Graph::Node;
400 using Tnode = Tree_Node<int>;
401 using Show_Arc = Dft_Show_Arc<Graph>;
402
403 // The tree of n nodes in which the node i is a child of (i - 1) / 2,
404 // with the info i % 7, built once: it outlives the objects built on it
405 struct Test_Tree
406 {
407 Graph g;
409
410 explicit Test_Tree(const size_t n)
411 {
412 for (size_t i = 0; i < n; ++i)
413 nodes.append(g.insert_node(static_cast<int>(i % 7)));
414 for (size_t i = 1; i < n; ++i)
415 g.insert_arc(nodes((i - 1) / 2), nodes(i), 1);
416 }
417 };
418
419 const Test_Tree & tree_of(const size_t n)
420 {
421 static DynList<Test_Tree> trees;
422 for (auto it = trees.get_it(); it.has_curr(); it.next_ne())
423 if (it.get_curr().nodes.size() == n)
424 return it.get_curr();
425 return trees.append(Test_Tree(n));
426 }
427
428 // The same tree of Tree_Node, which frees its nodes
429 struct Test_Node_Tree
430 {
432
433 explicit Test_Node_Tree(const size_t n)
434 {
435 for (size_t i = 0; i < n; ++i)
436 nodes.append(new Tnode(static_cast<int>(i % 7)));
437 for (size_t i = 1; i < n; ++i)
438 nodes((i - 1) / 2)->insert_rightmost_child(nodes(i));
439 }
440
441 Test_Node_Tree(Test_Node_Tree &&) = default;
442
443 ~Test_Node_Tree()
444 {
445 if (not nodes.is_empty())
447 }
448 };
449
450 const Test_Node_Tree & node_tree_of(const size_t n)
451 {
452 static DynList<Test_Node_Tree> trees;
453 for (auto it = trees.get_it(); it.has_curr(); it.next_ne())
454 if (it.get_curr().nodes.size() == n)
455 return it.get_curr();
456 return trees.append(Test_Node_Tree(n));
457 }
458
459 // The cycle of n nodes, i -> (i + 1) % n of weight 1 + i % 3
460 struct Test_Digraph
461 {
462 Dgraph g;
464
465 explicit Test_Digraph(const size_t n)
466 {
467 for (size_t i = 0; i < n; ++i)
468 nodes.append(g.insert_node(static_cast<int>(i)));
469 for (size_t i = 0; i < n; ++i)
470 g.insert_arc(nodes(i), nodes((i + 1) % n), static_cast<int>(1 + i % 3));
471 }
472 };
473
474 const Test_Digraph & digraph_of(const size_t n)
475 {
476 static DynList<Test_Digraph> digraphs;
477 for (auto it = digraphs.get_it(); it.has_curr(); it.next_ne())
478 if (it.get_curr().nodes.size() == n)
479 return it.get_curr();
480 return digraphs.append(Test_Digraph(n));
481 }
482
483 // What an ordinary user does with a structure of ranges: read every
484 // value, alone and in ranges
485 template <class R>
486 void use_ranges(R & r)
487 {
488 for (size_t i = 0; i < r.size(); ++i)
489 {
490 EXPECT_EQ(r.query(i, i), r.get(i));
491 (void) r.query(0, i);
492 }
493 EXPECT_EQ(r.values().size(), r.size());
494 }
495
496 // ... and with an ordered tree
497 template <class T>
498 void use_ordered(T & t)
499 {
500 EXPECT_TRUE(t.verify());
501 EXPECT_EQ(t.keys().size(), t.size());
502 EXPECT_TRUE(t.insert(-1));
503 EXPECT_TRUE(t.contains(-1));
504 EXPECT_TRUE(t.remove(-1));
505 EXPECT_TRUE(t.verify());
506 }
507
508 // ... and with a decomposition of a tree in ids, whose nodes it knows
509 template <class D>
510 void use_ids(D & d)
511 {
512 for (size_t id = 0; id < d.size(); ++id)
513 EXPECT_EQ(d.id_of(d.node_of(id)), id);
514 EXPECT_THROW((void) d.node_of(d.size()), std::out_of_range);
515 }
516
517 // ... and with an engine of rectangular lattices: step it and run it
518 template <class Engine>
519 void use_engine(Engine & e)
520 {
521 const size_t steps = e.steps_run();
522 e.step();
523 e.run(2);
524 EXPECT_EQ(e.steps_run(), steps + 3);
525 }
526
527 // The cells and the steps of an engine
528 template <class Engine>
529 std::pair<size_t, size_t> engine_print(const Engine & e)
530 {
531 return {e.frame().size(), e.steps_run()};
532 }
533
534 using Extents = std::array<ca_size_t, 2>;
537
538 // A lattice whose swap may throw, as the swaps of the kind (d), which
539 // LatticeLike allows: the engines exchange it in the same sequence as
540 // their types of the user
541 template <class L>
542 struct Swap_Throws_Lattice : L
543 {
544 using L::L;
545 Swap_Throws_Lattice(const L & l) : L(l) {}
546 Swap_Throws_Lattice(L && l) : L(std::move(l)) {}
547
548 void swap(Swap_Throws_Lattice & o)
549 {
550 risky_swap();
551 L::swap(o);
552 }
553 };
554
555 using Throwing_Grid = Swap_Throws_Lattice<Grid>;
556
557 // About n cells: one row for the smallest objects, ten rows otherwise
558 Extents extents_of(const size_t n)
559 {
560 const ca_size_t rows = n < 10 ? 1 : 10;
561 return {rows, n / rows};
562 }
563
564 // A lattice of about n cells, with a blinker where it fits
565 Grid life_grid(const size_t n)
566 {
567 Grid g(extents_of(n), 0);
568 if (g.size(1) >= 3)
569 for (ca_index_t j = 0; j < 3; ++j)
570 g.set({0, j}, 1);
571 return g;
572 }
573
574 ThreadPool & small_pool()
575 {
576 static ThreadPool pool(2);
577 return pool;
578 }
579
580 // The cases: Case<A> gives the class with its types of the user made
581 // by A, make(n), an object of about n items, and use(x), what a user
582 // does with one; print(x), what a move carries, when the class has no
583 // size(); moves_allocate, when its move constructor allocates
584
585 template <template <class> class A> struct Fenwick_Plus
586 {
587 static constexpr const char * name = "Gen_Fenwick_Tree_Plus";
589 static type make(const size_t n) { return type(values_of(n)); }
590 static void use(type & t) { use_ranges(t); }
591 };
592
593 template <template <class> class A> struct Fenwick_Minus
594 {
595 static constexpr const char * name = "Gen_Fenwick_Tree_Minus";
596 using type = Gen_Fenwick_Tree<int, Aleph::plus<int>, A<Aleph::minus<int>>>;
597 static type make(const size_t n) { return type(values_of(n)); }
598 static void use(type & t) { use_ranges(t); }
599 };
600
601 template <template <class> class A> struct Segment
602 {
603 static constexpr const char * name = "Gen_Segment_Tree";
605 static type make(const size_t n) { return type(values_of(n), 0); }
606 static void use(type & t) { use_ranges(t); }
607 };
608
609 // With tags pending below the root, which a policy swapped without its
610 // arrays would apply as the other one does
611 template <template <class> class A> struct Lazy_Segment
612 {
613 static constexpr const char * name = "Gen_Lazy_Segment_Tree";
615 static type make(const size_t n)
616 {
617 type t(values_of(n));
618 if (n > 2)
619 t.update(1, n - 2, 3);
620 return t;
621 }
622 static void use(type & t) { use_ranges(t); }
623 };
624
625 template <template <class> class A> struct Sparse
626 {
627 static constexpr const char * name = "Gen_Sparse_Table";
629 static type make(const size_t n) { return type(values_of(n)); }
630 static void use(type & t) { use_ranges(t); }
631 };
632
633 template <template <class> class A> struct Disjoint_Sparse
634 {
635 static constexpr const char * name = "Gen_Disjoint_Sparse_Table";
637 static type make(const size_t n) { return type(values_of(n)); }
638 static void use(type & t) { use_ranges(t); }
639 };
640
641 template <template <class> class A> struct Cartesian
642 {
643 static constexpr const char * name = "Gen_Cartesian_Tree";
645 static type make(const size_t n) { return type(values_of(n)); }
646 static void use(type & t)
647 {
648 for (size_t i = 0; i < t.size(); ++i)
649 if (const size_t p = t.parent_of(i); p != type::NONE)
650 {
651 EXPECT_LE(t.data_at(p), t.data_at(i));
652 }
653 EXPECT_EQ(t.inorder().size(), t.size());
654 }
655 };
656
657 template <template <class> class A> struct Euler_Tour
658 {
659 static constexpr const char * name = "Gen_Euler_Tour_LCA";
661 static type make(const size_t n) { return type(values_of(n)); }
662 static void use(type & l)
663 {
664 EXPECT_EQ(l.euler_tour().size(), l.euler_tour_size());
665 for (size_t i = 0; i < l.size(); ++i)
666 EXPECT_LE(l.depth_of(l.lca(0, i)), l.depth_of(i));
667 EXPECT_EQ(l.tree().size(), l.size());
668 }
669 };
670
671 template <template <class> class A> struct Cartesian_RMQ
672 {
673 static constexpr const char * name = "Gen_Cartesian_Tree_RMQ";
675 static type make(const size_t n) { return type(values_of(n)); }
676 static void use(type & q) { use_ranges(q); }
677 };
678
679 template <template <class> class A> struct Heap
680 {
681 static constexpr const char * name = "DynArrayHeap";
683 static type make(const size_t n)
684 {
685 type h;
686 for (const int v : values_of(n))
687 (void) h.insert(v);
688 return h;
689 }
690 static void use(type & h)
691 {
692 size_t count = 0;
693 h.for_each([&count] (const int) { ++count; });
694 EXPECT_EQ(count, h.size());
695 (void) h.insert(-1);
696 EXPECT_EQ(h.getMin(), -1);
697 }
698 };
699
700 // A dynamic array of values of the user, whose swap its own swap() runs
701 template <template <class> class A> struct Dyn_Array
702 {
703 static constexpr const char * name = "DynArray";
704 using V = A<Int>;
705 using type = DynArray<V>;
706 static type make(const size_t n)
707 {
708 type a;
709 for (const int v : values_of(n))
710 (void) a.append(value_from<V>(v));
711 return a;
712 }
713 static void use(type & a)
714 {
715 for (size_t i = 0; i < a.size(); ++i)
716 EXPECT_LE(value_of(a(i)), 100);
717 (void) a.append(value_from<V>(-1));
718 EXPECT_EQ(value_of(a(a.size() - 1)), -1);
719 }
720 };
721
722 // A heap of values of the user, whose swap that of the heap runs
723 template <template <class> class A> struct Heap_Value
724 {
725 static constexpr const char * name = "DynArrayHeap_Value";
726 using V = A<Int>;
727 using type = DynArrayHeap<V, Int_Less>;
728
729 template <class H>
730 static H make_with(const size_t n)
731 {
732 H h;
733 for (const int v : values_of(n))
734 (void) h.insert(value_from<V>(v));
735 return h;
736 }
737
738 template <class H>
739 static void use_with(H & h)
740 {
741 size_t count = 0;
742 h.for_each([&count] (const V &) { ++count; });
743 EXPECT_EQ(count, h.size());
744 (void) h.insert(value_from<V>(-1));
745 EXPECT_EQ(value_of(h.getMin()), -1);
746 }
747
748 static type make(const size_t n) { return make_with<type>(n); }
749 static void use(type & h) { use_with(h); }
750 };
751
752 template <template <class> class A> struct B_Tree_Case
753 {
754 static constexpr const char * name = "Gen_B_Tree";
756 static type make(const size_t n)
757 {
758 type t;
759 for (size_t i = 0; i < n; ++i)
760 (void) t.insert(static_cast<int>(i));
761 return t;
762 }
763 static void use(type & t) { use_ordered(t); }
764 };
765
766 template <template <class> class A> struct BPlus_Tree_Case
767 {
768 static constexpr const char * name = "Gen_BPlus_Tree";
770 static type make(const size_t n)
771 {
772 type t;
773 for (size_t i = 0; i < n; ++i)
774 (void) t.insert(static_cast<int>(i));
775 return t;
776 }
777 static void use(type & t) { use_ordered(t); }
778 };
779
780 template <template <class> class A> struct Hash_Map
781 {
782 static constexpr const char * name = "PersistentHashMap";
784 static type make(const size_t n)
785 {
786 type m;
787 for (size_t i = 0; i < n; ++i)
788 m = m.insert(static_cast<int>(i), static_cast<int>(i));
789 return m;
790 }
791 static void use(type & m)
792 {
793 EXPECT_TRUE(m.verify());
794 const Array<int> keys = m.keys();
795 EXPECT_EQ(keys.size(), m.size());
796 for (size_t i = 0; i < keys.size(); ++i)
797 EXPECT_NE(m.find(keys(i)), nullptr);
798 m = m.insert_or_assign(-1, 7);
799 EXPECT_TRUE(m.contains(-1));
800 EXPECT_TRUE(m.verify());
801 }
802 };
803
804 template <template <class> class A> struct Skip_List
805 {
806 static constexpr const char * name = "DynSkipList";
807 static constexpr bool moves_allocate = true; // a header and a generator for the source
808 static constexpr bool assignment_empties = true; // the destination, before it swaps
810 static type make(const size_t n)
811 {
812 type l(1UL);
813 for (size_t i = 0; i < n; ++i)
814 (void) l.insert(static_cast<int>(i));
815 return l;
816 }
817 static void use(type & l)
818 {
819 size_t count = 0;
820 l.for_each([&count] (const int) { ++count; });
821 EXPECT_EQ(count, l.size());
822 EXPECT_NE(l.insert(-1), nullptr);
823 EXPECT_TRUE(l.has(-1));
824 EXPECT_EQ(l.remove(-1), 1u);
825 }
826 };
827
828 // A randomized tree does not own its nodes: use() frees them, as its user
829 // would
830 template <template <class> class A> struct Rand_Tree_Case
831 {
832 static constexpr const char * name = "Gen_Rand_Tree";
833 static constexpr bool moves_allocate = true; // a generator for the source
835 using Node = typename type::Node;
836 static type make(const size_t n)
837 {
838 type t(1);
839 for (size_t i = 0; i < n; ++i)
840 (void) t.insert(new Node(static_cast<int>(i)));
841 return t;
842 }
843 static void use(type & t)
844 {
845 EXPECT_TRUE(t.verify());
846 Node * p = new Node(-1);
847 EXPECT_EQ(t.insert(p), p);
848 EXPECT_EQ(t.search(-1), p);
849 EXPECT_TRUE(t.verify());
850 destroyRec(t.getRoot());
851 }
852 };
853
854 template <template <class> class A> struct Binary_Lifting
855 {
856 static constexpr const char * name = "Gen_Binary_Lifting_LCA";
858 static type make(const size_t n) { return type(tree_of(n).g); }
859 static void use(type & l)
860 {
861 use_ids(l);
862 for (size_t id = 0; id < l.size(); ++id)
863 EXPECT_EQ(l.lca_id(l.root_id(), id), l.root_id());
864 }
865 };
866
867 template <template <class> class A> struct Euler_RMQ
868 {
869 static constexpr const char * name = "Gen_Euler_RMQ_LCA";
871 static type make(const size_t n) { return type(tree_of(n).g); }
872 static void use(type & l)
873 {
874 use_ids(l);
875 for (size_t id = 0; id < l.size(); ++id)
876 EXPECT_EQ(l.lca_id(l.root_id(), id), l.root_id());
877 EXPECT_EQ(l.euler_tour().size(), l.euler_tour_size());
878 }
879 };
880
881 // What a user does with an HLD of sums
882 template <class H>
883 void use_hld(H & h)
884 {
885 use_ids(h);
886 int total = 0;
887 for (size_t id = 0; id < h.size(); ++id)
888 {
889 EXPECT_EQ(value_of(h.path_query_id(id, id)), value_of(h.get_value_at_id(id)));
890 total += value_of(h.get_value_at_id(id));
891 }
892 EXPECT_TRUE(h.is_empty() or value_of(h.subtree_query_id(h.root_id())) == total);
893 }
894
895 template <template <class> class A> struct HLD_Op
896 {
897 static constexpr const char * name = "Gen_HLD_Op";
899 static type make(const size_t n)
900 {
901 return type(tree_of(n).g, [] (Gnode * p) { return p->get_info(); }, 0);
902 }
903 static void use(type & h) { use_hld(h); }
904 };
905
906 template <template <class> class A> struct HLD_Filter
907 {
908 static constexpr const char * name = "Gen_HLD_Filter";
909 using type = Gen_HLD<Graph, int, Aleph::plus<int>, A<Show_Arc>>;
910 static type make(const size_t n)
911 {
912 return type(tree_of(n).g, [] (Gnode * p) { return p->get_info(); }, 0);
913 }
914 static void use(type & h) { use_hld(h); }
915 };
916
917 template <template <class> class A> struct Heavy_Light
918 {
919 static constexpr const char * name = "Gen_Heavy_Light_Decomposition";
921 static type make(const size_t n) { return type(tree_of(n).g); }
922 static void use(type & h)
923 {
924 use_ids(h);
925 for (size_t id = 0; id < h.size(); ++id)
926 EXPECT_LT(h.position_of_id(id), h.size());
927 }
928 };
929
930 // What a user does with a path query of sums
931 template <class Q>
932 void use_path_query(Q & q)
933 {
934 using V = std::decay_t<decltype(q.get_node_id(0))>;
935 int total = 0;
936 for (size_t id = 0; id < q.size(); ++id)
937 {
938 q.update_node_id(id, value_from<V>(1));
939 EXPECT_EQ(value_of(q.query_path_id(id, id)), value_of(q.get_node_id(id)));
940 total += value_of(q.get_node_id(id));
941 }
942 EXPECT_TRUE(q.is_empty()
943 or value_of(q.query_subtree_id(q.decomposition().root_id())) == total);
944 }
945
946 template <template <class> class A> struct Path_Query_Op
947 {
948 static constexpr const char * name = "Gen_HLD_Path_Query_Op";
950 static type make(const size_t n) { return type(tree_of(n).g, 0); }
951 static void use(type & q) { use_path_query(q); }
952 };
953
954 template <template <class> class A> struct Path_Query_Filter
955 {
956 static constexpr const char * name = "Gen_HLD_Path_Query_Filter";
957 using type = Gen_HLD_Path_Query<Graph, int, Aleph::plus<int>, A<Show_Arc>>;
958 static type make(const size_t n) { return type(tree_of(n).g, 0); }
959 static void use(type & q) { use_path_query(q); }
960 };
961
962 template <template <class> class A> struct Centroid
963 {
964 static constexpr const char * name = "Gen_Centroid_Decomposition";
966 static type make(const size_t n) { return type(tree_of(n).g); }
967 static void use(type & c)
968 {
969 for (size_t id = 0; id < c.size(); ++id)
970 EXPECT_TRUE(c.is_centroid_ancestor_id(c.centroid_root_id(), id));
971 EXPECT_EQ(c.centroid_root() == nullptr, c.is_empty());
972 }
973 };
974
975 // The size of the subtree of each node
976 template <template <class> class A> struct Tree_DP_Case
977 {
978 static constexpr const char * name = "Gen_Tree_DP";
980 static type make(const size_t n)
981 {
982 const Test_Tree & t = tree_of(n);
983 return type(t.g, t.nodes(0), [] (Gnode *) { return 1L; },
984 [] (Gnode *, const long & acc, Gnode *, const long & child) { return acc + child; });
985 }
986 static void use(type & d)
987 {
988 use_ids(d);
989 for (size_t id = 0; id < d.size(); ++id)
990 EXPECT_EQ(d.value(d.node_of(id)), d.values()[id]);
991 EXPECT_EQ(d.values().size(), d.size());
992 }
993 };
994
995 // The eccentricity of each node
996 template <template <class> class A> struct Reroot_DP_Case
997 {
998 static constexpr const char * name = "Gen_Reroot_DP";
1000 static type make(const size_t n)
1001 {
1002 const Test_Tree & t = tree_of(n);
1003 return type(t.g, t.nodes(0), 0L, [] (Gnode *) { return 0L; },
1004 [] (const long & a, const long & b) { return std::max(a, b); },
1005 [] (Gnode *, Gnode *, const long & v) { return v + 1; });
1006 }
1007 static void use(type & r)
1008 {
1009 use_ids(r);
1010 for (size_t id = 0; id < r.size(); ++id)
1011 EXPECT_LT(r.value(r.node_of(id)), static_cast<long>(r.size()));
1012 EXPECT_EQ(r.values().size(), r.size());
1013 }
1014 };
1015
1016 // The engine gives no access to its nodes: they are those of the tree of
1017 // its size
1018 template <template <class> class A> struct Mo_On_Trees
1019 {
1020 static constexpr const char * name = "Gen_Mo_On_Trees";
1022 static type make(const size_t n)
1023 {
1024 const Test_Tree & t = tree_of(n);
1025 return type(t.g, t.nodes(0));
1026 }
1027 static void use(type & m)
1028 {
1029 if (m.is_empty())
1030 return;
1031 const Test_Tree & t = tree_of(m.size());
1032 EXPECT_EQ(m.subtree_solve({t.nodes(0)})(0), std::min<size_t>(m.size(), 7));
1033 }
1034 };
1035
1036 template <template <class> class A> struct Mo_On_Tree_Node
1037 {
1038 static constexpr const char * name = "Gen_Mo_On_Tree_Node";
1040 static type make(const size_t n) { return type(node_tree_of(n).nodes(0)); }
1041 static void use(type & m)
1042 {
1043 if (m.is_empty())
1044 return;
1045 const Test_Node_Tree & t = node_tree_of(m.size());
1046 EXPECT_EQ(m.subtree_solve({t.nodes(0)})(0), std::min<size_t>(m.size(), 7));
1047 }
1048 };
1049
1050 // What a user does with the shortest paths of a cycle
1051 template <class F>
1052 void use_floyd(F & f)
1053 {
1054 const long n = static_cast<long>(f.get_nodes_ref().size());
1055 for (long i = 0; i < n; ++i)
1056 EXPECT_EQ(f.get_min_path(i, (i + 1) % n).get_first_node(), f.select_node(i));
1057 EXPECT_THROW((void) f.get_min_path(n, 0), std::out_of_range);
1058 }
1059
1060 template <template <class> class A> struct Floyd_Distance
1061 {
1062 static constexpr const char * name = "Floyd_All_Shortest_Paths_Distance";
1064 static type make(const size_t n) { return type(digraph_of(std::min<size_t>(n, 10)).g); }
1065 static size_t print(const type & f) { return f.get_nodes_ref().size(); }
1066 static void use(type & f) { use_floyd(f); }
1067 };
1068
1069 template <template <class> class A> struct Floyd_Filter
1070 {
1071 static constexpr const char * name = "Floyd_All_Shortest_Paths_Filter";
1072 using type = Floyd_All_Shortest_Paths<Dgraph, Dft_Dist<Dgraph>, A<Dft_Show_Arc<Dgraph>>>;
1073 static type make(const size_t n) { return type(digraph_of(std::min<size_t>(n, 10)).g); }
1074 static size_t print(const type & f) { return f.get_nodes_ref().size(); }
1075 static void use(type & f) { use_floyd(f); }
1076 };
1077
1078 template <template <class> class A> struct Life
1079 {
1080 static constexpr const char * name = "Synchronous_Engine";
1081 using Rule = A<Game_Of_Life_Rule>;
1083 static type make(const size_t n)
1084 {
1085 type e(life_grid(n), Rule(make_game_of_life_rule()), Moore<2, 1>{});
1086 e.step();
1087 return e;
1088 }
1089 static auto print(const type & e) { return engine_print(e); }
1090 static void use(type & e) { use_engine(e); }
1091 };
1092
1093 template <template <class> class A> struct Async_Life
1094 {
1095 static constexpr const char * name = "Async_Engine_Rule";
1096 using Rule = A<Game_Of_Life_Rule>;
1098 static type make(const size_t n)
1099 {
1100 type e(life_grid(n), Rule(make_game_of_life_rule()), Moore<2, 1>{});
1101 e.step();
1102 return e;
1103 }
1104 static auto print(const type & e) { return engine_print(e); }
1105 static void use(type & e) { use_engine(e); }
1106 };
1107
1108 template <template <class> class A> struct Random_Life
1109 {
1110 static constexpr const char * name = "Async_Engine_Scheme";
1111 using Scheme = A<Random_Asynchronous_Update<>>;
1113 static type make(const size_t n)
1114 {
1115 type e(life_grid(n), make_game_of_life_rule(), Moore<2, 1>{},
1116 Scheme(Random_Asynchronous_Update<>(7, 10)));
1117 e.step();
1118 return e;
1119 }
1120 static auto print(const type & e) { return engine_print(e); }
1121 static void use(type & e) { use_engine(e); }
1122 };
1123
1124 template <template <class> class A> struct Parallel_Life
1125 {
1126 static constexpr const char * name = "Parallel_Synchronous_Engine";
1127 using Rule = A<Game_Of_Life_Rule>;
1129 static type make(const size_t n)
1130 {
1132 cfg.pool = &small_pool();
1133 cfg.num_partitions = 2;
1134 cfg.min_parallel_cells = 0;
1135 type e(life_grid(n), Rule(make_game_of_life_rule()), Moore<2, 1>{}, cfg);
1136 e.step();
1137 return e;
1138 }
1139 static auto print(const type & e) { return engine_print(e); }
1140 static void use(type & e) { use_engine(e); }
1141 };
1142
1143 template <template <class> class A> struct Field_Life
1144 {
1145 static constexpr const char * name = "Multi_Field_Engine";
1146 using Rule = A<Field_Slice_Rule<0, Game_Of_Life_Rule>>;
1148 static type make(const size_t n)
1149 {
1150 Two_Fields lat(extents_of(n), std::tuple{0, 7});
1151 type e(std::move(lat), Rule(Field_Slice_Rule<0, Game_Of_Life_Rule>(make_game_of_life_rule())),
1152 Moore<2, 1>{});
1153 e.step();
1154 return e;
1155 }
1156 static auto print(const type & e) { return engine_print(e); }
1157 static void use(type & e) { use_engine(e); }
1158 };
1159
1160 template <template <class> class A> struct Graph_Life
1161 {
1162 static constexpr const char * name = "Graph_Synchronous_Engine";
1163 using Rule = A<Game_Of_Life_Rule>;
1165 static type make(const size_t n)
1166 {
1168 for (size_t i = 0; i < n; i += 3)
1169 g.set_node(i, 1);
1170 type e(std::move(g), Rule(make_game_of_life_rule()));
1171 e.step();
1172 return e;
1173 }
1174 static auto print(const type & e) { return engine_print(e); }
1175 static void use(type & e)
1176 {
1177 const size_t steps = e.steps_run();
1178 e.step();
1179 EXPECT_EQ(e.steps_run(), steps + 1);
1180 }
1181 };
1182
1183 // The classes with two or more types of the user, with all of them of
1184 // the kind A, so that their swaps can throw at each of them in turn
1185
1186 template <template <class> class A> struct Fenwick_Both
1187 {
1188 static constexpr const char * name = "Gen_Fenwick_Tree_Both";
1189 using type = Gen_Fenwick_Tree<int, A<Aleph::plus<int>>, A<Aleph::minus<int>>>;
1190 static type make(const size_t n) { return type(values_of(n)); }
1191 static void use(type & t) { use_ranges(t); }
1192 };
1193
1194 template <class V>
1195 Array<V> values_of_type(const size_t n)
1196 {
1197 const Array<int> v = values_of(n);
1198 auto a = Array<V>::create(n);
1199 for (size_t i = 0; i < n; ++i)
1200 a(i) = value_from<V>(v(i));
1201 return a;
1202 }
1203
1204 template <template <class> class A> struct Segment_Both
1205 {
1206 static constexpr const char * name = "Gen_Segment_Tree_Both";
1207 using V = A<Int>;
1208 using type = Gen_Segment_Tree<V, A<Int_Plus>>;
1209 static type make(const size_t n) { return type(values_of_type<V>(n), V(Int(0))); }
1210 static void use(type & t) { use_ranges(t); }
1211 };
1212
1213 template <template <class> class A> struct HLD_All
1214 {
1215 static constexpr const char * name = "Gen_HLD_All";
1216 using V = A<Int>;
1217 using type = Gen_HLD<Graph, V, A<Int_Plus>, A<Show_Arc>>;
1218 static type make(const size_t n)
1219 {
1220 return type(tree_of(n).g, [] (Gnode * p) { return V(Int(p->get_info())); }, V(Int(0)));
1221 }
1222 static void use(type & h) { use_hld(h); }
1223 };
1224
1225 template <template <class> class A> struct Path_Query_All
1226 {
1227 static constexpr const char * name = "Gen_HLD_Path_Query_All";
1228 using V = A<Int>;
1229 using type = Gen_HLD_Path_Query<Graph, V, A<Int_Plus>, A<Show_Arc>>;
1230 static type make(const size_t n) { return type(tree_of(n).g, V(Int(0))); }
1231 static void use(type & q) { use_path_query(q); }
1232 };
1233
1234 template <template <class> class A> struct Life_All
1235 {
1236 static constexpr const char * name = "Synchronous_Engine_All";
1237 using Rule = A<Game_Of_Life_Rule>;
1238 using Nh = A<Moore<2, 1>>;
1240 static type make(const size_t n)
1241 {
1242 type e(life_grid(n), Rule(make_game_of_life_rule()), Nh(Moore<2, 1>{}));
1243 e.step();
1244 return e;
1245 }
1246 static auto print(const type & e) { return engine_print(e); }
1247 static void use(type & e) { use_engine(e); }
1248 };
1249
1250 template <template <class> class A> struct Async_All
1251 {
1252 static constexpr const char * name = "Async_Engine_All";
1253 using Rule = A<Game_Of_Life_Rule>;
1254 using Nh = A<Moore<2, 1>>;
1255 using Scheme = A<Random_Asynchronous_Update<>>;
1257 static type make(const size_t n)
1258 {
1259 type e(life_grid(n), Rule(make_game_of_life_rule()), Nh(Moore<2, 1>{}),
1260 Scheme(Random_Asynchronous_Update<>(7, 10)));
1261 e.step();
1262 return e;
1263 }
1264 static auto print(const type & e) { return engine_print(e); }
1265 static void use(type & e) { use_engine(e); }
1266 };
1267
1268 template <template <class> class A> struct Parallel_All
1269 {
1270 static constexpr const char * name = "Parallel_Synchronous_Engine_All";
1271 using Rule = A<Game_Of_Life_Rule>;
1272 using Nh = A<Moore<2, 1>>;
1274 static type make(const size_t n)
1275 {
1277 cfg.pool = &small_pool();
1278 cfg.num_partitions = 2;
1279 cfg.min_parallel_cells = 0;
1280 type e(life_grid(n), Rule(make_game_of_life_rule()), Nh(Moore<2, 1>{}), cfg);
1281 e.step();
1282 return e;
1283 }
1284 static auto print(const type & e) { return engine_print(e); }
1285 static void use(type & e) { use_engine(e); }
1286 };
1287
1288 template <template <class> class A> struct Field_All
1289 {
1290 static constexpr const char * name = "Multi_Field_Engine_All";
1291 using Rule = A<Field_Slice_Rule<0, Game_Of_Life_Rule>>;
1292 using Nh = A<Moore<2, 1>>;
1294 static type make(const size_t n)
1295 {
1296 Two_Fields lat(extents_of(n), std::tuple{0, 7});
1297 type e(std::move(lat), Rule(Field_Slice_Rule<0, Game_Of_Life_Rule>(make_game_of_life_rule())),
1298 Nh(Moore<2, 1>{}));
1299 e.step();
1300 return e;
1301 }
1302 static auto print(const type & e) { return engine_print(e); }
1303 static void use(type & e) { use_engine(e); }
1304 };
1305
1306 // A heap whose comparators and values are both of the kind A
1307
1308 template <template <class> class A> struct Heap_Both
1309 {
1310 static constexpr const char * name = "DynArrayHeap_Both";
1311 using V = A<Int>;
1312 using type = DynArrayHeap<V, A<Int_Less>>;
1313 static type make(const size_t n) { return Heap_Value<A>::template make_with<type>(n); }
1314 static void use(type & h) { Heap_Value<A>::template use_with<type>(h); }
1315 };
1316
1317 // The engines with a lattice whose swap throws as the types of the user
1318
1319 template <template <class> class A> struct Life_Lattice
1320 {
1321 static constexpr const char * name = "Synchronous_Engine_Lattice";
1322 using Rule = A<Game_Of_Life_Rule>;
1323 using Nh = A<Moore<2, 1>>;
1325 static type make(const size_t n)
1326 {
1327 type e(Throwing_Grid(life_grid(n)), Rule(make_game_of_life_rule()), Nh(Moore<2, 1>{}));
1328 e.step();
1329 return e;
1330 }
1331 static auto print(const type & e) { return engine_print(e); }
1332 static void use(type & e) { use_engine(e); }
1333 };
1334
1335 template <template <class> class A> struct Async_Lattice
1336 {
1337 static constexpr const char * name = "Async_Engine_Lattice";
1338 using Rule = A<Game_Of_Life_Rule>;
1339 using Nh = A<Moore<2, 1>>;
1340 using Scheme = A<Random_Asynchronous_Update<>>;
1342 static type make(const size_t n)
1343 {
1344 type e(Throwing_Grid(life_grid(n)), Rule(make_game_of_life_rule()), Nh(Moore<2, 1>{}),
1345 Scheme(Random_Asynchronous_Update<>(7, 10)));
1346 e.step();
1347 return e;
1348 }
1349 static auto print(const type & e) { return engine_print(e); }
1350 static void use(type & e) { use_engine(e); }
1351 };
1352
1353 template <template <class> class A> struct Parallel_Lattice
1354 {
1355 static constexpr const char * name = "Parallel_Synchronous_Engine_Lattice";
1356 using Rule = A<Game_Of_Life_Rule>;
1357 using Nh = A<Moore<2, 1>>;
1359 static type make(const size_t n)
1360 {
1362 cfg.pool = &small_pool();
1363 cfg.num_partitions = 2;
1364 cfg.min_parallel_cells = 0;
1365 type e(Throwing_Grid(life_grid(n)), Rule(make_game_of_life_rule()), Nh(Moore<2, 1>{}), cfg);
1366 e.step();
1367 return e;
1368 }
1369 static auto print(const type & e) { return engine_print(e); }
1370 static void use(type & e) { use_engine(e); }
1371 };
1372
1373 template <template <class> class A> struct Field_Lattice
1374 {
1375 static constexpr const char * name = "Multi_Field_Engine_Lattice";
1376 using Rule = A<Field_Slice_Rule<0, Game_Of_Life_Rule>>;
1377 using Nh = A<Moore<2, 1>>;
1378 using Fields = Swap_Throws_Lattice<Two_Fields>;
1380 static type make(const size_t n)
1381 {
1382 type e(Fields(Two_Fields(extents_of(n), std::tuple{0, 7})),
1384 e.step();
1385 return e;
1386 }
1387 static auto print(const type & e) { return engine_print(e); }
1388 static void use(type & e) { use_engine(e); }
1389 };
1390
1391 template <template <class> class A> struct Graph_Lattice_Case
1392 {
1393 static constexpr const char * name = "Graph_Synchronous_Engine_Lattice";
1394 using Rule = A<Game_Of_Life_Rule>;
1395 using Nodes = Swap_Throws_Lattice<Graph_Lattice<int>>;
1397 static type make(const size_t n)
1398 {
1400 for (size_t i = 0; i < n; i += 3)
1401 g.set_node(i, 1);
1402 type e(Nodes(std::move(g)), Rule(make_game_of_life_rule()));
1403 e.step();
1404 return e;
1405 }
1406 static auto print(const type & e) { return engine_print(e); }
1407 static void use(type & e)
1408 {
1409 const size_t steps = e.steps_run();
1410 e.step();
1411 EXPECT_EQ(e.steps_run(), steps + 1);
1412 }
1413 };
1414
1415 // A class of the test with the types of the user of the kind A
1416 template <template <template <class> class> class Case, template <class> class A>
1417 struct Contract
1418 {
1419 using C = Case<A>;
1420 static constexpr const char * kind = kind_name<A>;
1421 };
1422
1423 // Each class, with the five kinds of types of the user
1424 template <template <template <class> class> class... Cases>
1425 using Contract_Classes = ::testing::Types<Contract<Cases, Swap_Noexcept>...,
1426 Contract<Cases, Assign_Throws>...,
1427 Contract<Cases, Copy_Throws>...,
1428 Contract<Cases, Swap_Throws>...,
1429 Contract<Cases, Move_Ctor_Throws>...>;
1430
1431 using Classes = Contract_Classes<
1432 Fenwick_Plus, Fenwick_Minus, Segment, Lazy_Segment, Sparse, Disjoint_Sparse,
1433 Cartesian, Euler_Tour, Cartesian_RMQ, Heap, Heap_Value, Dyn_Array, B_Tree_Case,
1434 BPlus_Tree_Case, Hash_Map,
1435 Skip_List, Rand_Tree_Case,
1436 Binary_Lifting, Euler_RMQ, HLD_Op, HLD_Filter, Heavy_Light, Path_Query_Op,
1437 Path_Query_Filter, Centroid, Tree_DP_Case, Reroot_DP_Case, Mo_On_Trees,
1438 Mo_On_Tree_Node, Floyd_Distance, Floyd_Filter,
1439 Life, Async_Life, Random_Life, Parallel_Life, Field_Life, Graph_Life>;
1440
1441 struct Contract_Name
1442 {
1443 template <class T>
1444 static std::string GetName(int)
1445 {
1446 return std::string(T::C::name) + "_" + T::kind;
1447 }
1448 };
1449
1450 // What a move must carry to its destination: the print of the case, or
1451 // the size
1452 template <class Case, class T>
1453 auto print_of(const T & x)
1454 {
1455 if constexpr (requires { Case::print(x); })
1456 return Case::print(x);
1457 else
1458 return static_cast<size_t>(x.size());
1459 }
1460
1461 template <class Case>
1462 constexpr bool moves_allocate()
1463 {
1464 if constexpr (requires { Case::moves_allocate; })
1465 return Case::moves_allocate;
1466 else
1467 return false;
1468 }
1469
1470 // Whether the move assignment empties the destination before it swaps:
1471 // then one that throws leaves it empty
1472 template <class Case>
1473 constexpr bool assignment_empties()
1474 {
1475 if constexpr (requires { Case::assignment_empties; })
1476 return Case::assignment_empties;
1477 else
1478 return false;
1479 }
1480
1481 // What a swap or a move assignment that throws must leave of an object:
1482 // what it had, with its own types of the user
1483 template <class Case, class T, class P>
1484 void expect_as_it_was(const T & x, const P & print, const std::string & tags, const char * what,
1485 const char * which)
1486 {
1487 EXPECT_EQ(print_of<Case>(x), print) << what << " threw and left " << which << " changed";
1488 EXPECT_EQ(tags_of(x), tags) << what << " threw and left " << which
1489 << " with types of the user that are not its own";
1490 }
1491
1492 template <class Case, class T>
1493 void use_all(T & a, T & b, std::optional<T> & c)
1494 {
1495 Case::use(a);
1496 Case::use(b);
1497 if (c.has_value())
1498 Case::use(*c);
1499 }
1500
1501 // Checks the contract of an operation: op applies it to a and b, and may
1502 // build c; expect checks what it did, from the prints of a and b before,
1503 // and on_throw what it leaves when it throws, from their prints and their
1504 // tags before. Disarmed, it counts the operations of the types of the
1505 // user that may throw that op runs: if op is noexcept it must run none,
1506 // since armed it would end the program, and if it is not, it must run
1507 // some, unless allows_other says that something else may throw. Armed,
1508 // op throws if and only if it runs one, and then the exception of the
1509 // type of the user, and it leaves the objects usable
1510 template <class Case, class Op, class Expect, class On_Throw>
1511 void check_contract(const char * what, const bool is_noexcept, const bool allows_other,
1512 Op op, Expect expect, On_Throw on_throw)
1513 {
1514 using T = typename Case::type;
1515 size_t ran = 0;
1516 {
1517 T a = Case::make(20);
1518 T b = Case::make(5);
1519 const auto print_a = print_of<Case>(a);
1520 const auto print_b = print_of<Case>(b);
1521 std::optional<T> c;
1522 const size_t before = risky_calls;
1523 op(a, b, c);
1524 ran = risky_calls - before;
1525 expect(a, b, c, print_a, print_b);
1526 use_all<Case>(a, b, c);
1527 }
1528 if (is_noexcept)
1529 {
1530 EXPECT_EQ(ran, 0u) << what << " is noexcept, but runs " << ran
1531 << " operations of the types of the user that may throw";
1532 if (ran > 0)
1533 return; // armed, it would end the program
1534 }
1535 else if (not allows_other)
1536 {
1537 EXPECT_GT(ran, 0u) << what << " may throw, but runs no operation of the types of the "
1538 << "user that may throw";
1539 }
1540
1541 T a = Case::make(20);
1542 T b = Case::make(5);
1543 const auto print_a = print_of<Case>(a);
1544 const auto print_b = print_of<Case>(b);
1545 const std::string tags_a = tags_of(a);
1546 const std::string tags_b = tags_of(b);
1547 std::optional<T> c;
1548 bool threw = false;
1549 arm_swaps(1);
1550 try
1551 {
1552 op(a, b, c);
1553 }
1554 catch (const std::exception & e)
1555 {
1556 threw = true;
1557 EXPECT_TRUE(is_adversarial(e)) << what << " threw " << e.what();
1558 }
1559 armed = false;
1560 EXPECT_EQ(threw, ran > 0) << what;
1561 if (not threw)
1562 expect(a, b, c, print_a, print_b);
1563 else
1564 on_throw(a, b, print_a, print_b, tags_a, tags_b);
1565 use_all<Case>(a, b, c);
1566 }
1567} // namespace
1568
1569template <class T> class Noexcept_Contract : public ::testing::Test {};
1570
1571TYPED_TEST_SUITE(Noexcept_Contract, Classes, Contract_Name);
1572
1574{
1575 using Case = typename TypeParam::C;
1576 using T = typename Case::type;
1577 // A move constructor that throws may leave its source empty
1578 check_contract<Case>("the move constructor", std::is_nothrow_move_constructible_v<T>,
1579 moves_allocate<Case>(),
1580 [] (T & a, T &, std::optional<T> & c) { c.emplace(std::move(a)); },
1581 [] (T &, T &, std::optional<T> & c, const auto & pa, const auto &)
1582 {
1583 ASSERT_TRUE(c.has_value());
1584 EXPECT_EQ(print_of<Case>(*c), pa);
1585 },
1586 [] (T &, T &, const auto &, const auto &, const auto &, const auto &) {});
1587}
1588
1590{
1591 using Case = typename TypeParam::C;
1592 using T = typename Case::type;
1593 if constexpr (std::is_move_assignable_v<T>)
1594 check_contract<Case>("the move assignment", std::is_nothrow_move_assignable_v<T>, false,
1595 [] (T & a, T & b, std::optional<T> &) { b = std::move(a); },
1596 [] (T &, T & b, std::optional<T> &, const auto & pa, const auto &)
1597 {
1598 EXPECT_EQ(print_of<Case>(b), pa);
1599 },
1600 [] (T & a, T & b, const auto & pa, const auto & pb, const auto & ta,
1601 const auto & tb)
1602 {
1603 expect_as_it_was<Case>(a, pa, ta, "the move assignment", "the source");
1604 if constexpr (assignment_empties<Case>())
1605 {
1606 EXPECT_EQ(b.size(), 0u) << "the destination is not empty";
1607 EXPECT_EQ(tags_of(b), tb);
1608 }
1609 else
1610 expect_as_it_was<Case>(b, pb, tb, "the move assignment",
1611 "the destination");
1612 });
1613 else
1614 GTEST_SKIP() << "the class has no move assignment";
1615}
1616
1618{
1619 using Case = typename TypeParam::C;
1620 using T = typename Case::type;
1621 if constexpr (requires (T & x) { x.swap(x); })
1622 check_contract<Case>("swap()", noexcept(std::declval<T &>().swap(std::declval<T &>())), false,
1623 [] (T & a, T & b, std::optional<T> &) { a.swap(b); },
1624 [] (T & a, T & b, std::optional<T> &, const auto & pa, const auto & pb)
1625 {
1626 EXPECT_EQ(print_of<Case>(a), pb);
1627 EXPECT_EQ(print_of<Case>(b), pa);
1628 },
1629 [] (T & a, T & b, const auto & pa, const auto & pb, const auto & ta,
1630 const auto & tb)
1631 {
1632 expect_as_it_was<Case>(a, pa, ta, "swap()", "this object");
1633 expect_as_it_was<Case>(b, pb, tb, "swap()", "the other");
1634 });
1635 else
1636 GTEST_SKIP() << "the class has no swap()";
1637}
1638
1639namespace
1640{
1641 // The classes with two or more types of the user, all of the kind (d)
1642 using Rollback_Classes = ::testing::Types<
1643 Contract<Fenwick_Both, Swap_Throws>, Contract<Segment_Both, Swap_Throws>,
1644 Contract<HLD_All, Swap_Throws>, Contract<Path_Query_All, Swap_Throws>,
1645 Contract<Life_All, Swap_Throws>, Contract<Async_All, Swap_Throws>,
1646 Contract<Parallel_All, Swap_Throws>, Contract<Field_All, Swap_Throws>,
1647 Contract<Heap_Both, Swap_Throws>, Contract<Life_Lattice, Swap_Throws>,
1648 Contract<Async_Lattice, Swap_Throws>, Contract<Parallel_Lattice, Swap_Throws>,
1649 Contract<Field_Lattice, Swap_Throws>, Contract<Graph_Lattice_Case, Swap_Throws>>;
1650
1651 // op, a swap or a move assignment, throws at each swap of a type of the
1652 // user that it runs, in turn: each time it must leave both objects as
1653 // they were, with their types of the user, and usable
1654 template <class Case, class Op>
1655 void check_rollback(const char * what, Op op)
1656 {
1657 using T = typename Case::type;
1658 size_t swaps = 0;
1659 {
1660 T a = Case::make(20);
1661 T b = Case::make(5);
1662 const size_t before = risky_calls;
1663 op(a, b);
1664 swaps = risky_calls - before;
1665 }
1666 EXPECT_GE(swaps, 2u) << what << " exchanges fewer than two types of the user";
1667 for (size_t k = 1; k <= swaps; ++k)
1668 {
1669 T a = Case::make(20);
1670 T b = Case::make(5);
1671 const auto print_a = print_of<Case>(a);
1672 const auto print_b = print_of<Case>(b);
1673 const std::string tags_a = tags_of(a);
1674 const std::string tags_b = tags_of(b);
1675 bool threw = false;
1676 arm_swaps(static_cast<long>(k));
1677 try
1678 {
1679 op(a, b);
1680 }
1681 catch (const std::exception & e)
1682 {
1683 threw = true;
1684 EXPECT_TRUE(is_adversarial(e)) << what << " threw " << e.what();
1685 }
1686 armed = false;
1687 EXPECT_TRUE(threw) << what << " did not throw at its swap " << k;
1688 SCOPED_TRACE(std::string(what) + " threw at its swap " + std::to_string(k));
1689 expect_as_it_was<Case>(a, print_a, tags_a, what, "an object");
1690 expect_as_it_was<Case>(b, print_b, tags_b, what, "the other");
1691 Case::use(a);
1692 Case::use(b);
1693 }
1694 }
1695
1696 // op throws at each swap i of a type of the user and again at a swap j
1697 // that puts back one before it: then the objects may be neither as they
1698 // were nor exchanged (the classes say so), but they can be destroyed as
1699 // they are, or assigned and then used as any other
1700 template <class Case, class Op>
1701 void check_double_failure(const char * what, Op op)
1702 {
1703 using T = typename Case::type;
1704 size_t swaps = 0;
1705 {
1706 T a = Case::make(20);
1707 T b = Case::make(5);
1708 const size_t before = risky_calls;
1709 op(a, b);
1710 swaps = risky_calls - before;
1711 }
1712 size_t twice = 0;
1713 for (long i = 2; i <= static_cast<long>(swaps); ++i)
1714 for (long j = i + 1; j < 2 * i; ++j) // the i - 1 swaps that put back those before i
1715 for (const bool assign : {false, true})
1716 {
1717 SCOPED_TRACE(std::string(what) + " threw at its swaps " + std::to_string(i) + " and " +
1718 std::to_string(j));
1719 T a = Case::make(20);
1720 T b = Case::make(5);
1721 arm_swaps(i, j);
1722 try
1723 {
1724 op(a, b);
1725 ADD_FAILURE() << what << " did not throw";
1726 }
1727 catch (const std::exception & e)
1728 {
1729 EXPECT_TRUE(is_adversarial(e)) << what << " threw " << e.what();
1730 }
1731 armed = false;
1732 if (swaps_armed >= j)
1733 ++twice;
1734 if (assign)
1735 {
1736 a = Case::make(20);
1737 b = Case::make(5);
1738 Case::use(a);
1739 Case::use(b);
1740 }
1741 }
1742 EXPECT_GT(twice, 0u) << what << " never threw while it put back what it exchanged";
1743 }
1744} // namespace
1745
1746template <class T> class Noexcept_Contract_Rollback : public ::testing::Test {};
1747
1748TYPED_TEST_SUITE(Noexcept_Contract_Rollback, Rollback_Classes, Contract_Name);
1749
1751{
1752 using Case = typename TypeParam::C;
1753 using T = typename Case::type;
1754 check_rollback<Case>("swap()", [] (T & a, T & b) { a.swap(b); });
1755}
1756
1758{
1759 using Case = typename TypeParam::C;
1760 using T = typename Case::type;
1761 check_rollback<Case>("the move assignment", [] (T & a, T & b) { b = std::move(a); });
1762}
1763
1765{
1766 using Case = typename TypeParam::C;
1767 using T = typename Case::type;
1768 check_double_failure<Case>("swap()", [] (T & a, T & b) { a.swap(b); });
1769}
1770
1771TYPED_TEST(Noexcept_Contract_Rollback, MoveAssignmentThatThrowsTwice)
1772{
1773 using Case = typename TypeParam::C;
1774 using T = typename Case::type;
1775 check_double_failure<Case>("the move assignment", [] (T & a, T & b) { b = std::move(a); });
1776}
1777
1778namespace
1779{
1780 // A rule that owns a resource, an array that its move leaves to the
1781 // other rule. Its move assignment throws while armed, and it has no swap
1782 // of its own: std::swap() leaves it moved from when it throws, without
1783 // its resource. Its steps run with it or without it
1784 std::atomic<size_t> steps_without_resource{0};
1785
1786 template <class Base>
1787 struct Resource_Rule : Base
1788 {
1789 Array<int> resource = Array<int>::create(1);
1790
1791 explicit Resource_Rule(const Base & b) : Base(b) {}
1792 Resource_Rule(const Resource_Rule &) = default;
1793 Resource_Rule(Resource_Rule &&) noexcept = default;
1794 Resource_Rule & operator = (const Resource_Rule &) = default;
1795
1796 Resource_Rule & operator = (Resource_Rule && o)
1797 {
1798 risky_operation();
1799 Base::operator = (std::move(o));
1800 resource = std::move(o.resource);
1801 return *this;
1802 }
1803
1804 template <class... Args>
1805 auto operator () (const Args &... args) const
1806 -> decltype(std::declval<const Base &>()(args...))
1807 {
1808 if (resource.is_empty())
1809 ++steps_without_resource;
1810 return Base::operator () (args...);
1811 }
1812 };
1813
1814 // A swap of engines that throws in that of their rules leaves each
1815 // engine with its own lattices, and both step
1816 template <class Case>
1817 void check_rule_with_a_resource()
1818 {
1819 using T = typename Case::type;
1820 T a = Case::make(20);
1821 T b = Case::make(5);
1822 const auto print_a = print_of<Case>(a);
1823 const auto print_b = print_of<Case>(b);
1824 bool threw = false;
1825 armed = true;
1826 try
1827 {
1828 a.swap(b);
1829 }
1830 catch (const std::exception & e)
1831 {
1832 threw = true;
1833 EXPECT_TRUE(is_adversarial(e)) << Case::name << " threw " << e.what();
1834 }
1835 armed = false;
1836 EXPECT_TRUE(threw) << Case::name;
1837 EXPECT_EQ(print_of<Case>(a), print_a) << Case::name << " exchanged its lattices";
1838 EXPECT_EQ(print_of<Case>(b), print_b) << Case::name << " exchanged its lattices";
1839 Case::use(a);
1840 Case::use(b);
1841 }
1842} // namespace
1843
1844// The rule of an engine that owns a resource, and whose move assignment
1845// throws inside std::swap(): the lattices are not exchanged without the
1846// rules, and both engines go on stepping
1847TEST(Noexcept_Contract_Engines, RuleThatOwnsAResource)
1848{
1849 check_rule_with_a_resource<Life<Resource_Rule>>();
1850 check_rule_with_a_resource<Async_Life<Resource_Rule>>();
1851 check_rule_with_a_resource<Parallel_Life<Resource_Rule>>();
1852 check_rule_with_a_resource<Field_Life<Resource_Rule>>();
1853 check_rule_with_a_resource<Graph_Life<Resource_Rule>>();
1854}
1855
1856// The move constructor of DynSkipList builds its comparison as a copy of
1857// that of the source: with a comparison whose move assignment throws, it
1858// runs none of the operations of the comparison that may throw, and ends
1859// without throwing
1860TEST(Noexcept_Contract_Skip_List, MoveConstructorWhenTheComparisonAssignmentThrows)
1861{
1862 using Case = Skip_List<Assign_Throws>;
1863 using L = typename Case::type;
1864 L src = Case::make(20);
1865 std::optional<L> dst;
1866 const size_t before = risky_calls;
1867 armed = true;
1868 EXPECT_NO_THROW(dst.emplace(std::move(src)));
1869 armed = false;
1870 EXPECT_EQ(risky_calls - before, 0u) << "the move constructor ran an operation of the "
1871 << "comparison that may throw";
1872 ASSERT_TRUE(dst.has_value());
1873 EXPECT_EQ(dst->size(), 20u);
1874 Case::use(*dst);
1875 Case::use(src);
1876}
1877
1878namespace
1879{
1880 // A distance whose copy throws while armed, for the infinity that the
1881 // move of Floyd_All_Shortest_Paths copies
1882 struct Number
1883 {
1884 long v = 0;
1885
1886 Number() = default;
1887 Number(const long x) noexcept : v(x) {}
1888 Number(const Number & o) : v(o.v) { risky_operation(); }
1889 Number(Number && o) noexcept : v(o.v) {}
1890 Number & operator = (const Number & o) = default;
1891 Number & operator = (Number && o) noexcept = default;
1892
1893 friend Number operator + (const Number & a, const Number & b) noexcept { return a.v + b.v; }
1894 friend Number operator - (const Number & a, const Number & b) noexcept { return a.v - b.v; }
1895 friend bool operator == (const Number & a, const Number & b) noexcept { return a.v == b.v; }
1896 friend auto operator <=> (const Number & a, const Number & b) noexcept { return a.v <=> b.v; }
1897 };
1898
1899 struct Number_Distance
1900 {
1901 using Distance_Type = Number;
1902 Number operator () (Dgraph::Arc * a) const noexcept { return a->get_info(); }
1903 };
1904} // namespace
1905
1906template <> struct std::numeric_limits<Number> : std::numeric_limits<long>
1907{
1908 static Number max() noexcept { return std::numeric_limits<long>::max(); }
1909 static Number lowest() noexcept { return std::numeric_limits<long>::lowest(); }
1910 static constexpr bool is_integer = false;
1911};
1912
1913// The move of the solver copies its infinity, which is const: with a
1914// distance type whose copy throws it is not noexcept, and a copy that
1915// throws leaves the source without nodes, usable
1916TEST(Noexcept_Contract_Floyd, InfinityWhoseCopyThrows)
1917{
1919 F full(digraph_of(5).g);
1920 const size_t before = risky_calls;
1921 F moved(std::move(full));
1922 EXPECT_GT(risky_calls - before, 0u);
1923 // The solver orders its nodes by their addresses: ask by the nodes
1924 const auto & t = digraph_of(5);
1925 EXPECT_EQ(moved.get_min_path(t.nodes(0), t.nodes(1)).size(), 2u);
1926 ASSERT_FALSE(std::is_nothrow_move_constructible_v<F>)
1927 << "the move copies the infinity, whose copy may throw"; // armed, it would end the program
1928
1929 F src(digraph_of(5).g);
1930 bool threw = false;
1931 armed = true;
1932 try
1933 {
1934 F dst(std::move(src));
1935 }
1936 catch (const std::exception & e)
1937 {
1938 threw = true;
1939 EXPECT_TRUE(is_adversarial(e)) << e.what();
1940 }
1941 armed = false;
1942 EXPECT_TRUE(threw);
1943 EXPECT_EQ(src.get_nodes_ref().size(), 0u);
1944 EXPECT_THROW((void) src.get_min_path(0L, 0L), std::out_of_range);
1945}
1946
1947namespace
1948{
1949 // The moves of a set over a tree whose swap runs that of its comparison
1950 // (Rand_Tree and Splay_Tree_Rk), with a comparison whose own swap
1951 // throws. The move assignment swaps the trees: it throws, instead of
1952 // ending the program, and leaves the source as it was. The move
1953 // constructor swaps nothing: the tree that takes the nodes copies the
1954 // comparison, which cannot throw here, so that it is noexcept and takes
1955 // the elements though every swap would throw. With Aleph::less both
1956 // stay noexcept
1957 template <template <typename, class> class Tree>
1958 void check_set_moves(const char * tree)
1959 {
1961 EXPECT_TRUE((std::is_nothrow_move_constructible_v<DynSetTree<int, Tree>>)) << tree;
1962 EXPECT_TRUE((std::is_nothrow_move_assignable_v<DynSetTree<int, Tree>>)) << tree;
1963 EXPECT_TRUE(std::is_nothrow_move_constructible_v<Set>) << tree;
1964 // armed, a noexcept move assignment would end the program
1965 ASSERT_FALSE(std::is_nothrow_move_assignable_v<Set>) << tree;
1966
1967 Set a;
1968 for (int i = 0; i < 20; ++i)
1969 (void) a.insert(i);
1970 arm_swaps(1);
1971 {
1972 Set b(std::move(a));
1973 EXPECT_EQ(swaps_armed.load(), 0L) << tree << ": the move constructor swapped a comparison";
1974 EXPECT_EQ(b.size(), 20u) << tree;
1975 EXPECT_TRUE(b.verify()) << tree;
1976 EXPECT_EQ(a.size(), 0u) << tree;
1977 armed = false;
1978 a.swap(b); // the elements go back, for the assignment below
1979 }
1980 armed = false;
1981 EXPECT_EQ(a.size(), 20u) << tree;
1982 EXPECT_TRUE(a.verify()) << tree;
1983
1984 Set c;
1985 (void) c.insert(-1);
1986 arm_swaps(1);
1987 EXPECT_THROW(c = std::move(a), std::runtime_error) << tree;
1988 armed = false;
1989 EXPECT_EQ(a.size(), 20u) << tree;
1990 EXPECT_EQ(c.size(), 0u) << tree << ": the assignment frees the elements of its destination first";
1991 (void) c.insert(5);
1992 EXPECT_TRUE(c.contains(5)) << tree;
1993 }
1994} // namespace
1995
1996TEST(Noexcept_Contract_Dyn_Set, MovesOverARandTreeWhoseComparisonSwapThrows)
1997{
1998 check_set_moves<Rand_Tree>("Rand_Tree");
1999}
2000
2001TEST(Noexcept_Contract_Dyn_Set, MovesOverARankedSplayTreeWhoseComparisonSwapThrows)
2002{
2003 check_set_moves<Splay_Tree_Rk>("Splay_Tree_Rk");
2004}
Floyd-Warshall algorithm for all-pairs shortest paths.
Heavy-Light Decomposition for tree path queries on Aleph graphs.
Lowest Common Ancestor (LCA) on rooted trees represented as Aleph graphs.
Generic tree dynamic programming and rerooting algorithms.
Heavy-Light and Centroid decomposition on rooted trees represented as Aleph graphs.
Exception handling system with formatted messages for Aleph-w.
#define ah_runtime_error_if(C)
Throws std::runtime_error if condition holds.
Definition ah-errors.H:415
WeightedDigraph::Node Node
long double h
Definition btreepic.C:154
size_t steps
Definition ca-c-api.h:126
size_t * rows
Definition ca-c-api.h:112
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:141
constexpr bool is_empty() const noexcept
Checks if the container is empty.
Definition tpl_array.H:444
static Array create(const size_t n)
Create an array with n logical elements.
Definition tpl_array.H:245
Update-scheme aware engine.
void step()
Run one engine step using the configured update scheme.
Adaptor: apply a mono-field rule to field I, leave the other fields unchanged.
Graph lattice: one cell per node + precomputed adjacency.
Synchronous double-buffered engine for graph CAs.
Lattice that adds boundary-aware access on top of a storage.
Moore (Chebyshev) neighborhood of radius R in N dimensions.
Synchronous double-buffered engine for multi-field lattices.
void step()
Apply the multi-field rule to every cell once and swap buffers.
Multi-field lattice with selectable storage layout.
Parallel synchronous double-buffered engine.
Synchronous double-buffered engine.
void step()
Apply the rule to every cell once and swap buffers.
Generic directed graph (digraph) wrapper template.
Definition graph-dry.H:4377
Dynamic heap (priority queue) backed by DynArray.
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1231
T & append(const T &item)
Definition htlist.H:1362
T & insert(const T &item)
Definition htlist.H:1308
T remove()
Remove the first item of the list.
Definition htlist.H:1447
size_t size() const noexcept
Return the number of items of the list, in constant time.
Definition htlist.H:1264
Generic key-value map implemented on top of a binary search tree.
void remove_key(const Key &key)
Deletes the pair whose key is key.
Pair * search(const Key &key) noexcept(nothrow_search)
Collect all keys.
Pair * insert(const Key &key, const Data &data)
Insert a key-value pair.
Dynamic set backed by balanced binary search trees with automatic memory management.
Key * insert(const Key &key)
Inserts a key into the dynamic set.
Dynamic ordered set implemented with a Skip List.
Compute the all-pairs shortest path distance matrix and the path reconstruction matrix for a graph g ...
Generic B+ Tree with configurable minimum degree.
bool insert(const Key &key)
Insert a key if it is not already present.
Generic B-Tree with configurable minimum degree.
Definition tpl_b_tree.H:135
bool insert(const Key &key)
Insert a key if it is not already present.
Definition tpl_b_tree.H:756
LCA via binary lifting on a rooted tree.
Definition LCA.H:539
O(1) static range queries via the Cartesian Tree reduction.
Explicit Cartesian Tree built in O(n) with a monotonic stack.
static constexpr size_t NONE
Sentinel value for absent parent/child links.
Centroid decomposition over a rooted tree represented as an Aleph graph.
Disjoint Sparse Table over an arbitrary associative binary operation.
LCA via Euler tour + RMQ on depth in a rooted tree.
Definition LCA.H:857
Lowest Common Ancestor queries in O(1) via Euler Tour reduction to RMQ on a Cartesian Tree.
Fenwick tree over an arbitrary abelian group.
Segment-tree-powered path/subtree queries over an HLD layout.
Heavy-Light Decomposition with segment tree for path/subtree queries.
Definition HLD.H:624
Heavy-Light Decomposition over a rooted tree represented as an Aleph graph.
Lazy segment tree with range update and range query.
Offline subtree and path queries on N-ary trees (Tree_Node).
Offline subtree and path queries on trees via Mo's algorithm.
NodeType< Key > Node
Generic rerooting dynamic programming (all-roots DP).
Definition Tree_DP.H:544
Segment tree over an arbitrary associative binary operation.
Sparse Table over an arbitrary associative and idempotent binary operation.
Generic bottom-up tree dynamic programming.
Definition Tree_DP.H:406
unsigned long get() noexcept
The next number, as operator()().
Definition ah-random.H:289
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Definition tpl_graph.H:535
Node Node
The graph type.
Definition tpl_graph.H:443
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Definition tpl_graph.H:618
constexpr bool contains(const Key &key) const noexcept
Alias for has().
Definition hashDry.H:769
Immutable unordered map backed by a Hash Array Mapped Trie (HAMT).
Represents a line segment between two points.
Definition point.H:837
A reusable thread pool for efficient parallel task execution.
Forward declaration used by CRTP helpers before the full node definition.
void for_each(Operation &operation) const
Traverse all the container and performs an operation on each element.
Definition ah-dry.H:1475
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
Definition ah-dry.H:663
constexpr size_t size() const noexcept
Returns the number of entries in the table.
Definition hashDry.H:954
constexpr bool is_empty() const noexcept
Checks if the table is empty.
Definition hashDry.H:959
DynList< Key > keys() const
Returns a list containing all keys in the table.
Definition hashDry.H:1247
Key * insert(const Key &key)
Inserts a key into the hash table (copy version).
Definition hashDry.H:507
Key & find(const Key &key)
Finds a key and returns a reference to it.
Definition hashDry.H:782
QuadTree - Hierarchical spatial index for 2D points.
Definition quadtree.H:126
#define TEST(name)
pair< size_t, string > P
DynArray< Graph::Node * > nodes
Definition graphpic.C:406
void destroy_tree(Node *root)
Destroys (frees memory) the tree whose root is root.
void destroyRec(Node *&root) noexcept
Free all the memory occupied by the tree root
@ R
Recovered (and immune).
constexpr Game_Of_Life_Rule make_game_of_life_rule() noexcept
Build the canonical Game of Life rule.
std::ptrdiff_t ca_index_t
Signed coordinate component used by lattices and neighborhoods.
Definition ca-traits.H:60
Array< Array< std::size_t > > make_path_graph_adjacency(std::size_t n, bool cycle=false)
Build the adjacency of a path graph with n nodes.
std::size_t ca_size_t
Unsigned size component used for extents and counts.
Definition ca-traits.H:63
Main namespace for Aleph-w library functions.
Gen_MultiPolynomial< C, M > operator+(const C &s, const Gen_MultiPolynomial< C, M > &p)
Scalar + polynomial (commutative).
bool operator==(const DynList< T > &l1, const DynList< T > &l2)
Equality operator for DynList.
T & swap(T &t1, T &t2)
Generic swap using object's swap method.
Definition ahTypes.H:123
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:152
void expect(std::ifstream &input_stream, const std::string &expected)
Expect and consume a specific string/keyword.
Gen_MultiPolynomial< C, M > operator-(const C &s, const Gen_MultiPolynomial< C, M > &p)
Scalar - polynomial.
and
Check uniqueness with explicit hash + equality functors.
std::pair< First, Second > pair
Alias to std::pair kept for backwards compatibility.
Definition ahPair.H:89
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Definition ahAlgo.H:127
void print(const DynList< Parc< Net > > &sp)
Print a semi-path to stdout.
Definition tpl_net.H:1202
STL namespace.
TYPED_TEST_SUITE(Noexcept_Contract, Classes, Contract_Name)
TYPED_TEST(Noexcept_Contract, MoveConstructor)
Configuration for Parallel_Synchronous_Engine.
std::size_t num_partitions
Number of partitions per step.
ThreadPool * pool
Thread pool to schedule on. nullptr means Aleph::default_pool().
std::size_t min_parallel_cells
Workloads with fewer cells than this are executed sequentially without touching the pool.
Pick one cell at random per sub-step, update it in place.
The lattice wraps around on every axis.
Definition ca-traits.H:124
Default filter for filtered iterators on arcs.
Definition tpl_graph.H:1024
Arc of graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:231
Randomized binary search tree.
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
size_t V
int keys[]
void assign(BinTree< int >::Node *node, int, int)
static int * k
Gsl_Compatible_Rng r
A modern, efficient thread pool for parallel task execution.
B-Tree implementation with configurable minimum degree.
B+ Tree implementation with linked leaves and configurable degree.
Phase 13 update-scheme aware engine.
Synchronous double-buffered engine for cellular automata.
CA whose underlying topology is an arbitrary undirected graph.
Cellular automata lattice with pluggable boundary policies.
Phase 14 synchronous double-buffered engine for multi-field cellular automata.
Phase 14 multi-field cellular automata lattice (AoS / SoA).
Phase 14 multi-field local rules for Aleph::CA.
Neighborhoods catalogue for Aleph::CA.
Parallel synchronous engine for cellular automata (Phase 5).
Rule mechanisms for Aleph::CA.
Dense, contiguous storage for cellular automata cells (1D/2D/3D).
Cartesian Tree, LCA via Euler Tour, and RMQ via Cartesian Tree.
Disjoint Sparse Table for static range queries in O(1).
Array-based dynamic binary heap.
Dynamic key-value map based on balanced binary search trees.
Dynamic ordered set implemented with a Skip List.
Fenwick tree (Binary Indexed Tree) for prefix queries.
Generic graph and digraph implementations.
Mo's algorithm on trees: offline subtree and path queries.
Immutable path-copying hash map backed by a HAMT.
Randomized binary search tree.
Segment trees for dynamic range queries with lazy propagation.
Sparse Table for static range queries in O(1).
General tree (n-ary tree) node.
DynList< int > l