Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-08-06 09:38:19

0001 // -*- C++ -*-
0002 //
0003 // algorithm.h is a part of ThePEG - Toolkit for HEP Event Generation
0004 // Copyright (C) 1999-2019 Leif Lonnblad
0005 //
0006 // ThePEG is licenced under version 3 of the GPL, see COPYING for details.
0007 // Please respect the MCnet academic guidelines, see GUIDELINES for details.
0008 //
0009 #ifndef ThePEG_algorithm_H
0010 #define ThePEG_algorithm_H
0011 
0012 /** \file
0013  * This file implements a number of interfaces to <code>std::</code>
0014  * algorithms, modified to take a whole container as argument rather
0015  * than a range of iterators, Also defines IteratorRange to
0016  * encapsulate a range of iterators and corresponding algorithms.
0017  */
0018 
0019 #include "ThePEG/Config/ThePEG.h"
0020 #include <algorithm>
0021 
0022 namespace ThePEG {
0023 
0024 /**
0025  * A pair of iterators to be used in specialized algorithms instead
0026  * of the standard first, last construction.
0027  */
0028 template <typename Iterator>
0029 struct IteratorRange: public std::pair<Iterator,Iterator> {
0030 
0031   /** The underlying representation. */
0032   typedef std::pair<Iterator,Iterator> BaseType;
0033 
0034   /** Default constructor. */
0035   IteratorRange() {}
0036 
0037   /** Copy constructor */
0038   IteratorRange(const IteratorRange & ir): BaseType(ir) {}
0039 
0040   /** Constructor taking the underlying pair representation as
0041       argument. */
0042   IteratorRange(const BaseType & ir): BaseType(ir) {}
0043 
0044 };
0045 
0046 /** Return an IteratorRange corresponding to the whole container. */
0047 template <typename Container>
0048 inline IteratorRange<typename Container::iterator>
0049 range(Container & c) {
0050   return std::make_pair(c.begin(), c.end());
0051 }
0052 
0053 /** Return an IteratorRange of const iterators corresponding to the
0054  *  whole container. */
0055 template <typename Container>
0056 inline IteratorRange<typename Container::const_iterator>
0057 range(const Container & c) {
0058   return std::make_pair(c.begin(), c.end());
0059 }
0060 
0061 /** Return an IteratorRange of reverse iterators corresponding to the
0062  *  whole container. */
0063 template <typename Container>
0064 inline IteratorRange<typename Container::reverse_iterator>
0065 rrange(Container & c) {
0066   return std::make_pair(c.rbegin(), c.rend());
0067 }
0068 
0069 /** Return an IteratorRange of reverse const iterators corresponding
0070  *  to the whole container. */
0071 template <typename Container>
0072 inline IteratorRange<typename Container::const_reverse_iterator>
0073 rrange(const Container & c) {
0074   return std::make_pair(c.rbegin(), c.rend());
0075 }
0076 
0077 /** The std::for_each function taking an IteratorRange as argument. */
0078 template <typename Iterator, typename FNC>
0079 inline FNC for_each(IteratorRange<Iterator> r, FNC f) {
0080   return std::for_each(r.first, r.second, f);
0081 }
0082 
0083 /** The std::find function taking an IteratorRange as argument. */
0084 template <typename Iterator, typename T>
0085 inline Iterator find(IteratorRange<Iterator> r, const T & t) {
0086   return std::find(r.first, r.second, t);
0087 }
0088 
0089 /** The std::find_if function taking an IteratorRange as argument. */
0090 template <typename Iterator, typename Pred>
0091 inline Iterator find_if(IteratorRange<Iterator> r, Pred p) {
0092   return std::find_if(r.first, r.second, p);
0093 }
0094 
0095 /** The std::replace function taking an IteratorRange as argument. */
0096 template <typename Iterator, typename T>
0097 inline void replace(IteratorRange<Iterator> r, const T & oval, const T & nval) {
0098   return std::replace(r.first, r.second, oval, nval);
0099 }
0100 
0101 /** The std::for_each function taking a whole container as argument. */
0102 template <typename Cont, typename FNC>
0103 inline FNC for_each(Cont & c, FNC f) {
0104   return std::for_each(c.begin(), c.end(), f);
0105 }
0106 
0107 /** The std::for_each function taking a whole const container as argument. */
0108 template <typename Cont, typename FNC>
0109 inline FNC for_each(const Cont & c, FNC f) {
0110   return std::for_each(c.begin(), c.end(), f);
0111 }
0112 
0113 /** The std::find function taking a whole container as argument. */
0114 template <typename Cont, typename Type>
0115 inline typename Cont::iterator find(Cont & c, const Type & t) {
0116   return find(range(c), t);
0117 }
0118 
0119 /** The std::find function taking a whole const container as argument. */
0120 template <typename Cont, typename Type>
0121 inline typename Cont::const_iterator find(const Cont & c, const Type & t) {
0122   return find(range(c), t);
0123 }
0124 
0125 /** The std::find_if function taking a whole container as argument. */
0126 template <typename Cont, typename Pred>
0127 inline typename Cont::iterator find_if(Cont & c, const Pred & p) {
0128   return find_if(range(c), p);
0129 }
0130 
0131 /** The std::find_if function taking a whole const container as argument. */
0132 template <typename Cont, typename Pred>
0133 inline typename Cont::const_iterator find_if(const Cont & c, const Pred & p) {
0134   return find_if(range(c), p);
0135 }
0136 
0137 /** The std::replace function taking a whole container as argument. */
0138 template <typename Cont, typename T>
0139 inline void replace(Cont & c, const T & oval, const T & nval) {
0140   return replace(range(c), oval, nval);
0141 }
0142 
0143 }
0144 
0145 // #include "algorithm.icc"
0146 #ifndef ThePEG_TEMPLATES_IN_CC_FILE
0147 // #include "algorithm.tcc"
0148 #endif
0149 
0150 #endif /* ThePEG_algorithm_H */