Storm
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 typedef decltype((*std::declval<IteratorType>())) value_type;
16 STORM_LOG_ASSERT(its.size() == ites.size(), "Iterator begin/end mismatch.");
17
18 if (its.size() == 0) {
19 return;
20 }
21
22 bool allNonEmpty = true;
23 for (uint64_t index = 0; index < its.size(); ++index) {
24 if (its[index] == ites[index]) {
25 allNonEmpty = false;
26 break;
27 }
28 }
29 if (!allNonEmpty) {
30 return;
31 }
32
33 std::vector<IteratorType> currentIterators(its);
34
35 // Fill the initial combination.
36 for (uint64_t index = 0; index < currentIterators.size(); ++index) {
37 setValueCallback(index, *currentIterators[index]);
38 }
39
40 // Enumerate all combinations until the callback yields false (or there are no more combinations).
41 while (true) {
42 bool cont = newCombinationCallback();
43 if (!cont) {
44 break;
45 }
46
47 uint64_t index = 0;
48 for (; index < currentIterators.size(); ++index) {
49 ++currentIterators[index];
50 if (currentIterators[index] == ites[index]) {
51 currentIterators[index] = its[index];
52 } else {
53 break;
54 }
55 }
56
57 // If we are at the end, leave the loop.
58 if (index == currentIterators.size()) {
59 break;
60 } else {
61 for (uint64_t j = 0; j <= index; ++j) {
62 setValueCallback(j, *currentIterators[j]);
63 }
64 }
65 }
66}
67
68} // namespace combinatorics
69} // namespace utility
70} // 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)
LabParser.cpp.
Definition cli.cpp:18