Geant4 10.7.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
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//
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
55template<class OBJECT>
56 class G4FastList;
57template<class OBJECT>
59template<typename OBJECT>
61template<typename OBJECT>
63template<typename OBJECT>
64 class G4ManyFastLists;
65template<typename OBJECT>
67template<class OBJECT>
68 struct sortWatcher;
69
70
71/** Comments :
72 * - A track cannot belong to two different track lists
73 * - Erase a given track is constant complexity
74 * - This development was thought to be used together with G4IT
75 */
76
77#ifndef TYPE_WRAPPER
78#define TYPE_WRAPPER
79template < typename T>
81{
82 typedef T type;
83};
84#endif
85
86template<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;
97////#elif defined(__clang__)
98//// friend T;
99//// friend T::node;
100//// friend G4ManyFastLists_iterator<traits_type::type::object>;
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
117/**
118 * G4FastListNode is the entity actually stored
119 * by the G4FastList. A G4FastListNode should
120 * belong only to one list. Also, an object
121 * should belong only to one list.
122 */
123
124template<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;
140////#elif defined(__clang__)
141//// friend T;
142//// friend G4ManyFastLists_iterator<OBJECT>;
143//#else
144// friend class ListW::type;
145// friend class ManyListsW::type;
146// friend struct ManyListsIteratorW::type;
147//#endif
148
149 public:
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 }
179 {
180 return fAttachedToList;
181 }
182
183 //protected:
184 /** Default constructor */
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
203
205 G4shared_ptr<_ListRef<G4FastList<OBJECT> > > fListRef;
206 OBJECT* fpObject;
209 };
210
211/**
212 * G4FastList is used by G4TrackHolder to save
213 * G4IT tracks only. Its advantage lies to a fast
214 * search of a track in this list.
215 */
216
217template<class OBJECT>
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:
233 {
234 public:
236 {
242 };
243
245
247 {
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:
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
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;
370 /**
371 * return an iterator that contains an empty node
372 * use for boundary checking only
373 */
374
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*);
387 /**
388 * Complexity = constant
389 * By storing the node inside the object, we avoid
390 * searching through all the container.
391 */
392
393 iterator erase(iterator __first, iterator __last);
394 /**
395 * Complexity = linear in size between __first and __last
396 */
397
398 void clear();
400 /**
401 * Complexity = constant
402 */
403
405 static void SetNode(OBJECT* __obj, G4FastListNode<OBJECT>* __node);
406 static G4FastList<OBJECT>* GetList(OBJECT*);
408 static void Pop(OBJECT*);
409
410 protected:
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);
426 G4FastList<OBJECT> & operator=(const G4FastList<OBJECT> &right);
427 G4bool operator==(const G4FastList<OBJECT> &right) const;
428 G4bool operator!=(const G4FastList<OBJECT> &right) const;
429 };
430
431
432template<class OBJECT>
434 {
435 bool operator()(const typename G4FastList<OBJECT>::Watcher* left,
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
451/**
452 * G4FastList_iterator enables to go through
453 * the tracks contained by a list.
454 */
455
456template<typename OBJECT>
458 {
459// friend class G4FastList<OBJECT>;
462
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*
485
486 const OBJECT*
487 operator*() const;
488
489 OBJECT*
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
538/**
539 * G4FastList_iterator enables to go through
540 * the tracks contained by a list.
541 */
542
543template<typename OBJECT>
545 {
546// friend class G4FastList<OBJECT>;
549
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*
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"
bool G4bool
Definition: G4Types.hh:86
int G4int
Definition: G4Types.hh:85
bool IsAttached()
Definition: G4FastList.hh:178
const G4FastListNode< OBJECT > * GetNext() const
Definition: G4FastList.hh:166
const G4FastListNode< OBJECT > * GetPrevious() const
Definition: G4FastList.hh:174
G4FastListNode(OBJECT *track=0)
void SetNext(G4FastListNode< OBJECT > *node)
Definition: G4FastList.hh:187
G4FastListNode< OBJECT > * fpNext
Definition: G4FastList.hh:208
G4FastListNode< OBJECT > * GetPrevious()
Definition: G4FastList.hh:170
const OBJECT * GetObject() const
Definition: G4FastList.hh:157
bool fAttachedToList
Definition: G4FastList.hh:204
OBJECT * fpObject
Definition: G4FastList.hh:206
void DetachYourSelf()
G4shared_ptr< _ListRef< G4FastList< OBJECT > > > fListRef
Definition: G4FastList.hh:205
void SetPrevious(G4FastListNode< OBJECT > *node)
Definition: G4FastList.hh:191
G4FastListNode< OBJECT > * fpPrevious
Definition: G4FastList.hh:207
OBJECT * GetObject()
Definition: G4FastList.hh:152
void SetAttachedToList(bool flag)
Definition: G4FastList.hh:195
G4FastListNode< OBJECT > * GetNext()
Definition: G4FastList.hh:162
virtual G4String GetWatcherName()
Definition: G4FastList.hh:307
Priority GetPriority() const
Definition: G4FastList.hh:265
virtual void NotifyRemoveObject(OBJECT *, G4FastList< OBJECT > *)
Definition: G4FastList.hh:275
virtual void NotifyAddObject(OBJECT *, G4FastList< OBJECT > *)
Definition: G4FastList.hh:274
virtual G4String GetWatcherName()
Definition: G4FastList.hh:261
G4FastList< OBJECT > list
Definition: G4FastList.hh:244
void Watch(G4FastList< OBJECT > *fastList)
Definition: G4FastList.hh:280
void StopWatching(G4FastList< OBJECT > *fastList, bool removeWatcher=true)
Definition: G4FastList.hh:286
void NotifyDeletingList(G4FastList< OBJECT > *)
Definition: G4FastList.hh:271
std::set< typename G4FastList< OBJECT >::Watcher *, sortWatcher< OBJECT > > WatcherSet
Definition: G4FastList.hh:315
static void Pop(OBJECT *)
static G4FastList< OBJECT > * GetList(G4FastListNode< OBJECT > *__trackListNode)
G4FastListNode< OBJECT > * EraseListNode(OBJECT *)
G4FastListNode< OBJECT > * CreateNode(OBJECT *)
G4int size() const
Definition: G4FastList.hh:357
G4FastList_const_iterator< OBJECT > const_iterator
Definition: G4FastList.hh:322
iterator begin()
void RemoveWatcher(Watcher *watcher)
Definition: G4FastList.hh:343
static void SetNode(OBJECT *__obj, G4FastListNode< OBJECT > *__node)
void push_front(OBJECT *__track)
void remove(OBJECT *)
G4FastListNode< OBJECT > * Flag(OBJECT *)
bool empty() const
static G4FastList< OBJECT > * GetList(OBJECT *)
void transferTo(G4FastList< OBJECT > *)
iterator end()
void SetListNode(G4FastListNode< G4FastList< OBJECT > > *__node)
Definition: G4FastList.hh:328
OBJECT * pop_back()
G4FastListNode< OBJECT > * Unflag(OBJECT *)
void Unflag(G4FastListNode< OBJECT > *__trackListNode)
void CheckFlag(G4FastListNode< OBJECT > *)
void Unhook(G4FastListNode< OBJECT > *)
void clear()
iterator pop(OBJECT *)
const_iterator begin() const
static G4FastListNode< OBJECT > * __GetNode(OBJECT *)
G4FastListNode< G4FastList< OBJECT > > * GetListNode()
Definition: G4FastList.hh:333
bool Holds(const OBJECT *) const
iterator insert(iterator, OBJECT *)
G4int fNbObjects
Definition: G4FastList.hh:221
iterator pop(G4FastListNode< OBJECT > *)
G4FastListNode< OBJECT > fBoundary
Definition: G4FastList.hh:226
OBJECT object
Definition: G4FastList.hh:320
const_iterator end() const
G4FastListNode< OBJECT > node
Definition: G4FastList.hh:323
G4FastList_iterator< OBJECT > iterator
Definition: G4FastList.hh:321
iterator erase(iterator __first, iterator __last)
iterator erase(OBJECT *)
G4shared_ptr< _ListRef< G4FastList< OBJECT > > > fListRef
Definition: G4FastList.hh:224
OBJECT * back()
Definition: G4FastList.hh:350
static G4FastListNode< OBJECT > * GetNode(OBJECT *)
void AddWatcher(Watcher *watcher)
Definition: G4FastList.hh:338
void DeleteObject(OBJECT *)
void Hook(G4FastListNode< OBJECT > *, G4FastListNode< OBJECT > *)
void push_back(OBJECT *__track)
iterator pop(iterator __first, iterator __last)
WatcherSet fWatchers
Definition: G4FastList.hh:316
G4FastListNode< G4FastList< OBJECT > > * fpNodeInManyLists
Definition: G4FastList.hh:317
_Self & operator=(const G4FastList_const_iterator &right)=default
G4bool operator!=(const _Self &__x) const
Definition: G4FastList.hh:618
const OBJECT * operator->() const
Definition: G4FastList.hh:579
G4FastList_const_iterator()=default
G4FastList_const_iterator< OBJECT > _Self
Definition: G4FastList.hh:547
G4FastList_const_iterator(const G4FastList_iterator< OBJECT > &right)
Definition: G4FastList.hh:560
G4FastList_const_iterator(const _Node *__x)
Definition: G4FastList.hh:552
G4FastList_const_iterator(const G4FastList_const_iterator &right)=default
G4FastListNode< OBJECT > _Node
Definition: G4FastList.hh:548
_Self & operator=(const G4FastList_iterator< OBJECT > &right)
Definition: G4FastList.hh:565
G4bool operator==(const _Self &__x) const
Definition: G4FastList.hh:613
const OBJECT * operator*() const
Definition: G4FastList.hh:572
G4FastList_iterator(_Node *__x)
Definition: G4FastList.hh:465
G4FastListNode< OBJECT > _Node
Definition: G4FastList.hh:461
OBJECT * operator*()
const OBJECT * operator*() const
G4FastList_iterator< OBJECT > _Self
Definition: G4FastList.hh:460
const OBJECT * operator->() const
G4FastList_iterator()=default
_Self operator--(int)
Definition: G4FastList.hh:516
_Self operator++(int)
Definition: G4FastList.hh:502
G4bool operator!=(const _Self &__x) const
Definition: G4FastList.hh:528
G4bool operator==(const _Self &__x) const
Definition: G4FastList.hh:523
const _Node * GetNode() const
Definition: G4FastList.hh:478
_Self & operator=(const G4FastList_iterator &right)=default
G4FastList_iterator(const G4FastList_iterator &right)=default
_Self & operator--()
Definition: G4FastList.hh:510
_Self & operator++()
Definition: G4FastList.hh:496
OBJECT * operator->()
LIST * fpList
Definition: G4FastList.hh:107
_ListRef(LIST *__list)
Definition: G4FastList.hh:110
type_wrapper< LIST > traits_type
Definition: G4FastList.hh:89
type_wrapper< G4ManyFastLists_iterator< typename LIST::object > > mli_traits_type
Definition: G4FastList.hh:91
bool operator()(const typename G4FastList< OBJECT >::Watcher *left, const typename G4FastList< OBJECT >::Watcher *right) const
Definition: G4FastList.hh:435