BOSS 8.0.0
BESIII Offline Software System
Loading...
Searching...
No Matches
Calibration/facilities/include/facilities/Utility.h
Go to the documentation of this file.
1// $Header: /bes/bes/BossCvs/Calibration/facilities/facilities/Utility.h,v 1.1.1.1 2005/10/17
2// 06:11:40 maqm Exp $ author: Sawyer Gillespie
3
4#ifndef _H_FACILITES_UTILITY
5# define _H_FACILITIES_UTILITY
6
7# include <algorithm>
8# include <utility>
9# include <vector>
10
11// utility functions for use with iterators -
12
13// NOTE: In STL form, these functions could be encapsulated into iterators
14// themselves. However the gcc compiler does not necessarily support the
15// iterator class (?). It is
16// therefore more simple to create these functions
17// (it also requires less code, but might not be as useful down the road).
18
19template <class FwdIt, class UniPred, class BinPred>
20inline std::pair<FwdIt, FwdIt> adjacent_find_if( const FwdIt& first, const FwdIt& last,
21 UniPred if_pred, BinPred adj_pred ) {
22 FwdIt i = std::find_if( first, last, if_pred );
23 FwdIt j = i;
24 if ( i != last ) i = std::find_if( ++i, last, if_pred );
25 while ( i != last )
26 {
27 if ( adj_pred( ( *j ), ( *i ) ) ) return std::make_pair( j, i );
28 j = i++;
29 while ( !( if_pred( *i ) ) && ( i != last ) ) ++i;
30 }
31 return std::make_pair( last, last );
32}
33
34template <class FwdIt, class UniPred, class BinPred>
35inline std::vector<FwdIt> adjacent_find_multiple( const FwdIt& first, const FwdIt& last,
36 UniPred if_pred, BinPred adj_pred,
37 unsigned N = 2 )
38
39// FwdIt class is a forward iterator class into a container (must support ++ operator)
40// BinPred is a binary predicate function which returns bool (note that there are two separate
41// comparison functions: one for the individual element adjacency, and another for group
42// adjacency (that is there are two levels of adjacency here.)
43{
44 std::vector<FwdIt> itvec;
45 std::pair<FwdIt, FwdIt> adjpr1 = adjacent_find_if( first, last, if_pred, adj_pred );
46 if ( adjpr1.second == last ) return itvec;
47
48 itvec.push_back( adjpr1.first );
49 itvec.push_back( adjpr1.second );
50 std::pair<FwdIt, FwdIt> adjpr2;
51
52 while ( ( itvec.size() < N ) && ( adjpr1.second != last ) )
53 {
54 adjpr2 = adjacent_find_if( adjpr1.second, last, if_pred, adj_pred );
55 if ( adjpr2.second == last ) return std::vector<FwdIt>();
56 if ( adj_pred( ( *adjpr1.second ), ( *adjpr2.second ) ) ) itvec.push_back( adjpr2.second );
57 else
58 {
59 itvec.clear();
60 itvec.push_back( adjpr2.first );
61 itvec.push_back( adjpr2.second );
62 }
63 adjpr1.second = adjpr2.second;
64 }
65 return itvec;
66}
67#endif
std::vector< FwdIt > adjacent_find_multiple(const FwdIt &first, const FwdIt &last, UniPred if_pred, BinPred adj_pred, unsigned N=2)
std::pair< FwdIt, FwdIt > adjacent_find_if(const FwdIt &first, const FwdIt &last, UniPred if_pred, BinPred adj_pred)