Logo ROOT  
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  auto 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->PrevSP());
48  fSize++;
49  return;
50  } else {
51  TList::AddFirst(obj);
52  return;
53  }
54  }
55  lnk = lnk->NextSP();
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  auto 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->NextSP();
94  }
95  TList::Add(obj, opt);
96 }
TList::IsAscending
Bool_t IsAscending()
Definition: TList.h:114
TList::AddFirst
virtual void AddFirst(TObject *obj)
Add object at the beginning of the list.
Definition: TList.cxx:99
Option_t
const char Option_t
Definition: RtypesCore.h:66
TCollection::IsArgNull
Bool_t IsArgNull(const char *where, const TObject *obj) const
Returns true if object is a null pointer.
Definition: TCollection.cxx:372
ClassImp
#define ClassImp(name)
Definition: Rtypes.h:364
TList::fFirst
TObjLinkPtr_t fFirst
Definition: TList.h:52
TObject::Error
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:890
TList::AddLast
virtual void AddLast(TObject *obj)
Add object at the end of the list.
Definition: TList.cxx:151
TObject::IsSortable
virtual Bool_t IsSortable() const
Definition: TObject.h:131
TSortedList.h
TSortedList
A sorted doubly linked list.
Definition: TSortedList.h:28
TList::NewOptLink
TObjLinkPtr_t NewOptLink(TObject *obj, Option_t *opt, const TObjLinkPtr_t &prev=nullptr)
Return a new TObjOptLink (a TObjLink that also stores the option).
Definition: TList.cxx:747
TCollection::fSize
Int_t fSize
Definition: TCollection.h:148
TSortedList::Add
void Add(TObject *obj)
Add object in sorted list.
Definition: TSortedList.cxx:27
TList::Add
virtual void Add(TObject *obj)
Definition: TList.h:87
TObject
Mother of all ROOT objects.
Definition: TObject.h:37
TList::NewLink
TObjLinkPtr_t NewLink(TObject *obj, const TObjLinkPtr_t &prev=nullptr)
Return a new TObjLink.
Definition: TList.cxx:732
int