File: SubsetEnumerator.h

package info (click to toggle)
tulip 4.8.0dfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 179,264 kB
  • ctags: 64,517
  • sloc: cpp: 600,444; ansic: 36,311; makefile: 22,136; python: 1,304; sh: 946; yacc: 522; xml: 337; pascal: 157; php: 66; lex: 55
file content (263 lines) | stat: -rw-r--r-- 6,582 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*
 * $Revision: 3811 $
 *
 * last checkin:
 *   $Author: beyer $
 *   $Date: 2013-10-29 14:14:13 +0100 (Tue, 29 Oct 2013) $
 ***************************************************************/

/** \file
 * \brief A class that allows to enumerate k-subsets of lists.
 *
 * \author Stephan Beyer
 *
 * \par License:
 * This file is part of the Open Graph Drawing Framework (OGDF).
 *
 * \par
 * Copyright (C)<br>
 * See README.txt in the root directory of the OGDF installation for details.
 *
 * \par
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * Version 2 or 3 as published by the Free Software Foundation;
 * see the file LICENSE.txt included in the packaging of this file
 * for details.
 *
 * \par
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * \par
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 * \see  http://www.gnu.org/copyleft/gpl.html
 ***************************************************************/

#ifdef _MSC_VER
#pragma once
#endif

#ifndef OGDF_SUBSET_ENUMERATOR_H
#define OGDF_SUBSET_ENUMERATOR_H

#include <ogdf/basic/List.h>

namespace ogdf {

//! Enumerator for k-subsets of a given type.
/**
 * <H3>Usage examples</H3>
 * <ul>
 *   <li> Enumerate all subsets of edges with cardinality 3:
 *    \code
 *     List<edge> edges;
 *
 *     do_something_eg_fill_edges();
 *
 *     SubsetEnumerator<edge> edgeSubset(edges);
 *
 *     for (subset.begin(3); subset.valid(); subset.next()) {
 *       do_something_with(subset[0], subset[1], subset[2]);
 *     }
 *    \endcode
 *   </li>
 *   <li> Enumerate all subsets of edges:
 *    \code
 *     SubsetEnumerator<edge> edgeSubset(edges);
 *
 *     for (subset.begin(); subset.valid(); subset.next()) {
 *       for (int i = 0; i < subset.size(); ++i) {
 *         do_something_with(subset[i]);
 *       }
 *       do_stuff();
 *     }
 *    \endcode
 *   </li>
 *   <li> Do something with element lists and complement lists of all 2-, 3-, and 4-element subsets
 *    \code
 *     SubsetEnumerator<edge> edgeSubset(edges);
 *
 *     for (subset.begin(2, 4); subset.valid(); subset.next()) {
 *       List<edge> list1, list2;
 *       subset.list(list1, list2);
 *       // if subset = { 1, 3, 4 } of { 1, 2, 3, 4, 5 },
 *       // then list1 = 1 3 4 and list2 = 2 5
 *       do_something_with(list1);
 *       do_another_things_with(list2);
 *     }
 *    \endcode
 *   </li>
 * </ul>
 *
 * Please note that the internal data structures of SubsetEnumerator do not use references of the type T.
 * Hence, T should either be a simple type or a pointer to a complex type (which is also only sane for Lists, too).
 * Otherwise the data structure will slow down due to extensive implicit copying.
 */
template<typename T>
class SubsetEnumerator {
protected:
	const List<T> &m_set;
	bool m_valid;
	int m_maxCard;
	Array<T> m_subset;
	Array<int> m_index;

	void initSubset(int card)
	{
		if (card >= 0 && card <= m_subset.size()) {
			m_index.init(card);
			for (int i = 0; i < card; ++i) {
				m_index[i] = i;
			}
			m_valid = true;
		}
	}

public:
	//! \brief Constructor.
	//! @param set The list of elements we want to enumerate subsets for.
	SubsetEnumerator(const List<T> &set)
	  : m_set(set)
	  , m_valid(false)
	  , m_subset(set.size())
	{
		int i = 0;
		forall_listiterators(T, it, m_set) {
			m_subset[i++] = *it;
		}
	}

	//! Initialize the SubsetEnumerator to enumerate subsets of cardinalities from low to high.
	void begin(int low, int high)
	{
		m_maxCard = high;
		initSubset(low);
	}

	//! Initialize the SubsetEnumerator to enumerate subsets of given cardinality.
	void begin(int card)
	{
		begin(card, card);
	}

	//! Initialize the SubsetEnumerator to enumerate all subsets.
	void begin()
	{
		begin(0, m_set.size());
	}

	//! Return the cardinality of the subset.
	int size() const
	{
		return m_index.size();
	}

	//! Is the current subset valid? If not, all subsets have already been enumerated.
	bool valid() const
	{
		return m_valid;
	}

	//! Check in O(subset cardinality) whether the argument is a member of the subset.
	bool hasMember(const T &element) const
	{
		for (int i = 0; i < m_index.size(); ++i) {
			if (element == m_subset[m_index[i]]) {
				return true;
			}
		}
		return false;
	}

	//! Get element of subset by index (starting from 0).
	T operator[](int i) const
	{
		OGDF_ASSERT(i >= 0 && i < m_index.size());
		return m_subset[m_index[i]];
	}

	//! Obtain the next subset if possible. The result should be checked using the valid() method.
	void next()
	{
		if (m_valid) {
			const int t = m_index.size();
			if (t == 0) { // last (empty) subset has been found
				if (t < m_maxCard) {
					initSubset(t + 1);
				} else {
					m_valid = false;
				}
				return;
			}
			const int n = m_subset.size();
			int i;
			for (i = t - 1; m_index[i] == i + n - t; --i) {
				if (i == 0) { // the last subset of this cardinality has been found
					if (t < m_maxCard) {
						initSubset(t + 1);
					} else {
						m_valid = false;
					}
					return;
				}
			}
			for (++m_index[i]; i < t - 1; ++i) {
				m_index[i + 1] = m_index[i] + 1;
			}
		}
	}

	//! Obtain (append) a list of the elements in the subset.
	void list(List<T> &list) const
	{
		for (int i = 0; i < m_index.size(); ++i) {
			list.pushBack(m_subset[m_index[i]]);
		}
	}

	//! Obtain (append) a list of the elements in the subset and a list of the other elements of the set.
	void list(List<T> &list, List<T> &complement) const
	{
		int j = 0;
		for (int i = 0; i < m_subset.size(); ++i) {
			if (j < m_index.size() && m_index[j] == i) {
				list.pushBack(m_subset[i]);
				++j;
			} else {
				complement.pushBack(m_subset[i]);
			}
		}
	}
};


// prints subset to output stream os using delimiter delim
template<class T>
void print(ostream &os, const SubsetEnumerator<T> &subset, string delim = " ")
{
	OGDF_ASSERT(subset.valid());
	if (subset.size() > 0) {
		os << subset[0];
	}
	for (int i = 1; i < subset.size(); ++i) {
		os << delim << subset[i];
	}
}

// prints subset to output stream os
template<class T>
ostream &operator<<(ostream &os, const SubsetEnumerator<T> &subset)
{
	print(os, subset);
	return os;
}

}
#endif // OGDF_SUBSET_ENUMERATOR_H