From 7132666a635be4f6722427264dacbbaa99be7fe8 Mon Sep 17 00:00:00 2001 From: ferdymercury Date: Fri, 28 Feb 2025 10:36:55 +0100 Subject: [PATCH] [core] AddBefore/After/At option variant, as with Add/First/Last Fixes https://its.cern.ch/jira/browse/ROOT-9288 --- core/cont/inc/TList.h | 5 ++ core/cont/src/TList.cxx | 133 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 138 insertions(+) diff --git a/core/cont/inc/TList.h b/core/cont/inc/TList.h index a86af707c175c..47903a107e355 100644 --- a/core/cont/inc/TList.h +++ b/core/cont/inc/TList.h @@ -85,10 +85,15 @@ friend class TListIter; void AddLast(TObject *obj) override; virtual void AddLast(TObject *obj, Option_t *opt); void AddAt(TObject *obj, Int_t idx) override; + void AddAt(TObject *obj, Int_t idx, Option_t *option); void AddAfter(const TObject *after, TObject *obj) override; virtual void AddAfter(TObjLink *after, TObject *obj); + void AddAfter(const TObject *after, TObject *obj, Option_t *option); + void AddAfter(TObjLink *after, TObject *obj, Option_t *option); void AddBefore(const TObject *before, TObject *obj) override; virtual void AddBefore(TObjLink *before, TObject *obj); + void AddBefore(const TObject *before, TObject *obj, Option_t *option); + void AddBefore(TObjLink *before, TObject *obj, Option_t *option); TObject *Remove(TObject *obj) override; virtual TObject *Remove(TObjLink *lnk); TObject *Remove(const TObjLinkPtr_t &lnk) { return Remove(lnk.get()); } diff --git a/core/cont/src/TList.cxx b/core/cont/src/TList.cxx index 30a4b69628d62..23f58ac42f7cd 100644 --- a/core/cont/src/TList.cxx +++ b/core/cont/src/TList.cxx @@ -298,6 +298,116 @@ void TList::AddAfter(TObjLink *after, TObject *obj) } } +//////////////////////////////////////////////////////////////////////////////// +/// Insert object before object before in the list, with options. + +void TList::AddBefore(const TObject *before, TObject *obj, Option_t *option) +{ + R__COLLECTION_WRITE_GUARD(); + + if (IsArgNull("AddBefore", obj)) return; + + R__COLLECTION_WRITE_LOCKGUARD(ROOT::gCoreMutex); + + if (!before) + TList::AddFirst(obj, option); + else { + Int_t idx; + TObjLink *t = FindLink(before, idx); + if (!t) { + Error("AddBefore", "before not found, object not added"); + return; + } + if (t == fFirst.get()) + TList::AddFirst(obj, option); + else { + NewOptLink(obj, option, t->fPrev.lock()); + fSize++; + Changed(); + } + } +} + +//////////////////////////////////////////////////////////////////////////////// +/// Insert object before the specified ObjLink object. If before = 0 then add +/// to the head of the list. An ObjLink can be obtained by looping over a list +/// using the above describe iterator method 3. Options can be specified. + +void TList::AddBefore(TObjLink *before, TObject *obj, Option_t *option) +{ + R__COLLECTION_WRITE_GUARD(); + + if (IsArgNull("AddBefore", obj)) return; + + if (!before) + TList::AddFirst(obj, option); + else { + if (before == fFirst.get()) + TList::AddFirst(obj, option); + else { + NewOptLink(obj, option, before->fPrev.lock()); + fSize++; + Changed(); + } + } +} + +//////////////////////////////////////////////////////////////////////////////// +/// Insert object after object after in the list, with options. + +void TList::AddAfter(const TObject *after, TObject *obj, Option_t *option) +{ + R__COLLECTION_WRITE_GUARD(); + + if (IsArgNull("AddAfter", obj)) return; + + R__COLLECTION_WRITE_LOCKGUARD(ROOT::gCoreMutex); + + if (!after) + TList::AddLast(obj, option); + else { + Int_t idx; + TObjLink *t = FindLink(after, idx); + if (!t) { + Error("AddAfter", "after not found, object not added"); + return; + } + if (t == fLast.get()) + TList::AddLast(obj, option); + else { + NewOptLink(obj, option, t->shared_from_this()); + fSize++; + Changed(); + } + } +} + +//////////////////////////////////////////////////////////////////////////////// +/// Insert object after the specified ObjLink object. If after = 0 then add +/// to the tail of the list. An ObjLink can be obtained by looping over a list +/// using the above describe iterator method 3. Options can be specified. + +void TList::AddAfter(TObjLink *after, TObject *obj, Option_t *option) +{ + R__COLLECTION_WRITE_GUARD(); + + if (IsArgNull("AddAfter", obj)) return; + + R__COLLECTION_WRITE_LOCKGUARD(ROOT::gCoreMutex); + + if (!after) + TList::AddLast(obj, option); + else { + if (after == fLast.get()) + TList::AddLast(obj, option); + else { + NewOptLink(obj, option, after->shared_from_this()); + fSize++; + Changed(); + } + } +} + //////////////////////////////////////////////////////////////////////////////// /// Insert object at position idx in the list. @@ -321,6 +431,29 @@ void TList::AddAt(TObject *obj, Int_t idx) } } +//////////////////////////////////////////////////////////////////////////////// +/// Insert object at position idx in the list, with options. + +void TList::AddAt(TObject *obj, Int_t idx, Option_t *option) +{ + R__COLLECTION_WRITE_GUARD(); + + if (IsArgNull("AddAt", obj)) return; + + R__COLLECTION_WRITE_LOCKGUARD(ROOT::gCoreMutex); + + TObjLink *lnk = LinkAt(idx); + if (!lnk) + TList::AddLast(obj, option); + else if (lnk == fFirst.get()) + TList::AddFirst(obj, option); + else { + NewOptLink(obj, option, lnk->fPrev.lock()); + fSize++; + Changed(); + } +} + //////////////////////////////////////////////////////////////////////////////// /// Returns the object after object obj. Obj is found using the /// object's IsEqual() method. Returns 0 if obj is last in list.