Storm
A Modern Probabilistic Model Checker
Loading...
Searching...
No Matches
SubsetEnumerator.cpp
Go to the documentation of this file.
1#include "SubsetEnumerator.h"
2
5
6namespace storm {
7namespace storage {
8namespace geometry {
9
10template<typename DataType>
11SubsetEnumerator<DataType>::SubsetEnumerator(uint_fast64_t n, uint_fast64_t k, DataType const& data, SubsetFilter subsetFilter)
12 : n(n), k(k), data(data), filter(subsetFilter) {
13 // Intentionally left empty
14}
15
16template<typename DataType>
18 // Intentionally left empty
19}
20
21template<typename DataType>
22std::vector<uint_fast64_t> const& SubsetEnumerator<DataType>::getCurrentSubset() {
23 return this->current;
24}
25
26template<typename DataType>
28 if (n < k)
29 return false;
30 // set the upper boundaries first.
31 upperBoundaries.clear();
32 upperBoundaries.reserve(k);
33 for (uint_fast64_t bound = (n - k); bound < n; ++bound) {
34 upperBoundaries.push_back(bound);
35 }
36 // now set the current subset to the very first one
37 current.clear();
38 current.reserve(k);
39 uint_fast64_t newItem = 0;
40 while (current.size() != k && newItem <= upperBoundaries[current.size()]) {
41 // Check if it is okay to add the new item...
42 if (filter(current, newItem, data)) {
43 current.push_back(newItem);
44 }
45 ++newItem;
46 }
47 // Note that we only insert things into the vector if it is "okay" to do so.
48 // Hence, we have failed iff we were not able to insert k elements.
49 return current.size() == k;
50}
51
52template<typename DataType>
54 // The currentSelection will be the numbers that are already inside of our new subset.
55 std::vector<uint_fast64_t> currentSelection(current);
56 currentSelection.pop_back();
57 uint_fast64_t pos = k - 1;
58 while (true) {
59 // check whether we can increment at the current position
60 if (current[pos] == upperBoundaries[pos]) {
61 if (pos == 0) {
62 // we already moved to the very left and still can not increment.
63 // Hence, we are already at the last subset
64 return false;
65 }
66 currentSelection.pop_back();
67 --pos;
68 } else {
69 ++current[pos];
70 // check if the new subset is inside our filter
71 if (filter(currentSelection, current[pos], data)) {
72 // it is, so add it and go on with the position on the right
73 currentSelection.push_back(current[pos]);
74 ++pos;
75 if (pos == k) {
76 // we are already at the very right.
77 // Hence, we have found our new subset of size k
78 return true;
79 }
80 // initialize the value at the new position
81 current[pos] = current[pos - 1];
82 }
83 }
84 }
85}
86
87template<typename DataType>
88bool SubsetEnumerator<DataType>::trueFilter(std::vector<uint_fast64_t> const&, uint_fast64_t const&, DataType const&) {
89 return true;
90}
91
97} // namespace geometry
98} // namespace storage
99} // namespace storm
void clear()
Removes all set bits from the bit vector.
This class can be used to enumerate all k-sized subsets of {0,...,n-1}.
static bool trueFilter(std::vector< uint_fast64_t > const &subset, uint_fast64_t const &item, DataType const &data)
std::vector< uint_fast64_t > const & getCurrentSubset()
SubsetEnumerator(uint_fast64_t n, uint_fast64_t k, DataType const &data=DataType(), SubsetFilter subsetFilter=trueFilter)
LabParser.cpp.
Definition cli.cpp:18