Storm 1.11.1.1
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
combinatorics.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstdint>
4#include <functional>
5#include <vector>
6
7namespace storm {
8namespace utility {
9namespace combinatorics {
10
11template<typename IteratorType>
12void forEach(std::vector<IteratorType> const& its, std::vector<IteratorType> const& ites,
13 std::function<void(uint64_t, decltype(*std::declval<IteratorType>()))> const& setValueCallback,
14 std::function<bool()> const& newCombinationCallback) {
15 STORM_LOG_ASSERT(its.size() == ites.size(), "Iterator begin/end mismatch.");
16
17 if (its.size() == 0) {
18 return;
19 }
20
21 bool allNonEmpty = true;
22 for (uint64_t index = 0; index < its.size(); ++index) {
23 if (its[index] == ites[index]) {
24 allNonEmpty = false;
25 break;
26 }
27 }
28 if (!allNonEmpty) {
29 return;
30 }
31
32 std::vector<IteratorType> currentIterators(its);
33
34 // Fill the initial combination.
35 for (uint64_t index = 0; index < currentIterators.size(); ++index) {
36 setValueCallback(index, *currentIterators[index]);
37 }
38
39 // Enumerate all combinations until the callback yields false (or there are no more combinations).
40 while (true) {
41 bool cont = newCombinationCallback();
42 if (!cont) {
43 break;
44 }
45
46 uint64_t index = 0;
47 for (; index < currentIterators.size(); ++index) {
48 ++currentIterators[index];
49 if (currentIterators[index] == ites[index]) {
50 currentIterators[index] = its[index];
51 } else {
52 break;
53 }
54 }
55
56 // If we are at the end, leave the loop.
57 if (index == currentIterators.size()) {
58 break;
59 } else {
60 for (uint64_t j = 0; j <= index; ++j) {
61 setValueCallback(j, *currentIterators[j]);
62 }
63 }
64 }
65}
66
67} // namespace combinatorics
68} // namespace utility
69} // namespace storm
#define STORM_LOG_ASSERT(cond, message)
Definition macros.h:11
void forEach(std::vector< IteratorType > const &its, std::vector< IteratorType > const &ites, std::function< void(uint64_t, decltype(*std::declval< IteratorType >()))> const &setValueCallback, std::function< bool()> const &newCombinationCallback)