thepeg is hosted by Hepforge, IPPP Durham
ThePEG 2.3.0
ObjectIndexer.h
1// -*- C++ -*-
2//
3// ObjectIndexer.h is a part of ThePEG - Toolkit for HEP Event Generation
4// Copyright (C) 1999-2019 Leif Lonnblad
5//
6// ThePEG is licenced under version 3 of the GPL, see COPYING for details.
7// Please respect the MCnet academic guidelines, see GUIDELINES for details.
8//
9#ifndef THEPEG_ObjectIndexer_H
10#define THEPEG_ObjectIndexer_H
11// This is the declaration of the ObjectIndexer class.
12
14#include <limits>
15
16namespace ThePEG {
17
24template <typename IntT, typename ObjT, IntT NoIndex = static_cast<IntT>(-1)>
26
27public:
28
30
32 typedef map<IntT,tTPtr> IndexObjectMap;
33
35 typedef map<TPtr,IntT> ObjectIndexMap;
36
37public:
38
43
48 IntT operator()(tTPtr o) {
49 typename ObjectIndexMap::iterator it = objectIndex.find(o);
50 if ( it == objectIndex.end() ) {
51 IntT i = next++;
52 objectIndex[o] = i;
53 indexObject[i] = o;
54 return i;
55 } else
56 return it->second;
57 }
58
63 IntT operator()(tTPtr o) const {
64 return find(o);
65 }
66
71 IntT find(tTPtr o) const {
72 typename ObjectIndexMap::const_iterator it = objectIndex.find(o);
73 return it == objectIndex.end()? NoIndex: it->second;
74 }
75
80 tTPtr operator()(IntT i) {
81 if ( i == NoIndex ) return tTPtr();
82 typename IndexObjectMap::iterator it = indexObject.find(i);
83 if ( it == indexObject.end() ) {
84 TPtr o = new_ptr<ObjT>();
85 objectIndex[o] = i;
86 indexObject[i] = o;
87 next = max(next, i + 1);
88 return o;
89 }
90 else
91 return it->second;
92 }
93
98 tTPtr operator()(IntT i) const {
99 return find(i);
100 }
101
106 tTPtr find(IntT i) const {
107 typename IndexObjectMap::const_iterator it = indexObject.find(i);
108 return it == indexObject.end()? tTPtr(): it->second;
109 }
110
116 void operator()(IntT i, tTPtr o) {
117 if ( i == NoIndex ) return;
118 typename IndexObjectMap::iterator iit = indexObject.find(i);
119 if ( iit != indexObject.end() ) objectIndex.erase(iit->second);
120 typename ObjectIndexMap::iterator oit = objectIndex.find(o);
121 if ( oit != objectIndex.end() ) indexObject.erase(oit->second);
122 objectIndex[o] = i;
123 indexObject[i] = o;
124 next = max(next, i + 1);
125 }
126
130 bool included(tTPtr o) const {
131 return objectIndex.find(o) != objectIndex.end();
132 }
133
137 bool included(IntT i) const {
138 return indexObject.find(i) != indexObject.end();
139 }
140
144 void clear() {
145 indexObject.clear();
146 objectIndex.clear();
147 }
148
152 bool empty() const {
153 return indexObject.empty() && objectIndex.empty();
154 }
155
156private:
157
162
167
171 IntT next;
172
173private:
174
179
180};
181
182}
183
184#endif /* THEPEG_ObjectIndexer_H */
#define ThePEG_DECLARE_TEMPLATE_POINTERS(full, abbrev)
This macro helps us to declare pointers and stuff to standard classes.
Definition: Pointers.h:36
This is the main config header file for ThePEG.
This is a templated class which dynamically associates (reference counted) objects to integer indices...
Definition: ObjectIndexer.h:25
void operator()(IntT i, tTPtr o)
Associate the given object with the given index.
void clear()
Remove all associations.
map< IntT, tTPtr > IndexObjectMap
Map of objects to indices.
Definition: ObjectIndexer.h:32
map< TPtr, IntT > ObjectIndexMap
Map of indices to objects.
Definition: ObjectIndexer.h:35
ObjectIndexer & operator=(const ObjectIndexer &)=delete
Private and non-existent assignment operator.
tTPtr operator()(IntT i)
Return the object for the given index.
Definition: ObjectIndexer.h:80
bool empty() const
Return true if no associations has been made.
IntT next
The next index to be used.
ObjectIndexMap objectIndex
All known indices keyed by the corresponding objects.
IndexObjectMap indexObject
All known objects keyed by their indices.
bool included(IntT i) const
Return true if the given index is known.
tTPtr operator()(IntT i) const
Return the object for the given index.
Definition: ObjectIndexer.h:98
ThePEG::Ptr< ObjT >::transient_pointer tTPtr
Alias for a transient pointer to ObjT .
Definition: ObjectIndexer.h:29
bool included(tTPtr o) const
Return true if the given object is known.
IntT operator()(tTPtr o) const
Return the index for the given object.
Definition: ObjectIndexer.h:63
IntT operator()(tTPtr o)
Return the index for the given object.
Definition: ObjectIndexer.h:48
ObjectIndexer()
Empty constructor.
Definition: ObjectIndexer.h:42
IntT find(tTPtr o) const
Return the index for the given object.
Definition: ObjectIndexer.h:71
tTPtr find(IntT i) const
Return the object for the given index.
RCPtr is a reference counted (smart) pointer.
Definition: RCPtr.h:60
TransientRCPtr is a simple wrapper around a bare pointer which can be assigned to and from an RCPtr a...
Definition: RCPtr.h:519
This is the main namespace within which all identifiers in ThePEG are declared.
Definition: FactoryBase.h:28