00001 #ifndef DCHAIN_PARTIALCANDIDATEITR_H 00002 #define DCHAIN_PARTIALCANDIDATEITR_H 00003 // -*- C++ -*- 00004 // 00005 // Package: DChain 00006 // Module: PartialCandidateItr 00007 // 00008 // Description: Iterator through a part of a LabeledCandidateList. 00009 // 00010 // Usage: 00011 // <usage> 00012 // 00013 // Author: Simon Patton 00014 // Created: Wed Sep 11 21:51:25 EDT 1996 00015 // $Id: PartialCandidateItr.h,v 1.1.1.1 2009/03/03 06:06:56 maqm Exp $ 00016 // 00017 // Revision history 00018 // 00019 // $Log: PartialCandidateItr.h,v $ 00020 // Revision 1.1.1.1 2009/03/03 06:06:56 maqm 00021 // first import of DecayChain 00022 // 00023 // Revision 1.1 2006/01/11 20:28:18 cdj 00024 // massive class renaming, addition of [] for selection and unit tests 00025 // 00026 // 00027 00028 // system include files 00029 #include <iterator> 00030 00031 // user include files 00032 #include "DecayChain/Element/conjugation.h" // enumarator 00033 #include "DecayChain/Iterator/candidateitr.h" // distance 00034 #include "DecayChain/Element/LabeledCandidate.h" // equal function 00035 //#include "DecayChain/List/IndexedLabeledCandidates.h" // labeledStonceClass() 00036 00037 // forward declarations 00038 00039 template < class CandidateClass > class IndexedLabeledCandidates ; 00040 00041 namespace dchain { 00042 00043 template < class CandidateClass > class LabeledCandidate ; 00044 template < class CandidateClass > class LabeledCandidateList ; 00045 00046 template < class CandidateClass > 00047 class PartialCandidateItr 00048 { 00049 // friend classses and functions 00050 friend class LabeledCandidateList< CandidateClass > ; 00051 00052 public: 00053 // constants, enums and typedefs 00054 // typedef int distance ; 00055 typedef LabeledCandidate<CandidateClass> value_type; 00056 typedef dchain::candidateitr::difference_type difference_type; 00057 typedef dchain::candidateitr::size_type size_type; 00058 typedef value_type& reference; 00059 typedef value_type* pointer; 00060 00061 typedef std::bidirectional_iterator_tag iterator_category; 00062 00063 00064 // Constructors and destructor 00065 PartialCandidateItr() ; // used by CombinatoricList to build array 00066 PartialCandidateItr( const PartialCandidateItr< CandidateClass >& aOtherItr ) ; 00067 virtual ~PartialCandidateItr() {} 00068 00069 // assignment operator(s) 00070 const PartialCandidateItr< CandidateClass >& operator=( const PartialCandidateItr< CandidateClass >& aOtherItr ) ; 00071 00072 // member functions 00073 PartialCandidateItr< CandidateClass >& operator++() ; 00074 PartialCandidateItr< CandidateClass > operator++( int ) ; 00075 PartialCandidateItr< CandidateClass >& operator--() ; 00076 PartialCandidateItr< CandidateClass > operator--( int ) ; 00077 00078 // const member functions 00079 const LabeledCandidate< CandidateClass >& operator*() const ; 00080 bool operator==( const PartialCandidateItr< CandidateClass >& aOtherItr ) const ; 00081 bool operator!=( const PartialCandidateItr< CandidateClass >& aOtherItr ) const ; 00082 00083 // static member functions 00084 00085 protected: 00086 // Constructors and destructor 00087 PartialCandidateItr( const IndexedLabeledCandidates< CandidateClass >* aList , 00088 const size_type aIndex , 00089 const conjugation::Label aLabel ) ; 00090 00091 // protected member functions 00092 00093 // protected const member functions 00094 IndexedLabeledCandidates< CandidateClass >* indexedCandidates() const ; 00095 size_type index() const ; 00096 conjugation::Label label() const ; 00097 00098 private: 00099 // Constructors and destructor 00100 00101 // private member functions 00102 00103 // private const member functions 00104 00105 // data members 00106 IndexedLabeledCandidates< CandidateClass >* m_indexedCandidates ; 00107 size_type m_index ; 00108 conjugation::Label m_label ; 00109 00110 // static data members 00111 00112 } ; 00113 00114 // inline function definitions 00115 00116 // user include files 00117 00118 // 00119 // forward definitions of inline functions 00120 // 00121 00122 template < class CandidateClass > 00123 inline 00124 IndexedLabeledCandidates< CandidateClass >* PartialCandidateItr< CandidateClass >::indexedCandidates() const 00125 { 00126 return ( m_indexedCandidates ) ; 00127 } 00128 00129 template < class CandidateClass > 00130 inline 00131 typename PartialCandidateItr<CandidateClass>::size_type 00132 PartialCandidateItr< CandidateClass >::index() const 00133 { 00134 return ( m_index ) ; 00135 } 00136 00137 template < class CandidateClass > 00138 inline 00139 conjugation::Label PartialCandidateItr< CandidateClass >::label() const 00140 { 00141 return ( m_label ) ; 00142 } 00143 00144 // 00145 // constructors and destructor 00146 // 00147 00148 template < class CandidateClass > 00149 inline 00150 PartialCandidateItr< CandidateClass >::PartialCandidateItr() 00151 { 00152 } 00153 00154 template < class CandidateClass > 00155 inline 00156 PartialCandidateItr< CandidateClass >::PartialCandidateItr( const PartialCandidateItr< CandidateClass >& aOtherItr ) : 00157 m_indexedCandidates( aOtherItr.indexedCandidates() ) , 00158 m_index( aOtherItr.index() ) , 00159 m_label( aOtherItr.label() ) 00160 { 00161 } 00162 00163 template < class CandidateClass > 00164 inline 00165 PartialCandidateItr< CandidateClass >::PartialCandidateItr( const IndexedLabeledCandidates< CandidateClass >* aList , 00166 const size_type aIndex , 00167 const conjugation::Label aLabel ) : 00168 // cast away const as const_iterator can be assocciated with a non-const list, 00169 // but only const lists use this constructor 00170 m_indexedCandidates( (IndexedLabeledCandidates< CandidateClass >*)aList ) , 00171 m_index( aIndex ) , 00172 m_label( aLabel ) 00173 { 00174 // move to first entry with correct label 00175 if ( ( m_index < size_type( (*m_indexedCandidates).size() ) ) && 00176 ( (*m_indexedCandidates).labeledCandidateClass( m_index ) != m_label ) 00177 ) { 00178 operator++() ; 00179 } 00180 } 00181 00182 // 00183 // assignment operators 00184 // 00185 00186 template < class CandidateClass > 00187 inline 00188 const PartialCandidateItr< CandidateClass >& PartialCandidateItr< CandidateClass >::operator=( const PartialCandidateItr< CandidateClass >& aOtherItr ) 00189 { 00190 m_indexedCandidates = aOtherItr.indexedCandidates() ; 00191 m_index = aOtherItr.index() ; 00192 m_label = aOtherItr.label() ; 00193 return ( *this ) ; 00194 } 00195 00196 // 00197 // member functions 00198 // 00199 00200 template < class CandidateClass > 00201 inline 00202 PartialCandidateItr< CandidateClass >& PartialCandidateItr< CandidateClass >::operator++() 00203 { 00204 ++m_index ; 00205 // This while loop find next match to label. 00206 // The order is done for efficiency reasons. It is more likely a label 00207 // will match than the end of the list has been reached. However this 00208 // will can cause an access to an uninitiallized location, but the 00209 // loop will still terminate correctly. 00210 // update: the efficiency doesn't matter but reading invalid memory 00211 // makes our automated memory checkers have fits 00212 while ( 00213 ( m_index < size_type( (*m_indexedCandidates).size() ) && 00214 ( (*m_indexedCandidates).labeledCandidateClass( m_index ) != m_label ) 00215 ) ) { 00216 ++m_index ; 00217 } 00218 return ( *this ) ; 00219 } 00220 00221 template < class CandidateClass > 00222 inline 00223 PartialCandidateItr< CandidateClass > PartialCandidateItr< CandidateClass >::operator++( int ) 00224 { 00225 PartialCandidateItr< CandidateClass > tmp( *this ) ; 00226 ++m_index ; 00227 // This while loop find next match to label. 00228 // The order is done for efficiency reasons. It is more likely a label 00229 // will match than the end of the list has been reached. However this 00230 // will can cause an access to an uninitiallized location, but the 00231 // loop will still terminate correctly. 00232 while ( 00233 ( (*m_indexedCandidates).labeledCandidateClass( m_index ) != m_label ) && 00234 ( m_index < size_type( (*m_indexedCandidates).size() ) ) ) { 00235 ++m_index ; 00236 } 00237 return ( tmp ) ; 00238 } 00239 00240 template < class CandidateClass > 00241 inline 00242 PartialCandidateItr< CandidateClass >& PartialCandidateItr< CandidateClass >::operator--() 00243 { 00244 // As the index is 'unsigned' it should not go below zero. This behavior is completely 00245 // consistent with the STL reverse_iterator adaptor. (See pp254 of Musser & Saini) 00246 // 00247 if ( 0 != m_index ) { 00248 --m_index ; 00249 } 00250 // This while loop find next match to label. 00251 // The order is done for efficiency reasons. It is more likely a label 00252 // will match than the end of the list has been reached. However this 00253 // will can cause an access to an uninitiallized location, but the 00254 // loop will still terminate correctly. 00255 while ( 00256 ( (*m_indexedCandidates).labeledCandidateClass( m_index ) != m_label ) && 00257 ( m_index != 0 ) ) { 00258 --m_index ; 00259 } 00260 return ( *this ) ; 00261 } 00262 00263 template < class CandidateClass > 00264 inline 00265 PartialCandidateItr< CandidateClass > PartialCandidateItr< CandidateClass >::operator--( int ) 00266 { 00267 PartialCandidateItr< CandidateClass > tmp( *this ) ; 00268 // As the index is 'unsigned' it should not go below zero. This behavior is completely 00269 // consistent with the STL reverse_iterator adaptor. (See pp254 of Musser & Saini) 00270 // 00271 if ( 0 != m_index ) { 00272 --m_index ; 00273 } 00274 // This while loop find next match to label. 00275 // The order is done for efficiency reasons. It is more likely a label 00276 // will match than the end of the list has been reached. However this 00277 // will can cause an access to an uninitiallized location, but the 00278 // loop will still terminate correctly. 00279 while ( 00280 ( (*m_indexedCandidates).labeledCandidateClass( m_index ) != m_label ) && 00281 ( m_index != 0 ) ) { 00282 --m_index ; 00283 } 00284 return ( tmp ) ; 00285 } 00286 00287 // 00288 // const member functions 00289 // 00290 00291 template < class CandidateClass > 00292 inline 00293 const LabeledCandidate< CandidateClass >& PartialCandidateItr< CandidateClass >::operator*() const 00294 { 00295 return ( (*m_indexedCandidates).labeledCandidateClass( m_index ) ) ; 00296 } 00297 00298 template < class CandidateClass > 00299 inline 00300 bool PartialCandidateItr< CandidateClass >::operator==( const PartialCandidateItr< CandidateClass >& aOtherItr ) const 00301 { 00302 // There needs to be a final test on the label as itrs with two different labels 00303 // have the same values for the past-the-end value 00304 return ( ( m_index == aOtherItr.index() ) && 00305 ( m_indexedCandidates == aOtherItr.indexedCandidates() ) && 00306 ( m_label == aOtherItr.label() ) ) ; 00307 } 00308 00309 template < class CandidateClass > 00310 inline 00311 bool PartialCandidateItr< CandidateClass >::operator!=( const PartialCandidateItr< CandidateClass >& aOtherItr ) const 00312 { 00313 // There needs to be a final test on the label as itrs with two different labels 00314 // have the same values for the past-the-end value 00315 return ( ( m_index != aOtherItr.index() ) || 00316 ( m_indexedCandidates != aOtherItr.indexedCandidates() ) || 00317 ( m_label != aOtherItr.label() ) ) ; 00318 } 00319 } 00320 00321 #endif /* DCHAIN_PARTIALCANDIDATEITR_H */