ROOT  6.06/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 #ifndef ROOT_TSeqCollection
26 #include "TSeqCollection.h"
27 #endif
28 #ifndef ROOT_TString
29 #include "TString.h"
30 #endif
31 
32 #include <iterator>
33 
34 #if (__GNUC__ >= 3) && !defined(__INTEL_COMPILER)
35 // Prevent -Weffc++ from complaining about the inheritance
36 // TListIter from std::iterator.
37 #pragma GCC system_header
38 #endif
39 
42 
43 class TObjLink;
44 class TListIter;
45 
46 
47 class TList : public TSeqCollection {
48 
49 friend class TListIter;
50 
51 protected:
52  TObjLink *fFirst; //! pointer to first entry in linked list
53  TObjLink *fLast; //! pointer to last entry in linked list
54  TObjLink *fCache; //! cache to speedup sequential calling of Before() and After() functions
55  Bool_t fAscending; //! sorting order (when calling Sort() or for TSortedList)
56 
57  TObjLink *LinkAt(Int_t idx) const;
58  TObjLink *FindLink(const TObject *obj, Int_t &idx) const;
59  TObjLink **DoSort(TObjLink **head, Int_t n);
61  virtual TObjLink *NewLink(TObject *obj, TObjLink *prev = NULL);
62  virtual TObjLink *NewOptLink(TObject *obj, Option_t *opt, TObjLink *prev = NULL);
63  virtual void DeleteLink(TObjLink *lnk);
64 
65 private:
66  TList(const TList&); // not implemented
67  TList& operator=(const TList&); // not implemented
68 
69 public:
71 
72  TList() : fFirst(0), fLast(0), fCache(0), fAscending(kTRUE) { }
73  TList(TObject *) : fFirst(0), fLast(0), fCache(0), fAscending(kTRUE) { } // for backward compatibility, don't use
74  virtual ~TList();
75  virtual void Clear(Option_t *option="");
76  virtual void Delete(Option_t *option="");
77  virtual TObject *FindObject(const char *name) const;
78  virtual TObject *FindObject(const TObject *obj) const;
79  virtual TIterator *MakeIterator(Bool_t dir = kIterForward) const;
80 
81  virtual void Add(TObject *obj) { AddLast(obj); }
82  virtual void Add(TObject *obj, Option_t *opt) { AddLast(obj, opt); }
83  virtual void AddFirst(TObject *obj);
84  virtual void AddFirst(TObject *obj, Option_t *opt);
85  virtual void AddLast(TObject *obj);
86  virtual void AddLast(TObject *obj, Option_t *opt);
87  virtual void AddAt(TObject *obj, Int_t idx);
88  virtual void AddAfter(const TObject *after, TObject *obj);
89  virtual void AddAfter(TObjLink *after, TObject *obj);
90  virtual void AddBefore(const TObject *before, TObject *obj);
91  virtual void AddBefore(TObjLink *before, TObject *obj);
92  virtual TObject *Remove(TObject *obj);
93  virtual TObject *Remove(TObjLink *lnk);
94  virtual void RemoveLast();
95  virtual void RecursiveRemove(TObject *obj);
96 
97  virtual TObject *At(Int_t idx) const;
98  virtual TObject *After(const TObject *obj) const;
99  virtual TObject *Before(const TObject *obj) const;
100  virtual TObject *First() const;
101  virtual TObjLink *FirstLink() const { return fFirst; }
102  virtual TObject **GetObjectRef(const TObject *obj) const;
103  virtual TObject *Last() const;
104  virtual TObjLink *LastLink() const { return fLast; }
105 
106  virtual void Sort(Bool_t order = kSortAscending);
108 
109  ClassDef(TList,5) //Doubly linked list
110 };
111 
112 
113 //////////////////////////////////////////////////////////////////////////
114 // //
115 // TObjLink //
116 // //
117 // Wrapper around a TObject so it can be stored in a TList. //
118 // //
119 //////////////////////////////////////////////////////////////////////////
120 class TObjLink {
121 
122 friend class TList;
123 
124 private:
128 
129  TObjLink(const TObjLink&); // not implemented
130  TObjLink& operator=(const TObjLink&); // not implemented
131 
132 protected:
133  TObjLink() : fNext(NULL), fPrev(NULL), fObject(NULL) { fNext = fPrev = this; }
134 
135 public:
136  TObjLink(TObject *obj) : fNext(NULL), fPrev(NULL), fObject(obj) { }
137  TObjLink(TObject *obj, TObjLink *lnk);
138  virtual ~TObjLink() { }
139 
140  TObject *GetObject() const { return fObject; }
141  TObject **GetObjectRef() { return &fObject; }
142  void SetObject(TObject *obj) { fObject = obj; }
143  virtual Option_t *GetAddOption() const { return ""; }
144  virtual Option_t *GetOption() const { return fObject->GetOption(); }
145  virtual void SetOption(Option_t *) { }
146  TObjLink *Next() { return fNext; }
147  TObjLink *Prev() { return fPrev; }
148 };
149 
150 
151 //////////////////////////////////////////////////////////////////////////
152 // //
153 // TObjOptLink //
154 // //
155 // Wrapper around a TObject so it can be stored in a TList including //
156 // an option string. //
157 // //
158 //////////////////////////////////////////////////////////////////////////
159 class TObjOptLink : public TObjLink {
160 
161 private:
163 
164 public:
165  TObjOptLink(TObject *obj, Option_t *opt) : TObjLink(obj), fOption(opt) { }
166  TObjOptLink(TObject *obj, TObjLink *lnk, Option_t *opt) : TObjLink(obj, lnk), fOption(opt) { }
168  Option_t *GetAddOption() const { return fOption.Data(); }
169  Option_t *GetOption() const { return fOption.Data(); }
170  void SetOption(Option_t *option) { fOption = option; }
171 };
172 
173 
174 // Preventing warnings with -Weffc++ in GCC since it is a false positive for the TListIter destructor.
175 #if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) >= 40600
176 #pragma GCC diagnostic push
177 #pragma GCC diagnostic ignored "-Weffc++"
178 #endif
179 
180 //////////////////////////////////////////////////////////////////////////
181 // //
182 // TListIter //
183 // //
184 // Iterator of linked list. //
185 // //
186 //////////////////////////////////////////////////////////////////////////
187 class TListIter : public TIterator,
188  public std::iterator<std::bidirectional_iterator_tag,
189  TObject*, std::ptrdiff_t,
190  const TObject**, const TObject*&> {
191 
192 protected:
193  const TList *fList; //list being iterated
194  TObjLink *fCurCursor; //current position in list
195  TObjLink *fCursor; //next position in list
196  Bool_t fDirection; //iteration direction
197  Bool_t fStarted; //iteration started
198 
199  TListIter() : fList(0), fCurCursor(0), fCursor(0), fDirection(kIterForward),
200  fStarted(kFALSE) { }
201 
202 public:
203  TListIter(const TList *l, Bool_t dir = kIterForward);
204  TListIter(const TListIter &iter);
206  TIterator &operator=(const TIterator &rhs);
207  TListIter &operator=(const TListIter &rhs);
208 
209  const TCollection *GetCollection() const { return fList; }
210  Option_t *GetOption() const;
211  void SetOption(Option_t *option);
212  TObject *Next();
213  void Reset();
214  Bool_t operator!=(const TIterator &aIter) const;
215  Bool_t operator!=(const TListIter &aIter) const;
216  TObject *operator*() const { return (fCurCursor ? fCurCursor->GetObject() : nullptr); }
217 
218  ClassDef(TListIter,0) //Linked list iterator
219 };
220 
221 #if (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__) >= 40600
222 #pragma GCC diagnostic pop
223 #endif
224 
225 #endif
virtual void AddAt(TObject *obj, Int_t idx)
Insert object at position idx in the list.
Definition: TList.cxx:268
virtual ~TList()
virtual void Delete(Option_t *option="")
Remove all objects from the list AND delete all heap based objects.
Definition: TList.cxx:404
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:622
Bool_t fStarted
Definition: TList.h:197
TObjLink * LinkAt(Int_t idx) const
sorting order (when calling Sort() or for TSortedList)
Definition: TList.cxx:589
TListIter Iterator_t
Definition: TList.h:70
TObjLink * fCursor
Definition: TList.h:195
virtual void DeleteLink(TObjLink *lnk)
Delete a TObjLink object.
Definition: TList.cxx:483
const char Option_t
Definition: RtypesCore.h:62
virtual void RemoveLast()
Remove the last object of the list.
Definition: TList.cxx:746
virtual void AddFirst(TObject *obj)
Add object at the beginning of the list.
Definition: TList.cxx:92
const TList * fList
Definition: TList.h:193
Basic string class.
Definition: TString.h:137
Option_t * GetOption() const
Returns the object option stored in the list.
Definition: TList.cxx:959
int Int_t
Definition: RtypesCore.h:41
bool Bool_t
Definition: RtypesCore.h:59
const Bool_t kFALSE
Definition: Rtypes.h:92
virtual TObject * FindObject(const char *name) const
Find an object in this list using its name.
Definition: TList.cxx:496
TObjLink * fCurCursor
Definition: TList.h:194
TObject * Next()
Return next object in the list. Returns 0 when no more objects in list.
Definition: TList.cxx:932
virtual TObject * At(Int_t idx) const
Returns the object at position idx. Returns 0 if idx is out of range.
Definition: TList.cxx:310
Iterator abstract base class.
Definition: TIterator.h:32
Iterator of linked list.
Definition: TList.h:187
virtual void AddLast(TObject *obj)
Add object at the end of the list.
Definition: TList.cxx:136
TList & operator=(const TList &)
virtual void Sort(Bool_t order=kSortAscending)
Sort linked list.
Definition: TList.cxx:770
const char * Data() const
Definition: TString.h:349
Sequenceable collection abstract base class.
virtual TObject * After(const TObject *obj) const
Returns the object after object obj.
Definition: TList.cxx:288
#define ClassDef(name, id)
Definition: Rtypes.h:254
virtual void Add(TObject *obj, Option_t *opt)
Definition: TList.h:82
std::map< std::string, std::string >::const_iterator iter
Definition: TAlienJob.cxx:54
virtual TObjLink * LastLink() const
Definition: TList.h:104
Bool_t fDirection
Definition: TList.h:196
virtual Option_t * GetOption() const
Definition: TObject.h:129
const Bool_t kIterForward
Definition: TCollection.h:43
Bool_t LnkCompare(TObjLink *l1, TObjLink *l2)
Compares the objects stored in the TObjLink objects.
Definition: TList.cxx:802
const TCollection * GetCollection() const
Definition: TList.h:209
const Bool_t kSortDescending
Definition: TList.h:41
TList(TObject *)
Definition: TList.h:73
A doubly linked list.
Definition: TList.h:47
virtual TObject * Before(const TObject *obj) const
Returns the object before object obj.
Definition: TList.cxx:321
TListIter()
Definition: TList.h:199
virtual TObject * Remove(TObject *obj)
Remove object from the list.
Definition: TList.cxx:674
Collection abstract base class.
Definition: TCollection.h:48
TObjLink * fLast
pointer to first entry in linked list
Definition: TList.h:53
TLine * l
Definition: textangle.C:4
virtual TObjLink * FirstLink() const
Definition: TList.h:101
virtual void AddBefore(const TObject *before, TObject *obj)
Insert object before object before in the list.
Definition: TList.cxx:172
TObjLink * fFirst
Definition: TList.h:52
virtual TObject * Last() const
Return the last object in the list. Returns 0 when list is empty.
Definition: TList.cxx:580
Bool_t operator!=(const TIterator &aIter) const
This operator compares two TIterator objects.
Definition: TList.cxx:984
Bool_t fAscending
cache to speedup sequential calling of Before() and After() functions
Definition: TList.h:55
const Bool_t kSortAscending
Definition: TList.h:40
virtual void Clear(Option_t *option="")
Remove all objects from the list.
Definition: TList.cxx:348
void SetOption(Option_t *option)
Sets the object option stored in the list.
Definition: TList.cxx:968
virtual void AddAfter(const TObject *after, TObject *obj)
Insert object after object after in the list.
Definition: TList.cxx:220
#define name(a, b)
Definition: linkTestLib0.cpp:5
Mother of all ROOT objects.
Definition: TObject.h:58
TList()
Definition: TList.h:72
virtual TObject * First() const
Return the first object in the list. Returns 0 when list is empty.
Definition: TList.cxx:556
TObjLink ** DoSort(TObjLink **head, Int_t n)
Sort linked list.
Definition: TList.cxx:814
virtual void Add(TObject *obj)
Definition: TList.h:81
virtual void RecursiveRemove(TObject *obj)
Remove object from this collection and recursively remove the object from all other objects (and coll...
Definition: TList.cxx:634
TObjLink * FindLink(const TObject *obj, Int_t &idx) const
Returns the TObjLink object that contains object obj.
Definition: TList.cxx:532
TObjLink * fCache
pointer to last entry in linked list
Definition: TList.h:54
Bool_t IsAscending()
Definition: TList.h:107
#define NULL
Definition: Rtypes.h:82
void Reset()
Reset list iterator.
Definition: TList.cxx:976
virtual TIterator * MakeIterator(Bool_t dir=kIterForward) const
Return a list iterator.
Definition: TList.cxx:603
virtual TObject ** GetObjectRef(const TObject *obj) const
Return address of pointer to obj.
Definition: TList.cxx:565
TObject * operator*() const
Return current object or nullptr.
Definition: TList.h:216
virtual TObjLink * NewLink(TObject *obj, TObjLink *prev=NULL)
Return a new TObjLink.
Definition: TList.cxx:611
TIterator & operator=(const TIterator &rhs)
Overridden assignment operator.
Definition: TList.cxx:899
const Bool_t kTRUE
Definition: Rtypes.h:91
TObject * obj
~TListIter()
Definition: TList.h:205
const Int_t n
Definition: legend1.C:16