126# ifndef BOUNDED_CYCLE_INDEX_H
127# define BOUNDED_CYCLE_INDEX_H
133# include <initializer_list>
153 [[nodiscard]]
constexpr bool operator () (
const void *,
const void *)
const noexcept
182 template <AlephGraph GT>
196 static_assert(
sizeof(size_t) >= 8,
"Bounded_Cycle_Index needs a size_t of 64 bits");
198 static constexpr uint32_t
Not_Found = std::numeric_limits<uint32_t>::max();
214 while ((
size_t(1) <<
bits) < 2 * n)
223 :
bits(std::exchange(other.bits, 0u)),
keys(std::move(other.keys)),
224 numbers(std::move(other.numbers)) {}
233 [[nodiscard]]
size_t place_of(
const void *p)
const noexcept
235 const auto key = std::uint64_t(
reinterpret_cast<std::uintptr_t
>(p));
236 return size_t((key * 0x9E3779B97F4A7C15ULL) >> (64 -
bits));
239 void put(
const void *p,
const uint32_t number)
noexcept
241 const size_t mask = (size_t(1) <<
bits) - 1;
243 while (
keys(i) !=
nullptr)
249 [[nodiscard]] uint32_t
find(
const void *p)
const noexcept
253 const size_t mask = (size_t(1) <<
bits) - 1;
254 for (
size_t i =
place_of(p);
keys(i) !=
nullptr; i = (i + 1) & mask)
262 std::swap(
bits, other.bits);
315 template <
class On_Cycle>
319 const uint32_t head = path.
arcs[0];
320 const uint32_t origin = path.
nodes[0];
326 const uint32_t target = shape.
target(
arc);
327 if (target == origin)
329 if ((shape.
lengths >> (path.
n + 1) & 1u) != 0)
332 on_cycle(path.
arcs, path.
n + 1);
338 bool revisits = target == node;
339 for (
size_t j = 1; j < path.
n and not revisits; ++j)
340 revisits = path.
nodes[j] == target;
344 path.
nodes[path.
n] = node;
346 extend(shape, path, target, on_cycle);
353 template <
class On_Cycle>
358 for (uint32_t head = 0; head <
num_arcs; ++head)
363 if ((shape.
lengths >> 1 & 1u) != 0)
364 on_cycle(path.
arcs,
size_t(1));
379 for (; first != last; ++first)
381 const size_t k = *first;
383 <<
"Bounded_Cycle_Index: the length " <<
k <<
" is not between 1 and "
391 template <
class Arc_Less>
393 const size_t max_cycles)
396 <<
"Bounded_Cycle_Index: the graph must be directed";
400 <<
"Bounded_Cycle_Index: the graph has too many nodes or arcs";
405 if ((lengths >>
k & 1u) != 0)
421 for (
typename GT::Node_Iterator it(g); it.has_curr(); it.next_ne())
422 nodes.put(it.get_curr_ne(), v++);
424 for (
typename GT::Arc_Iterator it(g); it.has_curr(); it.next_ne())
426 Arc *a = it.get_curr_ne();
433 if (
less(a.arc, b.arc))
435 return not
less(b.arc, a.arc)
and a.turn < b.turn;
444 for (uint32_t a = 0; a < E; ++a)
448 shape.
source(a) = records(a).source;
449 shape.
target(a) = records(a).target;
452 for (
size_t v = 1; v <=
V; ++v)
456 for (uint32_t a = 0; a < E; ++a)
466 size_t num_members = 0;
471 <<
"Bounded_Cycle_Index: the graph has more than " << max_cycles
472 <<
" cycles of the lengths given";
475 <<
"Bounded_Cycle_Index: the cycles have too many arcs";
477 for (
size_t j = 0; j <
k; ++j)
481 size_t next_position[
Stride] = {};
482 for (
size_t k = 1, position = 0,
arc = 0;
k <
Stride; ++
k)
493 size_t block_size = 0;
495 for (
size_t a = 0; a < E; ++a)
504 block_size += size_t(n) *
k;
516 for (
size_t j = 0; j <
k; ++j)
522 const bool is_narrow = E <= size_t(1) << 16;
528 for (
size_t a = 0; a < E; ++a)
537 for (
size_t i = 0; i <
k; ++i)
539 const uint32_t a = cycle[i];
540 members_(next_member(a)++) = uint32_t(position);
541 size_t & at = next_in_block(a);
542 for (
size_t j = 0; j <
k; ++j, ++at)
544 narrow_(at) = uint16_t(cycle[j]);
546 wide_(at) = cycle[j];
556 [[nodiscard]]
size_t length_of(
const size_t position)
const noexcept
565 [[nodiscard]]
size_t first_arc_of(
const size_t position,
const size_t k)
const noexcept
573 <<
"Bounded_Cycle_Index: there is no arc with the number " <<
arc;
580 <<
"Bounded_Cycle_Index: there is no cycle at the position " << position;
587 <<
"Bounded_Cycle_Index: a weight is a number or plus infinity";
592 ah_domain_error_if(std::isnan(bound)) <<
"Bounded_Cycle_Index: the bound is not a number";
603 [[nodiscard]]
double sum_at(
const size_t first,
const size_t k)
const noexcept
606 for (
size_t j = 1; j <
k; ++j)
615 template <
class Number,
class Op>
617 const size_t max_length,
const bool skip, Op & op)
const
621 const double *weights = &
weight_(0);
622 const unsigned char *done = &
done_(0);
624 for (
size_t k = 1;
k <= max_length; ++
k)
626 const size_t n = member[
k + 1] - member[
k];
629 const Number *o = &block(first[
k]);
630 const uint32_t *position = &
members_(member[
k]);
632 for (
size_t c = 0; c < n; ++c, o +=
k)
634 double sum = weights[o[0]];
635 unsigned char seen_before = done[o[0]];
636 for (
size_t j = 1; j <
k; ++j)
638 sum += weights[o[j]];
639 seen_before |= done[o[j]];
641 if (seen_before != 0)
644 if (
sum < bound) [[unlikely]]
645 op(
size_t(position[c]),
sum);
649 for (
size_t c = 0; c < n; ++c, o +=
k)
651 double sum = weights[o[0]];
652 for (
size_t j = 1; j <
k; ++j)
653 sum += weights[o[j]];
654 if (
sum < bound) [[unlikely]]
655 op(
size_t(position[c]),
sum);
664 size_t look_at(
const size_t arc,
const double bound,
const size_t max_length,
665 const bool skip, Op & op)
const
681 for (
size_t i = 0; i <
n; ++i)
689 const size_t max_length, Op & op)
692 for (
size_t i = 0; i < n; ++i)
697 for (
size_t i = 0; i < n; ++i)
701 seen +=
look_at(
arcs[i], bound, longest, i > 0, op);
728 template <
class Arc_Less>
774 template <
class Arc_Less>
780 for (
size_t i = 0; i < lengths.
size(); ++i)
843 lengths_(std::exchange(other.lengths_, uint32_t(0))),
844 longest_(std::exchange(other.longest_,
size_t(0))),
849 wide_(std::move(other.wide_)),
weight_(std::move(other.weight_)),
850 stamp_(std::move(other.stamp_)),
clock_(std::exchange(other.clock_, uint64_t(0))),
851 done_(std::move(other.done_))
856 arcs_before_[
k] = std::exchange(other.arcs_before_[
k],
size_t(0));
905 <<
"Bounded_Cycle_Index: the arc is not of the indexed graph";
958 [[nodiscard]]
size_t length(
const size_t position)
const
973 [[nodiscard]] std::span<const uint32_t>
cycle_arcs(
const size_t position)
const
989 [[nodiscard]] std::span<const uint32_t>
cycles_of(
const size_t arc)
const
993 return n == 0 ? std::span<const uint32_t>() : std::span<const uint32_t>(&
members_(first), n);
1061 requires std::invocable<Op &, size_t, double>
1062 size_t update(
const size_t arc,
const double w,
const double bound,
1063 const size_t max_length, Op && op)
1098 requires std::invocable<Op &, size_t, double>
1100 const size_t max_length, Op && op)
1125 requires std::invocable<Op &, size_t, double>
1127 const size_t max_length, Op && op)
1150 requires std::invocable<Op &, size_t, double>
1155 for (
size_t k = 1;
k <= longest; ++
k)
1175 const double max_abs_weight)
noexcept
1183 const double u = 0x1p-53;
1184 const auto additions = double(
k - 1);
1185 return additions * u / (1 - additions * u) *
double(
k) * max_abs_weight * (1 + 0x1p-40);
1222 [[nodiscard]]
bool is_stale(
const size_t position,
const uint64_t computed_at)
const
1243 std::swap(
lengths_, other.lengths_);
1244 std::swap(
longest_, other.longest_);
1256 std::swap(
clock_, other.clock_);
C++20 concepts hub: comparison, BST policy, and Aleph container concepts.
Exception handling system with formatted messages for Aleph-w.
#define ah_length_error_if(C)
Throws std::length_error if condition holds.
#define ah_out_of_range_error_if(C)
Throws std::out_of_range if condition holds.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
C++20 concepts for the protocol shared by graph algorithms.
Simple dynamic array with automatic resizing and functional operations.
static Array create_reserved(const size_t cap)
Build an empty array with room for at least cap items.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
void swap(Array &s) noexcept
Swap this with s
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.
size_t first_arc_of(const size_t position, const size_t k) const noexcept
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.
Bounded_Cycle_Index & operator=(const Bounded_Cycle_Index &other)
Make the index a copy of another.
static void check_bound(const double bound)
size_t num_cycles(const size_t length) const noexcept
The number of cycles of a length.
void swap(Bounded_Cycle_Index &other) noexcept
Exchange two indexes in constant time, without failing: how to replace an index by another built apar...
Bounded_Cycle_Index(const Bounded_Cycle_Index &other)
Copy an index: its catalog, its weights, its stamps and its clock.
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.
Bounded_Cycle_Index(const GT &g, const Array< size_t > &lengths, const size_t max_cycles=Default_Max_Cycles)
Build the index of the cycles of g with the lengths of an array, with the arcs in the order of the ar...
static constexpr size_t Default_Max_Cycles
The number of cycles that an index holds, when no other is given.
Array< uint32_t > group_member_
static constexpr size_t Stride
uint64_t stamp(const size_t arc) const
The value of clock() when an arc was last written.
size_t look_at_each(const size_t *arcs, const size_t n, const double bound, const size_t max_length, Op &op)
Number_Table arc_numbers_
static uint32_t length_bits(It first, const It last)
Array< uint32_t > members_
size_t for_each_cycle_below(const double bound, const size_t max_length, Op &&op) const
Hand the cycles of the catalog whose weight is below a bound.
size_t longest_up_to(const size_t max_length) const noexcept
size_t length_first_[Stride]
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.
size_t checked_position(const size_t position) const
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.
Array< unsigned char > done_
size_t checked_arc(const size_t arc) const
Bounded_Cycle_Index(Bounded_Cycle_Index &&other) noexcept
Take the index of another, in constant time and without failing.
size_t look_at(const size_t arc, const double bound, const size_t max_length, const bool skip, Op &op) const
double sum_at(const size_t first, const size_t k) const noexcept
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.
Array< uint16_t > narrow_
Array< uint32_t > cycle_arcs_
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,...
Bounded_Cycle_Index(const GT &g, std::initializer_list< size_t > lengths, const size_t max_cycles=Default_Max_Cycles)
Build the index of the cycles of g with the lengths given, with the arcs in the order of the arc iter...
size_t for_each_cycle_below(std::initializer_list< size_t > arcs, const double bound, const size_t max_length, Op &&op)
Hand the cycles through any of the arcs of a list whose weight is below a bound, each cycle once.
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...
static constexpr size_t Max_Length
The longest length of a cycle that an index can hold.
static void enumerate(const Shape &shape, On_Cycle &&on_cycle)
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...
Array< uint32_t > member_first_
size_t arcs_before_[Stride]
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 build(const GT &g, const uint32_t lengths, Arc_Less &less, const size_t max_cycles)
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.
static constexpr uint32_t Not_Found
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.
Bounded_Cycle_Index(const GT &g, std::initializer_list< size_t > lengths, Arc_Less less, const size_t max_cycles=Default_Max_Cycles)
Build the index of the cycles of g with the lengths given, with the arcs in the order of a comparison...
typename GT::Arc Arc
The type of the arcs, const on a view.
static void extend(const Shape &shape, Path &path, const uint32_t node, On_Cycle &on_cycle)
Bounded_Cycle_Index(const GT &g, const Array< size_t > &lengths, Arc_Less less, const size_t max_cycles=Default_Max_Cycles)
Build the index of the cycles of g with the lengths of an array, with the arcs in the order of a comp...
static void check_weight(const double w)
size_t length_of(const size_t position) const noexcept
size_t look_at(const Array< Number > &block, const size_t arc, const double bound, const size_t max_length, const bool skip, Op &op) const
Array< size_t > group_first_
Graph_Arc< Arc_Info > Arc
The node class type.
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
bool is_digraph() const noexcept
Return true if the graph this is directed.
constexpr size_t get_num_arcs() const noexcept
Return the number of arcs of the graph.
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
Strict weak ordering constraint for BST comparators.
DynArray< Graph::Node * > nodes
DynArray< Graph::Arc * > arcs
Main namespace for Aleph-w library functions.
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
constexpr Bit_Column bits(const unsigned n)
Column of n bits without sign: the integers of [0, 2^n - 1].
and
Check uniqueness with explicit hash + equality functors.
void next()
Advance all underlying iterators (bounds-checked).
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.
void quicksort_op(C< T > &a, const Compare &cmp=Compare(), const size_t threshold=Quicksort_Threshold)
Optimized quicksort for containers using operator().
The order of the arc iterator of the graph, as a comparison for Bounded_Cycle_Index: it tells no arc ...
constexpr bool operator()(const void *, const void *) const noexcept
Return false: no arc goes before another.
Array< unsigned char > & done
Number_Table(Number_Table &&other) noexcept
Number_Table & operator=(Number_Table other) noexcept
size_t place_of(const void *p) const noexcept
Number_Table(const size_t n)
Array< const void * > keys
uint32_t find(const void *p) const noexcept
void swap(Number_Table &other) noexcept
void put(const void *p, const uint32_t number) noexcept
Number_Table(const Number_Table &)=default
Array< uint32_t > numbers
uint32_t arcs[Max_Length]
uint32_t nodes[Max_Length]
Array< uint32_t > out_arcs
Array< uint32_t > out_first
Dynamic array container with automatic resizing.
Generic graph and digraph implementations.
Comprehensive sorting algorithms and search utilities for Aleph-w.