Logo ROOT   6.10/09
Reference Guide
TSortedList.cxx
Go to the documentation of this file.
1 // @(#)root/cont:$Id$
2 // Author: Fons Rademakers 14/09/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 /** \class TSortedList
13 \ingroup Containers
14 A sorted doubly linked list. All sortable classes inheriting from
15 TObject can be inserted in a TSortedList.
16 */
17 
18 #include "TSortedList.h"
19 
20 
22 
23 ////////////////////////////////////////////////////////////////////////////////
24 /// Add object in sorted list. Uses object Compare() member to find right
25 /// position.
26 
28 {
29  if (IsArgNull("Add", obj)) return;
30 
31  if (!obj->IsSortable()) {
32  Error("Add", "object must be sortable");
33  return;
34  }
35 
36  if (!fFirst) {
37  TList::AddLast(obj);
38  return;
39  }
40 
41  TObjLink *lnk = fFirst;
42 
43  while (lnk) {
44  Int_t cmp = lnk->GetObject()->Compare(obj);
45  if ((IsAscending() && cmp > 0) || (!IsAscending() && cmp < 0)) {
46  if (lnk->Prev()) {
47  NewLink(obj, lnk->Prev());
48  fSize++;
49  return;
50  } else {
51  TList::AddFirst(obj);
52  return;
53  }
54  }
55  lnk = lnk->Next();
56  }
57  TList::AddLast(obj);
58 }
59 
60 ////////////////////////////////////////////////////////////////////////////////
61 /// Add object in sorted list. Uses object Compare() member to find right
62 /// position and also store option. See TList::Add for explanation of
63 /// usage of option.
64 
66 {
67  if (IsArgNull("Add", obj)) return;
68 
69  if (!obj->IsSortable()) {
70  Error("Add", "object must be sortable");
71  return;
72  }
73 
74  if (!fFirst) {
75  TList::Add(obj, opt);
76  return;
77  }
78 
79  TObjLink *lnk = fFirst;
80 
81  while (lnk) {
82  Int_t cmp = lnk->GetObject()->Compare(obj);
83  if ((IsAscending() && cmp > 0) || (!IsAscending() && cmp < 0)) {
84  if (lnk->Prev()) {
85  NewOptLink(obj, opt, lnk);
86  fSize++;
87  return;
88  } else {
89  TList::AddFirst(obj, opt);
90  return;
91  }
92  }
93  lnk = lnk->Next();
94  }
95  TList::Add(obj, opt);
96 }
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
const char Option_t
Definition: RtypesCore.h:62
Bool_t IsArgNull(const char *where, const TObject *obj) const
Returns true if object is a null pointer.
virtual void AddFirst(TObject *obj)
Add object at the beginning of the list.
Definition: TList.cxx:97
int Int_t
Definition: RtypesCore.h:41
virtual void AddLast(TObject *obj)
Add object at the end of the list.
Definition: TList.cxx:141
A sorted doubly linked list.
Definition: TSortedList.h:28
void Error(const char *location, const char *msgfmt,...)
virtual Bool_t IsSortable() const
Definition: TObject.h:120
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:873
Int_t fSize
Definition: TCollection.h:57
virtual Int_t Compare(const TObject *obj) const
Compare abstract method.
Definition: TObject.cxx:166
TObjLink * fFirst
Definition: TList.h:48
void Add(THist< DIMENSIONS, PRECISION_TO, STAT_TO... > &to, const THist< DIMENSIONS, PRECISION_FROM, STAT_FROM... > &from)
Add two histograms.
Definition: THist.hxx:336
#define ClassImp(name)
Definition: Rtypes.h:336
Mother of all ROOT objects.
Definition: TObject.h:37
typedef void((*Func_t)())
virtual void Add(TObject *obj)
Definition: TList.h:77
Bool_t IsAscending()
Definition: TList.h:103
void Add(TObject *obj)
Add object in sorted list.
Definition: TSortedList.cxx:27
T1 fFirst
Definition: X11Events.mm:86