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
14A sorted doubly linked list. All sortable classes inheriting from
15TObject 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}
int Int_t
Definition: RtypesCore.h:45
const char Option_t
Definition: RtypesCore.h:66
#define ClassImp(name)
Definition: Rtypes.h:364
Bool_t IsArgNull(const char *where, const TObject *obj) const
Returns true if object is a null pointer.
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:748
virtual void Add(TObject *obj)
Definition: TList.h:87
Bool_t IsAscending()
Definition: TList.h:114
virtual void AddFirst(TObject *obj)
Add object at the beginning of the list.
Definition: TList.cxx:100
TObjLinkPtr_t NewLink(TObject *obj, const TObjLinkPtr_t &prev=nullptr)
Return a new TObjLink.
Definition: TList.cxx:733
TObjLinkPtr_t fFirst
Definition: TList.h:52
virtual void AddLast(TObject *obj)
Add object at the end of the list.
Definition: TList.cxx:152
Mother of all ROOT objects.
Definition: TObject.h:37
virtual Bool_t IsSortable() const
Definition: TObject.h:146
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition: TObject.cxx:893
A sorted doubly linked list.
Definition: TSortedList.h:28
void Add(TObject *obj)
Add object in sorted list.
Definition: TSortedList.cxx:27