00001 #ifndef INCLUDE_DISTILLER_H
00002 #define INCLUDE_DISTILLER_H
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #include "Partial.h"
00037 #include "PartialList.h"
00038 #include "PartialUtils.h"
00039
00040 #include <algorithm>
00041
00042
00043 namespace Loris {
00044
00045
00046
00047
00071
00072 class Distiller
00073 {
00074
00075
00076 double _fadeTime, _gapTime;
00077
00078
00079 public:
00080
00081
00102 explicit
00103 Distiller( double partialFadeTime = 0.001 ,
00104 double partialSilentTime = 0.0001 );
00105
00106
00107
00108
00109
00136 #if ! defined(NO_TEMPLATE_MEMBERS)
00137 template< typename Container >
00138 typename Container::iterator distill( Container & partials );
00139 #else
00140 inline
00141 PartialList::iterator distill( PartialList & partials );
00142 #endif
00143
00145 #if ! defined(NO_TEMPLATE_MEMBERS)
00146 template< typename Container >
00147 typename Container::iterator operator() ( Container & partials );
00148 #else
00149 PartialList::iterator operator() ( PartialList & partials );
00150 #endif
00151
00174 #if ! defined(NO_TEMPLATE_MEMBERS)
00175 template< typename Container >
00176 static typename Container::iterator
00177 distill( Container & partials, double partialFadeTime,
00178 double partialSilentTime = 0.0001 );
00179 #else
00180 static inline PartialList::iterator
00181 distill( PartialList & partials, double partialFadeTime,
00182 double partialSilentTime = 0.0001 );
00183 #endif
00184
00185
00186 private:
00187
00188
00189
00195 void distillOne( PartialList & partials, Partial::label_type label,
00196 PartialList & distilled );
00197
00203 void collateUnlabeled( PartialList & unlabled, Partial::label_type startLabel );
00204
00205 };
00206
00207
00208
00209
00235
00236 #if ! defined(NO_TEMPLATE_MEMBERS)
00237 template< typename Container >
00238 typename Container::iterator Distiller::distill( Container & partials )
00239 #else
00240 inline
00241 PartialList::iterator Distiller::distill( PartialList & partials )
00242 #endif
00243 {
00244 #if ! defined(NO_TEMPLATE_MEMBERS)
00245 typedef typename Container::iterator Iterator;
00246 #else
00247 typedef PartialList::iterator Iterator;
00248 #endif
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264 PartialList distilled, savezeros;
00265
00266
00267
00268
00269 Partial::label_type maxlabel = 0;
00270
00271 Iterator lower = partials.begin();
00272 while ( lower != partials.end() )
00273 {
00274 Partial::label_type label = lower->label();
00275
00276
00277 Iterator upper =
00278 std::partition( lower, partials.end(),
00279 PartialUtils::isLabelEqual( label ) );
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290 if ( label != 0 )
00291 {
00292 maxlabel = std::max( label, maxlabel );
00293
00294
00295
00296 PartialList samelabel( lower, upper );
00297 distillOne( samelabel, label, distilled );
00298 }
00299 else
00300 {
00301
00302
00303 savezeros.insert( savezeros.begin(), lower, upper );
00304 }
00305
00306 lower = upper;
00307 }
00308
00309
00310
00311
00312 Iterator enddistilled =
00313 std::copy( distilled.begin(), distilled.end(), partials.begin() );
00314
00315
00316
00317
00318 if ( savezeros.empty() )
00319 {
00320
00321
00322
00323 enddistilled = partials.erase( enddistilled, partials.end() );
00324 }
00325 else
00326 {
00327
00328 collateUnlabeled( savezeros, std::max( maxlabel+1, 1 ) );
00329
00330
00331
00332
00333 Iterator endcollated =
00334 std::copy( savezeros.begin(), savezeros.end(), enddistilled );
00335
00336
00337
00338
00339
00340 partials.erase( endcollated, partials.end() );
00341 }
00342
00343 return enddistilled;
00344 }
00345
00346
00347
00348
00352
00353 #if ! defined(NO_TEMPLATE_MEMBERS)
00354 template< typename Container >
00355 typename Container::iterator Distiller::operator()( Container & partials )
00356 #else
00357 inline
00358 PartialList::iterator Distiller::operator()( PartialList & partials )
00359 #endif
00360 {
00361 return distill( partials );
00362 }
00363
00364
00365
00366
00389
00390 #if ! defined(NO_TEMPLATE_MEMBERS)
00391 template< typename Container >
00392 typename Container::iterator
00393 Distiller::distill( Container & partials, double partialFadeTime,
00394 double partialSilentTime )
00395 #else
00396 inline
00397 PartialList::iterator
00398 Distiller::distill( PartialList & partials, double partialFadeTime,
00399 double partialSilentTime )
00400 #endif
00401 {
00402 Distiller instance( partialFadeTime, partialSilentTime );
00403 return instance.distill( partials );
00404 }
00405
00406 }
00407
00408 #endif