ROOT  6.06/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 A sorted doubly linked list. All sortable classes inheriting from
14 TObject can be inserted in a TSortedList.
15 */
16 
17 #include "TSortedList.h"
18 
19 
21 
22 ////////////////////////////////////////////////////////////////////////////////
23 /// Add object in sorted list. Uses object Compare() member to find right
24 /// position.
25 
27 {
28  if (IsArgNull("Add", obj)) return;
29 
30  if (!obj->IsSortable()) {
31  Error("Add", "object must be sortable");
32  return;
33  }
34 
35  if (!fFirst) {
36  TList::AddLast(obj);
37  return;
38  }
39 
40  TObjLink *lnk = fFirst;
41 
42  while (lnk) {
43  Int_t cmp = lnk->GetObject()->Compare(obj);
44  if ((IsAscending() && cmp > 0) || (!IsAscending() && cmp < 0)) {
45  if (lnk->Prev()) {
46  NewLink(obj, lnk->Prev());
47  fSize++;
48  return;
49  } else {
50  TList::AddFirst(obj);
51  return;
52  }
53  }
54  lnk = lnk->Next();
55  }
56  TList::AddLast(obj);
57 }
58 
59 ////////////////////////////////////////////////////////////////////////////////
60 /// Add object in sorted list. Uses object Compare() member to find right
61 /// position and also store option. See TList::Add for explanation of
62 /// usage of option.
63 
65 {
66  if (IsArgNull("Add", obj)) return;
67 
68  if (!obj->IsSortable()) {
69  Error("Add", "object must be sortable");
70  return;
71  }
72 
73  if (!fFirst) {
74  TList::Add(obj, opt);
75  return;
76  }
77 
78  TObjLink *lnk = fFirst;
79 
80  while (lnk) {
81  Int_t cmp = lnk->GetObject()->Compare(obj);
82  if ((IsAscending() && cmp > 0) || (!IsAscending() && cmp < 0)) {
83  if (lnk->Prev()) {
84  NewOptLink(obj, opt, lnk);
85  fSize++;
86  return;
87  } else {
88  TList::AddFirst(obj, opt);
89  return;
90  }
91  }
92  lnk = lnk->Next();
93  }
94  TList::Add(obj, opt);
95 }
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
void Add(THist< DIMENSION, PRECISIONA > &to, THist< DIMENSION, PRECISIONB > &from)
Definition: THist.h:335
const char Option_t
Definition: RtypesCore.h:62
virtual void AddFirst(TObject *obj)
Add object at the beginning of the list.
Definition: TList.cxx:92
int Int_t
Definition: RtypesCore.h:41
virtual void AddLast(TObject *obj)
Add object at the end of the list.
Definition: TList.cxx:136
Bool_t IsArgNull(const char *where, const TObject *obj) const
Returns true if object is a null pointer.
A sorted doubly linked list.
Definition: TSortedList.h:30
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:918
void Error(const char *location, const char *msgfmt,...)
virtual Bool_t IsSortable() const
Definition: TObject.h:139
ClassImp(TSortedList) void TSortedList
Add object in sorted list.
Definition: TSortedList.cxx:20
Int_t fSize
Definition: TCollection.h:63
TObjLink * fFirst
Definition: TList.h:52
Mother of all ROOT objects.
Definition: TObject.h:58
typedef void((*Func_t)())
virtual void Add(TObject *obj)
Definition: TList.h:81
Bool_t IsAscending()
Definition: TList.h:107
virtual Int_t Compare(const TObject *obj) const
Compare abstract method.
Definition: TObject.cxx:218
void Add(TObject *obj)
TObject * obj
T1 fFirst
Definition: X11Events.mm:85