Logo ROOT   6.10/09
Reference Guide
TList.h
Go to the documentation of this file.
1 // @(#)root/cont:$Id$
2 // Author: Fons Rademakers 10/08/95
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 #ifndef ROOT_TList
13 #define ROOT_TList
14 
15 
16 //////////////////////////////////////////////////////////////////////////
17 // //
18 // TList //
19 // //
20 // A doubly linked list. All classes inheriting from TObject can be //
21 // inserted in a TList. //
22 // //
23 //////////////////////////////////////////////////////////////////////////
24 
25 #include "TSeqCollection.h"
26 #include "TString.h"
27 
28 #include <iterator>
29 
30 #if (__GNUC__ >= 3) && !defined(__INTEL_COMPILER)
31 // Prevent -Weffc++ from complaining about the inheritance
32 // TListIter from std::iterator.
33 #pragma GCC system_header
34 #endif
35 
38 
39 class TObjLink;
40 class TListIter;
41 
42 
43 class TList : public TSeqCollection {
44 
45 friend class TListIter;
46 
47 protected:
48  TObjLink *fFirst; //! pointer to first entry in linked list
49  TObjLink *fLast; //! pointer to last entry in linked list
50  TObjLink *fCache; //! cache to speedup sequential calling of Before() and After() functions
51  Bool_t fAscending; //! sorting order (when calling Sort() or for TSortedList)
52 
53  TObjLink *LinkAt(Int_t idx) const;
54  TObjLink *FindLink(const TObject *obj, Int_t &idx) const;
55  TObjLink **DoSort(TObjLink **head, Int_t n);
57  virtual TObjLink *NewLink(TObject *obj, TObjLink *prev = NULL);
58  virtual TObjLink *NewOptLink(TObject *obj, Option_t *opt, TObjLink *prev = NULL);
59  virtual void DeleteLink(TObjLink *lnk);
60 
61 private:
62  TList(const TList&); // not implemented
63  TList& operator=(const TList&); // not implemented
64 
65 public:
67 
68  TList() : fFirst(0), fLast(0), fCache(0), fAscending(kTRUE) { }
69  TList(TObject *) : fFirst(0), fLast(0), fCache(0), fAscending(kTRUE) { } // for backward compatibility, don't use
70  virtual ~TList();
71  virtual void Clear(Option_t *option="");
72  virtual void Delete(Option_t *option="");
73  virtual TObject *FindObject(const char *name) const;
74  virtual TObject *FindObject(const TObject *obj) const;
75  virtual TIterator *MakeIterator(Bool_t dir = kIterForward) const;
76 
77  virtual void Add(TObject *obj) { AddLast(obj); }
78  virtual void Add(TObject *obj, Option_t *opt) { AddLast(obj, opt); }
79  virtual void AddFirst(TObject *obj);
80  virtual void AddFirst(TObject *obj, Option_t *opt);
81  virtual void AddLast(TObject *obj);
82  virtual void AddLast(TObject *obj, Option_t *opt);
83  virtual void AddAt(TObject *obj, Int_t idx);
84  virtual void AddAfter(const TObject *after, TObject *obj);
85  virtual void AddAfter(TObjLink *after, TObject *obj);
86  virtual void AddBefore(const TObject *before, TObject *obj);
87  virtual void AddBefore(TObjLink *before, TObject *obj);
88  virtual TObject *Remove(TObject *obj);
89  virtual TObject *Remove(TObjLink *lnk);
90  virtual void RemoveLast();
91  virtual void RecursiveRemove(TObject *obj);
92 
93  virtual TObject *At(Int_t idx) const;
94  virtual TObject *After(const TObject *obj) const;
95  virtual TObject *Before(const TObject *obj) const;
96  virtual TObject *First() const;
97  virtual TObjLink *FirstLink() const { return fFirst; }
98  virtual TObject **GetObjectRef(const TObject *obj) const;
99  virtual TObject *Last() const;
100  virtual TObjLink *LastLink() const { return fLast; }
101 
102  virtual void Sort(Bool_t order = kSortAscending);
104 
105  ClassDef(TList,5) //Doubly linked list
106 };
107 
108 
109 //////////////////////////////////////////////////////////////////////////
110 // //
111 // TObjLink //
112 // //
113 // Wrapper around a TObject so it can be stored in a TList. //
114 // //
115 //////////////////////////////////////////////////////////////////////////
116 class TObjLink {
117 
118 friend class TList;
119 
120 private:
124 
125  TObjLink(const TObjLink&); // not implemented
126  TObjLink& operator=(const TObjLink&); // not implemented
127 
128 protected:
129  TObjLink() : fNext(NULL), fPrev(NULL), fObject(NULL) { fNext = fPrev = this; }
130 
131 public:
132  TObjLink(TObject *obj) : fNext(NULL), fPrev(NULL), fObject(obj) { }
133  TObjLink(TObject *obj, TObjLink *lnk);
134  virtual ~TObjLink() { }
135 
136  TObject *GetObject() const { return fObject; }
137  TObject **GetObjectRef() { return &fObject; }
138  void SetObject(TObject *obj) { fObject = obj; }
139  virtual Option_t *GetAddOption() const { return ""; }
140  virtual Option_t *GetOption() const { return fObject->GetOption(); }
141  virtual void SetOption(Option_t *) { }
142  TObjLink *Next() { return fNext; }
143  TObjLink *Prev() { return fPrev; }
144 };
145 
146 
147 //////////////////////////////////////////////////////////////////////////
148 // //
149 // TObjOptLink //
150 // //
151 // Wrapper around a TObject so it can be stored in a TList including //
152 // an option string. //
153 // //
154 //////////////////////////////////////////////////////////////////////////
155 class TObjOptLink : public TObjLink {
156 
157 private:
159 
160 public:
161  TObjOptLink(TObject *obj, Option_t *opt) : TObjLink(obj), fOption(opt) { }
162  TObjOptLink(TObject *obj, TObjLink *lnk, Option_t *opt) : TObjLink(obj, lnk), fOption(opt) { }
164  Option_t *GetAddOption() const { return fOption.Data(); }
165  Option_t *GetOption() const { return fOption.Data(); }
166  void SetOption(Option_t *option) { fOption = option; }
167 };
168 
169 
170 // Preventing warnings with -Weffc++ in GCC since it is a false positive for the TListIter destructor.
171 #if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) >= 40600
172 #pragma GCC diagnostic push
173 #pragma GCC diagnostic ignored "-Weffc++"
174 #endif
175 
176 //////////////////////////////////////////////////////////////////////////
177 // //
178 // TListIter //
179 // //
180 // Iterator of linked list. //
181 // //
182 //////////////////////////////////////////////////////////////////////////
183 class TListIter : public TIterator,
184  public std::iterator<std::bidirectional_iterator_tag,
185  TObject*, std::ptrdiff_t,
186  const TObject**, const TObject*&> {
187 
188 protected:
189  const TList *fList; //list being iterated
190  TObjLink *fCurCursor; //current position in list
191  TObjLink *fCursor; //next position in list
192  Bool_t fDirection; //iteration direction
193  Bool_t fStarted; //iteration started
194 
195  TListIter() : fList(0), fCurCursor(0), fCursor(0), fDirection(kIterForward),
196  fStarted(kFALSE) { }
197 
198 public:
199  TListIter(const TList *l, Bool_t dir = kIterForward);
200  TListIter(const TListIter &iter);
202  TIterator &operator=(const TIterator &rhs);
203  TListIter &operator=(const TListIter &rhs);
204 
205  const TCollection *GetCollection() const { return fList; }
206  Option_t *GetOption() const;
207  void SetOption(Option_t *option);
208  TObject *Next();
209  void Reset();
210  Bool_t operator!=(const TIterator &aIter) const;
211  Bool_t operator!=(const TListIter &aIter) const;
212  TObject *operator*() const { return (fCurCursor ? fCurCursor->GetObject() : nullptr); }
213 
214  ClassDef(TListIter,0) //Linked list iterator
215 };
216 
217 #if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) >= 40600
218 #pragma GCC diagnostic pop
219 #endif
220 
221 #endif
virtual void AddAt(TObject *obj, Int_t idx)
Insert object at position idx in the list.
Definition: TList.cxx:273
virtual void Delete(Option_t *option="")
Remove all objects from the list AND delete all heap based objects.
Definition: TList.cxx:409
virtual TObjLink * NewOptLink(TObject *obj, Option_t *opt, TObjLink *prev=NULL)
Return a new TObjOptLink (a TObjLink that also stores the option).
Definition: TList.cxx:627
Bool_t fStarted
Definition: TList.h:193
TListIter Iterator_t
Definition: TList.h:66
TObjLink * fCursor
Definition: TList.h:191
virtual void DeleteLink(TObjLink *lnk)
Delete a TObjLink object.
Definition: TList.cxx:488
const char Option_t
Definition: RtypesCore.h:62
virtual void RemoveLast()
Remove the last object of the list.
Definition: TList.cxx:751
TObjLink * LinkAt(Int_t idx) const
sorting order (when calling Sort() or for TSortedList)
Definition: TList.cxx:594
friend class TListIter
Definition: TList.h:45
virtual TObject * Last() const
Return the last object in the list. Returns 0 when list is empty.
Definition: TList.cxx:585
virtual void AddFirst(TObject *obj)
Add object at the beginning of the list.
Definition: TList.cxx:97
const TList * fList
Definition: TList.h:189
Basic string class.
Definition: TString.h:129
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
TObjLink * fCurCursor
Definition: TList.h:190
#define NULL
Definition: RtypesCore.h:88
Iterator abstract base class.
Definition: TIterator.h:30
Iterator of linked list.
Definition: TList.h:183
virtual void AddLast(TObject *obj)
Add object at the end of the list.
Definition: TList.cxx:141
virtual TObject * FindObject(const char *name) const
Find an object in this list using its name.
Definition: TList.cxx:501
TList & operator=(const TList &)
virtual void Sort(Bool_t order=kSortAscending)
Sort linked list.
Definition: TList.cxx:775
Sequenceable collection abstract base class.
#define ClassDef(name, id)
Definition: Rtypes.h:297
virtual void Add(TObject *obj, Option_t *opt)
Definition: TList.h:78
Bool_t fDirection
Definition: TList.h:192
const Bool_t kIterForward
Definition: TCollection.h:37
Bool_t LnkCompare(TObjLink *l1, TObjLink *l2)
Compares the objects stored in the TObjLink objects.
Definition: TList.cxx:807
Bool_t operator!=(const TDatime &d1, const TDatime &d2)
Definition: TDatime.h:104
const Bool_t kSortDescending
Definition: TList.h:37
virtual TIterator * MakeIterator(Bool_t dir=kIterForward) const
Return a list iterator.
Definition: TList.cxx:608
TList(TObject *)
Definition: TList.h:69
A doubly linked list.
Definition: TList.h:43
virtual ~TList()
Delete the list.
Definition: TList.cxx:89
const TCollection * GetCollection() const
Definition: TList.h:205
virtual TObject * First() const
Return the first object in the list. Returns 0 when list is empty.
Definition: TList.cxx:561
TListIter()
Definition: TList.h:195
virtual TObject * Remove(TObject *obj)
Remove object from the list.
Definition: TList.cxx:679
Collection abstract base class.
Definition: TCollection.h:42
virtual TObject * Before(const TObject *obj) const
Returns the object before object obj.
Definition: TList.cxx:326
TObjLink * fLast
pointer to first entry in linked list
Definition: TList.h:49
TLine * l
Definition: textangle.C:4
virtual TObject * After(const TObject *obj) const
Returns the object after object obj.
Definition: TList.cxx:293
virtual TObject * At(Int_t idx) const
Returns the object at position idx. Returns 0 if idx is out of range.
Definition: TList.cxx:315
void Reset(Detail::TBranchProxy *x)
virtual void AddBefore(const TObject *before, TObject *obj)
Insert object before object before in the list.
Definition: TList.cxx:177
TObjLink * fFirst
Definition: TList.h:48
virtual TObjLink * FirstLink() const
Definition: TList.h:97
const Bool_t kFALSE
Definition: RtypesCore.h:92
Bool_t fAscending
cache to speedup sequential calling of Before() and After() functions
Definition: TList.h:51
TObject * operator*() const
Return current object or nullptr.
Definition: TList.h:212
const Bool_t kSortAscending
Definition: TList.h:36
virtual Option_t * GetOption() const
Definition: TObject.h:110
virtual void Clear(Option_t *option="")
Remove all objects from the list.
Definition: TList.cxx:353
virtual void AddAfter(const TObject *after, TObject *obj)
Insert object after object after in the list.
Definition: TList.cxx:225
Mother of all ROOT objects.
Definition: TObject.h:37
TList()
Definition: TList.h:68
TObjLink ** DoSort(TObjLink **head, Int_t n)
Sort linked list.
Definition: TList.cxx:819
virtual void Add(TObject *obj)
Definition: TList.h:77
virtual void RecursiveRemove(TObject *obj)
Remove object from this collection and recursively remove the object from all other objects (and coll...
Definition: TList.cxx:639
TObjLink * fCache
pointer to last entry in linked list
Definition: TList.h:50
Bool_t IsAscending()
Definition: TList.h:103
virtual TObjLink * LastLink() const
Definition: TList.h:100
virtual TObjLink * NewLink(TObject *obj, TObjLink *prev=NULL)
Return a new TObjLink.
Definition: TList.cxx:616
const Bool_t kTRUE
Definition: RtypesCore.h:91
~TListIter()
Definition: TList.h:201
virtual TObject ** GetObjectRef(const TObject *obj) const
Return address of pointer to obj.
Definition: TList.cxx:570
const Int_t n
Definition: legend1.C:16
TObjLink * FindLink(const TObject *obj, Int_t &idx) const
Returns the TObjLink object that contains object obj.
Definition: TList.cxx:537
const char * Data() const
Definition: TString.h:347