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_INPUT_ITERATOR_HH
0022 #define EVT_STREAM_INPUT_ITERATOR_HH
0023 
0024 #include "EvtGenBase/EvtStreamAdapter.hh"
0025 
0026 #include <cstddef>
0027 #include <iterator>
0028 
0029 using std::input_iterator_tag;
0030 
0031 // Adapters are used to convert various types of input streams
0032 // into an iteratable interface.
0033 
0034 template <class Point>
0035 class EvtStreamInputIterator {
0036   public:
0037     typedef input_iterator_tag iterator_category;
0038     typedef Point value_type;
0039     typedef ptrdiff_t difference_type;
0040     typedef const Point* pointer;
0041     typedef const Point& reference;
0042 
0043     EvtStreamInputIterator() : _counter( 0 ) {}
0044 
0045     EvtStreamInputIterator( const EvtStreamInputIterator& other ) :
0046         _counter( other._counter ? other._counter->clone() : 0 ),
0047         _currentValue( other._currentValue )
0048     {
0049     }
0050 
0051     EvtStreamInputIterator( EvtStreamAdapter<Point>& counter ) :
0052         _counter( counter.clone() )
0053     {
0054         _currentValue = _counter->currentValue();
0055     }
0056 
0057     ~EvtStreamInputIterator()
0058     {
0059         if ( _counter )
0060             delete _counter;
0061     }
0062 
0063     reference operator*() const { return _currentValue; }
0064 
0065     EvtStreamInputIterator& operator++()
0066     {
0067         _read();
0068         return *this;
0069     }
0070 
0071     EvtStreamInputIterator operator++( int )
0072     {
0073         EvtStreamInputIterator tmp = *this;
0074         _read();
0075         return tmp;
0076     }
0077 
0078     bool operator==( const EvtStreamInputIterator& other ) const
0079     {
0080         // Equality is only defined for two past the end iterators
0081         return ( pastEnd() && other.pastEnd() );
0082     }
0083 
0084   protected:
0085     EvtStreamAdapter<Point>* _counter;
0086     value_type _currentValue;
0087 
0088     bool pastEnd() const
0089     {
0090         bool ret = true;
0091         if ( _counter )
0092             ret = _counter->pastEnd();
0093         return ret;
0094     }
0095 
0096     // Advances the iterator
0097 
0098     void _read()
0099     {
0100         _counter->advance();
0101         _currentValue = _counter->currentValue();
0102     }
0103 };
0104 
0105 // For adaptable generators these shorthand functions can be used
0106 // to construct iterators.
0107 
0108 template <class Generator>
0109 EvtStreamInputIterator<typename Generator::result_type> iter( Generator gen,
0110                                                               int N = 0 )
0111 {
0112     typedef typename Generator::result_type Point;
0113     EvtGenStreamAdapter<Point, Generator> counter( gen, N );
0114     return EvtStreamInputIterator<Point>( counter );
0115 }
0116 
0117 #endif