Open
Graph Drawing
Framework

 v. 2020.02 (Catalpa)
 

SubsetEnumerator.h
Go to the documentation of this file.
1 
32 #pragma once
33 
34 #include <ogdf/basic/List.h>
35 
36 namespace ogdf {
37 
39 
87 template<typename T>
89 protected:
90  bool m_valid;
91  int m_maxCard;
94 
95  void initSubset(int card)
96  {
97  if (card >= 0 && card <= m_subset.size()) {
98  m_index.init(card);
99  for (int i = 0; i < card; ++i) {
100  m_index[i] = i;
101  }
102  m_valid = true;
103  }
104  }
105 
106 public:
112  template<typename ContainerType>
113  explicit SubsetEnumerator(const ContainerType &set)
114  : m_valid(false)
115  , m_maxCard(-1)
116  , m_subset(set.size())
117  {
118  for (auto x : set) {
119  m_subset.push(x);
120  }
121  }
122 
124  void begin(int low, int high)
125  {
126  if (high >= low) {
127  m_maxCard = high;
128  m_maxCard = min(m_maxCard, m_subset.size());
129  initSubset(low);
130  } else {
131  m_valid = false;
132  }
133  }
134 
136  void begin(int card)
137  {
138  begin(card, card);
139  }
140 
142  void begin()
143  {
144  begin(0, m_subset.size());
145  }
146 
148  int size() const
149  {
150  return m_index.size();
151  }
152 
156  {
157  return m_subset.size();
158  }
159 
162  bool valid() const
163  {
164  return m_valid;
165  }
166 
168  bool hasMember(const T &element) const
169  {
170  for (int index : m_index) {
171  if (element == m_subset[index]) {
172  return true;
173  }
174  }
175  return false;
176  }
177 
179  T operator[](int i) const
180  {
181  OGDF_ASSERT(i >= 0);
182  OGDF_ASSERT(i < m_index.size());
183  return m_subset[m_index[i]];
184  }
185 
187  void next()
188  {
189  if (m_valid) {
190  const int t = m_index.size();
191  if (t == 0) { // last (empty) subset has been found
192  if (t < m_maxCard) {
193  initSubset(t + 1);
194  } else {
195  m_valid = false;
196  }
197  return;
198  }
199  const int n = m_subset.size();
200  int i;
201  for (i = t - 1; m_index[i] == i + n - t; --i) {
202  if (i == 0) { // the last subset of this cardinality has been found
203  if (t < m_maxCard) {
204  initSubset(t + 1);
205  } else {
206  m_valid = false;
207  }
208  return;
209  }
210  }
211  for (++m_index[i]; i < t - 1; ++i) {
212  m_index[i + 1] = m_index[i] + 1;
213  }
214  }
215  }
216 
218  void forEachMember(std::function<void(const T &)> func) const
219  {
220  for (int index : m_index) {
221  func(m_subset[index]);
222  }
223  }
224 
226  void list(List<T> &subset) const
227  {
228  forEachMember([&](const T &member) {
229  subset.pushBack(member);
230  });
231  }
232 
234  void array(Array<T> &array) const
235  {
236  array.init(m_index.size());
237  for (int i = 0; i < m_index.size(); ++i) {
238  array[i] = m_subset[m_index[i]];
239  }
240  }
241 
243  void forEachMemberAndNonmember(std::function<void(const T &)> funcIn, std::function<void(const T &)> funcNotIn) const
244  {
245  for (int i = 0, j = 0; i < m_subset.size(); ++i) {
246  if (j < m_index.size() && m_index[j] == i) {
247  funcIn(m_subset[i]);
248  ++j;
249  } else {
250  funcNotIn(m_subset[i]);
251  }
252  }
253  }
254 
256  template<typename ContainerType>
257  void getSubsetAndComplement(ContainerType &subset, ContainerType &complement, std::function<void(ContainerType &, T)> func) const
258  {
259  forEachMemberAndNonmember([&](const T &member) {
260  func(subset, member);
261  }, [&](const T &nonmember) {
262  func(complement, nonmember);
263  });
264  }
265 
267  void list(List<T> &subset, List<T> &complement) const
268  {
269  getSubsetAndComplement<List<T>>(subset, complement, [](List<T> &lc, T element) {
270  lc.pushBack(element);
271  });
272  }
273 
275  bool testForAll(std::function<bool(const T&)> predicate) const
276  {
277  for (int index : m_index) {
278  if (!predicate(m_subset[index])) {
279  return false;
280  }
281  }
282  return true;
283  }
284 
286  void print(std::ostream &os, string delim = " ") const
287  {
288  if (valid()) {
289  if (size() > 0) {
290  os << m_subset[m_index[0]];
291  for (int i = 1; i < size(); ++i) {
292  os << delim << m_subset[m_index[i]];
293  }
294  }
295  } else {
296  os << "<<invalid subset>>";
297  }
298  }
299 };
300 
301 template<class T>
302 std::ostream &operator<<(std::ostream &os, const SubsetEnumerator<T> &subset)
303 {
304  subset.print(os);
305  return os;
306 }
307 
308 }
iterator pushBack(const E &x)
Adds element x at the end of the list.
Definition: List.h:1373
SubsetEnumerator(const ContainerType &set)
Constructor.
void init()
Reinitializes the array to an array with empty index set.
Definition: Array.h:354
void begin(int card)
Initializes the SubsetEnumerator to enumerate subsets of given cardinality.
void push(E e)
Puts a new element in the buffer.
Definition: ArrayBuffer.h:102
bool valid() const
Checks if the current subset is valid. If not, the subset is either not initialized or all subsets ha...
void begin()
Initializes the SubsetEnumerator to enumerate all subsets.
void list(List< T > &subset, List< T > &complement) const
Obtains (appends) a list of the subset members and a list of the other elements of the set...
void begin(int low, int high)
Initializes the SubsetEnumerator to enumerate subsets of cardinalities from low to high...
void print(std::ostream &os, string delim=" ") const
Prints subset to output stream os using delimiter delim.
INDEX size() const
Returns the size (number of elements) of the array.
Definition: Array.h:282
Enumerator for k-subsets of a given type.
Doubly linked lists (maintaining the length of the list).
Definition: List.h:40
void getSubsetAndComplement(ContainerType &subset, ContainerType &complement, std::function< void(ContainerType &, T)> func) const
Obtains a container of the subset members and a container of the other elements of the set...
ArrayBuffer< T > m_subset
INDEX size() const
Returns number of elements in the buffer.
Definition: ArrayBuffer.h:122
void initSubset(int card)
bool hasMember(const T &element) const
Checks in O(subset cardinality) whether element is a member of the subset.
void forEachMemberAndNonmember(std::function< void(const T &)> funcIn, std::function< void(const T &)> funcNotIn) const
Calls funcIn for each subset member and funcNotIn for each other element of the set.
void list(List< T > &subset) const
Obtains (appends) a list of the subset members.
void next()
Obtains the next subset if possible. The result should be checked using the valid() method...
void forEachMember(std::function< void(const T &)> func) const
Calls func for each member in the subset.
bool testForAll(std::function< bool(const T &)> predicate) const
Tests predicate for all subset members.
int size() const
Returns the cardinality of the subset.
The namespace for all OGDF objects.
T operator[](int i) const
Gets a member of subset by index (starting from 0).
int numberOfMembersAndNonmembers() const
Returns the cardinality of the (super-)set. This is the maximum size that can be used for a subset...
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:41
Declaration of doubly linked lists and iterators.
void array(Array< T > &array) const
Obtains an array of the subset members.