Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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
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 auto 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->PrevSP());
47 fSize++;
48 return;
49 } else {
50 TList::AddFirst(obj);
51 return;
52 }
53 }
54 lnk = lnk->NextSP();
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 auto 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->NextSP();
93 }
94 TList::Add(obj, opt);
95}
const char Option_t
Option string (const char)
Definition RtypesCore.h:80
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:745
void Add(TObject *obj) override
Definition TList.h:81
void AddLast(TObject *obj) override
Add object at the end of the list.
Definition TList.cxx:149
Bool_t IsAscending()
Definition TList.h:108
TObjLinkPtr_t NewLink(TObject *obj, const TObjLinkPtr_t &prev=nullptr)
Return a new TObjLink.
Definition TList.cxx:730
TObjLinkPtr_t fFirst
Definition TList.h:46
void AddFirst(TObject *obj) override
Add object at the beginning of the list.
Definition TList.cxx:97
Mother of all ROOT objects.
Definition TObject.h:41
virtual Bool_t IsSortable() const
Definition TObject.h:156
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition TObject.cxx:1071
void Add(TObject *obj) override
Add object in sorted list.