Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

Utility.h

Go to the documentation of this file.
00001 // $Header: /nfs/slac/g/glast/ground/cvs/facilities/facilities/Utility.h,v 1.1.1.1 1999/12/20 22:27:42 burnett Exp $
00002 // author: Sawyer Gillespie
00003 
00004 #ifndef _H_FACILITES_UTILITY
00005 #define _H_FACILITIES_UTILITY
00006 
00007 #include <vector>
00008 #include <algorithm>
00009 #include <utility>
00010 
00011 // utility functions for use with iterators - 
00012 
00013 // NOTE: In STL form, these functions could be encapsulated into iterators 
00014 //  themselves. However the gcc compiler does not necessarily support the 
00015 //  iterator class (?). It is 
00016 //  therefore more simple to create these functions 
00017 //  (it also requires less code, but might not be as useful down the road).
00018 
00019 template <class FwdIt, class UniPred, class BinPred>
00020 inline std::pair<FwdIt, FwdIt>  adjacent_find_if ( const FwdIt& first, const FwdIt& last, UniPred if_pred, BinPred adj_pred )
00021 {
00022     FwdIt   i = std::find_if( first, last, if_pred );
00023     FwdIt   j = i;
00024     if (i != last)  i = std::find_if( ++i, last, if_pred );
00025     while (i  != last) {
00026         if (adj_pred ( (*j), (*i) ))       return std::make_pair(j,i);
00027         j = i++;
00028         while (!(if_pred(*i)) && (i != last)) ++i;
00029     }
00030     return std::make_pair(last, last);
00031 }
00032 
00033 template <class FwdIt, class UniPred, class BinPred>
00034 inline std::vector<FwdIt>  adjacent_find_multiple( const FwdIt& first, 
00035                                             const FwdIt& last, 
00036                                             UniPred if_pred, 
00037                                             BinPred adj_pred, 
00038                                             unsigned N = 2 )
00039 
00040 // FwdIt class is a forward iterator class into a container (must support ++ operator)
00041 // BinPred is a binary predicate function which returns bool (note that there are two separate
00042 //  comparison functions: one for the individual element adjacency, and another for group
00043 //  adjacency (that is there are two levels of adjacency here.)
00044 {
00045     std::vector<FwdIt>  itvec;
00046     std::pair<FwdIt, FwdIt>     adjpr1 = adjacent_find_if( first, last, if_pred, adj_pred );
00047     if (adjpr1.second == last)  return itvec;
00048 
00049     itvec.push_back( adjpr1.first);
00050     itvec.push_back( adjpr1.second );
00051     std::pair<FwdIt, FwdIt> adjpr2;
00052 
00053     while ((itvec.size() < N) && (adjpr1.second != last)) {
00054         adjpr2 = adjacent_find_if( adjpr1.second, last, if_pred, adj_pred );
00055         if (adjpr2.second == last)  return  std::vector<FwdIt>();
00056         if (adj_pred((*adjpr1.second), (*adjpr2.second)))  itvec.push_back(adjpr2.second);
00057         else    {
00058             itvec.clear();
00059             itvec.push_back(adjpr2.first);
00060             itvec.push_back(adjpr2.second);
00061         }
00062         adjpr1.second = adjpr2.second;
00063     }
00064     return itvec;
00065 }
00066 #endif
00067 

Generated at Mon Nov 26 18:18:26 2001 by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000