Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-08-06 09:20:04

0001 
0002 /***********************************************************************
0003 * Copyright 1998-2020 CERN for the benefit of the EvtGen authors       *
0004 *                                                                      *
0005 * This file is part of EvtGen.                                         *
0006 *                                                                      *
0007 * EvtGen is free software: you can redistribute it and/or modify       *
0008 * it under the terms of the GNU General Public License as published by *
0009 * the Free Software Foundation, either version 3 of the License, or    *
0010 * (at your option) any later version.                                  *
0011 *                                                                      *
0012 * EvtGen is distributed in the hope that it will be useful,            *
0013 * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
0014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
0015 * GNU General Public License for more details.                         *
0016 *                                                                      *
0017 * You should have received a copy of the GNU General Public License    *
0018 * along with EvtGen.  If not, see <https://www.gnu.org/licenses/>.     *
0019 ***********************************************************************/
0020 
0021 #ifndef EVT_STREAM_ADAPTER_HH
0022 #define EVT_STREAM_ADAPTER_HH
0023 
0024 // Stream adapters are used to convert a stream-like input (for example,
0025 // a file containing N entries) to an STL like iterator interface. There
0026 // must be a way to get point from the stream, and also an indicator of the
0027 // end of the stream.
0028 
0029 template <class Point>
0030 class EvtStreamAdapter {
0031   public:
0032     EvtStreamAdapter() {}
0033     virtual ~EvtStreamAdapter() {}
0034     virtual EvtStreamAdapter* clone() const = 0;
0035     virtual Point currentValue() = 0;
0036     virtual void advance() = 0;
0037     virtual bool pastEnd() = 0;
0038 };
0039 
0040 // N points are read from a generated stream.
0041 
0042 template <class Point, class Generator>
0043 class EvtGenStreamAdapter : public EvtStreamAdapter<Point> {
0044   public:
0045     EvtGenStreamAdapter( Generator gen, int count ) :
0046         _gen( gen ), _count( count )
0047     {
0048     }
0049 
0050     virtual ~EvtGenStreamAdapter() {}
0051 
0052     EvtStreamAdapter<Point>* clone() const override
0053     {
0054         return new EvtGenStreamAdapter( *this );
0055     }
0056     Point currentValue() override { return _gen(); }
0057     bool pastEnd() override { return ( _count <= 0 ); }
0058     void advance() override { _count--; }
0059 
0060   private:
0061     Generator _gen;
0062     int _count;    // also serves as past the end indicator
0063 };
0064 
0065 // Only points satisfying a predicate are read from the stream.
0066 
0067 template <class Point, class Iterator, class Predicate>
0068 class EvtPredStreamAdapter : public EvtStreamAdapter<Point> {
0069   public:
0070     EvtPredStreamAdapter( Predicate pred, Iterator it, Iterator end ) :
0071         _pred( pred ), _it( it ), _end( end )
0072     {
0073     }
0074     virtual ~EvtPredStreamAdapter() {}
0075 
0076     virtual EvtStreamAdapter<Point>* clone() const
0077     {
0078         return new EvtPredStreamAdapter( *this );
0079     }
0080     virtual Point currentValue()
0081     {
0082         Point value;
0083         while ( !pastEnd() ) {
0084             value = *_it;
0085             if ( _pred( value ) )
0086                 break;
0087             _it++;
0088         }
0089         return value;
0090     }
0091 
0092     virtual bool pastEnd() { return _it == _end; }
0093     virtual void advance() { _it++; }
0094 
0095   private:
0096     Predicate _pred;
0097     Iterator _it;
0098     Iterator _end;
0099 };
0100 
0101 #endif