Open
Graph Drawing
Framework

 v. snapshot-2017-02-16
 

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  const List<T> &m_set;
91  bool m_valid;
92  int m_maxCard;
95 
96  void initSubset(int card)
97  {
98  if (card >= 0 && card <= m_subset.size()) {
99  m_index.init(card);
100  for (int i = 0; i < card; ++i) {
101  m_index[i] = i;
102  }
103  m_valid = true;
104  }
105  }
106 
107 public:
110  explicit SubsetEnumerator(const List<T> &set)
111  : m_set(set)
112  , m_valid(false)
113  , m_subset(set.size())
114  {
115  int i = 0;
116  for(const T &x : m_set) {
117  m_subset[i++] = x;
118  }
119  }
120 
122  void begin(int low, int high)
123  {
124  m_maxCard = high;
125  initSubset(low);
126  }
127 
129  void begin(int card)
130  {
131  begin(card, card);
132  }
133 
135  void begin()
136  {
137  begin(0, m_set.size());
138  }
139 
141  int size() const
142  {
143  return m_index.size();
144  }
145 
147  bool valid() const
148  {
149  return m_valid;
150  }
151 
153  bool hasMember(const T &element) const
154  {
155  for (int i = 0; i < m_index.size(); ++i) {
156  if (element == m_subset[m_index[i]]) {
157  return true;
158  }
159  }
160  return false;
161  }
162 
164  T operator[](int i) const
165  {
166  OGDF_ASSERT(i >= 0);
167  OGDF_ASSERT(i < m_index.size());
168  return m_subset[m_index[i]];
169  }
170 
172  void next()
173  {
174  if (m_valid) {
175  const int t = m_index.size();
176  if (t == 0) { // last (empty) subset has been found
177  if (t < m_maxCard) {
178  initSubset(t + 1);
179  } else {
180  m_valid = false;
181  }
182  return;
183  }
184  const int n = m_subset.size();
185  int i;
186  for (i = t - 1; m_index[i] == i + n - t; --i) {
187  if (i == 0) { // the last subset of this cardinality has been found
188  if (t < m_maxCard) {
189  initSubset(t + 1);
190  } else {
191  m_valid = false;
192  }
193  return;
194  }
195  }
196  for (++m_index[i]; i < t - 1; ++i) {
197  m_index[i + 1] = m_index[i] + 1;
198  }
199  }
200  }
201 
203  void list(List<T> &list) const
204  {
205  for (int i = 0; i < m_index.size(); ++i) {
206  list.pushBack(m_subset[m_index[i]]);
207  }
208  }
209 
211  void array(Array<T> &array) const
212  {
213  array.init(m_index.size());
214  for (int i = 0; i < m_index.size(); ++i) {
215  array[i] = m_subset[m_index[i]];
216  }
217  }
218 
220  void list(List<T> &list, List<T> &complement) const
221  {
222  int j = 0;
223  for (int i = 0; i < m_subset.size(); ++i) {
224  if (j < m_index.size() && m_index[j] == i) {
225  list.pushBack(m_subset[i]);
226  ++j;
227  } else {
228  complement.pushBack(m_subset[i]);
229  }
230  }
231  }
232 };
233 
234 
235 // prints subset to output stream os using delimiter delim
236 template<class T>
237 void print(ostream &os, const SubsetEnumerator<T> &subset, string delim = " ")
238 {
239  OGDF_ASSERT(subset.valid());
240  if (subset.size() > 0) {
241  os << subset[0];
242  }
243  for (int i = 1; i < subset.size(); ++i) {
244  os << delim << subset[i];
245  }
246 }
247 
248 // prints subset to output stream os
249 template<class T>
250 ostream &operator<<(ostream &os, const SubsetEnumerator<T> &subset)
251 {
252  print(os, subset);
253  return os;
254 }
255 
256 }
iterator pushBack(const E &x)
Adds element x at the end of the list.
Definition: List.h:1316
void init()
Reinitializes the array to an array with empty index set.
Definition: Array.h:206
void begin(int card)
Initialize the SubsetEnumerator to enumerate subsets of given cardinality.
bool valid() const
Is the current subset valid? If not, all subsets have already been enumerated.
void begin()
Initialize the SubsetEnumerator to enumerate all subsets.
void list(List< T > &list, List< T > &complement) const
Obtain (append) a list of the elements in the subset and a list of the other elements of the set...
void begin(int low, int high)
Initialize the SubsetEnumerator to enumerate subsets of cardinalities from low to high...
INDEX size() const
Returns the size (number of elements) of the array.
Definition: Array.h:134
Enumerator for k-subsets of a given type.
void initSubset(int card)
bool hasMember(const T &element) const
Check in O(subset cardinality) whether the argument is a member of the subset.
const List< T > & m_set
SubsetEnumerator(const List< T > &set)
Constructor.
void list(List< T > &list) const
Obtain (append) a list of the elements in the subset.
void next()
Obtain the next subset if possible. The result should be checked using the valid() method...
int size() const
Returns the number of elements in the list.
Definition: List.h:1255
int size() const
Return the cardinality of the subset.
The namespace for all OGDF objects.
T operator[](int i) const
Get element of subset by index (starting from 0).
void print(ostream &os, const Array< E, INDEX > &a, char delim=' ')
Prints array a to output stream os using delimiter delim.
Definition: Array.h:753
#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
Obtain an array of the elements in the subset.