39# include <gtest/gtest.h>
50# include <type_traits>
70 using Ends = std::vector<std::pair<size_t, size_t>>;
71 using Cycle = std::vector<size_t>;
73 const double Infinity = std::numeric_limits<double>::infinity();
82 std::vector<typename GT::Node *>
nodes;
83 std::vector<typename GT::Arc *>
arcs;
85 Built(
const size_t n,
const Ends & ends,
const bool reversed =
false)
88 for (
size_t i = 0; i < n; ++i)
90 for (
size_t i = 0; i < ends.size(); ++i)
92 const size_t id =
reversed ? ends.size() - 1 - i : i;
93 arcs[id] = g.
insert_arc(nodes[ends[
id].first], nodes[ends[
id].second],
int(
id));
102 bool operator () (
const Arc *a,
const Arc *b)
const
104 return a->
get_info() < b->get_info();
116 std::vector<Arc *>
arcs;
117 std::map<const void *, size_t> number;
118 std::vector<Cycle> cycles;
119 std::vector<std::vector<size_t>> of_arc;
121 template <
class Less>
122 Oracle(
const GT & g,
const std::set<size_t> & lengths,
Less less)
124 for (
typename GT::Arc_Iterator it(g); it.has_curr(); it.next_ne())
125 arcs.push_back(it.get_curr_ne());
127 std::vector<const void *> source(
arcs.size()), target(
arcs.size());
128 std::map<const void *, std::vector<size_t>>
out;
129 for (
size_t a = 0; a <
arcs.size(); ++a)
134 out[source[a]].push_back(a);
137 const size_t longest = lengths.empty() ? 0 : *lengths.rbegin();
138 std::set<Cycle> found;
139 for (
typename GT::Node_Iterator it(g); it.has_curr(); it.next_ne())
141 const void *start = it.get_curr_ne();
143 std::set<const void *> on_path = {start};
144 const std::function<void(
const void *)> walk = [&] (
const void *node)
146 for (
const size_t a :
out[node])
147 if (target[a] == start)
149 if (lengths.count(path.size() + 1) == 0)
153 std::rotate(cycle.begin(), std::min_element(cycle.begin(), cycle.end()),
157 else if (path.size() + 2 <= longest
and on_path.count(target[a]) == 0)
160 on_path.insert(target[a]);
162 on_path.erase(target[a]);
168 cycles.assign(found.begin(), found.end());
169 std::sort(cycles.begin(), cycles.end(), [] (
const Cycle & a,
const Cycle & b)
171 return a.size() != b.size() ? a.size() < b.size() : a < b;
173 of_arc.resize(
arcs.size());
174 for (
size_t position = 0; position < cycles.size(); ++position)
175 for (
const size_t a : cycles[position])
176 of_arc[a].push_back(position);
180 template <
class Span>
181 std::vector<size_t> as_vector(
const Span & items)
183 return std::vector<size_t>(items.begin(), items.end());
192 for (
size_t a = 0; a <
oracle.arcs.size(); ++a)
194 ASSERT_EQ(index.
arc(a),
oracle.arcs[a]) <<
"arc " << a;
199 for (
size_t position = 0; position <
oracle.cycles.size(); ++position)
202 <<
"position " << position;
203 ASSERT_EQ(index.
length(position),
oracle.cycles[position].size());
204 ++by_length[
oracle.cycles[position].size()];
207 for (
size_t k = 1; k <= Bounded_Cycle_Index<GT>::Max_Length; ++
k)
209 EXPECT_EQ(index.
num_cycles(
k), by_length[
k]) <<
"length " <<
k;
210 up_to += by_length[
k];
213 for (
size_t a = 0; a <
oracle.arcs.size(); ++a)
214 ASSERT_EQ(as_vector(index.
cycles_of(a)),
oracle.of_arc[a]) <<
"arc " << a;
219 using Handed = std::vector<std::pair<size_t, double>>;
225 const double bound,
const size_t max_length)
228 for (
const size_t position : index.cycles_of(arc))
229 if (index.length(position) <= max_length
and index.weight_sum(position) < bound)
230 ret.emplace_back(position, index.weight_sum(position));
235 Ends random_ends(std::mt19937 &
rng,
const size_t n,
const size_t m)
237 std::uniform_int_distribution<size_t> node(0, n - 1);
239 for (
size_t i = 0; i <
m; ++i)
241 const size_t source = node(
rng);
242 ends.emplace_back(source, node(
rng));
254 std::vector<const void *>
arcs;
255 std::vector<double> weights;
256 std::vector<uint64_t> stamps;
257 std::vector<std::vector<size_t>> of_arc;
258 std::vector<Cycle> cycles;
259 std::vector<double> sums;
260 std::vector<uint64_t> counts;
267 for (
size_t a = 0; a < index.
num_arcs(); ++a)
270 weights.push_back(index.
weight(a));
271 stamps.push_back(index.
stamp(a));
272 of_arc.push_back(as_vector(index.
cycles_of(a)));
275 for (
size_t position = 0; position < index.
num_cycles(); ++position)
277 cycles.push_back(as_vector(index.
cycle_arcs(position)));
280 for (
size_t k = 0; k <= Bounded_Cycle_Index<GT>::Max_Length + 1; ++
k)
287 counts.push_back(index.
clock());
289 [
this] (
size_t p,
double s) { handed.emplace_back(p, s); });
292 bool operator == (
const Answers &)
const =
default;
302 for (
size_t k = 0; k <= Bounded_Cycle_Index<GT>::Max_Length + 1; ++
k)
309 EXPECT_EQ(index.
clock(), 0u);
310 EXPECT_THROW((
void) index.
arc_number(arc), std::domain_error);
311 EXPECT_THROW((
void) index.
arc(0), std::out_of_range);
312 EXPECT_THROW((
void) index.
weight(0), std::out_of_range);
313 EXPECT_THROW((
void) index.
stamp(0), std::out_of_range);
314 EXPECT_THROW((
void) index.
cycles_of(0), std::out_of_range);
315 EXPECT_THROW((
void) index.
cycle_arcs(0), std::out_of_range);
316 EXPECT_THROW((
void) index.
weight_sum(0), std::out_of_range);
317 EXPECT_THROW((
void) index.
length(0), std::out_of_range);
318 EXPECT_THROW((
void) index.
is_stale(0, 0), std::out_of_range);
319 EXPECT_THROW(index.
set_weight(0, 1), std::out_of_range);
320 EXPECT_THROW(index.
touch(0), std::out_of_range);
322 const auto count = [&handed] (size_t, double) { ++handed; };
323 EXPECT_THROW(index.
update(0, 1, Infinity, 8,
count), std::out_of_range);
327 EXPECT_EQ(handed, 0u);
330 Ends complete_ends(
const size_t n)
333 for (
size_t i = 0; i < n; ++i)
334 for (
size_t j = 0; j < n; ++j)
336 ends.emplace_back(i, j);
340 size_t falling(
const size_t n,
const size_t k)
343 for (
size_t i = 0; i <
k; ++i)
349 class BoundedCycleIndexOn :
public ::testing::Test {};
351 using Digraph_Types = ::testing::Types<List_Type, Sparse_Type, Array_Type>;
358TYPED_TEST(BoundedCycleIndexOn, HasTheCatalogOfASearchMadeApart)
360 using GT = TypeParam;
362 const std::vector<std::set<size_t>> sets =
363 {{3}, {1, 2, 3}, {2, 3, 4}, {3, 4, 5, 6}, {2, 4}, {1, 2, 3, 4, 5, 6, 7, 8}, {8}, {1}};
364 std::mt19937
rng(20261113);
365 size_t compared = 0, with_loops = 0, with_parallel = 0;
366 for (
size_t round = 0; round < 600; ++round)
368 const size_t n = 1 +
rng() % 7;
369 const Ends ends = random_ends(
rng, n,
rng() % 26);
370 const Built<GT> built(n, ends, round % 2 == 1);
371 const auto & lengths = sets[round % sets.size()];
372 const Array<size_t> array_of_lengths(lengths.begin(), lengths.end());
373 const auto by_info_down = [] (
const typename GT::Arc *a,
const typename GT::Arc *b)
375 return a->
get_info() > b->get_info();
377 const auto by_parity = [] (
const typename GT::Arc *a,
const typename GT::Arc *b)
379 return a->
get_info() % 2 < b->get_info() % 2;
383 expect_the_catalog(in_list_order, Oracle<GT>(built.g, lengths,
Arc_List_Order()));
385 expect_the_catalog(by_info, Oracle<GT>(built.g, lengths, By_Info()));
387 expect_the_catalog(down, Oracle<GT>(built.g, lengths, by_info_down));
389 expect_the_catalog(parity, Oracle<GT>(built.g, lengths, by_parity));
391 const View view(built.g);
393 expect_the_catalog(through_view, Oracle<View>(view, lengths, By_Info()));
395 for (
size_t position = 0; position < by_info.
num_cycles(); ++position)
396 ASSERT_EQ(as_vector(through_view.
cycle_arcs(position)),
400 std::set<std::pair<size_t, size_t>> seen;
401 bool loop =
false, parallel =
false;
402 for (
const auto & e : ends)
404 loop = loop or e.first == e.second;
405 parallel = parallel or not seen.insert(e).second;
408 with_parallel += parallel;
409 if (::testing::Test::HasFatalFailure())
412 EXPECT_GT(compared, 3000u);
413 EXPECT_GT(with_loops, 300u);
414 EXPECT_GT(with_parallel, 300u);
419TYPED_TEST(BoundedCycleIndexOn, DoesNotDependOnTheOrderOfInsertion)
421 using GT = TypeParam;
423 for (
size_t round = 0; round < 40; ++round)
425 const size_t n = 2 +
rng() % 5;
426 const Ends ends = random_ends(
rng, n, 4 +
rng() % 12);
427 const Built<GT> forward(n, ends), backward(n, ends,
true);
431 for (
size_t arc = 0; arc < a.
num_arcs(); ++arc)
432 ASSERT_EQ(a.
arc(arc)->get_info(), b.
arc(arc)->get_info());
433 for (
size_t position = 0; position < a.
num_cycles(); ++position)
440TEST(BoundedCycleIndex, CountsTheCyclesOfACompleteDigraph)
442 for (
size_t n = 1; n <= 8; ++n)
444 const Built<List_Type> built(n, complete_ends(n));
448 for (
size_t k = 2;
k <= 8; ++
k)
450 const size_t expected =
k > n ? 0 : falling(n,
k) /
k;
465TEST(BoundedCycleIndex, OrdersTheCatalogAndKeepsThePrefixes)
467 std::mt19937
rng(11);
468 for (
size_t round = 0; round < 30; ++round)
470 const size_t n = 3 +
rng() % 4;
471 const Built<List_Type> built(n, random_ends(
rng, n, 8 +
rng() % 12));
473 for (
size_t position = 0; position < whole.
num_cycles(); ++position)
476 EXPECT_EQ(*std::min_element(
arcs.begin(),
arcs.end()),
arcs.front());
477 EXPECT_EQ(std::set<size_t>(
arcs.begin(),
arcs.end()).size(),
arcs.size());
480 const auto before = as_vector(whole.
cycle_arcs(position - 1));
481 EXPECT_TRUE(before.size() <
arcs.size() or
484 for (
size_t m = 1;
m <= 6; ++
m)
487 for (
size_t k = 1;
k <=
m; ++
k)
491 EXPECT_EQ(
prefix.longest_length(),
m);
492 for (
size_t position = 0; position <
prefix.num_cycles(); ++position)
493 ASSERT_EQ(as_vector(
prefix.cycle_arcs(position)),
505TEST(BoundedCycleIndex, TellsParallelArcsLoopsAndSensesApart)
508 const Built<List_Type> built(3, {{0, 1}, {0, 1}, {1, 0}, {1, 1}, {1, 2}, {2, 0}, {2, 1}, {0, 2}});
510 EXPECT_EQ(index.num_cycles(1), 1u);
511 EXPECT_EQ(as_vector(index.cycle_arcs(0)),
Cycle({3}));
513 EXPECT_EQ(index.num_cycles(2), 4u);
514 EXPECT_EQ(as_vector(index.cycle_arcs(1)),
Cycle({0, 2}));
515 EXPECT_EQ(as_vector(index.cycle_arcs(2)),
Cycle({1, 2}));
517 EXPECT_EQ(index.num_cycles(3), 3u);
518 EXPECT_EQ(as_vector(index.cycle_arcs(5)),
Cycle({0, 4, 5}));
519 EXPECT_EQ(as_vector(index.cycle_arcs(6)),
Cycle({1, 4, 5}));
520 EXPECT_EQ(as_vector(index.cycle_arcs(7)),
Cycle({2, 7, 6}));
521 EXPECT_EQ(as_vector(index.cycles_of(3)), std::vector<size_t>({0}));
527TEST(BoundedCycleIndex, NumbersTheArcsInTheOrderGiven)
529 const Built<List_Type> built(3, {{0, 1}, {1, 2}, {2, 0}, {0, 2}});
530 const Built<List_Type> other(2, {{0, 1}});
531 const auto down = [] (
const List_Type::Arc *a,
const List_Type::Arc *b)
533 return a->get_info() > b->get_info();
537 for (
size_t number = 0; number < 4; ++number)
539 EXPECT_EQ(index.
arc(number)->get_info(),
int(3 - number));
542 EXPECT_THROW((
void) index.
arc(4), std::out_of_range);
543 EXPECT_THROW((
void) index.
arc_number(other.arcs[0]), std::domain_error);
544 EXPECT_THROW((
void) index.
arc_number(
nullptr), std::domain_error);
548 for (List_Type::Arc_Iterator it(built.g); it.has_curr(); it.next_ne(), ++number)
549 EXPECT_EQ(in_list_order.
arc(number), it.get_curr_ne());
554TEST(BoundedCycleIndex, StartsWithArcsThatCannotBeUsed)
556 const Built<List_Type> built(4, complete_ends(4));
559 const auto count = [&handed] (size_t, double) { ++handed; };
560 for (
size_t a = 0; a < index.
num_arcs(); ++a)
561 EXPECT_EQ(index.
weight(a), Infinity);
563 EXPECT_EQ(handed, 0u);
564 for (
size_t position = 0; position < index.
num_cycles(); ++position)
565 EXPECT_EQ(index.
weight_sum(position), Infinity);
568 for (
size_t a = 1; a < index.
num_arcs(); ++a)
574 EXPECT_EQ(handed, index.
cycles_of(0).size());
577 EXPECT_EQ(handed, 0u);
583TYPED_TEST(BoundedCycleIndexOn, UpdateHandsTheCyclesBelowTheBound)
585 using GT = TypeParam;
586 std::mt19937
rng(20261114);
587 std::uniform_real_distribution<double> weight(-3, 3);
588 size_t handed_in_all = 0, looked_at = 0;
589 for (
size_t round = 0; round < 60; ++round)
591 const size_t n = 2 +
rng() % 5;
592 const Built<GT> built(n, random_ends(
rng, n, 6 +
rng() % 14));
594 std::vector<double> weights(index.
num_arcs());
595 for (
size_t a = 0; a < index.
num_arcs(); ++a)
597 for (
size_t step = 0; step < 40; ++step)
600 const double w =
rng() % 11 == 0 ? Infinity : weight(
rng);
601 const double bound = weight(
rng);
602 const size_t max_length =
rng() % 10;
605 const size_t seen = index.
update(arc,
w, bound, max_length, [&] (
size_t p,
double s)
607 handed.emplace_back(p, s);
609 ASSERT_EQ(index.
weight(arc),
w);
610 ASSERT_EQ(handed, below_of(index, arc, bound, max_length));
611 size_t expected_seen = 0;
612 for (
const size_t position : index.
cycles_of(arc))
613 expected_seen += index.
length(position) <= max_length;
614 ASSERT_EQ(seen, expected_seen);
615 for (
const auto & [position,
sum] : handed)
619 for (
size_t j = 1; j <
arcs.size(); ++j)
623 handed_in_all += handed.size();
627 EXPECT_GT(handed_in_all, 500u);
628 EXPECT_GT(looked_at, handed_in_all);
632TEST(BoundedCycleIndex, HandsTheSameSumWhicheverArcChanged)
634 const Built<List_Type> built(4, {{0, 1}, {1, 2}, {2, 3}, {3, 0}});
636 const double weights[] = {0.1, 1e16, -1e16, 0.3};
637 for (
size_t a = 0; a < 4; ++a)
638 index.set_weight(a, weights[a]);
639 const double sum = ((0.1 + 1e16) - 1e16) + 0.3;
640 EXPECT_EQ(index.weight_sum(0),
sum);
641 for (
size_t a = 0; a < 4; ++a)
644 index.update(a, weights[a], Infinity, 4, [&] (
size_t p,
double s)
646 handed.emplace_back(p, s);
648 ASSERT_EQ(handed, Handed({{0,
sum}})) <<
"arc " << a;
654TYPED_TEST(BoundedCycleIndexOn, HandsOnceEachCycleOfSeveralArcs)
656 using GT = TypeParam;
657 std::mt19937
rng(42);
658 std::uniform_real_distribution<double> weight(-2, 2);
660 for (
size_t round = 0; round < 80; ++round)
662 const size_t n = 2 +
rng() % 4;
663 const Built<GT> built(n, random_ends(
rng, n, 5 +
rng() % 12));
665 for (
size_t a = 0; a < index.
num_arcs(); ++a)
667 for (
size_t step = 0; step < 12; ++step)
670 for (
size_t i = 0,
m =
rng() % 5; i <
m; ++i)
672 for (
size_t i = 0; i <
arcs.size(); ++i)
674 const double bound = weight(
rng);
675 const size_t max_length = 1 +
rng() % 6;
678 size_t expected_seen = 0, memberships = 0;
679 std::set<size_t> of_any;
680 for (
size_t i = 0; i <
arcs.size(); ++i)
682 if (index.
length(position) <= max_length)
685 if (not of_any.insert(position).second)
691 shared += memberships - expected_seen;
697 handed.emplace_back(p, s);
699 ASSERT_EQ(seen, expected_seen);
700 std::set<size_t> positions;
701 for (
const auto & [position,
sum] : handed)
703 ASSERT_TRUE(positions.insert(position).second) <<
"handed twice";
711 [&again] (
size_t,
double) { ++again; });
712 ASSERT_EQ(again, handed.size());
715 EXPECT_GT(shared, 200u);
718 const Built<GT>
pair(2, {{0, 1}, {1, 0}});
720 index.set_weight(0, -1);
721 index.set_weight(1, -1);
723 EXPECT_EQ(index.for_each_cycle_below({0, 1, 0}, 0, 2, [&] (
size_t p,
double s)
725 handed.emplace_back(p, s);
727 EXPECT_EQ(handed, Handed({{0, -2.0}}));
731TEST(BoundedCycleIndex, WalksTheWholeCatalog)
734 std::uniform_real_distribution<double> weight(-1, 1);
735 const Built<List_Type> built(6, complete_ends(6));
737 for (
size_t a = 0; a < index.
num_arcs(); ++a)
739 const auto & constant = index;
740 for (
size_t max_length = 0; max_length <= 9; ++max_length)
743 const size_t seen = constant.for_each_cycle_below(-0.5, max_length, [&] (
size_t p,
double s)
745 handed.emplace_back(p, s);
748 for (
size_t position = 0; position < index.
num_cycles_up_to(max_length); ++position)
752 EXPECT_FALSE(max_length >= 2
and handed.empty());
759TEST(BoundedCycleIndex, GivesEmptyRanges)
762 const auto count = [&handed] (size_t, double) { ++handed; };
764 const Built<List_Type> one_arc(2, {{0, 1}});
766 EXPECT_EQ(index.num_cycles(), 0u);
767 EXPECT_TRUE(index.cycles_of(0).empty());
768 EXPECT_EQ(index.update(0, -1, Infinity, 8,
count), 0u);
769 EXPECT_EQ(index.for_each_cycle_below({0}, Infinity, 8,
count), 0u);
770 EXPECT_EQ(index.for_each_cycle_below(Infinity, 8,
count), 0u);
771 EXPECT_EQ(index.weight(0), -1);
772 EXPECT_THROW((
void) index.length(0), std::out_of_range);
773 EXPECT_THROW((
void) index.cycle_arcs(0), std::out_of_range);
774 EXPECT_THROW((
void) index.weight_sum(0), std::out_of_range);
775 EXPECT_THROW((
void) index.is_stale(0, 0), std::out_of_range);
779 const Built<List_Type> with_tail(4, {{0, 1}, {1, 2}, {2, 0}, {2, 3}});
781 EXPECT_EQ(index.num_cycles(), 1u);
782 EXPECT_TRUE(index.cycles_of(3).empty());
783 EXPECT_EQ(index.update(3, -1, Infinity, 8,
count), 0u);
785 for (
size_t a = 0; a < 3; ++a)
786 EXPECT_EQ(index.update(a, -1, Infinity, 2,
count), 0u);
787 EXPECT_EQ(handed, 0u);
788 EXPECT_EQ(index.update(0, -1, Infinity, 3,
count), 1u);
789 EXPECT_EQ(handed, 1u);
792 const Built<List_Type> acyclic(5, {{0, 1}, {1, 2}, {0, 2}, {2, 3}, {3, 4}, {0, 4}});
794 EXPECT_EQ(index.num_cycles(), 0u);
795 for (
size_t a = 0; a < index.num_arcs(); ++a)
797 EXPECT_TRUE(index.cycles_of(a).empty());
798 EXPECT_EQ(index.update(a, -1, Infinity, 8,
count), 0u);
802 const Built<List_Type> no_arcs(3, {});
804 EXPECT_EQ(index.num_arcs(), 0u);
805 EXPECT_EQ(index.num_cycles(), 0u);
806 EXPECT_THROW((
void) index.cycles_of(0), std::out_of_range);
807 EXPECT_THROW(index.update(0, 1, 0, 3,
count), std::out_of_range);
809 EXPECT_EQ(index.for_each_cycle_below(0, 3,
count), 0u);
812 EXPECT_EQ(of_empty.num_arcs(), 0u);
814 EXPECT_EQ(handed, 1u);
819TEST(BoundedCycleIndex, RejectsWhatIsNotValid)
821 const double nan = std::numeric_limits<double>::quiet_NaN();
822 const Built<List_Type> built(3, {{0, 1}, {1, 2}, {2, 0}});
824 for (
size_t a = 0; a < 3; ++a)
825 index.set_weight(a, 1);
827 const auto count = [&handed] (size_t, double) { ++handed; };
828 const uint64_t clock = index.clock();
830 EXPECT_THROW(index.set_weight(0, nan), std::domain_error);
831 EXPECT_THROW(index.set_weight(0, -Infinity), std::domain_error);
832 EXPECT_THROW(index.set_weight(3, 1), std::out_of_range);
833 EXPECT_THROW(index.update(0, nan, 10, 3,
count), std::domain_error);
834 EXPECT_THROW(index.update(0, -Infinity, 10, 3,
count), std::domain_error);
835 EXPECT_THROW(index.update(0, 2, nan, 3,
count), std::domain_error);
836 EXPECT_THROW(index.update(3, 2, 10, 3,
count), std::out_of_range);
837 EXPECT_THROW(index.for_each_cycle_below(nan, 3,
count), std::domain_error);
838 EXPECT_THROW(index.for_each_cycle_below({0}, nan, 3,
count), std::domain_error);
839 EXPECT_THROW(index.for_each_cycle_below({0, 3}, 10, 3,
count), std::out_of_range);
840 EXPECT_THROW((
void) index.weight(3), std::out_of_range);
841 EXPECT_THROW((
void) index.stamp(3), std::out_of_range);
842 EXPECT_THROW(index.touch(3), std::out_of_range);
843 EXPECT_THROW((
void) index.cycles_of(3), std::out_of_range);
844 EXPECT_THROW((
void) index.length(1), std::out_of_range);
846 EXPECT_EQ(handed, 0u);
847 EXPECT_EQ(index.clock(), clock);
848 for (
size_t a = 0; a < 3; ++a)
849 EXPECT_EQ(index.weight(a), 1);
850 EXPECT_EQ(index.for_each_cycle_below({0, 1, 2}, 10, 3,
count), 1u);
851 EXPECT_EQ(handed, 1u);
852 EXPECT_EQ(index.update(0, Infinity, 10, 3,
count), 1u);
853 EXPECT_EQ(handed, 1u);
857TEST(BoundedCycleIndex, HasLimits)
859 const Built<List_Type> built(5, complete_ends(5));
862 EXPECT_THROW(
Bounded_Cycle_Index(built.g, std::initializer_list<size_t>()), std::domain_error);
875 g.insert_arc(p, q, 0);
881TEST(BoundedCycleIndex, HoldsMoreArcsThanFitIn16Bits)
883 const size_t chain = 70000;
885 for (
size_t i = 0; i <
chain; ++i)
886 ends.emplace_back(i, i + 1);
887 const size_t base =
chain + 1;
888 for (
size_t i = 0; i < 4; ++i)
889 for (
size_t j = 0; j < 4; ++j)
891 ends.emplace_back(base + i, base + j);
892 const Built<List_Type> built(base + 4, ends);
898 expect_the_catalog(index, Oracle<List_Type>(built.g, {2, 3, 4},
Arc_List_Order()));
901 std::uniform_real_distribution<double> weight(-1, 1);
904 size_t handed_in_all = 0;
905 for (
size_t step = 0; step < 200; ++step)
907 const size_t arc =
chain +
rng() % 12;
908 const double w = weight(
rng), bound = weight(
rng);
910 index.
update(arc,
w, bound, 4, [&] (
size_t p,
double s) { handed.emplace_back(p, s); });
911 ASSERT_EQ(handed, below_of(index, arc, bound, 4));
912 handed_in_all += handed.size();
914 EXPECT_GT(handed_in_all, 100u);
916 EXPECT_EQ(index.
update(
chain - 1, -5, Infinity, 4, [] (
size_t,
double) {}), 0u);
919 [&] (
size_t p,
double s) { once.emplace_back(p, s); });
920 std::set<size_t> positions;
921 for (
const auto & [position,
sum] : once)
922 EXPECT_TRUE(positions.insert(position).second);
927TEST(BoundedCycleIndex, StaysWholeWhenTheFunctionThrows)
929 const Built<List_Type> built(4, complete_ends(4));
931 for (
size_t a = 0; a < index.
num_arcs(); ++a)
934 const auto third_throws = [&calls] (size_t, double)
937 throw std::runtime_error(
"the third one");
939 EXPECT_THROW(index.
update(0, -7, Infinity, 4, third_throws), std::runtime_error);
940 EXPECT_EQ(calls, 3u);
941 EXPECT_EQ(index.
weight(0), -7);
951 const auto count = [&handed] (size_t, double) { ++handed; };
952 std::set<size_t> of_any;
953 for (
const size_t a : {1, 2, 3})
954 for (
const size_t position : index.
cycles_of(a))
955 of_any.insert(position);
957 EXPECT_EQ(handed, of_any.size());
960 EXPECT_EQ(handed, index.
cycles_of(0).size());
965TEST(BoundedCycleIndex, StampsTheWritesOfEachArc)
968 const Built<List_Type> built(4, {{0, 1}, {1, 2}, {2, 0}, {1, 3}, {3, 0}});
970 ASSERT_EQ(index.num_cycles(), 2u);
971 EXPECT_EQ(index.clock(), 0u);
972 for (
size_t a = 0; a < 5; ++a)
973 EXPECT_EQ(index.stamp(a), 0u);
974 EXPECT_FALSE(index.is_stale(0, 0));
976 for (
size_t a = 0; a < 5; ++a)
977 index.set_weight(a, 1);
978 EXPECT_EQ(index.clock(), 5u);
979 EXPECT_EQ(index.stamp(3), 4u);
980 const uint64_t computed_at = index.clock();
981 EXPECT_FALSE(index.is_stale(0, computed_at));
982 EXPECT_FALSE(index.is_stale(1, computed_at));
984 index.update(4, 2, 0, 3, [] (
size_t,
double) {});
985 EXPECT_EQ(index.stamp(4), 6u);
986 EXPECT_FALSE(index.is_stale(0, computed_at));
987 EXPECT_TRUE(index.is_stale(1, computed_at));
988 EXPECT_FALSE(index.is_stale(1, index.clock()));
991 EXPECT_EQ(index.weight(0), 1);
992 EXPECT_EQ(index.stamp(0), 7u);
993 EXPECT_TRUE(index.is_stale(0, computed_at));
994 EXPECT_TRUE(index.is_stale(1, 6));
995 index.set_weight(0, 1);
996 EXPECT_EQ(index.clock(), 8u);
1001TEST(BoundedCycleIndex, SwapsAndCopies)
1003 const Built<List_Type> built(5, complete_ends(5));
1004 const Built<List_Type> other(3, {{0, 1}, {1, 2}, {2, 0}});
1008 static_assert(
noexcept(small.swap(large)));
1009 small.set_weight(0, 0.5);
1011 const size_t in_small = small.num_cycles(), in_large = large.
num_cycles();
1014 EXPECT_EQ(small.num_cycles(), in_large);
1016 EXPECT_EQ(small.longest_length(), 5u);
1018 EXPECT_EQ(small.weight(0), 0.25);
1019 EXPECT_EQ(large.
weight(0), 0.5);
1020 EXPECT_EQ(small.clock(), 1u);
1021 expect_the_catalog(small, Oracle<List_Type>(built.g, {3, 4, 5},
Arc_List_Order()));
1022 expect_the_catalog(large, Oracle<List_Type>(built.g, {3},
Arc_List_Order()));
1024 small.swap(triangle);
1025 EXPECT_EQ(small.num_arcs(), 3u);
1026 EXPECT_EQ(triangle.
num_arcs(), 20u);
1027 EXPECT_EQ(small.arc_number(other.arcs[1]), 1u);
1028 EXPECT_THROW((
void) small.arc_number(built.arcs[0]), std::domain_error);
1029 expect_the_catalog(triangle, Oracle<List_Type>(built.g, {3, 4, 5},
Arc_List_Order()));
1030 EXPECT_EQ(small.clock(), 0u);
1031 EXPECT_EQ(triangle.
clock(), 1u);
1032 EXPECT_EQ(triangle.
stamp(0), 1u);
1037 for (
size_t a = 0; a < index->
num_arcs(); ++a)
1038 index->
set_weight(a, a % 3 == 0 ? -1.0 : 0.25);
1039 const size_t last = index->
num_arcs() - 1;
1041 const auto keep = [&handed] (
size_t p,
double s) { handed.emplace_back(p, s); };
1042 index->
update(last, -0.5, 0, 8, keep);
1043 EXPECT_EQ(handed, below_of(*index, last, 0, 8));
1044 EXPECT_FALSE(handed.empty());
1047 std::set<size_t> of_any;
1048 for (
const size_t a : {size_t(0), last})
1049 for (
const size_t position : index->
cycles_of(a))
1050 of_any.insert(position);
1051 EXPECT_EQ(seen, of_any.size());
1053 for (
const size_t position : of_any)
1055 EXPECT_EQ(handed.size(), below);
1060 EXPECT_EQ(triangle.
weight(0), -1);
1061 EXPECT_EQ(
copy.weight(0), 9);
1062 EXPECT_EQ(
copy.clock(), triangle.
clock() + 1);
1065 EXPECT_EQ(moved.
weight(0), 9);
1066 expect_the_catalog(moved, Oracle<List_Type>(built.g, {3, 4, 5},
Arc_List_Order()));
1072TEST(BoundedCycleIndex, StartsACopyMadeInsideAQueryWithoutMarks)
1075 const Built<List_Type> built(4, {{0, 1}, {1, 0}, {2, 3}, {3, 2}});
1076 const Built<List_Type> other(3, {{0, 1}, {1, 2}, {2, 0}});
1078 ASSERT_EQ(index.num_cycles(), 2u);
1079 for (
size_t a = 0; a < 4; ++a)
1080 index.set_weight(a, -1);
1082 std::vector<Bounded_Cycle_Index<List_Type>> copies;
1085 index.for_each_cycle_below({0, 2}, 0, 2, [&] (size_t, double)
1089 copies.push_back(index);
1093 ASSERT_EQ(calls, 2u);
1094 ASSERT_EQ(copies.size(), 1u);
1100 EXPECT_EQ(
each->for_each_cycle_below({2, 1}, 0, 2, [&handed] (
size_t,
double) { ++handed; }),
1102 EXPECT_EQ(handed, 2u);
1104 EXPECT_EQ(Answers(copies[0]), Answers(index));
1105 EXPECT_EQ(Answers(assigned), Answers(index));
1111TEST(BoundedCycleIndex, LeavesAnIndexThatIsMovedFromWithoutArcsAndCycles)
1113 static_assert(std::is_nothrow_move_constructible_v<Bounded_Cycle_Index<List_Type>>);
1114 static_assert(std::is_nothrow_move_assignable_v<Bounded_Cycle_Index<List_Type>>);
1115 const Built<List_Type> built(4, complete_ends(4));
1116 const Built<List_Type> other(3, {{0, 1}, {1, 2}, {2, 0}});
1118 for (
size_t a = 0; a < index.num_arcs(); ++a)
1119 index.set_weight(a, a % 2 == 0 ? -1.0 : 0.5);
1121 const Answers before(
copy);
1122 ASSERT_GT(before.cycles.size(), 0u);
1123 ASSERT_GT(before.handed.size(), 0u);
1126 EXPECT_EQ(Answers(taken), before);
1127 expect_no_arcs_and_no_cycles(index, built.arcs[0]);
1131 triangle = std::move(taken);
1132 EXPECT_EQ(Answers(triangle), before);
1133 expect_no_arcs_and_no_cycles(taken, built.arcs[0]);
1138 expect_no_arcs_and_no_cycles(of_none, built.arcs[0]);
1139 of_none.
swap(triangle);
1140 EXPECT_EQ(Answers(of_none), before);
1141 expect_no_arcs_and_no_cycles(triangle, built.arcs[0]);
1145 EXPECT_EQ(Answers(index), before);
1146 taken = std::move(index);
1147 EXPECT_EQ(Answers(taken), before);
1148 expect_no_arcs_and_no_cycles(index, built.arcs[0]);
1150 EXPECT_EQ(
copy.weight(0), -1);
1155TEST(BoundedCycleIndex, KeepsWhatItHadWhenItIsAssignedToItself)
1157 const Built<List_Type> built(4, complete_ends(4));
1159 for (
size_t a = 0; a < index.
num_arcs(); ++a)
1160 index.
set_weight(a, a % 3 == 0 ? -2.0 : 0.25);
1161 const Answers before(index);
1164 EXPECT_EQ(Answers(index), before);
1165 index = std::move(same);
1166 EXPECT_EQ(Answers(index), before);
1168 EXPECT_EQ(Answers(index), before);
1173TEST(BoundedCycleIndex, ClearsTheMarkOfAnArcWhenTheFunctionThrowsAfterIt)
1175 const Built<List_Type> built(4, complete_ends(4));
1177 for (
size_t a = 0; a < index.
num_arcs(); ++a)
1180 const size_t of_first = index.
cycles_of(1).size();
1181 ASSERT_GT(of_first, 0u);
1183 const auto throws_at_the_second_arc = [&calls, of_first] (size_t, double)
1185 if (++calls == of_first + 1)
1186 throw std::runtime_error(
"the first cycle of the second arc");
1189 std::runtime_error);
1190 EXPECT_EQ(calls, of_first + 1);
1193 const auto count = [&handed] (size_t, double) { ++handed; };
1194 const size_t of_both = of_first + index.
cycles_of(2).size();
1196 EXPECT_EQ(handed, of_both);
1202TEST(BoundedCycleIndex, NeedsTheBoundRoundedUp)
1206 const Built<List_Type> built(1, {{0, 0}});
1208 const double approximate = 1, its_error = 0x1p-54;
1210 EXPECT_EQ(of_the_sum, 0);
1212 const auto count = [&handed] (size_t, double) { ++handed; };
1214 const double rounded_down = approximate + of_the_sum + its_error;
1215 EXPECT_EQ(rounded_down, 1);
1216 index.update(0, 1, rounded_down, 1,
count);
1217 EXPECT_EQ(handed, 0u);
1219 double bound = std::nextafter(approximate + of_the_sum, Infinity);
1220 bound = std::nextafter(bound + its_error, Infinity);
1221 index.update(0, 1, bound, 1,
count);
1222 EXPECT_EQ(handed, 1u);
1227TEST(BoundedCycleIndex, HasOnlyTheLoopsWhenOnlyTheyAreAskedFor)
1230 for (
size_t i = 0; i < 300; ++i)
1232 ends.emplace_back(0, 1);
1233 ends.emplace_back(1, 2);
1235 ends.emplace_back(i / 100, i / 100);
1237 ends.emplace_back(2, 0);
1238 const Built<List_Type> built(3, ends);
1240 expect_the_catalog(index, Oracle<List_Type>(built.g, {1},
Arc_List_Order()));
1242 for (
size_t position = 0; position < index.
num_cycles(); ++position)
1244 const size_t loop = index.
cycle_arcs(position)[0];
1245 EXPECT_EQ(built.g.get_src_node(index.
arc(loop)), built.g.get_tgt_node(index.
arc(loop)));
1246 EXPECT_EQ(as_vector(index.
cycles_of(loop)), std::vector<size_t>{position});
1253TEST(BoundedCycleIndex, BoundsTheErrorOfTheSum)
1256 EXPECT_EQ(Index::sum_error_bound(0, 100), 0);
1257 EXPECT_EQ(Index::sum_error_bound(1, 100), 0);
1258 EXPECT_GT(Index::sum_error_bound(2, 100), 0);
1259 static_assert(Index::sum_error_bound(6, 64) < 4e-13);
1260 static_assert(Index::sum_error_bound(6, 64) > 2e-13);
1262 std::mt19937_64
rng(20261115);
1263 size_t inexact = 0, kept_by_the_margin = 0;
1264 for (
size_t k = 2;
k <= 8; ++
k)
1267 for (
size_t i = 0; i <
k; ++i)
1268 ring.emplace_back(i, (i + 1) %
k);
1269 const Built<List_Type> built(
k, ring);
1270 Index index(built.g, {k});
1271 ASSERT_EQ(index.num_cycles(), 1u);
1272 for (
size_t round = 0; round < 4000; ++round)
1275 const double scale = std::ldexp(1.0,
int(
rng() % 60));
1278 double previous = 0;
1279 for (
size_t a = 0; a <
k; ++a)
1283 const double fraction = double(int64_t(
rng() >> 11)) / 9007199254740992.0 - 0.5;
1284 double w = std::ldexp(fraction,
int(
rng() % 60));
1286 w = -previous * (1 + std::ldexp(1.0, -30 -
int(
rng() % 20)));
1287 w = std::fmin(std::fmax(
w, -scale), scale);
1288 index.set_weight(a, previous =
w);
1289 largest = std::fmax(largest, std::fabs(
w));
1292 const double sum = index.weight_sum(0);
1293 const double error_bound = Index::sum_error_bound(
k, largest);
1298 inexact +=
error != 0;
1302 exact +
abs(exact) /
mpz_class(
"1000000000000000000") +
1304 const double t = threshold.
get_d();
1305 const double t_error = std::fabs(t) * 0x1p-52 + 0x1p-1000;
1307 index.for_each_cycle_below(t + t_error + error_bound,
k,
1308 [&handed] (
size_t,
double) { ++handed; });
1309 ASSERT_EQ(handed, 1u) <<
"a cycle below the threshold was lost";
1311 index.for_each_cycle_below(t,
k, [&handed] (
size_t,
double) { ++handed; });
1312 kept_by_the_margin += handed == 0;
1315 EXPECT_GT(inexact, 5000u);
1316 EXPECT_GT(kept_by_the_margin, 500u);
Index of the simple cycles of bounded length of a digraph, with a weight for each arc and a filter by...
TYPED_TEST_SUITE(ArcHeapLifecycle, ArcHeapKinds)
TYPED_TEST(BoundedCycleIndexOn, HasTheCatalogOfASearchMadeApart)
size_t size_t int32_t * out
Simple dynamic array with automatic resizing and functional operations.
T & append(const T &data)
Append a copy of data
Index of the simple cycles of bounded length of a digraph.
uint64_t clock() const noexcept
The number of writes of a weight so far: each set_weight(), update() and touch() adds one.
static constexpr double sum_error_bound(const size_t k, const double max_abs_weight) noexcept
A bound of the rounding error of the sum of the weights of a cycle.
void swap(Bounded_Cycle_Index &other) noexcept
Exchange two indexes in constant time, without failing: how to replace an index by another built apar...
size_t arc_number(const Arc *a) const
The number of an arc of the graph, in O(1) expected.
size_t length(const size_t position) const
The length of a cycle.
uint64_t stamp(const size_t arc) const
The value of clock() when an arc was last written.
bool has_length(const size_t length) const noexcept
Return true if length is one of the lengths of the index.
std::span< const uint32_t > cycles_of(const size_t arc) const
The cycles that go through an arc, by their positions, in ascending order.
double weight(const size_t arc) const
The weight of an arc.
double weight_sum(const size_t position) const
The weight of a cycle: the sum of the weights of its arcs, in the order of its canonical rotation.
size_t for_each_cycle_below(const Array< size_t > &arcs, const double bound, const size_t max_length, Op &&op)
Hand the cycles through any of some arcs whose weight is below a bound, each cycle once.
size_t num_arcs() const noexcept
The number of arcs of the graph when the index was built.
std::span< const uint32_t > cycle_arcs(const size_t position) const
The arcs of a cycle, by their numbers, in its canonical rotation: from the arc of least number,...
void touch(const size_t arc)
Mark an arc as written without changing its weight, for what the caller keeps of its cycles and depen...
bool is_stale(const size_t position, const uint64_t computed_at) const
Return true if an arc of a cycle was written after a moment: what was computed of the cycle then is o...
size_t num_cycles_up_to(const size_t max_length) const noexcept
The number of cycles whose length is at most max_length: the size of the prefix of the catalog that t...
size_t num_cycles() const noexcept
The number of cycles of the catalog.
void set_weight(const size_t arc, const double w)
Change the weight of an arc, without looking at its cycles.
Arc * arc(const size_t number) const
The arc with a number.
size_t update(const size_t arc, const double w, const double bound, const size_t max_length, Op &&op)
Change the weight of an arc and hand the cycles through it whose weight is below a bound.
size_t longest_length() const noexcept
The longest length of the set of the index.
Strict read-only view of a graph.
Generic directed graph (digraph) wrapper template.
Arc for graphs implemented with simple adjacency lists.
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Graph_Arc< Arc_Info > Arc
The node class type.
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Minimal std::expected-style result type for C++20.
ArcInfo & get_info() noexcept
Return a modifiable reference to the arc data.
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
static Tree_Node< int > * chain(size_t n)
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_abs_function > > abs(const __gmp_expr< T, U > &expr)
__gmp_expr< mpz_t, mpz_t > mpz_class
__gmp_expr< mpq_t, mpq_t > mpq_class
DynArray< Graph::Node * > nodes
DynArray< Graph::Arc * > arcs
Main namespace for Aleph-w library functions.
void each(const size_t start, const size_t end, Op &op)
Execute an operation repeatedly over a range of indices.
bool operator==(const DynList< T > &l1, const DynList< T > &l2)
Equality operator for DynList.
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
bool all(Container &container, Operation &operation)
Return true if all elements satisfy a predicate.
static void prefix(Node *root, DynList< Node * > &acc)
@ Cycle
Graph has an Eulerian cycle.
and
Check uniqueness with explicit hash + equality functors.
std::pair< First, Second > pair
Alias to std::pair kept for backwards compatibility.
void error(const char *file, int line, const char *format,...)
Print an error message with file and line info.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
Spec reversed(const Spec &s)
The spec with each arc reversed; the arcs keep their numbers.
The order of the arc iterator of the graph, as a comparison for Bounded_Cycle_Index: it tells no arc ...
Arc of graph implemented with double-linked adjacency lists.
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
Array-based graph implementation.
Strict read-only access to a graph: Const_Graph_View and the read-only iterators on the nodes,...
Generic graph and digraph implementations.
Simple graph implementation with adjacency lists.