-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNonDominatedSet.cpp
299 lines (260 loc) · 7.61 KB
/
NonDominatedSet.cpp
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*! \file NonDominatedSet.cpp
* \brief The implementation of the NonDominatedSet<T> class template.
* \author Christos Nitsas
* \date 2012
*
* Won't `include` NonDominatedSet.h. In fact NonDominatedSet.h will
* `include` NonDominatedSet.cpp because it describes a class template
* (which doesn't allow us to split declaration from definition).
*/
/*!
* \weakgroup ParetoApproximator Everything needed for the Pareto set approximation algorithms.
* @{
*/
//! The namespace containing everything needed for the Pareto set approximation algorithms.
namespace pareto_approximator {
//! Default constructor. Makes an empty set.
template <class T>
NonDominatedSet<T>::NonDominatedSet() : contents_() { }
//! Iteration constructor.
/*!
* \param first Input iterator to the initial position in a sequence
* of T instances.
* \param last Input iterator to the past-the-end position in a
* sequence of T instances.
*
* Reminder: T instances represent some kind of point. (e.g. the Point
* or PointAndSolution<T> classes)
*
* Iterates between first and last, filtering and inserting the
* elements of the sequence into the container object. The range used
* is [first, last), which includes all the elements between first and
* last, including the element pointed by first but not the element
* pointed by last.
*
* Elements will be filtered during the insertion process so that
* only non-dominated T instances are kept.
*
* The function template type can be any type of input iterator
* that points to T instances.
*
* \sa NonDominatedSet
*/
template <class T>
template <class InputIterator>
NonDominatedSet<T>::NonDominatedSet(InputIterator first, InputIterator last)
{
insert(first, last);
}
//! Destructor. (all the contained elements' destructors will be called)
template <class T>
NonDominatedSet<T>::~NonDominatedSet() { }
//! Return iterator to beginning.
/*!
* \return An iterator referring to the first T element in the
* container.
*
* \sa NonDominatedSet
*/
template <class T>
typename NonDominatedSet<T>::iterator
NonDominatedSet<T>::begin()
{
return contents_.begin();
}
//! Return const_iterator to beginning.
/*!
* \return A const_iterator referring to the first T element in the
* container.
*
* \sa NonDominatedSet
*/
template <class T>
typename NonDominatedSet<T>::const_iterator
NonDominatedSet<T>::begin() const
{
return contents_.begin();
}
//! Return iterator to end.
/*!
* \return An iterator pointing to the past-the-end T element in the
* container.
*
* \sa NonDominatedSet
*/
template <class T>
typename NonDominatedSet<T>::iterator
NonDominatedSet<T>::end()
{
return contents_.end();
}
//! Return const_iterator to end.
/*!
* \return A const_iterator pointing to the past-the-end T element
* in the container.
*
* \sa NonDominatedSet
*/
template <class T>
typename NonDominatedSet<T>::const_iterator
NonDominatedSet<T>::end() const
{
return contents_.end();
}
//! Test whether container is empty.
/*!
* \return true if the container is empty; false otherwise.
*
* \sa NonDominatedSet
*/
template <class T>
bool
NonDominatedSet<T>::empty() const
{
return contents_.empty();
}
//! Returns the number of elements in the container.
/*!
* \return The number of elements that form the set's content.
*
* Member type size_type is (usually) an unsigned integral type.
*
* \sa NonDominatedSet
*/
template <class T>
typename NonDominatedSet<T>::size_type
NonDominatedSet<T>::size() const
{
return contents_.size();
}
//! Insert element.
/*!
* \param t The T instance to insert.
* \return true if the element was actually inserted (was not
* dominated); false otherwise.
*
* Reminder: T instances represent some kind of point. (e.g. the Point
* or PointAndSolution<T> classes)
*
* The container is extended by inserting a single new element iff the
* new element is not already dominated.
*
* The element will be inserted if and only if it is not dominated by
* any other element in the set. Any elements dominated by the newly
* inserted element will be deleted.
*
* \sa NonDominatedSet
*/
template <class T>
bool
NonDominatedSet<T>::insert(const T & t)
{
// if t is dominated by some element in the set don't insert it
bool isDominated = this->dominates(t);
if (!isDominated) {
// first remove any elements dominated by t
for (iterator it = contents_.begin(); it != contents_.end(); )
if (t.dominates(*it))
// increment "it" before removing its previous value from the set
contents_.erase(it++);
else
// just increment "it"
it++;
// now insert the new element
contents_.insert(t);
}
return (!isDominated);
}
//! Insert elements.
/*!
* \param first Input iterator to the initial position in a sequence
* of T instances.
* \param last Input iterator to the past-the-end position in a
* sequence of T instances.
* \return true if at least one element was actually inserted (not
* dominated); false otherwise.
*
* Reminder: T instances represent some kind of point. (e.g. the Point
* or PointAndSolution<T> classes)
*
* Iterates between first and last, filtering and inserting the
* elements of the sequence into the container object. The range used
* is [first, last), which includes all the elements between first and
* last, including the element pointed by first but not the element
* pointed by last.
*
* Elements will be filtered during the insertion process so that
* only non-dominated T instances are kept.
*
* The function template type can be any type of input iterator
* that points to T instances.
*
* \sa NonDominatedSet
*/
template <class T>
template <class InputIterator>
bool
NonDominatedSet<T>::insert(InputIterator first, InputIterator last)
{
bool insertedAtLeastOneElement = false;
for ( ; first != last; ++first)
insertedAtLeastOneElement |= this->insert(*first);
return insertedAtLeastOneElement;
}
//! Check if some element in the set dominates the given instance.
/*!
* \param t A T instance.
* \return true if some element in the set dominates t; false otherwise.
*
* For every element (T instance) in the set, check if it dominates t.
* Return true if some does; false otherwise.
*
* \sa NonDominatedSet
*/
template <class T>
bool
NonDominatedSet<T>::dominates(const T & t) const
{
iterator it;
for (it = contents_.begin(); it != contents_.end(); ++it)
if (it->dominates(t))
return true;
return false;
}
//! Clear content.
/*!
* All the elements' destructors are called and then they are removed
* from the container, leaving it with a size of 0.
*
* \sa NonDominatedSet
*/
template <class T>
void
NonDominatedSet<T>::clear()
{
contents_.clear();
}
//! Get iterator to element.
/*!
* \param t A T instance.
* \return An iterator to the T element which is equal to t if one
* exists; an iterator to the past-the-end element otherwise.
*
* Searches the container for a T instance that is equal to the given
* instance (t) and returns an iterator to it if it exists, otherwise
* it returns an iterator to NonDominatedSet::end().
*
* \sa NonDominatedSet and NonDominatedSet::end()
*/
template <class T>
typename NonDominatedSet<T>::iterator
NonDominatedSet<T>::find(const T & t) const
{
iterator it;
for (it = contents_.begin(); it != contents_.end(); ++it)
if (*it == t)
break;
return it;
}
} // namespace pareto_approximator
/* @} */