ThePEG  1.8.0
ObjectIndexer.h
1 // -*- C++ -*-
2 //
3 // ObjectIndexer.h is a part of ThePEG - Toolkit for HEP Event Generation
4 // Copyright (C) 1999-2011 Leif Lonnblad
5 //
6 // ThePEG is licenced under version 2 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 
13 #include "ThePEG/Config/ThePEG.h"
14 #include <limits>
15 
16 namespace ThePEG {
17 
24 template <typename IntT, typename ObjT, IntT NoIndex = static_cast<IntT>(-1)>
26 
27 public:
28 
30 
32  typedef map<IntT,tTPtr> IndexObjectMap;
33 
35  typedef map<TPtr,IntT> ObjectIndexMap;
36 
37 public:
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 
156 private:
157 
162 
167 
171  IntT next;
172 
173 private:
174 
179 
180 };
181 
182 }
183 
184 #endif /* THEPEG_ObjectIndexer_H */