81#include <gtest/gtest.h>
117using namespace Aleph;
125 std::atomic<bool> armed{
false};
126 std::atomic<size_t> risky_calls{0};
128 constexpr const char * adversarial_what =
"an adversarial type of the user threw";
130 void risky_operation()
136 bool is_adversarial(
const std::exception & e)
138 return std::string(e.what()).find(adversarial_what) != std::string::npos;
143 template <
class Base>
144 struct Swap_Noexcept : Base
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(); }
151 Swap_Noexcept & operator = (
const Swap_Noexcept & o)
noexcept
153 Base::operator = (o);
157 Swap_Noexcept & operator = (Swap_Noexcept && o)
160 Base::operator = (std::move(o));
164 friend void swap(Swap_Noexcept & a, Swap_Noexcept & b)
noexcept
166 std::swap(
static_cast<Base &
>(a),
static_cast<Base &
>(b));
171 template <
class Base>
172 struct Assign_Throws : Base
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)) {}
179 Assign_Throws & operator = (
const Assign_Throws & o)
noexcept
181 Base::operator = (o);
185 Assign_Throws & operator = (Assign_Throws && o)
188 Base::operator = (std::move(o));
194 template <
class Base>
195 struct Copy_Throws : Base
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)) {}
202 Copy_Throws & operator = (
const Copy_Throws & o)
205 Base::operator = (o);
209 Copy_Throws & operator = (Copy_Throws && o)
noexcept
211 Base::operator = (std::move(o));
219 std::atomic<long> throw_at{1};
220 std::atomic<long> throw_also{0};
221 std::atomic<long> swaps_armed{0};
226 if (not armed.load())
228 const long k = ++swaps_armed;
234 void arm_swaps(
const long first,
const long second = 0)
243 std::mutex tags_mutex;
245 std::atomic<long> last_tag{0};
247 void set_tag(
const void * p,
const long tag)
249 const std::lock_guard<std::mutex> lock(tags_mutex);
250 const auto key =
reinterpret_cast<std::uintptr_t
>(p);
254 (
void) live_tags.
insert(key, tag);
257 void drop_tag(
const void * p)
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)
268 std::string tags_of(
const T & x)
270 const std::lock_guard<std::mutex> lock(tags_mutex);
271 const auto lo =
reinterpret_cast<std::uintptr_t
>(&x);
273 live_tags.
for_each([&tags, lo] (
const auto & p)
275 if (p.first >= lo
and p.first < lo +
sizeof(
T))
276 tags += std::to_string(p.second) +
" ";
287 template <
class Base>
288 struct Swap_Throws : Base
290 long tag = ++last_tag;
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); }
298 Swap_Throws & operator = (
const Swap_Throws & o)
noexcept
300 Base::operator = (o);
301 set_tag(
this, tag = o.tag);
305 Swap_Throws & operator = (Swap_Throws && o)
noexcept
307 Base::operator = (std::move(o));
308 set_tag(
this, tag = o.tag);
312 friend void swap(Swap_Throws & a, Swap_Throws & b)
315 std::swap(
static_cast<Base &
>(a),
static_cast<Base &
>(b));
316 std::swap(a.tag, b.tag);
325 template <
class Base>
326 struct Move_Ctor_Throws : Base
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))) {}
333 Move_Ctor_Throws & operator = (
const Move_Ctor_Throws & o)
noexcept
335 Base::operator = (o);
339 Move_Ctor_Throws & operator = (Move_Ctor_Throws && o)
noexcept
341 Base::operator = (std::move(o));
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";
360 Int(
const int x) noexcept : v(x) {}
362 friend bool operator == (
const Int & a,
const Int & b)
noexcept {
return a.v == b.v; }
367 Int operator () (
const Int & a,
const Int & b)
const noexcept {
return a.v + b.v; }
372 bool operator () (
const Int & a,
const Int & b)
const noexcept {
return a.v < b.v; }
375 int value_of(
const int x)
noexcept {
return x; }
376 int value_of(
const Int & x)
noexcept {
return x.v; }
380 V value_from(
const int x)
382 if constexpr (std::is_same_v<V, int>)
392 for (
size_t i = 0; i < n; ++i)
393 a(i) =
static_cast<int>((i * 37 + 11) % 101);
410 explicit Test_Tree(
const size_t n)
412 for (
size_t i = 0; i < n; ++i)
414 for (
size_t i = 1; i < n; ++i)
419 const Test_Tree & tree_of(
const size_t n)
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));
429 struct Test_Node_Tree
433 explicit Test_Node_Tree(
const size_t n)
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));
441 Test_Node_Tree(Test_Node_Tree &&) =
default;
445 if (not
nodes.is_empty())
450 const Test_Node_Tree & node_tree_of(
const size_t n)
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));
465 explicit Test_Digraph(
const size_t n)
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));
474 const Test_Digraph & digraph_of(
const size_t n)
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));
486 void use_ranges(
R &
r)
488 for (
size_t i = 0; i <
r.size(); ++i)
490 EXPECT_EQ(
r.query(i, i),
r.
get(i));
491 (void)
r.query(0, i);
493 EXPECT_EQ(
r.values().size(),
r.size());
498 void use_ordered(
T & t)
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());
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);
518 template <
class Engine>
519 void use_engine(Engine & e)
521 const size_t steps = e.steps_run();
524 EXPECT_EQ(e.steps_run(),
steps + 3);
528 template <
class Engine>
529 std::pair<size_t, size_t> engine_print(
const Engine & e)
531 return {e.frame().size(), e.steps_run()};
534 using Extents = std::array<ca_size_t, 2>;
542 struct Swap_Throws_Lattice : L
545 Swap_Throws_Lattice(
const L &
l) : L(
l) {}
546 Swap_Throws_Lattice(L &&
l) : L(
std::move(
l)) {}
548 void swap(Swap_Throws_Lattice & o)
555 using Throwing_Grid = Swap_Throws_Lattice<Grid>;
558 Extents extents_of(
const size_t n)
565 Grid life_grid(
const size_t n)
567 Grid g(extents_of(n), 0);
585 template <
template <
class>
class A>
struct Fenwick_Plus
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); }
593 template <
template <
class>
class A>
struct Fenwick_Minus
595 static constexpr const char * name =
"Gen_Fenwick_Tree_Minus";
597 static type make(
const size_t n) {
return type(values_of(n)); }
598 static void use(type & t) { use_ranges(t); }
601 template <
template <
class>
class A>
struct Segment
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); }
611 template <
template <
class>
class A>
struct Lazy_Segment
613 static constexpr const char * name =
"Gen_Lazy_Segment_Tree";
615 static type make(
const size_t n)
617 type t(values_of(n));
619 t.update(1, n - 2, 3);
622 static void use(type & t) { use_ranges(t); }
625 template <
template <
class>
class A>
struct Sparse
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); }
633 template <
template <
class>
class A>
struct Disjoint_Sparse
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); }
641 template <
template <
class>
class A>
struct Cartesian
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)
648 for (
size_t i = 0; i < t.size(); ++i)
649 if (
const size_t p = t.parent_of(i); p !=
type::NONE)
651 EXPECT_LE(t.data_at(p), t.data_at(i));
653 EXPECT_EQ(t.inorder().size(), t.size());
657 template <
template <
class>
class A>
struct Euler_Tour
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)
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));
671 template <
template <
class>
class A>
struct Cartesian_RMQ
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); }
679 template <
template <
class>
class A>
struct Heap
681 static constexpr const char * name =
"DynArrayHeap";
683 static type make(
const size_t n)
686 for (
const int v : values_of(n))
690 static void use(type &
h)
694 EXPECT_EQ(
count,
h.size());
696 EXPECT_EQ(
h.getMin(), -1);
701 template <
template <
class>
class A>
struct Dyn_Array
703 static constexpr const char * name =
"DynArray";
706 static type make(
const size_t n)
709 for (
const int v : values_of(n))
710 (void) a.append(value_from<
V>(v));
713 static void use(type & a)
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);
723 template <
template <
class>
class A>
struct Heap_Value
725 static constexpr const char * name =
"DynArrayHeap_Value";
730 static H make_with(
const size_t n)
733 for (
const int v : values_of(n))
739 static void use_with(H &
h)
743 EXPECT_EQ(
count,
h.size());
744 (void)
h.insert(value_from<V>(-1));
745 EXPECT_EQ(value_of(
h.getMin()), -1);
748 static type make(
const size_t n) {
return make_with<type>(n); }
749 static void use(type &
h) { use_with(
h); }
752 template <
template <
class>
class A>
struct B_Tree_Case
754 static constexpr const char * name =
"Gen_B_Tree";
756 static type make(
const size_t n)
759 for (
size_t i = 0; i < n; ++i)
760 (
void) t.
insert(
static_cast<int>(i));
763 static void use(type & t) { use_ordered(t); }
766 template <
template <
class>
class A>
struct BPlus_Tree_Case
768 static constexpr const char * name =
"Gen_BPlus_Tree";
770 static type make(
const size_t n)
773 for (
size_t i = 0; i < n; ++i)
774 (
void) t.
insert(
static_cast<int>(i));
777 static void use(type & t) { use_ordered(t); }
780 template <
template <
class>
class A>
struct Hash_Map
782 static constexpr const char * name =
"PersistentHashMap";
784 static type make(
const size_t n)
787 for (
size_t i = 0; i < n; ++i)
788 m =
m.
insert(
static_cast<int>(i),
static_cast<int>(i));
791 static void use(type &
m)
793 EXPECT_TRUE(
m.verify());
796 for (
size_t i = 0; i <
keys.size(); ++i)
798 m =
m.insert_or_assign(-1, 7);
800 EXPECT_TRUE(
m.verify());
804 template <
template <
class>
class A>
struct Skip_List
806 static constexpr const char * name =
"DynSkipList";
807 static constexpr bool moves_allocate =
true;
808 static constexpr bool assignment_empties =
true;
810 static type make(
const size_t n)
813 for (
size_t i = 0; i < n; ++i)
814 (
void)
l.
insert(
static_cast<int>(i));
817 static void use(type &
l)
822 EXPECT_NE(
l.
insert(-1),
nullptr);
823 EXPECT_TRUE(
l.has(-1));
830 template <
template <
class>
class A>
struct Rand_Tree_Case
832 static constexpr const char * name =
"Gen_Rand_Tree";
833 static constexpr bool moves_allocate =
true;
836 static type make(
const size_t n)
839 for (
size_t i = 0; i < n; ++i)
840 (
void) t.insert(
new Node(
static_cast<int>(i)));
843 static void use(type & t)
845 EXPECT_TRUE(t.verify());
847 EXPECT_EQ(t.insert(p), p);
848 EXPECT_EQ(t.search(-1), p);
849 EXPECT_TRUE(t.verify());
854 template <
template <
class>
class A>
struct Binary_Lifting
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)
862 for (
size_t id = 0;
id <
l.
size(); ++id)
863 EXPECT_EQ(
l.lca_id(
l.root_id(),
id),
l.root_id());
867 template <
template <
class>
class A>
struct Euler_RMQ
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)
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());
887 for (
size_t id = 0;
id <
h.size(); ++id)
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));
892 EXPECT_TRUE(
h.is_empty() or value_of(
h.subtree_query_id(
h.root_id())) == total);
895 template <
template <
class>
class A>
struct HLD_Op
897 static constexpr const char * name =
"Gen_HLD_Op";
899 static type make(
const size_t n)
901 return type(tree_of(n).g, [] (Gnode * p) {
return p->get_info(); }, 0);
903 static void use(type &
h) { use_hld(
h); }
906 template <
template <
class>
class A>
struct HLD_Filter
908 static constexpr const char * name =
"Gen_HLD_Filter";
910 static type make(
const size_t n)
912 return type(tree_of(n).g, [] (Gnode * p) {
return p->get_info(); }, 0);
914 static void use(type &
h) { use_hld(
h); }
917 template <
template <
class>
class A>
struct Heavy_Light
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)
925 for (
size_t id = 0;
id <
h.size(); ++id)
926 EXPECT_LT(
h.position_of_id(
id),
h.size());
932 void use_path_query(Q & q)
934 using V = std::decay_t<
decltype(q.get_node_id(0))>;
936 for (
size_t id = 0;
id < q.size(); ++id)
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));
942 EXPECT_TRUE(q.is_empty()
943 or value_of(q.query_subtree_id(q.decomposition().root_id())) == total);
946 template <
template <
class>
class A>
struct Path_Query_Op
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); }
954 template <
template <
class>
class A>
struct Path_Query_Filter
956 static constexpr const char * name =
"Gen_HLD_Path_Query_Filter";
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); }
962 template <
template <
class>
class A>
struct Centroid
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)
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());
976 template <
template <
class>
class A>
struct Tree_DP_Case
978 static constexpr const char * name =
"Gen_Tree_DP";
980 static type make(
const size_t n)
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; });
986 static void use(type & 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());
996 template <
template <
class>
class A>
struct Reroot_DP_Case
998 static constexpr const char * name =
"Gen_Reroot_DP";
1000 static type make(
const size_t n)
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; });
1007 static void use(type &
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());
1018 template <
template <
class>
class A>
struct Mo_On_Trees
1020 static constexpr const char * name =
"Gen_Mo_On_Trees";
1022 static type make(
const size_t n)
1024 const Test_Tree & t = tree_of(n);
1025 return type(t.g, t.nodes(0));
1027 static void use(type &
m)
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));
1036 template <
template <
class>
class A>
struct Mo_On_Tree_Node
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)
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));
1052 void use_floyd(
F & f)
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);
1060 template <
template <
class>
class A>
struct Floyd_Distance
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); }
1069 template <
template <
class>
class A>
struct Floyd_Filter
1071 static constexpr const char * name =
"Floyd_All_Shortest_Paths_Filter";
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); }
1078 template <
template <
class>
class A>
struct Life
1080 static constexpr const char * name =
"Synchronous_Engine";
1081 using Rule = A<Game_Of_Life_Rule>;
1083 static type make(
const size_t n)
1089 static auto print(
const type & e) {
return engine_print(e); }
1090 static void use(type & e) { use_engine(e); }
1093 template <
template <
class>
class A>
struct Async_Life
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)
1104 static auto print(
const type & e) {
return engine_print(e); }
1105 static void use(type & e) { use_engine(e); }
1108 template <
template <
class>
class A>
struct Random_Life
1110 static constexpr const char * name =
"Async_Engine_Scheme";
1111 using Scheme = A<Random_Asynchronous_Update<>>;
1113 static type make(
const size_t n)
1120 static auto print(
const type & e) {
return engine_print(e); }
1121 static void use(type & e) { use_engine(e); }
1124 template <
template <
class>
class A>
struct Parallel_Life
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)
1132 cfg.
pool = &small_pool();
1139 static auto print(
const type & e) {
return engine_print(e); }
1140 static void use(type & e) { use_engine(e); }
1143 template <
template <
class>
class A>
struct Field_Life
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)
1150 Two_Fields lat(extents_of(n), std::tuple{0, 7});
1156 static auto print(
const type & e) {
return engine_print(e); }
1157 static void use(type & e) { use_engine(e); }
1160 template <
template <
class>
class A>
struct Graph_Life
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)
1168 for (
size_t i = 0; i < n; i += 3)
1174 static auto print(
const type & e) {
return engine_print(e); }
1175 static void use(type & e)
1177 const size_t steps = e.steps_run();
1179 EXPECT_EQ(e.steps_run(),
steps + 1);
1186 template <
template <
class>
class A>
struct Fenwick_Both
1188 static constexpr const char * name =
"Gen_Fenwick_Tree_Both";
1190 static type make(
const size_t n) {
return type(values_of(n)); }
1191 static void use(type & t) { use_ranges(t); }
1195 Array<V> values_of_type(
const size_t n)
1199 for (
size_t i = 0; i < n; ++i)
1200 a(i) = value_from<V>(v(i));
1204 template <
template <
class>
class A>
struct Segment_Both
1206 static constexpr const char * name =
"Gen_Segment_Tree_Both";
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); }
1213 template <
template <
class>
class A>
struct HLD_All
1215 static constexpr const char * name =
"Gen_HLD_All";
1218 static type make(
const size_t n)
1220 return type(tree_of(n).g, [] (Gnode * p) {
return V(Int(p->get_info())); },
V(Int(0)));
1222 static void use(type &
h) { use_hld(
h); }
1225 template <
template <
class>
class A>
struct Path_Query_All
1227 static constexpr const char * name =
"Gen_HLD_Path_Query_All";
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); }
1234 template <
template <
class>
class A>
struct Life_All
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)
1246 static auto print(
const type & e) {
return engine_print(e); }
1247 static void use(type & e) { use_engine(e); }
1250 template <
template <
class>
class A>
struct Async_All
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)
1264 static auto print(
const type & e) {
return engine_print(e); }
1265 static void use(type & e) { use_engine(e); }
1268 template <
template <
class>
class A>
struct Parallel_All
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)
1277 cfg.
pool = &small_pool();
1284 static auto print(
const type & e) {
return engine_print(e); }
1285 static void use(type & e) { use_engine(e); }
1288 template <
template <
class>
class A>
struct Field_All
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)
1296 Two_Fields lat(extents_of(n), std::tuple{0, 7});
1302 static auto print(
const type & e) {
return engine_print(e); }
1303 static void use(type & e) { use_engine(e); }
1308 template <
template <
class>
class A>
struct Heap_Both
1310 static constexpr const char * name =
"DynArrayHeap_Both";
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); }
1319 template <
template <
class>
class A>
struct Life_Lattice
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)
1331 static auto print(
const type & e) {
return engine_print(e); }
1332 static void use(type & e) { use_engine(e); }
1335 template <
template <
class>
class A>
struct Async_Lattice
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)
1349 static auto print(
const type & e) {
return engine_print(e); }
1350 static void use(type & e) { use_engine(e); }
1353 template <
template <
class>
class A>
struct Parallel_Lattice
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)
1362 cfg.
pool = &small_pool();
1369 static auto print(
const type & e) {
return engine_print(e); }
1370 static void use(type & e) { use_engine(e); }
1373 template <
template <
class>
class A>
struct Field_Lattice
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)
1382 type e(Fields(Two_Fields(extents_of(n), std::tuple{0, 7})),
1387 static auto print(
const type & e) {
return engine_print(e); }
1388 static void use(type & e) { use_engine(e); }
1391 template <
template <
class>
class A>
struct Graph_Lattice_Case
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)
1400 for (
size_t i = 0; i < n; i += 3)
1406 static auto print(
const type & e) {
return engine_print(e); }
1407 static void use(type & e)
1409 const size_t steps = e.steps_run();
1411 EXPECT_EQ(e.steps_run(),
steps + 1);
1416 template <
template <
template <
class>
class>
class Case, template <class> class A>
1420 static constexpr const char * kind = kind_name<A>;
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>...>;
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>;
1441 struct Contract_Name
1444 static std::string GetName(
int)
1446 return std::string(T::C::name) +
"_" + T::kind;
1452 template <
class Case,
class T>
1453 auto print_of(
const T & x)
1455 if constexpr (
requires { Case::print(x); })
1456 return Case::print(x);
1458 return static_cast<size_t>(x.size());
1461 template <
class Case>
1462 constexpr bool moves_allocate()
1464 if constexpr (
requires { Case::moves_allocate; })
1465 return Case::moves_allocate;
1472 template <
class Case>
1473 constexpr bool assignment_empties()
1475 if constexpr (
requires { Case::assignment_empties; })
1476 return Case::assignment_empties;
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,
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";
1492 template <
class Case,
class T>
1493 void use_all(
T & a,
T & b, std::optional<T> & c)
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)
1514 using T =
typename Case::type;
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);
1522 const size_t before = risky_calls;
1524 ran = risky_calls - before;
1525 expect(a, b, c, print_a, print_b);
1526 use_all<Case>(a, b, c);
1530 EXPECT_EQ(ran, 0u) << what <<
" is noexcept, but runs " << ran
1531 <<
" operations of the types of the user that may throw";
1535 else if (not allows_other)
1537 EXPECT_GT(ran, 0u) << what <<
" may throw, but runs no operation of the types of the "
1538 <<
"user that may throw";
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);
1554 catch (
const std::exception & e)
1557 EXPECT_TRUE(is_adversarial(e)) << what <<
" threw " << e.what();
1560 EXPECT_EQ(threw, ran > 0) << what;
1562 expect(a, b, c, print_a, print_b);
1564 on_throw(a, b, print_a, print_b, tags_a, tags_b);
1565 use_all<Case>(a, b, c);
1575 using Case =
typename TypeParam::C;
1576 using T =
typename Case::type;
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 &)
1583 ASSERT_TRUE(c.has_value());
1584 EXPECT_EQ(print_of<Case>(*c), pa);
1586 [] (
T &,
T &,
const auto &,
const auto &,
const auto &,
const auto &) {});
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 &)
1598 EXPECT_EQ(print_of<Case>(b), pa);
1600 [] (
T & a,
T & b,
const auto & pa,
const auto & pb,
const auto & ta,
1603 expect_as_it_was<Case>(a, pa, ta,
"the move assignment",
"the source");
1604 if constexpr (assignment_empties<Case>())
1606 EXPECT_EQ(b.size(), 0u) <<
"the destination is not empty";
1607 EXPECT_EQ(tags_of(b), tb);
1610 expect_as_it_was<Case>(b, pb, tb,
"the move assignment",
1614 GTEST_SKIP() <<
"the class has no move assignment";
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)
1626 EXPECT_EQ(print_of<Case>(a), pb);
1627 EXPECT_EQ(print_of<Case>(b), pa);
1629 [] (
T & a,
T & b,
const auto & pa,
const auto & pb,
const auto & ta,
1632 expect_as_it_was<Case>(a, pa, ta,
"swap()",
"this object");
1633 expect_as_it_was<Case>(b, pb, tb,
"swap()",
"the other");
1636 GTEST_SKIP() <<
"the class has no swap()";
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>>;
1654 template <
class Case,
class Op>
1655 void check_rollback(
const char * what, Op op)
1657 using T =
typename Case::type;
1660 T a = Case::make(20);
1661 T b = Case::make(5);
1662 const size_t before = risky_calls;
1664 swaps = risky_calls - before;
1666 EXPECT_GE(swaps, 2u) << what <<
" exchanges fewer than two types of the user";
1667 for (
size_t k = 1;
k <= swaps; ++
k)
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);
1676 arm_swaps(
static_cast<long>(
k));
1681 catch (
const std::exception & e)
1684 EXPECT_TRUE(is_adversarial(e)) << what <<
" threw " << e.what();
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");
1700 template <
class Case,
class Op>
1701 void check_double_failure(
const char * what, Op op)
1703 using T =
typename Case::type;
1706 T a = Case::make(20);
1707 T b = Case::make(5);
1708 const size_t before = risky_calls;
1710 swaps = risky_calls - before;
1713 for (
long i = 2; i <= static_cast<long>(swaps); ++i)
1714 for (
long j = i + 1; j < 2 * i; ++j)
1715 for (
const bool assign : {
false,
true})
1717 SCOPED_TRACE(std::string(what) +
" threw at its swaps " + std::to_string(i) +
" and " +
1719 T a = Case::make(20);
1720 T b = Case::make(5);
1725 ADD_FAILURE() << what <<
" did not throw";
1727 catch (
const std::exception & e)
1729 EXPECT_TRUE(is_adversarial(e)) << what <<
" threw " << e.what();
1732 if (swaps_armed >= j)
1742 EXPECT_GT(twice, 0u) << what <<
" never threw while it put back what it exchanged";
1752 using Case =
typename TypeParam::C;
1753 using T =
typename Case::type;
1754 check_rollback<Case>(
"swap()", [] (
T & a,
T & b) { a.swap(b); });
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); });
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); });
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); });
1784 std::atomic<size_t> steps_without_resource{0};
1786 template <
class Base>
1787 struct Resource_Rule : Base
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;
1796 Resource_Rule & operator = (Resource_Rule && o)
1799 Base::operator = (std::move(o));
1800 resource = std::move(o.resource);
1804 template <
class... Args>
1805 auto operator () (
const Args &... args)
const
1806 ->
decltype(std::declval<const Base &>()(args...))
1809 ++steps_without_resource;
1810 return Base::operator () (args...);
1816 template <
class Case>
1817 void check_rule_with_a_resource()
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);
1830 catch (
const std::exception & e)
1833 EXPECT_TRUE(is_adversarial(e)) << Case::name <<
" threw " << e.what();
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";
1847TEST(Noexcept_Contract_Engines, RuleThatOwnsAResource)
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>>();
1860TEST(Noexcept_Contract_Skip_List, MoveConstructorWhenTheComparisonAssignmentThrows)
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;
1868 EXPECT_NO_THROW(dst.emplace(std::move(src)));
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);
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;
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; }
1899 struct Number_Distance
1901 using Distance_Type = Number;
1902 Number operator () (Dgraph::Arc * a)
const noexcept {
return a->get_info(); }
1906template <>
struct std::numeric_limits<Number> : std::numeric_limits<long>
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;
1916TEST(Noexcept_Contract_Floyd, InfinityWhoseCopyThrows)
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);
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";
1929 F src(digraph_of(5).g);
1934 F dst(std::move(src));
1936 catch (
const std::exception & e)
1939 EXPECT_TRUE(is_adversarial(e)) << e.what();
1943 EXPECT_EQ(src.get_nodes_ref().size(), 0u);
1944 EXPECT_THROW((
void) src.get_min_path(0L, 0L), std::out_of_range);
1957 template <
template <
typename,
class>
class Tree>
1958 void check_set_moves(
const char * tree)
1963 EXPECT_TRUE(std::is_nothrow_move_constructible_v<Set>) << tree;
1965 ASSERT_FALSE(std::is_nothrow_move_assignable_v<Set>) << tree;
1968 for (
int i = 0; i < 20; ++i)
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;
1981 EXPECT_EQ(a.size(), 20u) << tree;
1982 EXPECT_TRUE(a.verify()) << tree;
1985 (void) c.insert(-1);
1987 EXPECT_THROW(c = std::move(a), std::runtime_error) << tree;
1989 EXPECT_EQ(a.size(), 20u) << tree;
1990 EXPECT_EQ(c.size(), 0u) << tree <<
": the assignment frees the elements of its destination first";
1992 EXPECT_TRUE(c.contains(5)) << tree;
1996TEST(Noexcept_Contract_Dyn_Set, MovesOverARandTreeWhoseComparisonSwapThrows)
1998 check_set_moves<Rand_Tree>(
"Rand_Tree");
2001TEST(Noexcept_Contract_Dyn_Set, MovesOverARankedSplayTreeWhoseComparisonSwapThrows)
2003 check_set_moves<Splay_Tree_Rk>(
"Splay_Tree_Rk");
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.
WeightedDigraph::Node Node
Simple dynamic array with automatic resizing and functional operations.
constexpr bool is_empty() const noexcept
Checks if the container is empty.
static Array create(const size_t n)
Create an array with n logical elements.
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.
Dynamic heap (priority queue) backed by DynArray.
Doubly-linked list (defined in tpl_dynList.H).
T & append(const T &item)
T & insert(const T &item)
T remove()
Remove the first item of the list.
size_t size() const noexcept
Return the number of items of the list, in constant time.
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.
bool insert(const Key &key)
Insert a key if it is not already present.
LCA via binary lifting on a rooted tree.
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.
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.
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.
Generic rerooting dynamic programming (all-roots DP).
Segment tree over an arbitrary associative binary operation.
Sparse Table over an arbitrary associative and idempotent binary operation.
Generic bottom-up tree dynamic programming.
unsigned long get() noexcept
The next number, as operator()().
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
constexpr bool contains(const Key &key) const noexcept
Alias for has().
Immutable unordered map backed by a Hash Array Mapped Trie (HAMT).
Represents a line segment between two points.
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.
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
constexpr size_t size() const noexcept
Returns the number of entries in the table.
constexpr bool is_empty() const noexcept
Checks if the table is empty.
DynList< Key > keys() const
Returns a list containing all keys in the table.
Key * insert(const Key &key)
Inserts a key into the hash table (copy version).
Key & find(const Key &key)
Finds a key and returns a reference to it.
QuadTree - Hierarchical spatial index for 2D points.
DynArray< Graph::Node * > nodes
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.
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.
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.
std::decay_t< typename HeadC::Item_Type > T
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.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
void print(const DynList< Parc< Net > > &sp)
Print a semi-path to stdout.
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.
Default filter for filtered iterators on arcs.
Arc of graph implemented with double-linked adjacency lists.
Randomized binary search tree.
static Number max() noexcept
static Number lowest() noexcept
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
void assign(BinTree< int >::Node *node, int, int)
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.