Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Bounded_Cycle_Index.H
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
126# ifndef BOUNDED_CYCLE_INDEX_H
127# define BOUNDED_CYCLE_INDEX_H
128
129# include <cmath>
130# include <concepts>
131# include <cstddef>
132# include <cstdint>
133# include <initializer_list>
134# include <limits>
135# include <span>
136# include <utility>
137
138# include <ah-concepts.H>
139# include <ah-errors.H>
140# include <ah-graph-concepts.H>
141# include <tpl_array.H>
142# include <tpl_graph.H>
143# include <tpl_sort_utils.H>
144
145namespace Aleph
146{
151 {
153 [[nodiscard]] constexpr bool operator () (const void *, const void *) const noexcept
154 {
155 return false;
156 }
157 };
158
182 template <AlephGraph GT>
184 {
185 public:
186 using Arc = typename GT::Arc;
187
189 static constexpr size_t Max_Length = 8;
190
192 static constexpr size_t Default_Max_Cycles = size_t(1) << 24;
193
194 private:
195 // The counts of the build are checked against 2^32 in a size_t
196 static_assert(sizeof(size_t) >= 8, "Bounded_Cycle_Index needs a size_t of 64 bits");
197
198 static constexpr uint32_t Not_Found = std::numeric_limits<uint32_t>::max();
199 static constexpr size_t Stride = Max_Length + 2; // entries of an arc, by length
200
201 // Numbers by address: an open table with linear probing, as that of
202 // In_Arcs_Index, with 2^bits places, at least twice the number of keys.
203 // A table that was moved from has no place: its bits are 0.
205 {
206 unsigned bits = 1;
209
210 Number_Table() = default;
211
212 explicit Number_Table(const size_t n)
213 {
214 while ((size_t(1) << bits) < 2 * n)
215 ++bits;
216 keys = Array<const void *>(size_t(1) << bits, nullptr);
217 numbers = Array<uint32_t>(size_t(1) << bits, uint32_t(0));
218 }
219
220 Number_Table(const Number_Table &) = default;
221
222 Number_Table(Number_Table && other) noexcept
223 : bits(std::exchange(other.bits, 0u)), keys(std::move(other.keys)),
224 numbers(std::move(other.numbers)) {}
225
226 // By value: the table is copied or moved apart, and then exchanged
228 {
229 swap(other);
230 return *this;
231 }
232
233 [[nodiscard]] size_t place_of(const void *p) const noexcept
234 {
235 const auto key = std::uint64_t(reinterpret_cast<std::uintptr_t>(p));
236 return size_t((key * 0x9E3779B97F4A7C15ULL) >> (64 - bits));
237 }
238
239 void put(const void *p, const uint32_t number) noexcept
240 {
241 const size_t mask = (size_t(1) << bits) - 1;
242 size_t i = place_of(p);
243 while (keys(i) != nullptr)
244 i = (i + 1) & mask;
245 keys(i) = p;
246 numbers(i) = number;
247 }
248
249 [[nodiscard]] uint32_t find(const void *p) const noexcept
250 {
251 if (bits == 0)
252 return Not_Found;
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)
255 if (keys(i) == p)
256 return numbers(i);
257 return Not_Found;
258 }
259
260 void swap(Number_Table & other) noexcept
261 {
262 std::swap(bits, other.bits);
263 keys.swap(other.keys);
264 numbers.swap(other.numbers);
265 }
266 };
267
268 // The graph while the index is built: by the number of an arc, its
269 // ends, and by the number of a node, the arcs that leave it, in
270 // ascending order of their numbers
280
281 // A simple path from the source of its first arc
282 struct Path
283 {
284 uint32_t arcs[Max_Length];
285 uint32_t nodes[Max_Length]; // nodes[i]: the source of arcs[i]
286 size_t n = 0;
287 };
288
289 Number_Table arc_numbers_; // by the address of an arc
290 Array<Arc *> arcs_; // by number
291 uint32_t lengths_ = 0; // bit k: the length k is in the set
292 size_t longest_ = 0;
293 size_t length_first_[Stride] = {}; // by length, its first position
294 size_t arcs_before_[Stride] = {}; // by length, where its cycles start in cycle_arcs_
295 Array<uint32_t> cycle_arcs_; // the numbers of the arcs of each cycle
296 Array<uint32_t> member_first_; // by arc, in members_
297 Array<uint32_t> members_; // the positions of the cycles of each arc, ascending
298
299 // For each arc and each length, the cycles of the arc with that
300 // length: where their positions start in members_, and where their
301 // arcs start in the blocks. The cycles of an arc are contiguous in
302 // the blocks, each with all its arcs in its canonical rotation.
305 Array<uint16_t> narrow_; // the blocks, if the numbers of the arcs fit in 16 bits
306 Array<uint32_t> wide_; // and if they do not
307
309 Array<uint64_t> stamp_; // by arc, the value of clock_ when it was last written
310 uint64_t clock_ = 0;
311 Array<unsigned char> done_; // by arc: for_each_cycle_below() has gone through it
312
313 // Extend the path from `node`, its end, by the arcs after its first
314 // one, and hand each cycle that closes with a length of the set
315 template <class On_Cycle>
316 static void extend(const Shape & shape, Path & path, const uint32_t node,
317 On_Cycle & on_cycle)
318 {
319 const uint32_t head = path.arcs[0];
320 const uint32_t origin = path.nodes[0];
321 for (uint32_t i = shape.out_first(node); i < shape.out_first(node + 1); ++i)
322 {
323 const uint32_t arc = shape.out_arcs(i);
324 if (arc <= head)
325 continue;
326 const uint32_t target = shape.target(arc);
327 if (target == origin)
328 {
329 if ((shape.lengths >> (path.n + 1) & 1u) != 0)
330 {
331 path.arcs[path.n] = arc;
332 on_cycle(path.arcs, path.n + 1);
333 }
334 continue;
335 }
336 if (path.n + 2 > shape.longest)
337 continue;
338 bool revisits = target == node; // a loop
339 for (size_t j = 1; j < path.n and not revisits; ++j)
340 revisits = path.nodes[j] == target;
341 if (revisits)
342 continue;
343 path.arcs[path.n] = arc;
344 path.nodes[path.n] = node;
345 ++path.n;
346 extend(shape, path, target, on_cycle);
347 --path.n;
348 }
349 }
350
351 // Hand each cycle once, in its canonical rotation: with each arc as
352 // the first one, in order, the cycles come out in lexicographic order
353 template <class On_Cycle>
354 static void enumerate(const Shape & shape, On_Cycle && on_cycle)
355 {
356 Path path;
357 const auto num_arcs = uint32_t(shape.source.size());
358 for (uint32_t head = 0; head < num_arcs; ++head)
359 {
360 path.arcs[0] = head;
361 if (shape.source(head) == shape.target(head))
362 {
363 if ((shape.lengths >> 1 & 1u) != 0)
364 on_cycle(path.arcs, size_t(1));
365 continue;
366 }
367 if (shape.longest == 1)
368 continue; // only loops are asked for: there is no path to extend
369 path.nodes[0] = shape.source(head);
370 path.n = 1;
371 extend(shape, path, shape.target(head), on_cycle);
372 }
373 }
374
375 template <class It>
376 static uint32_t length_bits(It first, const It last)
377 {
378 uint32_t bits = 0;
379 for (; first != last; ++first)
380 {
381 const size_t k = *first;
382 ah_domain_error_if(k < 1 or k > Max_Length)
383 << "Bounded_Cycle_Index: the length " << k << " is not between 1 and "
384 << Max_Length;
385 bits |= 1u << k;
386 }
387 ah_domain_error_if(bits == 0) << "Bounded_Cycle_Index: no length of cycle";
388 return bits;
389 }
390
391 template <class Arc_Less>
392 void build(const GT & g, const uint32_t lengths, Arc_Less & less,
393 const size_t max_cycles)
394 {
396 << "Bounded_Cycle_Index: the graph must be directed";
397 const size_t V = g.get_num_nodes();
398 const size_t E = g.get_num_arcs();
400 << "Bounded_Cycle_Index: the graph has too many nodes or arcs";
401
402 Shape shape;
403 shape.lengths = lengths_ = lengths;
404 for (size_t k = 1; k <= Max_Length; ++k)
405 if ((lengths >> k & 1u) != 0)
406 shape.longest = longest_ = k;
407
408 // The arcs with their ends, read from the iterator, in the order
409 // of `less` and, where it does not decide, in that of the iterator
410 struct Record
411 {
412 Arc *arc = nullptr;
413 uint32_t source = 0;
414 uint32_t target = 0;
415 uint32_t turn = 0;
416 };
418 {
420 uint32_t v = 0;
421 for (typename GT::Node_Iterator it(g); it.has_curr(); it.next_ne())
422 nodes.put(it.get_curr_ne(), v++);
423 uint32_t turn = 0;
424 for (typename GT::Arc_Iterator it(g); it.has_curr(); it.next_ne())
425 {
426 Arc *a = it.get_curr_ne();
427 records.append(Record{a, nodes.find(g.get_src_node(a)),
428 nodes.find(g.get_tgt_node(a)), turn++});
429 }
430 }
431 quicksort_op(records, [&less] (const Record & a, const Record & b)
432 {
433 if (less(a.arc, b.arc))
434 return true;
435 return not less(b.arc, a.arc) and a.turn < b.turn;
436 });
437
440 shape.source = Array<uint32_t>(E, uint32_t(0));
441 shape.target = Array<uint32_t>(E, uint32_t(0));
442 shape.out_first = Array<uint32_t>(V + 1, uint32_t(0));
443 shape.out_arcs = Array<uint32_t>(E, uint32_t(0));
444 for (uint32_t a = 0; a < E; ++a)
445 {
446 arcs_.append(records(a).arc);
447 arc_numbers_.put(records(a).arc, a);
448 shape.source(a) = records(a).source;
449 shape.target(a) = records(a).target;
450 ++shape.out_first(shape.source(a) + 1);
451 }
452 for (size_t v = 1; v <= V; ++v)
453 shape.out_first(v) += shape.out_first(v - 1);
454 {
455 Array<uint32_t> next(V, uint32_t(0));
456 for (uint32_t a = 0; a < E; ++a) // in ascending order of number
457 shape.out_arcs(shape.out_first(shape.source(a)) + next(shape.source(a))++) = a;
458 }
459
460 // First the cycles are counted, by length and by arc, so that the
461 // index takes the memory that it needs and no more, and so that a
462 // graph with more cycles than the limit takes none
463 group_member_ = Array<uint32_t>(E * Stride, uint32_t(0));
464 size_t count[Stride] = {};
465 size_t num_cycles = 0;
466 size_t num_members = 0;
467 enumerate(shape, [&] (const uint32_t *arcs, const size_t k)
468 {
469 ++num_cycles;
470 ah_length_error_if(num_cycles > max_cycles)
471 << "Bounded_Cycle_Index: the graph has more than " << max_cycles
472 << " cycles of the lengths given";
473 num_members += k;
474 ah_length_error_if(num_members >= Not_Found)
475 << "Bounded_Cycle_Index: the cycles have too many arcs";
476 ++count[k];
477 for (size_t j = 0; j < k; ++j)
478 ++group_member_(arcs[j] * Stride + k);
479 });
480
481 size_t next_position[Stride] = {};
482 for (size_t k = 1, position = 0, arc = 0; k < Stride; ++k)
483 {
484 length_first_[k] = next_position[k] = position;
485 arcs_before_[k] = arc;
486 position += count[k];
487 arc += count[k] * k;
488 }
489
490 // by arc and length, the number of cycles becomes where they start
491 member_first_ = Array<uint32_t>(E + 1, uint32_t(0));
492 group_first_ = Array<size_t>(E * Stride, size_t(0));
493 size_t block_size = 0;
494 uint32_t member = 0;
495 for (size_t a = 0; a < E; ++a)
496 {
497 member_first_(a) = member;
498 for (size_t k = 0; k < Stride; ++k)
499 {
500 const uint32_t n = group_member_(a * Stride + k);
501 group_member_(a * Stride + k) = member;
502 group_first_(a * Stride + k) = block_size;
503 member += n;
504 block_size += size_t(n) * k;
505 }
506 }
507 if (E > 0)
508 member_first_(E) = member;
509
510 // Then they are written, each in its position: those of a length
511 // come out in order
512 cycle_arcs_ = Array<uint32_t>(num_members, uint32_t(0));
513 enumerate(shape, [&] (const uint32_t *arcs, const size_t k)
514 {
515 const size_t first = arcs_before_[k] + (next_position[k]++ - length_first_[k]) * k;
516 for (size_t j = 0; j < k; ++j)
517 cycle_arcs_(first + j) = arcs[j];
518 });
519
520 // and, by ascending position, each one goes to its arcs
521 members_ = Array<uint32_t>(num_members, uint32_t(0));
522 const bool is_narrow = E <= size_t(1) << 16;
523 narrow_ = Array<uint16_t>(is_narrow ? block_size : 0, uint16_t(0));
524 wide_ = Array<uint32_t>(is_narrow ? 0 : block_size, uint32_t(0));
525 {
528 for (size_t a = 0; a < E; ++a)
529 {
530 next_member.append(member_first_(a));
531 next_in_block.append(group_first_(a * Stride));
532 }
533 for (size_t k = 1; k <= Max_Length; ++k)
534 for (size_t position = length_first_[k]; position < length_first_[k + 1]; ++position)
535 {
536 const uint32_t *cycle = &cycle_arcs_(first_arc_of(position, k));
537 for (size_t i = 0; i < k; ++i)
538 {
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)
543 if (is_narrow)
544 narrow_(at) = uint16_t(cycle[j]);
545 else
546 wide_(at) = cycle[j];
547 }
548 }
549 }
550
551 weight_ = Array<double>(E, std::numeric_limits<double>::infinity());
552 stamp_ = Array<uint64_t>(E, uint64_t(0));
553 done_ = Array<unsigned char>(E, static_cast<unsigned char>(0));
554 }
555
556 [[nodiscard]] size_t length_of(const size_t position) const noexcept
557 {
558 size_t k = 1;
559 while (position >= length_first_[k + 1])
560 ++k;
561 return k;
562 }
563
564 // Where the arcs of the cycle at `position`, of length k, start
565 [[nodiscard]] size_t first_arc_of(const size_t position, const size_t k) const noexcept
566 {
567 return arcs_before_[k] + (position - length_first_[k]) * k;
568 }
569
570 [[nodiscard]] size_t checked_arc(const size_t arc) const
571 {
573 << "Bounded_Cycle_Index: there is no arc with the number " << arc;
574 return arc;
575 }
576
577 [[nodiscard]] size_t checked_position(const size_t position) const
578 {
580 << "Bounded_Cycle_Index: there is no cycle at the position " << position;
581 return position;
582 }
583
584 static void check_weight(const double w)
585 {
586 ah_domain_error_if(std::isnan(w) or w == -std::numeric_limits<double>::infinity())
587 << "Bounded_Cycle_Index: a weight is a number or plus infinity";
588 }
589
590 static void check_bound(const double bound)
591 {
592 ah_domain_error_if(std::isnan(bound)) << "Bounded_Cycle_Index: the bound is not a number";
593 }
594
595 // The longest length of the set that is not over max_length
596 [[nodiscard]] size_t longest_up_to(const size_t max_length) const noexcept
597 {
598 return max_length < longest_ ? max_length : longest_;
599 }
600
601 // The sum of a cycle of k arcs from `first`: its first weight, plus
602 // each of the others in order. look_at() adds the same way.
603 [[nodiscard]] double sum_at(const size_t first, const size_t k) const noexcept
604 {
605 double sum = weight_(cycle_arcs_(first));
606 for (size_t j = 1; j < k; ++j)
607 sum += weight_(cycle_arcs_(first + j));
608 return sum;
609 }
610
611 // The cycles of `arc` whose length is at most max_length: each sum,
612 // from the block of the arc, and `op` on those below the bound.
613 // With `skip`, a cycle through an arc that is marked in done_ is not
614 // looked at. Returns how many cycles were looked at.
615 template <class Number, class Op>
616 size_t look_at(const Array<Number> & block, const size_t arc, const double bound,
617 const size_t max_length, const bool skip, Op & op) const
618 {
619 const size_t *first = &group_first_(arc * Stride);
620 const uint32_t *member = &group_member_(arc * Stride);
621 const double *weights = &weight_(0);
622 const unsigned char *done = &done_(0);
623 size_t seen = 0;
624 for (size_t k = 1; k <= max_length; ++k)
625 {
626 const size_t n = member[k + 1] - member[k];
627 if (n == 0)
628 continue;
629 const Number *o = &block(first[k]);
630 const uint32_t *position = &members_(member[k]);
631 if (skip)
632 for (size_t c = 0; c < n; ++c, o += k)
633 {
634 double sum = weights[o[0]];
635 unsigned char seen_before = done[o[0]];
636 for (size_t j = 1; j < k; ++j)
637 {
638 sum += weights[o[j]];
639 seen_before |= done[o[j]];
640 }
641 if (seen_before != 0)
642 continue;
643 ++seen;
644 if (sum < bound) [[unlikely]]
645 op(size_t(position[c]), sum);
646 }
647 else
648 {
649 for (size_t c = 0; c < n; ++c, o += k)
650 {
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);
656 }
657 seen += n;
658 }
659 }
660 return seen;
661 }
662
663 template <class Op>
664 size_t look_at(const size_t arc, const double bound, const size_t max_length,
665 const bool skip, Op & op) const
666 {
667 return narrow_.size() != 0 or wide_.size() == 0
668 ? look_at(narrow_, arc, bound, max_length, skip, op)
669 : look_at(wide_, arc, bound, max_length, skip, op);
670 }
671
672 // Clears the marks of for_each_cycle_below(), also if `op` throws
673 struct Marks
674 {
676 const size_t *arcs;
677 size_t n;
678
680 {
681 for (size_t i = 0; i < n; ++i)
682 if (arcs[i] < done.size())
683 done(arcs[i]) = 0;
684 }
685 };
686
687 template <class Op>
688 size_t look_at_each(const size_t *arcs, const size_t n, const double bound,
689 const size_t max_length, Op & op)
690 {
691 check_bound(bound);
692 for (size_t i = 0; i < n; ++i)
693 (void) checked_arc(arcs[i]);
694 const size_t longest = longest_up_to(max_length);
695 Marks marks{done_, arcs, n};
696 size_t seen = 0;
697 for (size_t i = 0; i < n; ++i)
698 {
699 if (done_(arcs[i]) != 0)
700 continue; // given again: all its cycles have it marked
701 seen += look_at(arcs[i], bound, longest, i > 0, op);
702 done_(arcs[i]) = 1;
703 }
704 return seen;
705 }
706
707 public:
728 template <class Arc_Less>
730 Bounded_Cycle_Index(const GT & g, std::initializer_list<size_t> lengths, Arc_Less less,
731 const size_t max_cycles = Default_Max_Cycles)
732 {
733 build(g, length_bits(lengths.begin(), lengths.end()), less, max_cycles);
734 }
735
751 Bounded_Cycle_Index(const GT & g, std::initializer_list<size_t> lengths,
752 const size_t max_cycles = Default_Max_Cycles)
753 : Bounded_Cycle_Index(g, lengths, Arc_List_Order(), max_cycles) {}
754
774 template <class Arc_Less>
776 Bounded_Cycle_Index(const GT & g, const Array<size_t> & lengths, Arc_Less less,
777 const size_t max_cycles = Default_Max_Cycles)
778 {
779 uint32_t bits = 0;
780 for (size_t i = 0; i < lengths.size(); ++i)
781 bits |= length_bits(&lengths(i), &lengths(i) + 1);
782 ah_domain_error_if(bits == 0) << "Bounded_Cycle_Index: no length of cycle";
783 build(g, bits, less, max_cycles);
784 }
785
802 Bounded_Cycle_Index(const GT & g, const Array<size_t> & lengths,
803 const size_t max_cycles = Default_Max_Cycles)
804 : Bounded_Cycle_Index(g, lengths, Arc_List_Order(), max_cycles) {}
805
819 : arc_numbers_(other.arc_numbers_), arcs_(other.arcs_), lengths_(other.lengths_),
823 narrow_(other.narrow_), wide_(other.wide_), weight_(other.weight_),
824 stamp_(other.stamp_), clock_(other.clock_),
825 done_(other.done_.size(), static_cast<unsigned char>(0)) // no mark of a query
826 {
827 for (size_t k = 0; k < Stride; ++k)
828 {
829 length_first_[k] = other.length_first_[k];
830 arcs_before_[k] = other.arcs_before_[k];
831 }
832 }
833
842 : arc_numbers_(std::move(other.arc_numbers_)), arcs_(std::move(other.arcs_)),
843 lengths_(std::exchange(other.lengths_, uint32_t(0))),
844 longest_(std::exchange(other.longest_, size_t(0))),
845 cycle_arcs_(std::move(other.cycle_arcs_)),
846 member_first_(std::move(other.member_first_)), members_(std::move(other.members_)),
847 group_member_(std::move(other.group_member_)),
848 group_first_(std::move(other.group_first_)), narrow_(std::move(other.narrow_)),
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_))
852 {
853 for (size_t k = 0; k < Stride; ++k)
854 {
855 length_first_[k] = std::exchange(other.length_first_[k], size_t(0));
856 arcs_before_[k] = std::exchange(other.arcs_before_[k], size_t(0));
857 }
858 }
859
873 {
874 if (this != &other)
875 Bounded_Cycle_Index(other).swap(*this);
876 return *this;
877 }
878
887 {
888 Bounded_Cycle_Index(std::move(other)).swap(*this);
889 return *this;
890 }
891
893 [[nodiscard]] size_t num_arcs() const noexcept { return arcs_.size(); }
894
901 [[nodiscard]] size_t arc_number(const Arc *a) const
902 {
903 const uint32_t number = arc_numbers_.find(a);
905 << "Bounded_Cycle_Index: the arc is not of the indexed graph";
906 return number;
907 }
908
915 [[nodiscard]] Arc * arc(const size_t number) const { return arcs_(checked_arc(number)); }
916
918 [[nodiscard]] size_t num_cycles() const noexcept { return length_first_[Max_Length + 1]; }
919
926 [[nodiscard]] size_t num_cycles(const size_t length) const noexcept
927 {
928 return length < 1 or length > Max_Length
930 }
931
938 [[nodiscard]] size_t num_cycles_up_to(const size_t max_length) const noexcept
939 {
940 return length_first_[(max_length < Max_Length ? max_length : Max_Length) + 1];
941 }
942
944 [[nodiscard]] bool has_length(const size_t length) const noexcept
945 {
946 return length >= 1 and length <= Max_Length and (lengths_ >> length & 1u) != 0;
947 }
948
950 [[nodiscard]] size_t longest_length() const noexcept { return longest_; }
951
958 [[nodiscard]] size_t length(const size_t position) const
959 {
960 return length_of(checked_position(position));
961 }
962
973 [[nodiscard]] std::span<const uint32_t> cycle_arcs(const size_t position) const
974 {
975 const size_t k = length_of(checked_position(position));
976 return {&cycle_arcs_(first_arc_of(position, k)), k};
977 }
978
989 [[nodiscard]] std::span<const uint32_t> cycles_of(const size_t arc) const
990 {
991 const size_t first = member_first_(checked_arc(arc));
992 const size_t n = member_first_(arc + 1) - first;
993 return n == 0 ? std::span<const uint32_t>() : std::span<const uint32_t>(&members_(first), n);
994 }
995
1005 void set_weight(const size_t arc, const double w)
1006 {
1007 check_weight(w);
1009 stamp_(arc) = ++clock_;
1010 }
1011
1018 [[nodiscard]] double weight(const size_t arc) const { return weight_(checked_arc(arc)); }
1019
1030 [[nodiscard]] double weight_sum(const size_t position) const
1031 {
1032 const size_t k = length_of(checked_position(position));
1033 return sum_at(first_arc_of(position, k), k);
1034 }
1035
1060 template <class Op>
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)
1064 {
1065 check_bound(bound);
1066 set_weight(arc, w);
1067 return look_at(arc, bound, longest_up_to(max_length), false, op);
1068 }
1069
1097 template <class Op>
1098 requires std::invocable<Op &, size_t, double>
1099 size_t for_each_cycle_below(const Array<size_t> & arcs, const double bound,
1100 const size_t max_length, Op && op)
1101 {
1102 return look_at_each(arcs.size() == 0 ? nullptr : &arcs(0), arcs.size(), bound,
1103 max_length, op);
1104 }
1105
1124 template <class Op>
1125 requires std::invocable<Op &, size_t, double>
1126 size_t for_each_cycle_below(std::initializer_list<size_t> arcs, const double bound,
1127 const size_t max_length, Op && op)
1128 {
1129 return look_at_each(arcs.begin(), arcs.size(), bound, max_length, op);
1130 }
1131
1149 template <class Op>
1150 requires std::invocable<Op &, size_t, double>
1151 size_t for_each_cycle_below(const double bound, const size_t max_length, Op && op) const
1152 {
1153 check_bound(bound);
1154 const size_t longest = longest_up_to(max_length);
1155 for (size_t k = 1; k <= longest; ++k)
1156 for (size_t position = length_first_[k]; position < length_first_[k + 1]; ++position)
1157 if (const double sum = sum_at(first_arc_of(position, k), k); sum < bound)
1158 op(position, sum);
1159 return length_first_[longest + 1];
1160 }
1161
1174 [[nodiscard]] static constexpr double sum_error_bound(const size_t k,
1175 const double max_abs_weight) noexcept
1176 {
1177 if (k < 2)
1178 return 0;
1179 // k - 1 rounded additions of partial sums of magnitude at most
1180 // k * max_abs_weight: gamma(k - 1) = (k - 1) u / (1 - (k - 1) u),
1181 // with u = 2^-53, times that magnitude; the last factor covers the
1182 // rounding of this very product
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);
1186 }
1187
1191 [[nodiscard]] uint64_t clock() const noexcept { return clock_; }
1192
1199 [[nodiscard]] uint64_t stamp(const size_t arc) const { return stamp_(checked_arc(arc)); }
1200
1208 void touch(const size_t arc)
1209 {
1210 (void) checked_arc(arc);
1211 stamp_(arc) = ++clock_;
1212 }
1213
1222 [[nodiscard]] bool is_stale(const size_t position, const uint64_t computed_at) const
1223 {
1224 for (const uint32_t arc : cycle_arcs(position))
1225 if (stamp_(arc) > computed_at)
1226 return true;
1227 return false;
1228 }
1229
1239 void swap(Bounded_Cycle_Index & other) noexcept
1240 {
1241 arc_numbers_.swap(other.arc_numbers_);
1242 arcs_.swap(other.arcs_);
1243 std::swap(lengths_, other.lengths_);
1244 std::swap(longest_, other.longest_);
1245 std::swap(length_first_, other.length_first_);
1246 std::swap(arcs_before_, other.arcs_before_);
1247 cycle_arcs_.swap(other.cycle_arcs_);
1248 member_first_.swap(other.member_first_);
1249 members_.swap(other.members_);
1250 group_member_.swap(other.group_member_);
1251 group_first_.swap(other.group_first_);
1252 narrow_.swap(other.narrow_);
1253 wide_.swap(other.wide_);
1254 weight_.swap(other.weight_);
1255 stamp_.swap(other.stamp_);
1256 std::swap(clock_, other.clock_);
1257 done_.swap(other.done_);
1258 }
1259 };
1260} // namespace Aleph
1261
1262# endif // BOUNDED_CYCLE_INDEX_H
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.
Definition ah-errors.H:846
#define ah_out_of_range_error_if(C)
Throws std::out_of_range if condition holds.
Definition ah-errors.H:727
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
Definition ah-errors.H:670
C++20 concepts for the protocol shared by graph algorithms.
long double w
Definition btreepic.C:153
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:141
static Array create_reserved(const size_t cap)
Build an empty array with room for at least cap items.
Definition tpl_array.H:194
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
Definition tpl_array.H:450
void swap(Array &s) noexcept
Swap this with s
Definition tpl_array.H:313
T & append(const T &data)
Append a copy of data
Definition tpl_array.H:331
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.
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)
static uint32_t length_bits(It first, const It last)
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
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.
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...
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
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
Definition graph-dry.H:975
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
Definition graph-dry.H:1017
bool is_digraph() const noexcept
Return true if the graph this is directed.
Definition graph-dry.H:937
constexpr size_t get_num_arcs() const noexcept
Return the number of arcs of the graph.
Definition graph-dry.H:1064
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
Definition graph-dry.H:1023
Strict weak ordering constraint for BST comparators.
DynArray< Graph::Node * > nodes
Definition graphpic.C:406
DynArray< Graph::Arc * > arcs
Definition graphpic.C:408
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).
Definition ah-zip.H:236
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Definition ahAlgo.H:127
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.
Number_Table(Number_Table &&other) noexcept
Number_Table & operator=(Number_Table other) noexcept
size_t place_of(const void *p) const noexcept
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
size_t V
static int * k
Dynamic array container with automatic resizing.
Generic graph and digraph implementations.
Comprehensive sorting algorithms and search utilities for Aleph-w.