Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[core] AddBefore/After/At option variant, as with Add/First/Last #17846

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions core/cont/inc/TList.h
Original file line number Diff line number Diff line change
Expand Up @@ -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()); }
Expand Down
133 changes: 133 additions & 0 deletions core/cont/src/TList.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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.
Expand Down
Loading