Back to home page

EIC code displayed by LXR

 
 

    


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

0001 // -*- C++ -*-
0002 //
0003 // ObjectIndexer.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_ObjectIndexer_H
0010 #define THEPEG_ObjectIndexer_H
0011 // This is the declaration of the ObjectIndexer class.
0012 
0013 #include "ThePEG/Config/ThePEG.h"
0014 #include <limits>
0015 
0016 namespace ThePEG {
0017 
0018 /**
0019  * This is a templated class which dynamically associates (reference
0020  * counted) objects to integer indices. By default, all indices will
0021  * be non-negative, but explicit usage of negative indices is
0022  * allowed as long as they do not include NoIndex.
0023  */
0024 template <typename IntT, typename ObjT, IntT NoIndex = static_cast<IntT>(-1)>
0025 class ObjectIndexer {
0026 
0027 public:
0028 
0029   ThePEG_DECLARE_TEMPLATE_POINTERS(ObjT,TPtr);
0030 
0031   /** Map of objects to indices */
0032   typedef map<IntT,tTPtr> IndexObjectMap;
0033 
0034   /** Map of indices to objects. */
0035   typedef map<TPtr,IntT> ObjectIndexMap;
0036 
0037 public:
0038 
0039   /**
0040    * Empty constructor.
0041    */
0042   ObjectIndexer(): next(0) {}
0043 
0044   /**
0045    * Return the index for the given object. If the object is not known,
0046    * a new index will be created.
0047    */
0048   IntT operator()(tTPtr o) {
0049     typename ObjectIndexMap::iterator it = objectIndex.find(o);
0050     if ( it == objectIndex.end() ) {
0051       IntT i = next++;
0052       objectIndex[o] = i;
0053       indexObject[i] = o;
0054       return i;
0055     } else
0056       return it->second;
0057   }
0058 
0059   /**
0060    * Return the index for the given object. If the object is not known,
0061    * NoIndex will be returned.
0062    */
0063   IntT operator()(tTPtr o) const {
0064     return find(o);
0065   }
0066 
0067   /**
0068    * Return the index for the given object. If the object is not known,
0069    * NoIndex will be returned.
0070    */
0071   IntT find(tTPtr o) const {
0072     typename ObjectIndexMap::const_iterator it = objectIndex.find(o);
0073     return it == objectIndex.end()? NoIndex: it->second;
0074   }
0075 
0076   /**
0077    * Return the object for the given index. If the index is not known,
0078    * a new object will be (default) created.
0079    */
0080   tTPtr operator()(IntT i) {
0081     if ( i == NoIndex ) return tTPtr();
0082     typename IndexObjectMap::iterator it = indexObject.find(i);
0083     if ( it == indexObject.end() ) {
0084       TPtr o = new_ptr<ObjT>();
0085       objectIndex[o] = i;
0086       indexObject[i] = o;
0087       next = max(next, i + 1);
0088       return o;
0089     } 
0090     else
0091       return it->second;
0092   }
0093 
0094   /**
0095    * Return the object for the given index. If the index is not known,
0096    * a null pointer will be returned.
0097    */
0098   tTPtr operator()(IntT i) const {
0099     return find(i);
0100   }
0101 
0102   /**
0103    * Return the object for the given index. If the index is not known,
0104    * a null pointer will be returned.
0105    */
0106   tTPtr find(IntT i) const {
0107     typename IndexObjectMap::const_iterator it = indexObject.find(i);
0108     return it == indexObject.end()? tTPtr(): it->second;
0109   }
0110 
0111   /**
0112    * Associate the given object with the given index. Possible other
0113    * associations involving the index or the object is removed. If the
0114    * given index is NoIndex, this function does nothing.
0115    */
0116   void operator()(IntT i, tTPtr o) {
0117     if ( i == NoIndex ) return;
0118     typename IndexObjectMap::iterator iit = indexObject.find(i);
0119     if ( iit != indexObject.end() ) objectIndex.erase(iit->second);
0120     typename ObjectIndexMap::iterator oit = objectIndex.find(o);
0121     if ( oit != objectIndex.end() ) indexObject.erase(oit->second);
0122     objectIndex[o] = i;
0123     indexObject[i] = o;
0124     next = max(next, i + 1);
0125   }
0126 
0127   /**
0128    * Return true if the given object is known.
0129    */
0130   bool included(tTPtr o) const {
0131     return objectIndex.find(o) != objectIndex.end();
0132   }
0133 
0134   /**
0135    * Return true if the given index is known.
0136    */
0137   bool included(IntT i) const {
0138     return indexObject.find(i) != indexObject.end();
0139   }
0140 
0141   /**
0142    * Remove all associations.
0143    */
0144   void clear() {
0145     indexObject.clear();
0146     objectIndex.clear();
0147   }
0148 
0149   /**
0150    * Return true if no associations has been made.
0151    */
0152   bool empty() const {
0153     return indexObject.empty() && objectIndex.empty();
0154   }
0155 
0156 private:
0157 
0158   /**
0159    * All known objects keyed by their indices.
0160    */
0161   IndexObjectMap indexObject;
0162 
0163   /**
0164    * All known indices keyed by the corresponding objects.
0165    */
0166   ObjectIndexMap objectIndex;
0167 
0168   /**
0169    * The next index to be used.
0170    */
0171   IntT next;
0172 
0173 private:
0174 
0175   /**
0176    * Private and non-existent assignment operator.
0177    */
0178   ObjectIndexer & operator=(const ObjectIndexer &) = delete;
0179 
0180 };
0181 
0182 }
0183 
0184 #endif /* THEPEG_ObjectIndexer_H */