File indexing completed on 2026-08-06 09:38:32
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef THEPEG_ObjectIndexer_H
0010 #define THEPEG_ObjectIndexer_H
0011
0012
0013 #include "ThePEG/Config/ThePEG.h"
0014 #include <limits>
0015
0016 namespace ThePEG {
0017
0018
0019
0020
0021
0022
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
0032 typedef map<IntT,tTPtr> IndexObjectMap;
0033
0034
0035 typedef map<TPtr,IntT> ObjectIndexMap;
0036
0037 public:
0038
0039
0040
0041
0042 ObjectIndexer(): next(0) {}
0043
0044
0045
0046
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
0061
0062
0063 IntT operator()(tTPtr o) const {
0064 return find(o);
0065 }
0066
0067
0068
0069
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
0078
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
0096
0097
0098 tTPtr operator()(IntT i) const {
0099 return find(i);
0100 }
0101
0102
0103
0104
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
0113
0114
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
0129
0130 bool included(tTPtr o) const {
0131 return objectIndex.find(o) != objectIndex.end();
0132 }
0133
0134
0135
0136
0137 bool included(IntT i) const {
0138 return indexObject.find(i) != indexObject.end();
0139 }
0140
0141
0142
0143
0144 void clear() {
0145 indexObject.clear();
0146 objectIndex.clear();
0147 }
0148
0149
0150
0151
0152 bool empty() const {
0153 return indexObject.empty() && objectIndex.empty();
0154 }
0155
0156 private:
0157
0158
0159
0160
0161 IndexObjectMap indexObject;
0162
0163
0164
0165
0166 ObjectIndexMap objectIndex;
0167
0168
0169
0170
0171 IntT next;
0172
0173 private:
0174
0175
0176
0177
0178 ObjectIndexer & operator=(const ObjectIndexer &) = delete;
0179
0180 };
0181
0182 }
0183
0184 #endif