Geant4  10.01
G4FastList.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 // $Id: G4FastList.hh 85244 2014-10-27 08:24:13Z gcosmo $
27 //
28 // Author: Mathieu Karamitros, kara@cenbg.in2p3.fr
29 
30 // The code is developed in the framework of the ESA AO7146
31 //
32 // We would be very happy hearing from you, send us your feedback! :)
33 //
34 // In order for Geant4-DNA to be maintained and still open-source,
35 // article citations are crucial.
36 // If you use Geant4-DNA chemistry and you publish papers about your software,
37 // in addition to the general paper on Geant4-DNA:
38 //
39 // Int. J. Model. Simul. Sci. Comput. 1 (2010) 157–178
40 //
41 // we would be very happy if you could please also cite the following
42 // reference papers on chemistry:
43 //
44 // J. Comput. Phys. 274 (2014) 841-882
45 // Prog. Nucl. Sci. Tec. 2 (2011) 503-508
46 
47 #ifndef G4FastList_H
48 #define G4FastList_H
49 
50 #include "globals.hh"
52 #include <CLHEP/Utility/memory.h>
53 #include <vector>
54 #include <set>
55 //#include "G4ManyFastLists.hh"
56 
57 template<class OBJECT>
58  class G4FastList;
59 template<class OBJECT>
61 template<typename OBJECT>
63 template<typename OBJECT>
65 template<typename OBJECT>
67 template<typename OBJECT>
69 
70 
77 #ifndef TYPE_WRAPPER
78 #define TYPE_WRAPPER
79 template < typename T>
81 {
82  typedef T type;
83 };
84 #endif
85 
86 template<class LIST>
87  struct _ListRef
88  {
91 
92 //#ifdef WIN32
93 // friend typename traits_type::type;
94 // friend typename traits_type::type::node;
95 // friend typename mli_traits_type::type;
100 //#else
101 // friend class traits_type::type;
102 // friend class traits_type::type::node;
103 // friend class mli_traits_type::type;
104 //#endif
105 
106  LIST* fpList;
107 
108 // protected:
109  inline _ListRef(LIST* __list) :
110  fpList(__list)
111  {
112  ;
113  }
114  };
115 
123 template<class OBJECT>
125  {
128 // typedef type_wrapper<LIST> > ListW;
130 // typedef type_wrapper<G4ManyFastLists<typename ObjectW::type> > ManyListsW;
132 // typedef type_wrapper<G4ManyFastLists_iterator<typename ObjectW::type> > ManyListsIteratorW;
134 
135 //#ifdef WIN32
136 // friend typename ListW::type;
137 // friend typename ManyListsW::type;
138 // friend typename ManyListsIteratorW::type;
142 //#else
143 // friend class ListW::type;
144 // friend class ManyListsW::type;
145 // friend struct ManyListsIteratorW::type;
146 //#endif
147 
148  public:
149  ~G4FastListNode();
150 
151  OBJECT* GetObject()
152  {
153  return fpObject;
154  }
155 
156  const OBJECT* GetObject() const
157  {
158  return fpObject;
159  }
160 
162  {
163  return fpNext;
164  }
166  {
167  return fpNext;
168  }
170  {
171  return fpPrevious;
172  }
174  {
175  return fpPrevious;
176  }
177  bool IsAttached()
178  {
179  return fAttachedToList;
180  }
181 
182  //protected:
184  G4FastListNode(OBJECT* track = 0);
185 
187  {
188  fpNext = node;
189  }
191  {
192  fpPrevious = node;
193  }
194  void SetAttachedToList(bool flag)
195  {
196  fAttachedToList = flag;
197  }
198 
199  void UnHook();
200 
201  void DetachYourSelf();
202 
204  CLHEP::shared_ptr<_ListRef<G4FastList<OBJECT> > > fListRef;
205  OBJECT* fpObject;
208  };
209 
216 template<class OBJECT>
217  class G4FastList
218  {
219  protected:
221 // G4FastListNode<OBJECT> * fpStart;
222 // G4FastListNode<OBJECT> * fpFinish;
223  CLHEP::shared_ptr<_ListRef<G4FastList<OBJECT> > > fListRef;
224 
226  // Must be empty and link to the last non-empty node of the list
227  // and to the first non-empty node of the list (begin())
228  // The iterator returned by end() is linked to this empty node
229 
230  public:
231  class Watcher
232  {
233  public:
235  Watcher(){;}
236  virtual ~Watcher()
237  {
238  typename std::set<G4FastList<OBJECT>*>::iterator it = fWatching.begin();
239  typename std::set<G4FastList<OBJECT>*>::iterator end = fWatching.end();
240  for(;it!=end;it++)
241  {
242  (*it)->RemoveWatcher(this);
243  }
244  }
245 
249  void NotifyEmpty(OBJECT*, G4FastList<OBJECT>*){;}
250 
251  void Watch(G4FastList<OBJECT>* fastList)
252  {
253  fWatching.insert(fastList);
254  fastList->AddWatcher(this);
255  }
256 
257  void StopWatching(G4FastList<OBJECT>* fastList, bool removeWatcher = true)
258  {
259  typename std::set<G4FastList<OBJECT>*>::iterator it = fWatching.find(fastList);
260  if(it == fWatching.end()) return; //TODO: exception?
261  fWatching.erase(it);
262  if(removeWatcher) fastList->RemoveWatcher(this);
263  }
264 
265  private:
266  std::set<G4FastList<OBJECT>*> fWatching;
267  };
268 
269  protected:
270  std::set<Watcher*> fWatchers;
272 
273  public:
274  typedef OBJECT object;
278 
279  G4FastList();
280  ~G4FastList();
281 
283  {
284  fpNodeInManyLists = __node;
285  }
286 
288  {
289  return fpNodeInManyLists;
290  }
291 
292  void AddWatcher(Watcher* watcher)
293  {
294  fWatchers.insert(watcher);
295  }
296 
297  void RemoveWatcher(Watcher* watcher)
298  {
299  typename std::set<Watcher*>::iterator it = fWatchers.find(watcher);
300  if(it == fWatchers.end()) return; //TODO: exception?
301  fWatchers.erase(it);
302  }
303 
304  inline OBJECT* back()
305  {
306 // if (fNbObjects != 0) return fpFinish->GetObject();
307  if (fNbObjects != 0) return fBoundary.GetPrevious()->GetObject();
308  else return 0;
309  }
310 
311  inline G4int size() const
312  {
313  return fNbObjects;
314  }
315 
316  inline bool empty() const;
317  iterator insert(iterator /*position*/, OBJECT*);
318 
319  inline iterator begin();
320  inline const_iterator begin() const;
321 
322  inline iterator end();
323  inline const_iterator end() const;
329  bool Holds(const OBJECT*) const;
330 
331  inline void push_front(OBJECT* __track);
332  inline void push_back(OBJECT* __track);
333  OBJECT* pop_back();
334 
335  void remove(OBJECT*);
336 
337  iterator pop(OBJECT*);
338  iterator pop(G4FastListNode<OBJECT>*);
339  iterator pop(iterator __first, iterator __last);
340  iterator erase(OBJECT*);
347  iterator erase(iterator __first, iterator __last);
352  void clear();
358  static G4FastListNode<OBJECT>* GetNode(OBJECT*);
359  static void SetNode(OBJECT* __obj, G4FastListNode<OBJECT>* __node);
360  static G4FastList<OBJECT>* GetList(OBJECT*);
361  static G4FastList<OBJECT>* GetList(G4FastListNode<OBJECT>* __trackListNode);
362  static void Pop(OBJECT*);
363 
364  protected:
366  static G4FastListNode<OBJECT>* __GetNode(OBJECT*);
367  G4FastListNode<OBJECT>* Flag(OBJECT*);
368  G4FastListNode<OBJECT>* Unflag(OBJECT*);
369  void Unflag(G4FastListNode<OBJECT>* __trackListNode);
371  void DeleteObject(OBJECT*);
372 
373  void Hook(G4FastListNode<OBJECT>* /*position*/,
374  G4FastListNode<OBJECT>* /*toHook*/);
377 
378  private:
379  G4FastList(const G4FastList<OBJECT>& other);
383  };
384 
385 
391 template<typename OBJECT>
392  struct G4FastList_iterator
393  {
394 // friend class G4FastList<OBJECT>;
397 
399  fpNode(0)
400  {
401  }
402 
403  explicit G4FastList_iterator(_Node* __x) :
404  fpNode(__x)
405  {
406  }
407 
409  fpNode(right.fpNode)
410  {
411  }
412 
413  _Node* GetNode()
414  {
415  return fpNode;
416  }
417 
418  const _Node* GetNode() const
419  {
420  return fpNode;
421  }
422 
423  OBJECT*
424  operator*();
425 
426  const OBJECT*
427  operator*() const;
428 
429  OBJECT*
430  operator->();
431 
432  const OBJECT*
433  operator->() const;
434 
435  _Self&
437  {
438  fpNode = fpNode->GetNext();
439  return *this;
440  }
441 
442  _Self operator++(int)
443  {
444  _Self __tmp = *this;
445  fpNode = fpNode->GetNext();
446  return __tmp;
447  }
448 
449  _Self&
451  {
453  return *this;
454  }
455 
456  _Self operator--(int)
457  {
458  _Self __tmp = *this;
460  return __tmp;
461  }
462 
463  bool operator==(const _Self& __x) const
464  {
465  return (fpNode == __x.fpNode);
466  }
467 
468  bool operator!=(const _Self& __x) const
469  {
470  return (fpNode != __x.fpNode);
471  }
472 
473 // private:
474  // The only member points to the G4FastList_iterator element.
475  _Node* fpNode;
476  };
477 
483 template<typename OBJECT>
485  {
486 // friend class G4FastList<OBJECT>;
489 
491  fpNode(0)
492  {
493  }
494 
495  explicit G4FastList_const_iterator(const _Node* __x) :
496  fpNode(__x)
497  {
498  }
499 
501  fpNode(right.fpNode)
502  {
503  }
504 
506  fpNode(right.GetNode())
507  {
508  }
509 
510  const OBJECT*
511  operator*() const
512  {
513  if(fpNode == 0) return 0;
514  return fpNode->GetObject();
515  }
516 
517  const OBJECT*
518  operator->() const
519  {
520  if(fpNode == 0) return 0;
521  return fpNode->GetObject();
522  }
523 
524  _Self&
526  {
527  fpNode = fpNode->GetNext();
528  return *this;
529  }
530 
531  _Self operator++(int)
532  {
533  _Self __tmp = *this;
534  fpNode = fpNode->GetNext();
535  return __tmp;
536  }
537 
538  _Self&
540  {
542  return *this;
543  }
544 
545  _Self operator--(int)
546  {
547  _Self __tmp = *this;
549  return __tmp;
550  }
551 
552  bool operator==(const _Self& __x) const
553  {
554  return (fpNode == __x.fpNode);
555  }
556 
557  bool operator!=(const _Self& __x) const
558  {
559  return (fpNode != __x.fpNode);
560  }
561 
562 // private:
563  // The only member points to the G4FastList_iterator element.
564  const _Node* fpNode;
565  };
566 
567 #include "G4FastList.icc"
568 
569 #endif // G4FastList_H
G4FastListNode is the entity actually stored by the G4FastList.
Definition: G4FastList.hh:124
void SetAttachedToList(bool flag)
Definition: G4FastList.hh:194
G4int size() const
Definition: G4FastList.hh:311
G4FastList_const_iterator< OBJECT > _Self
Definition: G4FastList.hh:487
G4FastList is used by G4TrackHolder to save G4IT tracks only.
Definition: G4FastList.hh:58
void AddWatcher(Watcher *watcher)
Definition: G4FastList.hh:292
void NotifyRemoveObject(OBJECT *, G4FastList< OBJECT > *)
Definition: G4FastList.hh:248
G4FastList_iterator enables to go through the tracks contained by a list.
Definition: G4FastList.hh:62
G4FastListNode< OBJECT > * Unflag(OBJECT *)
G4FastListNode< OBJECT > * GetNext()
Definition: G4FastList.hh:161
void DeleteObject(OBJECT *)
type_wrapper< G4ManyFastLists_iterator< OBJECT > > ManyListsIteratorW
Definition: G4FastList.hh:133
G4FastListNode< G4FastList< OBJECT > > * fpNodeInManyLists
Definition: G4FastList.hh:271
void SetPrevious(G4FastListNode< OBJECT > *node)
Definition: G4FastList.hh:190
const OBJECT * GetObject() const
Definition: G4FastList.hh:156
OBJECT object
Definition: G4FastList.hh:274
OBJECT * fpObject
Definition: G4FastList.hh:205
OBJECT * GetObject()
Definition: G4FastList.hh:151
static G4FastList< OBJECT > * GetList(OBJECT *)
G4FastList_const_iterator(const _Node *__x)
Definition: G4FastList.hh:495
G4FastListNode< OBJECT > * EraseListNode(OBJECT *)
std::set< G4FastList< OBJECT > * > fWatching
Definition: G4FastList.hh:266
bool operator!=(const _Self &__x) const
Definition: G4FastList.hh:557
static void Pop(OBJECT *)
const _Node * GetNode() const
Definition: G4FastList.hh:418
type_wrapper< OBJECT > ObjectW
Definition: G4FastList.hh:126
G4FastList_iterator< OBJECT > iterator
Definition: G4FastList.hh:275
const OBJECT * operator*() const
Definition: G4FastList.hh:511
_ListRef(LIST *__list)
Definition: G4FastList.hh:109
int G4int
Definition: G4Types.hh:78
iterator pop(OBJECT *)
G4FastListNode< OBJECT > fBoundary
Definition: G4FastList.hh:225
_Self & operator++()
Definition: G4FastList.hh:436
G4FastListNode< OBJECT > * GetPrevious()
Definition: G4FastList.hh:169
G4FastList_const_iterator(const G4FastList_iterator< OBJECT > &right)
Definition: G4FastList.hh:505
type_wrapper< G4ManyFastLists< OBJECT > > ManyListsW
Definition: G4FastList.hh:131
void CheckFlag(G4FastListNode< OBJECT > *)
iterator erase(OBJECT *)
iterator begin()
bool empty() const
type_wrapper< G4ManyFastLists_iterator< typename LIST::object > > mli_traits_type
Definition: G4FastList.hh:90
LIST * fpList
Definition: G4FastList.hh:106
G4FastListNode< OBJECT > _Node
Definition: G4FastList.hh:396
void clear()
Complexity = linear in size between __first and __last.
G4FastListNode< OBJECT > _Node
Definition: G4FastList.hh:488
iterator end()
bool Holds(const OBJECT *) const
return an iterator that contains an empty node use for boundary checking only
void NotifyEmpty(OBJECT *, G4FastList< OBJECT > *)
Definition: G4FastList.hh:249
iterator insert(iterator, OBJECT *)
bool operator==(const _Self &__x) const
Definition: G4FastList.hh:463
G4int operator==(const G4FastList< OBJECT > &right) const
void NotifyDeletingList(G4FastList< OBJECT > *)
Definition: G4FastList.hh:246
const OBJECT * operator->() const
Definition: G4FastList.hh:518
G4FastList_iterator(const G4FastList_iterator &right)
Definition: G4FastList.hh:408
void SetNext(G4FastListNode< OBJECT > *node)
Definition: G4FastList.hh:186
const G4FastListNode< OBJECT > * GetPrevious() const
Definition: G4FastList.hh:173
_Self operator--(int)
Definition: G4FastList.hh:456
type_wrapper< LIST > traits_type
Definition: G4FastList.hh:89
void RemoveWatcher(Watcher *watcher)
Definition: G4FastList.hh:297
G4FastListNode< OBJECT > node
Definition: G4FastList.hh:277
void DetachYourSelf()
const G4FastListNode< OBJECT > * GetNext() const
Definition: G4FastList.hh:165
static G4FastListNode< OBJECT > * GetNode(OBJECT *)
Complexity = constant.
OBJECT * pop_back()
CLHEP::shared_ptr< _ListRef< G4FastList< OBJECT > > > fListRef
Definition: G4FastList.hh:223
void push_back(OBJECT *__track)
OBJECT * operator*()
_Self operator++(int)
Definition: G4FastList.hh:442
void SetListNode(G4FastListNode< G4FastList< OBJECT > > *__node)
Definition: G4FastList.hh:282
G4FastListNode< G4FastList< OBJECT > > * GetListNode()
Definition: G4FastList.hh:287
static void SetNode(OBJECT *__obj, G4FastListNode< OBJECT > *__node)
G4FastListNode< OBJECT > * CreateNode(OBJECT *)
G4FastListNode< OBJECT > * fpNext
Definition: G4FastList.hh:207
G4FastListNode< OBJECT > * fpPrevious
Definition: G4FastList.hh:206
bool fAttachedToList
Definition: G4FastList.hh:203
G4FastList_const_iterator< OBJECT > const_iterator
Definition: G4FastList.hh:276
OBJECT * back()
Definition: G4FastList.hh:304
void Hook(G4FastListNode< OBJECT > *, G4FastListNode< OBJECT > *)
void transferTo(G4FastList< OBJECT > *)
void NotifyNewObject(OBJECT *, G4FastList< OBJECT > *)
Definition: G4FastList.hh:247
void StopWatching(G4FastList< OBJECT > *fastList, bool removeWatcher=true)
Definition: G4FastList.hh:257
OBJECT * operator->()
std::set< Watcher * > fWatchers
Definition: G4FastList.hh:270
CLHEP::shared_ptr< _ListRef< G4FastList< OBJECT > > > fListRef
Definition: G4FastList.hh:204
G4FastListNode< OBJECT > * Flag(OBJECT *)
void push_front(OBJECT *__track)
G4FastList_iterator enables to go through the tracks contained by a list.
Definition: G4FastList.hh:64
G4FastList< OBJECT > & operator=(const G4FastList< OBJECT > &right)
G4FastList_const_iterator(const G4FastList_const_iterator &right)
Definition: G4FastList.hh:500
_Self & operator--()
Definition: G4FastList.hh:450
void Watch(G4FastList< OBJECT > *fastList)
Definition: G4FastList.hh:251
G4FastListNode(OBJECT *track=0)
Default constructor.
static G4FastListNode< OBJECT > * __GetNode(OBJECT *)
G4int fNbObjects
Definition: G4FastList.hh:220
G4FastList_iterator< OBJECT > _Self
Definition: G4FastList.hh:395
G4FastList< typename ObjectW::type > LIST
Definition: G4FastList.hh:127
bool operator!=(const _Self &__x) const
Definition: G4FastList.hh:468
void Unhook(G4FastListNode< OBJECT > *)
bool IsAttached()
Definition: G4FastList.hh:177
G4int operator!=(const G4FastList< OBJECT > &right) const
G4FastList_iterator(_Node *__x)
Definition: G4FastList.hh:403
G4FastList< OBJECT > list
Definition: G4FastList.hh:234
bool operator==(const _Self &__x) const
Definition: G4FastList.hh:552
type_wrapper< G4FastList< OBJECT > > ListW
Definition: G4FastList.hh:129