ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4FastList.hh
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file G4FastList.hh
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 // Author: Mathieu Karamitros
28 
29 // The code is developed in the framework of the ESA AO7146
30 //
31 // We would be very happy hearing from you, send us your feedback! :)
32 //
33 // In order for Geant4-DNA to be maintained and still open-source,
34 // article citations are crucial.
35 // If you use Geant4-DNA chemistry and you publish papers about your software,
36 // in addition to the general paper on Geant4-DNA:
37 //
38 // Int. J. Model. Simul. Sci. Comput. 1 (2010) 157–178
39 //
40 // we would be very happy if you could please also cite the following
41 // reference papers on chemistry:
42 //
43 // J. Comput. Phys. 274 (2014) 841-882
44 // Prog. Nucl. Sci. Tec. 2 (2011) 503-508
45 
46 #pragma once
47 
48 #include "globals.hh"
50 #include <G4memory.hh>
51 #include <vector>
52 #include <set>
53 //#include "G4ManyFastLists.hh"
54 
55 template<class OBJECT>
56  class G4FastList;
57 template<class OBJECT>
59 template<typename OBJECT>
61 template<typename OBJECT>
63 template<typename OBJECT>
65 template<typename OBJECT>
67 template<class OBJECT>
68  struct sortWatcher;
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  {
92 
93 //#ifdef WIN32
94 // friend typename traits_type::type;
95 // friend typename traits_type::type::node;
96 // friend typename mli_traits_type::type;
101 //#else
102 // friend class traits_type::type;
103 // friend class traits_type::type::node;
104 // friend class mli_traits_type::type;
105 //#endif
106 
107  LIST* fpList;
108 
109 // protected:
110  inline _ListRef(LIST* __list) :
111  fpList(__list)
112  {
113  ;
114  }
115  };
116 
124 template<class OBJECT>
126  {
129 // typedef type_wrapper<LIST> > ListW;
131 // typedef type_wrapper<G4ManyFastLists<typename ObjectW::type> > ManyListsW;
133 // typedef type_wrapper<G4ManyFastLists_iterator<typename ObjectW::type> > ManyListsIteratorW;
135 
136 //#ifdef WIN32
137 // friend typename ListW::type;
138 // friend typename ManyListsW::type;
139 // friend typename ManyListsIteratorW::type;
143 //#else
144 // friend class ListW::type;
145 // friend class ManyListsW::type;
146 // friend struct ManyListsIteratorW::type;
147 //#endif
148 
149  public:
150  ~G4FastListNode();
151 
152  OBJECT* GetObject()
153  {
154  return fpObject;
155  }
156 
157  const OBJECT* GetObject() const
158  {
159  return fpObject;
160  }
161 
163  {
164  return fpNext;
165  }
167  {
168  return fpNext;
169  }
171  {
172  return fpPrevious;
173  }
175  {
176  return fpPrevious;
177  }
178  bool IsAttached()
179  {
180  return fAttachedToList;
181  }
182 
183  //protected:
185  G4FastListNode(OBJECT* track = 0);
186 
188  {
189  fpNext = node;
190  }
192  {
193  fpPrevious = node;
194  }
195  void SetAttachedToList(bool flag)
196  {
197  fAttachedToList = flag;
198  }
199 
200  void UnHook();
201 
202  void DetachYourSelf();
203 
205  G4shared_ptr<_ListRef<G4FastList<OBJECT> > > fListRef;
206  OBJECT* fpObject;
209  };
210 
217 template<class OBJECT>
218  class G4FastList
219  {
220  protected:
222 // G4FastListNode<OBJECT> * fpStart;
223 // G4FastListNode<OBJECT> * fpFinish;
224  G4shared_ptr<_ListRef<G4FastList<OBJECT> > > fListRef;
225 
227  // Must be empty and link to the last non-empty node of the list
228  // and to the first non-empty node of the list (begin())
229  // The iterator returned by end() is linked to this empty node
230 
231  public:
232  class Watcher
233  {
234  public:
235  enum Priority
236  {
242  };
243 
245 
247  {
248  fPriority = Priority::eVeryLow;
249  }
250 
251  virtual ~Watcher()
252  {
253  typename std::set<G4FastList<OBJECT>*>::iterator it = fWatching.begin();
254  typename std::set<G4FastList<OBJECT>*>::iterator end = fWatching.end();
255  for(;it!=end;it++)
256  {
257  (*it)->RemoveWatcher(this);
258  }
259  }
260 
262  return "";
263  }
264 
266  return fPriority;
267  }
268 
269  // ===============================
270  // NOTIFICATIONS
272  // used by PriorityList & ManyFastLists
273 
274  virtual void NotifyAddObject(OBJECT*, G4FastList<OBJECT>*){;}
275  virtual void NotifyRemoveObject(OBJECT*, G4FastList<OBJECT>*){;}
276 // void NotifyEmpty(OBJECT*, G4FastList<OBJECT>*){;}
277 
278  // ===============================
279 
280  void Watch(G4FastList<OBJECT>* fastList)
281  {
282  fWatching.insert(fastList);
283  fastList->AddWatcher(this);
284  }
285 
286  void StopWatching(G4FastList<OBJECT>* fastList, bool removeWatcher = true)
287  {
288  typename std::set<G4FastList<OBJECT>*>::iterator it = fWatching.find(fastList);
289  if(it == fWatching.end()) return; //TODO: exception?
290  fWatching.erase(it);
291  if(removeWatcher) fastList->RemoveWatcher(this);
292  }
293 
294  protected:
296 
297  private:
298  std::set<G4FastList<OBJECT>*> fWatching;
299  };
300 
301  template<typename WATCHER_TYPE>
302  class TWatcher : public Watcher
303  {
304  public:
305  TWatcher() : Watcher(){;}
306  virtual ~TWatcher(){}
308  {
309  return typeid(WATCHER_TYPE).name();
310  }
311  };
312 
313  protected:
314  typedef std::set<typename G4FastList<OBJECT>::Watcher*,
318 
319  public:
320  typedef OBJECT object;
324 
325  G4FastList();
326  ~G4FastList();
327 
329  {
330  fpNodeInManyLists = __node;
331  }
332 
334  {
335  return fpNodeInManyLists;
336  }
337 
338  void AddWatcher(Watcher* watcher)
339  {
340  fWatchers.insert(watcher);
341  }
342 
343  void RemoveWatcher(Watcher* watcher)
344  {
345  typename WatcherSet::iterator it = fWatchers.find(watcher);
346  if(it == fWatchers.end()) return; //TODO: exception?
347  fWatchers.erase(it);
348  }
349 
350  inline OBJECT* back()
351  {
352 // if (fNbObjects != 0) return fpFinish->GetObject();
353  if (fNbObjects != 0) return fBoundary.GetPrevious()->GetObject();
354  else return 0;
355  }
356 
357  inline G4int size() const
358  {
359  return fNbObjects;
360  }
361 
362  inline bool empty() const;
363  iterator insert(iterator /*position*/, OBJECT*);
364 
365  inline iterator begin();
366  inline const_iterator begin() const;
367 
368  inline iterator end();
369  inline const_iterator end() const;
375  bool Holds(const OBJECT*) const;
376 
377  inline void push_front(OBJECT* __track);
378  inline void push_back(OBJECT* __track);
379  OBJECT* pop_back();
380 
381  void remove(OBJECT*);
382 
383  iterator pop(OBJECT*);
385  iterator pop(iterator __first, iterator __last);
386  iterator erase(OBJECT*);
393  iterator erase(iterator __first, iterator __last);
398  void clear();
404  static G4FastListNode<OBJECT>* GetNode(OBJECT*);
405  static void SetNode(OBJECT* __obj, G4FastListNode<OBJECT>* __node);
406  static G4FastList<OBJECT>* GetList(OBJECT*);
407  static G4FastList<OBJECT>* GetList(G4FastListNode<OBJECT>* __trackListNode);
408  static void Pop(OBJECT*);
409 
410  protected:
412  static G4FastListNode<OBJECT>* __GetNode(OBJECT*);
413  G4FastListNode<OBJECT>* Flag(OBJECT*);
414  G4FastListNode<OBJECT>* Unflag(OBJECT*);
415  void Unflag(G4FastListNode<OBJECT>* __trackListNode);
417  void DeleteObject(OBJECT*);
418 
419  void Hook(G4FastListNode<OBJECT>* /*position*/,
420  G4FastListNode<OBJECT>* /*toHook*/);
423 
424  private:
425  G4FastList(const G4FastList<OBJECT>& other);
429  };
430 
431 
432 template<class OBJECT>
433  struct sortWatcher
434  {
436  const typename G4FastList<OBJECT>::Watcher* right) const
437  {
438  if(left && right)
439  {
440  if(left->GetPriority() != right->GetPriority())
441  {
442  return left->GetPriority() < right->GetPriority();
443  }
444  return left < right;
445  }
446  return false;
447  }
448  };
449 
450 
456 template<typename OBJECT>
457  struct G4FastList_iterator
458  {
459 // friend class G4FastList<OBJECT>;
462 
463  G4FastList_iterator() = default;
464 
465  explicit G4FastList_iterator(_Node* __x) :
466  fpNode(__x)
467  {
468  }
469 
471  _Self& operator=(const G4FastList_iterator& right) = default;
472 
474  {
475  return fpNode;
476  }
477 
478  const _Node* GetNode() const
479  {
480  return fpNode;
481  }
482 
483  OBJECT*
484  operator*();
485 
486  const OBJECT*
487  operator*() const;
488 
489  OBJECT*
490  operator->();
491 
492  const OBJECT*
493  operator->() const;
494 
495  _Self&
497  {
498  fpNode = fpNode->GetNext();
499  return *this;
500  }
501 
503  {
504  _Self __tmp = *this;
505  fpNode = fpNode->GetNext();
506  return __tmp;
507  }
508 
509  _Self&
511  {
513  return *this;
514  }
515 
517  {
518  _Self __tmp = *this;
520  return __tmp;
521  }
522 
523  G4bool operator==(const _Self& __x) const
524  {
525  return (fpNode == __x.fpNode);
526  }
527 
528  G4bool operator!=(const _Self& __x) const
529  {
530  return (fpNode != __x.fpNode);
531  }
532 
533 // private:
534  // The only member points to the G4FastList_iterator element.
535  _Node* fpNode = nullptr;
536  };
537 
543 template<typename OBJECT>
545  {
546 // friend class G4FastList<OBJECT>;
549 
550  G4FastList_const_iterator() = default;
551 
552  explicit G4FastList_const_iterator(const _Node* __x) :
553  fpNode(__x)
554  {
555  }
556 
558  _Self& operator=(const G4FastList_const_iterator& right) = default;
559 
561  fpNode(right.GetNode())
562  {
563  }
564 
566  {
567  fpNode = right.GetNode();
568  return *this;
569  }
570 
571  const OBJECT*
572  operator*() const
573  {
574  if(fpNode == 0) return 0;
575  return fpNode->GetObject();
576  }
577 
578  const OBJECT*
579  operator->() const
580  {
581  if(fpNode == 0) return 0;
582  return fpNode->GetObject();
583  }
584 
585  _Self&
587  {
588  fpNode = fpNode->GetNext();
589  return *this;
590  }
591 
593  {
594  _Self __tmp = *this;
595  fpNode = fpNode->GetNext();
596  return __tmp;
597  }
598 
599  _Self&
601  {
603  return *this;
604  }
605 
607  {
608  _Self __tmp = *this;
610  return __tmp;
611  }
612 
613  G4bool operator==(const _Self& __x) const
614  {
615  return (fpNode == __x.fpNode);
616  }
617 
618  G4bool operator!=(const _Self& __x) const
619  {
620  return (fpNode != __x.fpNode);
621  }
622 
623 // private:
624  // The only member points to the G4FastList_iterator element.
625  const _Node* fpNode = nullptr;
626  };
627 
628 #include "G4FastList.icc"