BOSS 6.6.4.p03
BESIII Offline Software System
Loading...
Searching...
No Matches
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 06:11:40 maqm Exp $
2// author: Sawyer Gillespie
3
4#ifndef _H_FACILITES_UTILITY
5#define _H_FACILITIES_UTILITY
6
7#include <vector>
8#include <algorithm>
9#include <utility>
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, UniPred if_pred, BinPred adj_pred )
21{
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 if (adj_pred ( (*j), (*i) )) return std::make_pair(j,i);
27 j = i++;
28 while (!(if_pred(*i)) && (i != last)) ++i;
29 }
30 return std::make_pair(last, last);
31}
32
33template <class FwdIt, class UniPred, class BinPred>
34inline std::vector<FwdIt> adjacent_find_multiple( const FwdIt& first,
35 const FwdIt& last,
36 UniPred if_pred,
37 BinPred adj_pred,
38 unsigned N = 2 )
39
40// FwdIt class is a forward iterator class into a container (must support ++ operator)
41// BinPred is a binary predicate function which returns bool (note that there are two separate
42// comparison functions: one for the individual element adjacency, and another for group
43// adjacency (that is there are two levels of adjacency here.)
44{
45 std::vector<FwdIt> itvec;
46 std::pair<FwdIt, FwdIt> adjpr1 = adjacent_find_if( first, last, if_pred, adj_pred );
47 if (adjpr1.second == last) return itvec;
48
49 itvec.push_back( adjpr1.first);
50 itvec.push_back( adjpr1.second );
51 std::pair<FwdIt, FwdIt> adjpr2;
52
53 while ((itvec.size() < N) && (adjpr1.second != last)) {
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 itvec.clear();
59 itvec.push_back(adjpr2.first);
60 itvec.push_back(adjpr2.second);
61 }
62 adjpr1.second = adjpr2.second;
63 }
64 return itvec;
65}
66#endif
67
std::vector< FwdIt > adjacent_find_multiple(const FwdIt &first, const FwdIt &last, UniPred if_pred, BinPred adj_pred, unsigned N=2)
Definition: Utility.h:34
std::pair< FwdIt, FwdIt > adjacent_find_if(const FwdIt &first, const FwdIt &last, UniPred if_pred, BinPred adj_pred)
Definition: Utility.h:20