Geant4  10.01.p02
G4ManyFastLists.hh
Go to the documentation of this file.
1 //
2 // ********************************************************************
3 // * License and Disclaimer *
4 // * *
5 // * The Geant4 software is copyright of the Copyright Holders of *
6 // * the Geant4 Collaboration. It is provided under the terms and *
7 // * conditions of the Geant4 Software License, included in the file *
8 // * LICENSE and available at http://cern.ch/geant4/license . These *
9 // * include a list of copyright holders. *
10 // * *
11 // * Neither the authors of this software system, nor their employing *
12 // * institutes,nor the agencies providing financial support for this *
13 // * work make any representation or warranty, express or implied, *
14 // * regarding this software system or assume any liability for its *
15 // * use. Please see the license in the file LICENSE and URL above *
16 // * for the full disclaimer and the limitation of liability. *
17 // * *
18 // * This code implementation is the result of the scientific and *
19 // * technical work of the GEANT4 collaboration. *
20 // * By using, copying, modifying or distributing the software (or *
21 // * any work based on the software) you agree to acknowledge its *
22 // * use in resulting scientific publications, and indicate your *
23 // * acceptance of all terms of the Geant4 Software license. *
24 // ********************************************************************
25 //
26 /*
27  * G4ManyFastLists.hh
28  *
29  * Created on: 17 nov. 2014
30  * Author: kara
31  */
32 
33 #ifndef G4MANYFASTLISTS_HH_
34 #define G4MANYFASTLISTS_HH_
35 
36 #include "G4FastList.hh"
37 
38 template<class OBJECT>
40 
41 /*
42  * Roll over many list as if it was one
43  */
44 template<class OBJECT>
45  class G4ManyFastLists : public G4FastList<OBJECT>::Watcher
46  {
47  protected:
49  ManyLists fAssociatedLists;
50  // TODO use "marked list" insted of vector
51 
52  public:
54 
56  fAssociatedLists()
57  {
58  }
59 
60  virtual ~G4ManyFastLists(){;}
61 
62  virtual void NotifyDeletingList(G4FastList<OBJECT>* __list)
63  {
64  fAssociatedLists.pop(__list);
65  }
66 
67  inline void Add(G4FastList<OBJECT>* __list)
68  {
69  if (__list == 0) return;
70  fAssociatedLists.push_back(__list); // TODO use the table doubling tech
71  __list->AddWatcher(this);
72  this->Watch(__list);
73 
74  /*
75  typename ManyLists::const_iterator __it = fAssociatedLists
76  .begin();
77  typename ManyLists::const_iterator __end = fAssociatedLists
78  .end();
79  for (; __it != __end; __it++)
80  {
81  assert(*__it);
82  }
83  */
84  }
85 
86  inline void Remove(G4FastList<OBJECT>* __list)
87  {
88  if (__list == 0) return;
89  fAssociatedLists.pop(__list); // TODO use the table doubling tech
90  __list->RemoveWatcher(this);
91  this->StopWatching(__list);
92 // typename ManyLists::node* __node = __list->GetListNode();
93 // if(__node)
94 // {
95 // __list->SetListNode(0);
96 // delete __node;
97 // }
98  }
99 
100  inline bool Holds(OBJECT* __track) const
101  {
102  typename ManyLists::const_iterator __it = fAssociatedLists.begin();
103  typename ManyLists::const_iterator __end = fAssociatedLists.end();
104  for (; __it != __end; __it++)
105  if ((*__it)->Holds(__track)) return true;
106  return false;
107  }
108 
109  inline size_t size() const
110  {
111  size_t __size(0);
112  typename ManyLists::const_iterator __it = fAssociatedLists
113  .begin();
114  typename ManyLists::const_iterator __end = fAssociatedLists
115  .end();
116  for (; __it != __end; __it++)
117  {
118  __size += (*__it)->size();
119  }
120  return __size;
121  }
122 
123  inline void RemoveLists()
124  {
125  typename ManyLists::iterator __it = fAssociatedLists.begin();
126  typename ManyLists::iterator __end = fAssociatedLists.end();
127  for (; __it != __end; __it++)
128  {
129  if (*__it)
130  {
131  (*__it)->clear();
132  typename ManyLists::iterator next = __it;
133  next++;
134  Remove(*__it);
135  typename ManyLists::node* __node = __it.GetNode();
136  if(__node)
137  {
138  __node->GetObject()->SetListNode(0);
139  delete __node;
140  }
141 // delete (*__it);
142 
143  __it = next;
144  }
145  }
146  fAssociatedLists.clear();
147  }
148 
149  inline void ClearLists()
150  {
151  typename ManyLists::iterator __it = fAssociatedLists.begin();
152  typename ManyLists::iterator __end = fAssociatedLists.end();
153  for (; __it != __end; __it++)
154  if (*__it) (*__it)->clear();
155  }
156 
157  inline iterator begin();
158  inline iterator end();
159 
160  void pop(OBJECT*);
161  };
162 
163 template<class OBJECT>
165  {
166 // friend class G4ManyFastLists<OBJECT>;
168 
171 
174  ManyLists* fLists;
175 
176  private:
178  fIterator(), fLists(0)
179  {
180  }
181 
182  public:
183 
185  typename ManyLists::iterator __it,
186  ManyLists* __lists) :
187  fIterator(__x), fCurrentListIt(__it), fLists(__lists)
188  {
189  }
190 
192  fIterator(__x.fIterator),
193  fCurrentListIt(__x.fCurrentListIt),
194  fLists(__x.fLists)
195  {
196  }
197 
198  _Node* GetNode()
199  {
200  return fIterator.GetNode();
201  }
202 
204  {
205  return *fCurrentListIt;
206  }
207 
208  OBJECT* operator*()
209  {
210  return *fIterator;
211  }
212  const OBJECT* operator*() const
213  {
214  return *fIterator;
215  }
216  OBJECT* operator->()
217  {
218  return *fIterator;
219  }
220  const OBJECT* operator->() const
221  {
222  return *fIterator;
223  }
224 
225  _Self UpdateToNextValidList();
226  _Self& operator++();
227 
228  _Self operator++(int)
229  {
230  return operator++();
231  }
232 
233  _Self&
235  {
236  if (fLists->empty())
237  {
238  fIterator = G4FastList_iterator<OBJECT>();
239  return *this;
240  }
241  if (fCurrentListIt == fLists->begin())
242  {
243  if (fIterator == (*fCurrentListIt)->begin())
244  {
245  fIterator = G4FastList_iterator<OBJECT>();
246  return *this;
247  }
248  }
249 
250  if (fCurrentListIt == fLists->end())
251  {
252  fCurrentListIt--;
253  fIterator = (*fCurrentListIt)->end();
254  }
255  else if (fIterator == (*fCurrentListIt)->begin())
256  {
257  fCurrentListIt--;
258  fIterator = (*fCurrentListIt)->end();
259  }
260 
261  fIterator--;
262 
263  while (((*fCurrentListIt)->empty() || fIterator.GetNode() == 0
264  || fIterator.GetNode()->GetObject() == 0)
265  && fCurrentListIt != fLists->begin())
266  {
267  fIterator = (*fCurrentListIt)->begin();
268  fCurrentListIt--;
269  fIterator = (*fCurrentListIt)->end();
270  fIterator--;
271  }
272 
273  if (fIterator.GetNode() == 0 && fCurrentListIt == fLists->begin())
274  {
275  fIterator = G4FastList_iterator<OBJECT>();
276  return *this;
277  }
278 
279  return *this;
280  }
281 
282  _Self operator--(int)
283  {
284  return operator--();
285  }
286 
287  bool operator==(const _Self& __x) const
288  {
289  return (fIterator == __x.fIterator && fCurrentListIt == __x.fCurrentListIt);
290  } // Fast check
291 
292  bool operator!=(const _Self& __x) const
293  {
294  return !(this->operator ==(__x));
295  }
296 
297  protected:
299  {
300  if (fLists->empty() == false)
301  {
302  fIterator = (*(fLists->end()--))->end();
303  }
304  else
305  {
306  fIterator = G4FastList_iterator<OBJECT>();
307  }
308  }
309  };
310 
311 template<class OBJECT>
313  {
314  if (fAssociatedLists.empty())
315  {
317  fAssociatedLists.end(),
318  &fAssociatedLists);
319  }
320 
321  typename G4FastList<OBJECT>::iterator trackList_it;
322  int i = 0;
323 
324  typename ManyLists::iterator it = fAssociatedLists.begin();
325  typename ManyLists::iterator _end = fAssociatedLists.end();
326 
327  while (it != _end)
328  {
329  if (*it && (*it)->empty() == false)
330  {
331  trackList_it = (*it)->begin();
332  break;
333  }
334  i++;
335  it++;
336  };
337 
338  if (i == fAssociatedLists.size() || it == _end)
339  {
340  return end();
341  }
342 
343  return G4ManyFastLists_iterator<OBJECT>(trackList_it,
344 // fAssociatedLists.begin(),
345  it,
346  &fAssociatedLists);
347  }
348 
349 template<class OBJECT>
351  {
352  if (fAssociatedLists.empty())
353  {
355  fAssociatedLists.end(),
356  &fAssociatedLists);
357  }
358 
359  return G4ManyFastLists_iterator<OBJECT>((fAssociatedLists.end()--)->end(),
360  fAssociatedLists.end(),
361  &fAssociatedLists);
362  }
363 
364 #include "G4ManyFastLists.icc"
365 #endif /* G4MANYFASTLISTS_HH_ */
G4ManyFastLists_iterator _Self
G4FastList is used by G4TrackHolder to save G4IT tracks only.
Definition: G4FastList.hh:58
void AddWatcher(Watcher *watcher)
Definition: G4FastList.hh:292
void Remove(G4FastList< OBJECT > *__list)
G4FastList_iterator enables to go through the tracks contained by a list.
Definition: G4FastList.hh:62
G4FastList< G4FastList< OBJECT > > ManyLists
G4ManyFastLists< OBJECT > * GetTrackList()
const OBJECT * operator*() const
OBJECT * GetObject()
Definition: G4FastList.hh:151
virtual void NotifyDeletingList(G4FastList< OBJECT > *__list)
iterator pop(OBJECT *)
void Add(G4FastList< OBJECT > *__list)
iterator begin()
G4ManyFastLists_iterator(G4FastList_iterator< OBJECT > __x, typename ManyLists::iterator __it, ManyLists *__lists)
bool empty() const
void clear()
Complexity = linear in size between __first and __last.
ManyLists::iterator fCurrentListIt
iterator end()
const OBJECT * operator->() const
G4FastListNode< OBJECT > _Node
G4FastList< G4FastList< OBJECT > > ManyLists
void RemoveWatcher(Watcher *watcher)
Definition: G4FastList.hh:297
bool operator!=(const _Self &__x) const
void pop(OBJECT *)
G4ManyFastLists_iterator< OBJECT > iterator
void push_back(OBJECT *__track)
bool Holds(OBJECT *__track) const
void SetListNode(G4FastListNode< G4FastList< OBJECT > > *__node)
Definition: G4FastList.hh:282
size_t size() const
virtual ~G4ManyFastLists()
ManyLists fAssociatedLists
G4ManyFastLists_iterator(const G4ManyFastLists_iterator &__x)
void StopWatching(G4FastList< OBJECT > *fastList, bool removeWatcher=true)
Definition: G4FastList.hh:257
G4FastList_iterator enables to go through the tracks contained by a list.
Definition: G4FastList.hh:64
G4FastList_iterator< OBJECT > fIterator
void Watch(G4FastList< OBJECT > *fastList)
Definition: G4FastList.hh:251
bool operator==(const _Self &__x) const