Newer
Older
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
#ifndef NNClusterer_h
#define NNClusterer_h 1
#include <list>
#include <vector>
// #include "LCRTRelations.h"
/** Nearest neighbour type clusering for arbitrary types.
*
* @author F.Gaede (DESY)
* @version $Id: NNClusterer.h 4052 2012-09-12 09:56:04Z gaede $
*/
namespace nnclu {
/** fast check if integer is in a given range [Min,Max] */
inline bool inRange( int i){ return ( (unsigned int) ( i - Min ) <= (unsigned int) ( Max - Min ) ); }
/** fast check if integer is not in a given range [Min,Max] */
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
inline bool notInRange( int i){ return ( (unsigned int) ( i - Min ) > (unsigned int) ( Max - Min ) ); }
// forward declaration:
template <class U>
class Cluster ;
/** Wrapper class for elements that are clustered, holding a pointer to the actual
* object "->first" and a pointer to the cluster this obejct belongs to "->second".
*
* @see Cluster
* @author F.Gaede (DESY)
* @version $Id: NNClusterer.h 4052 2012-09-12 09:56:04Z gaede $
*/
template <class T>
class Element : public std::pair< T*, Cluster<T>* >{
typedef T value_type ;
typedef Cluster<T> cluster_type ;
public:
/** Default c'tor takes a pointer to the original element type object. The optional index can be used to
* code nearest neighbour bins, e.g. in z-coordinate to speed up the clustering process.
*/
Element(T* element, int index0 = 0 ) : Index0( index0 ) {
Pair::first = element ;
Pair::second = 0 ;
}
/** C'tor that also takes a pointer to the cluster this element belongs to - in case seed elements/clusters are used.
*/
Element(T* element , Cluster<T>* cl , int index0 = 0) : Index0( index0 ) {
Pair::first = element ;
Pair::second = cl ;
}
/** Index that can be used to code nearest neighbour bins, e.g. in z-coordinate
* to speed up the clustering process.
*/
int Index0 ;
protected:
typedef std::pair< T*, Cluster<T>* > Pair ;
/** Don't allow default c'tor w/o element */
Element() ;
} ;
/** Helper class that creates an Elements for an objects of type T.
*/
template <class T>
struct MakeElement{
Element<T> operator()( T* t) { return new Element<T>( t ) ; }
} ;
/** Extension of std::vector that allows to take ownership of objects pointed to and
* delete these when going out of scope.
*/
template <class T>
class PtrVector : public std::vector<T*> {
typedef std::vector<T*> vec ;
bool _isOwner ;
public:
PtrVector() : _isOwner( false ) {}
~PtrVector() {
if( _isOwner )
for( typename vec::iterator i = vec::begin(),end = vec::end(); i != end ; delete *i++ ) ; //++i ) delete *i ;
}
void setOwner( bool val=true ) { _isOwner = val ; }
};
/** Extension of std::list that allows to take ownership of objects pointed to and
* delete these when going out of scope.
*/
template <class T>
class PtrList : public std::list<T*> {
typedef std::list<T*> vec ;
bool _isOwner ;
public:
PtrList() : _isOwner( false ) {}
~PtrList() {
if( _isOwner )
for( typename vec::iterator i = vec::begin(),end = vec::end(); i != end ; delete *i++ ) ; //++i ) delete *i ;
}
void setOwner( bool val=true ) { _isOwner = val ; }
};
/** Templated class for generic clusters of Elements that are clustered with
* an NN-like clustering algorithm. Effectively this is just a list of elements.
*
* @see Element
* @author F.Gaede (DESY)
* @version $Id: NNClusterer.h 4052 2012-09-12 09:56:04Z gaede $
*/
template <class T >
class Cluster : public std::list< Element<T> * >{ //, public lcrtrel::LCRTRelations {
public :
typedef Element<T> element_type ;
typedef std::list< Element<T> * > base ;
int ID ; //DEBUG
Cluster() : ID(0) {}
/** C'tor that takes the first element */
Cluster( Element<T>* element) {
static int SID=0 ; //DEBUG
ID = SID++ ; //DEBUG
addElement( element ) ;
}
/** Add a element to this cluster - updates the element's pointer to cluster */
void addElement( Element<T>* element ) {
element->second = this ;
base::push_back( element ) ;
}
// /** Remove all elements from the cluster and reset the cluster association, i.e. elements can be
// * used for another clustering procedure.
// */
// template <class Out>
// void takeElements(Out result){
// typename Cluster<T>::iterator it = this->begin() ;
// while( it != this->end() ){
// (*it)->second = 0 ;
// result++ = *it ;
// it = this->erase(it) ;
// }
// }
/** Free all elements, ie. reset their cluster association - the elements are still in the list !.
*/
void freeElements(){
for( typename Cluster<T>::iterator it = this->begin(), end = this->end() ; it != end ; it++ ){
(*it)->second = 0 ;
}
// typename Cluster<T>::iterator it = this->begin() ;
// while( it != this->end() ){
// (*it)->second = 0 ;
// it = this->erase(it) ;
// }
}
/** Merges all elements from the other cluster cl into this cluster */
void mergeClusters( Cluster<T>* cl ) {
for( typename Cluster<T>::iterator it = cl->begin(), end = cl->end() ; it != end ; it++ ){
(*it)->second = this ;
}
this->merge( *cl ) ;
}
/** D'tor frees all remaining elements that still belong to this cluster */
~Cluster() {
// typename Cluster<T>::iterator it = this->begin() ;
// while( it != this->end() )
for( typename Cluster<T>::iterator it = this->begin() , end = this->end() ; it != end ; ++it ){
typename Cluster<T>::value_type h = *it ;
if( h != 0 && h->second == this )
h->second = 0 ;
}
}
} ;
template <class T>
/** Main class for a nearest neighbour type clustering.
*
* @author F.Gaede (DESY)
* @version $Id: NNClusterer.h 4052 2012-09-12 09:56:04Z gaede $
*/
class NNClusterer{
public:
typedef T value_type ;
typedef Cluster<T> cluster_type ;
typedef Element<T> element_type ;
typedef PtrVector< element_type > element_vector ;
typedef PtrVector< cluster_type > cluster_vector ;
typedef PtrList< element_type > element_list ;
typedef PtrList< cluster_type > cluster_list ;
/** Simple nearest neighbour (NN) clustering algorithm. Users have to provide an input iterator of
* Element objects and an output iterator for the clusters found. The predicate has to have
* a method with the following signature: bool operator()( const Element<T>*, const Element<T>*).
* All pairs of elements for which this method returns 'true' will be merged into one output cluster
* - all other pairs of elements will be in different clusters.
*/
template <class In, class Out, class Pred >
void cluster( In first, In last, Out result, Pred& pred , const unsigned minSize=1) {
cluster_vector tmp ;
tmp.reserve( 1024 ) ;
while( first != last ) {
for( In other = first+1 ; other != last ; other ++ ) {
if( pred( (*first) , (*other) ) ) {
if( (*first)->second == 0 && (*other)->second == 0 ) { // no cluster exists
cluster_type* cl = new cluster_type( (*first) ) ;
cl->addElement( (*other) ) ;
tmp.push_back( cl ) ;
}
else if( (*first)->second != 0 && (*other)->second != 0 ) { // two clusters
if( (*first)->second != (*other)->second ) // don't call merge on identical clusters
(*first)->second->mergeClusters( (*other)->second ) ;
} else { // one cluster exists
if( (*first)->second != 0 ) {
(*first)->second->addElement( (*other) ) ;
} else {
(*other)->second->addElement( (*first) ) ;
}
}
}
}
++first ;
}
// remove empty clusters
for( typename cluster_vector::iterator i = tmp.begin(); i != tmp.end() ; i++ ){
if( (*i)->size() > minSize-1 ) {
result++ = *i ;
}
else {
delete *i ;
}
}
}
/** Same as above - but requires the elements to be sorted in index0 (only compare neighbouring bins in index0). */
template <class In, class Out, class Pred >
void cluster_sorted( In first, In last, Out result, Pred& pred , const unsigned minSize=1) {
cluster_vector tmp ;
tmp.reserve( 1024 ) ;
while( first != last ) {
for( In other = first+1 ; other != last ; other ++ ) {
// if the elements are sorted we can skip the rest of the inner loop
if( notInRange<-1,1>( (*first)->Index0 - (*other)->Index0 ) )
break ;
if( pred( (*first) , (*other) ) ) {
if( (*first)->second == 0 && (*other)->second == 0 ) { // no cluster exists
cluster_type* cl = new cluster_type( (*first) ) ;
cl->addElement( (*other) ) ;
tmp.push_back( cl ) ;
}
else if( (*first)->second != 0 && (*other)->second != 0 ) { // two clusters
if( (*first)->second != (*other)->second ) // don't call merge on identical clusters
(*first)->second->mergeClusters( (*other)->second ) ;
} else { // one cluster exists
if( (*first)->second != 0 ) {
(*first)->second->addElement( (*other) ) ;
} else {
(*other)->second->addElement( (*first) ) ;
}
}
}
}
++first ;
}
// remove empty clusters
for( typename cluster_vector::iterator i = tmp.begin(); i != tmp.end() ; i++ ){
if( (*i)->size() > minSize-1 ) {
result++ = *i ;
}
else {
delete *i ;
}
}
}
};
//-----------------------------------------------------------------------------------------------------------------------
/**Splits a list into two based on a predicate. The new list will
* hold all elements for which Pred is true which are in turn removed
* from the original list.
*/
template <class List, class Out, class Pred >
void split_list( List& list, Out result, Pred pred) {
typename List::iterator it = list.begin() ;
while( it != list.end() ){
if( pred( *it ) ){
result++ = *it ;
it = list.erase( it ) ;
} else
++it ;
}
}
//-----------------------------------------------------------------------------------------------------------------------
} // namespace nnclu
#endif