00001 // -*- C++ -*- 00002 // 00003 // ObjectIndexer.h is a part of ThePEG - Toolkit for HEP Event Generation 00004 // Copyright (C) 1999-2007 Leif Lonnblad 00005 // 00006 // ThePEG is licenced under version 2 of the GPL, see COPYING for details. 00007 // Please respect the MCnet academic guidelines, see GUIDELINES for details. 00008 // 00009 #ifndef THEPEG_ObjectIndexer_H 00010 #define THEPEG_ObjectIndexer_H 00011 // This is the declaration of the ObjectIndexer class. 00012 00013 #include "ThePEG/Config/ThePEG.h" 00014 00015 namespace ThePEG { 00016 00021 template <typename IntT, typename ObjT> 00022 class ObjectIndexer { 00023 00024 public: 00025 00026 ThePEG_DECLARE_TEMPLATE_POINTERS(ObjT,TPtr); 00027 00029 typedef map<IntT,tTPtr> IndexObjectMap; 00030 00032 typedef map<TPtr,IntT> ObjectIndexMap; 00033 00034 public: 00035 00040 IntT operator()(tTPtr o) { 00041 typename ObjectIndexMap::iterator it = objectIndex.find(o); 00042 if ( it == objectIndex.end() ) { 00043 IntT i = 0; 00044 while ( indexObject.find(i) != indexObject.end() ) ++i; 00045 objectIndex[o] = i; 00046 indexObject[i] = o; 00047 return i; 00048 } else 00049 return it->second; 00050 } 00051 00056 tTPtr operator()(IntT i) { 00057 typename IndexObjectMap::iterator it = indexObject.find(i); 00058 if ( it == indexObject.end() ) { 00059 TPtr o = new_ptr<ObjT>(); 00060 objectIndex[o] = i; 00061 indexObject[i] = o; 00062 return o; 00063 } 00064 else 00065 return it->second; 00066 } 00067 00072 tTPtr operator()(IntT i) const { 00073 return find(i); 00074 } 00075 00080 tTPtr find(IntT i) const { 00081 typename IndexObjectMap::const_iterator it = indexObject.find(i); 00082 return it == indexObject.end()? tTPtr(): it->second; 00083 } 00084 00089 void operator()(IntT i, tTPtr o) { 00090 typename IndexObjectMap::iterator iit = indexObject.find(i); 00091 if ( iit != indexObject.end() ) objectIndex.erase(iit->second); 00092 typename ObjectIndexMap::iterator oit = objectIndex.find(o); 00093 if ( oit != objectIndex.end() ) indexObject.erase(oit->second); 00094 objectIndex[o] = i; 00095 indexObject[i] = o; 00096 } 00097 00101 bool included(tTPtr o) const { 00102 return objectIndex.find(o) != objectIndex.end(); 00103 } 00104 00108 bool included(IntT i) const { 00109 return indexObject.find(i) != indexObject.end(); 00110 } 00111 00115 void clear() { 00116 indexObject.clear(); 00117 objectIndex.clear(); 00118 } 00119 00123 bool empty() const { 00124 return indexObject.empty() && objectIndex.empty(); 00125 } 00126 00127 private: 00128 00132 IndexObjectMap indexObject; 00133 00137 ObjectIndexMap objectIndex; 00138 00139 private: 00140 00144 ObjectIndexer & operator=(const ObjectIndexer &); 00145 00146 }; 00147 00148 } 00149 00150 #endif /* THEPEG_ObjectIndexer_H */