Geant4  10.03.p01
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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
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 <G4memory.hh>
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 template<class OBJECT>
70  struct sortWatcher;
71 
72 
79 #ifndef TYPE_WRAPPER
80 #define TYPE_WRAPPER
81 template < typename T>
83 {
84  typedef T type;
85 };
86 #endif
87 
88 template<class LIST>
89  struct _ListRef
90  {
94 
95 //#ifdef WIN32
96 // friend typename traits_type::type;
97 // friend typename traits_type::type::node;
98 // friend typename mli_traits_type::type;
103 //#else
104 // friend class traits_type::type;
105 // friend class traits_type::type::node;
106 // friend class mli_traits_type::type;
107 //#endif
108 
109  LIST* fpList;
110 
111 // protected:
112  inline _ListRef(LIST* __list) :
113  fpList(__list)
114  {
115  ;
116  }
117  };
118 
126 template<class OBJECT>
128  {
131 // typedef type_wrapper<LIST> > ListW;
133 // typedef type_wrapper<G4ManyFastLists<typename ObjectW::type> > ManyListsW;
135 // typedef type_wrapper<G4ManyFastLists_iterator<typename ObjectW::type> > ManyListsIteratorW;
137 
138 //#ifdef WIN32
139 // friend typename ListW::type;
140 // friend typename ManyListsW::type;
141 // friend typename ManyListsIteratorW::type;
145 //#else
146 // friend class ListW::type;
147 // friend class ManyListsW::type;
148 // friend struct ManyListsIteratorW::type;
149 //#endif
150 
151  public:
152  ~G4FastListNode();
153 
154  OBJECT* GetObject()
155  {
156  return fpObject;
157  }
158 
159  const OBJECT* GetObject() const
160  {
161  return fpObject;
162  }
163 
165  {
166  return fpNext;
167  }
169  {
170  return fpNext;
171  }
173  {
174  return fpPrevious;
175  }
177  {
178  return fpPrevious;
179  }
180  bool IsAttached()
181  {
182  return fAttachedToList;
183  }
184 
185  //protected:
187  G4FastListNode(OBJECT* track = 0);
188 
190  {
191  fpNext = node;
192  }
194  {
195  fpPrevious = node;
196  }
197  void SetAttachedToList(bool flag)
198  {
199  fAttachedToList = flag;
200  }
201 
202  void UnHook();
203 
204  void DetachYourSelf();
205 
207  G4shared_ptr<_ListRef<G4FastList<OBJECT> > > fListRef;
208  OBJECT* fpObject;
211  };
212 
219 template<class OBJECT>
220  class G4FastList
221  {
222  protected:
224 // G4FastListNode<OBJECT> * fpStart;
225 // G4FastListNode<OBJECT> * fpFinish;
226  G4shared_ptr<_ListRef<G4FastList<OBJECT> > > fListRef;
227 
229  // Must be empty and link to the last non-empty node of the list
230  // and to the first non-empty node of the list (begin())
231  // The iterator returned by end() is linked to this empty node
232 
233  public:
234  class Watcher
235  {
236  public:
237  enum Priority
238  {
244  };
245 
247 
249  {
250  fPriority = Priority::eVeryLow;
251  }
252 
253  virtual ~Watcher()
254  {
255  typename std::set<G4FastList<OBJECT>*>::iterator it = fWatching.begin();
256  typename std::set<G4FastList<OBJECT>*>::iterator end = fWatching.end();
257  for(;it!=end;it++)
258  {
259  (*it)->RemoveWatcher(this);
260  }
261  }
262 
264  return "";
265  }
266 
268  return fPriority;
269  }
270 
271  // ===============================
272  // NOTIFICATIONS
274  // used by PriorityList & ManyFastLists
275 
276  virtual void NotifyAddObject(OBJECT*, G4FastList<OBJECT>*){;}
277  virtual void NotifyRemoveObject(OBJECT*, G4FastList<OBJECT>*){;}
278 // void NotifyEmpty(OBJECT*, G4FastList<OBJECT>*){;}
279 
280  // ===============================
281 
282  void Watch(G4FastList<OBJECT>* fastList)
283  {
284  fWatching.insert(fastList);
285  fastList->AddWatcher(this);
286  }
287 
288  void StopWatching(G4FastList<OBJECT>* fastList, bool removeWatcher = true)
289  {
290  typename std::set<G4FastList<OBJECT>*>::iterator it = fWatching.find(fastList);
291  if(it == fWatching.end()) return; //TODO: exception?
292  fWatching.erase(it);
293  if(removeWatcher) fastList->RemoveWatcher(this);
294  }
295 
296  protected:
298 
299  private:
300  std::set<G4FastList<OBJECT>*> fWatching;
301  };
302 
303  template<typename WATCHER_TYPE>
304  class TWatcher : public Watcher
305  {
306  public:
307  TWatcher() : Watcher(){;}
308  virtual ~TWatcher(){}
310  {
311  return typeid(WATCHER_TYPE).name();
312  }
313  };
314 
315  protected:
316  typedef std::set<typename G4FastList<OBJECT>::Watcher*,
320 
321  public:
322  typedef OBJECT object;
326 
327  G4FastList();
328  ~G4FastList();
329 
331  {
332  fpNodeInManyLists = __node;
333  }
334 
336  {
337  return fpNodeInManyLists;
338  }
339 
340  void AddWatcher(Watcher* watcher)
341  {
342  fWatchers.insert(watcher);
343  }
344 
345  void RemoveWatcher(Watcher* watcher)
346  {
347  typename WatcherSet::iterator it = fWatchers.find(watcher);
348  if(it == fWatchers.end()) return; //TODO: exception?
349  fWatchers.erase(it);
350  }
351 
352  inline OBJECT* back()
353  {
354 // if (fNbObjects != 0) return fpFinish->GetObject();
355  if (fNbObjects != 0) return fBoundary.GetPrevious()->GetObject();
356  else return 0;
357  }
358 
359  inline G4int size() const
360  {
361  return fNbObjects;
362  }
363 
364  inline bool empty() const;
365  iterator insert(iterator /*position*/, OBJECT*);
366 
367  inline iterator begin();
368  inline const_iterator begin() const;
369 
370  inline iterator end();
371  inline const_iterator end() const;
377  bool Holds(const OBJECT*) const;
378 
379  inline void push_front(OBJECT* __track);
380  inline void push_back(OBJECT* __track);
381  OBJECT* pop_back();
382 
383  void remove(OBJECT*);
384 
385  iterator pop(OBJECT*);
387  iterator pop(iterator __first, iterator __last);
388  iterator erase(OBJECT*);
395  iterator erase(iterator __first, iterator __last);
400  void clear();
406  static G4FastListNode<OBJECT>* GetNode(OBJECT*);
407  static void SetNode(OBJECT* __obj, G4FastListNode<OBJECT>* __node);
408  static G4FastList<OBJECT>* GetList(OBJECT*);
409  static G4FastList<OBJECT>* GetList(G4FastListNode<OBJECT>* __trackListNode);
410  static void Pop(OBJECT*);
411 
412  protected:
414  static G4FastListNode<OBJECT>* __GetNode(OBJECT*);
415  G4FastListNode<OBJECT>* Flag(OBJECT*);
416  G4FastListNode<OBJECT>* Unflag(OBJECT*);
417  void Unflag(G4FastListNode<OBJECT>* __trackListNode);
419  void DeleteObject(OBJECT*);
420 
421  void Hook(G4FastListNode<OBJECT>* /*position*/,
422  G4FastListNode<OBJECT>* /*toHook*/);
425 
426  private:
427  G4FastList(const G4FastList<OBJECT>& other);
428  G4FastList<OBJECT> & operator=(const G4FastList<OBJECT> &right);
429  G4int operator==(const G4FastList<OBJECT> &right) const;
430  G4int operator!=(const G4FastList<OBJECT> &right) const;
431  };
432 
433 
434 template<class OBJECT>
435  struct sortWatcher
436  {
437  bool operator()(const typename G4FastList<OBJECT>::Watcher* left,
438  const typename G4FastList<OBJECT>::Watcher* right) const
439  {
440  if(left && right)
441  {
442  if(left->GetPriority() != right->GetPriority())
443  {
444  return left->GetPriority() < right->GetPriority();
445  }
446  return left < right;
447  }
448  return false;
449  }
450  };
451 
452 
458 template<typename OBJECT>
459  struct G4FastList_iterator
460  {
461 // friend class G4FastList<OBJECT>;
464 
466  fpNode(0)
467  {
468  }
469 
470  explicit G4FastList_iterator(_Node* __x) :
471  fpNode(__x)
472  {
473  }
474 
476  fpNode(right.fpNode)
477  {
478  }
479 
481  {
482  return fpNode;
483  }
484 
485  const _Node* GetNode() const
486  {
487  return fpNode;
488  }
489 
490  OBJECT*
491  operator*();
492 
493  const OBJECT*
494  operator*() const;
495 
496  OBJECT*
497  operator->();
498 
499  const OBJECT*
500  operator->() const;
501 
502  _Self&
504  {
505  fpNode = fpNode->GetNext();
506  return *this;
507  }
508 
510  {
511  _Self __tmp = *this;
512  fpNode = fpNode->GetNext();
513  return __tmp;
514  }
515 
516  _Self&
518  {
520  return *this;
521  }
522 
524  {
525  _Self __tmp = *this;
527  return __tmp;
528  }
529 
530  bool operator==(const _Self& __x) const
531  {
532  return (fpNode == __x.fpNode);
533  }
534 
535  bool operator!=(const _Self& __x) const
536  {
537  return (fpNode != __x.fpNode);
538  }
539 
540 // private:
541  // The only member points to the G4FastList_iterator element.
543  };
544 
550 template<typename OBJECT>
552  {
553 // friend class G4FastList<OBJECT>;
556 
558  fpNode(0)
559  {
560  }
561 
562  explicit G4FastList_const_iterator(const _Node* __x) :
563  fpNode(__x)
564  {
565  }
566 
568  fpNode(right.fpNode)
569  {
570  }
571 
573  fpNode(right.GetNode())
574  {
575  }
576 
577  const OBJECT*
578  operator*() const
579  {
580  if(fpNode == 0) return 0;
581  return fpNode->GetObject();
582  }
583 
584  const OBJECT*
585  operator->() const
586  {
587  if(fpNode == 0) return 0;
588  return fpNode->GetObject();
589  }
590 
591  _Self&
593  {
594  fpNode = fpNode->GetNext();
595  return *this;
596  }
597 
599  {
600  _Self __tmp = *this;
601  fpNode = fpNode->GetNext();
602  return __tmp;
603  }
604 
605  _Self&
607  {
609  return *this;
610  }
611 
613  {
614  _Self __tmp = *this;
616  return __tmp;
617  }
618 
619  bool operator==(const _Self& __x) const
620  {
621  return (fpNode == __x.fpNode);
622  }
623 
624  bool operator!=(const _Self& __x) const
625  {
626  return (fpNode != __x.fpNode);
627  }
628 
629 // private:
630  // The only member points to the G4FastList_iterator element.
631  const _Node* fpNode;
632  };
633 
634 #include "G4FastList.icc"
635 
636 #endif // G4FastList_H
void SetAttachedToList(bool flag)
Definition: G4FastList.hh:197
G4FastList_const_iterator< OBJECT > _Self
Definition: G4FastList.hh:554
const XML_Char * name
Definition: expat.h:151
iterator end()
G4FastListNode< OBJECT > * GetNext()
Definition: G4FastList.hh:164
void SetPrevious(G4FastListNode< OBJECT > *node)
Definition: G4FastList.hh:193
G4FastListNode< OBJECT > * Flag(OBJECT *)
G4FastList_iterator< OBJECT > iterator
Definition: G4FastList.hh:323
G4FastList_const_iterator< OBJECT > const_iterator
Definition: G4FastList.hh:324
G4int fNbObjects
Definition: G4FastList.hh:223
const OBJECT * GetObject() const
Definition: G4FastList.hh:159
OBJECT * pop_back()
OBJECT object
Definition: G4FastList.hh:322
G4FastListNode< G4FastList< OBJECT > > * fpNodeInManyLists
Definition: G4FastList.hh:319
void clear()
G4FastListNode< OBJECT > node
Definition: G4FastList.hh:325
iterator pop(OBJECT *)
static G4FastListNode< OBJECT > * GetNode(OBJECT *)
OBJECT * fpObject
Definition: G4FastList.hh:208
OBJECT * GetObject()
Definition: G4FastList.hh:154
G4FastListNode< OBJECT > * CreateNode(OBJECT *)
G4FastList_const_iterator(const _Node *__x)
Definition: G4FastList.hh:562
bool operator!=(const _Self &__x) const
Definition: G4FastList.hh:624
G4FastListNode< G4FastList< OBJECT > > * GetListNode()
Definition: G4FastList.hh:335
virtual void NotifyRemoveObject(OBJECT *, G4FastList< OBJECT > *)
Definition: G4FastList.hh:277
OBJECT * back()
Definition: G4FastList.hh:352
const _Node * GetNode() const
Definition: G4FastList.hh:485
const OBJECT * operator*() const
Definition: G4FastList.hh:578
G4shared_ptr< _ListRef< G4FastList< OBJECT > > > fListRef
Definition: G4FastList.hh:207
_ListRef(LIST *__list)
Definition: G4FastList.hh:112
int G4int
Definition: G4Types.hh:78
std::set< typename G4FastList< OBJECT >::Watcher *, sortWatcher< OBJECT > > WatcherSet
Definition: G4FastList.hh:317
_Self & operator++()
Definition: G4FastList.hh:503
G4FastListNode< OBJECT > * GetPrevious()
Definition: G4FastList.hh:172
bool empty() const
G4FastList_const_iterator(const G4FastList_iterator< OBJECT > &right)
Definition: G4FastList.hh:572
void AddWatcher(Watcher *watcher)
Definition: G4FastList.hh:340
static void Pop(OBJECT *)
virtual G4String GetWatcherName()
Definition: G4FastList.hh:309
type_wrapper< G4ManyFastLists_iterator< typename LIST::object > > mli_traits_type
Definition: G4FastList.hh:93
LIST * fpList
Definition: G4FastList.hh:109
void DeleteObject(OBJECT *)
G4FastListNode< OBJECT > _Node
Definition: G4FastList.hh:463
void CheckFlag(G4FastListNode< OBJECT > *)
void Hook(G4FastListNode< OBJECT > *, G4FastListNode< OBJECT > *)
bool operator()(const typename G4FastList< OBJECT >::Watcher *left, const typename G4FastList< OBJECT >::Watcher *right) const
Definition: G4FastList.hh:437
G4FastListNode< OBJECT > _Node
Definition: G4FastList.hh:555
virtual G4String GetWatcherName()
Definition: G4FastList.hh:263
virtual void NotifyAddObject(OBJECT *, G4FastList< OBJECT > *)
Definition: G4FastList.hh:276
static G4FastListNode< OBJECT > * __GetNode(OBJECT *)
WatcherSet fWatchers
Definition: G4FastList.hh:318
bool operator==(const _Self &__x) const
Definition: G4FastList.hh:530
void NotifyDeletingList(G4FastList< OBJECT > *)
Definition: G4FastList.hh:273
const OBJECT * operator->() const
Definition: G4FastList.hh:585
G4FastList_iterator(const G4FastList_iterator &right)
Definition: G4FastList.hh:475
void SetListNode(G4FastListNode< G4FastList< OBJECT > > *__node)
Definition: G4FastList.hh:330
void SetNext(G4FastListNode< OBJECT > *node)
Definition: G4FastList.hh:189
const G4FastListNode< OBJECT > * GetPrevious() const
Definition: G4FastList.hh:176
_Self operator--(int)
Definition: G4FastList.hh:523
type_wrapper< LIST > traits_type
Definition: G4FastList.hh:91
void DetachYourSelf()
bool Holds(const OBJECT *) const
const G4FastListNode< OBJECT > * GetNext() const
Definition: G4FastList.hh:168
iterator erase(OBJECT *)
G4FastListNode< OBJECT > * Unflag(OBJECT *)
OBJECT * operator*()
_Self operator++(int)
Definition: G4FastList.hh:509
void push_back(OBJECT *__track)
void transferTo(G4FastList< OBJECT > *)
static G4FastList< OBJECT > * GetList(OBJECT *)
G4FastListNode< OBJECT > * fpNext
Definition: G4FastList.hh:210
G4FastListNode< OBJECT > * fpPrevious
Definition: G4FastList.hh:209
bool fAttachedToList
Definition: G4FastList.hh:206
void push_front(OBJECT *__track)
Priority GetPriority() const
Definition: G4FastList.hh:267
void RemoveWatcher(Watcher *watcher)
Definition: G4FastList.hh:345
G4int size() const
Definition: G4FastList.hh:359
void StopWatching(G4FastList< OBJECT > *fastList, bool removeWatcher=true)
Definition: G4FastList.hh:288
OBJECT * operator->()
G4FastListNode< OBJECT > * EraseListNode(OBJECT *)
G4FastListNode< OBJECT > fBoundary
Definition: G4FastList.hh:228
G4FastList_const_iterator(const G4FastList_const_iterator &right)
Definition: G4FastList.hh:567
_Self & operator--()
Definition: G4FastList.hh:517
void Watch(G4FastList< OBJECT > *fastList)
Definition: G4FastList.hh:282
G4FastListNode(OBJECT *track=0)
static void SetNode(OBJECT *__obj, G4FastListNode< OBJECT > *__node)
G4FastList_iterator< OBJECT > _Self
Definition: G4FastList.hh:462
void Unhook(G4FastListNode< OBJECT > *)
bool operator!=(const _Self &__x) const
Definition: G4FastList.hh:535
iterator begin()
G4shared_ptr< _ListRef< G4FastList< OBJECT > > > fListRef
Definition: G4FastList.hh:226
iterator insert(iterator, OBJECT *)
bool IsAttached()
Definition: G4FastList.hh:180
G4FastList_iterator(_Node *__x)
Definition: G4FastList.hh:470
G4FastList< OBJECT > list
Definition: G4FastList.hh:246
bool operator==(const _Self &__x) const
Definition: G4FastList.hh:619