Skip to content

Commit

Permalink
ADT: Use typedefs for ilist_base and ilist_node_base, NFC
Browse files Browse the repository at this point in the history
This is a prep commit to minimize changes in a follow-up that is adding
a template parameter to ilist_node_base and ilist_base.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@281141 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
dexonsmith committed Sep 10, 2016
1 parent e117e50 commit 631d14e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 42 deletions.
26 changes: 14 additions & 12 deletions include/llvm/ADT/ilist_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,20 @@ namespace llvm {

/// Implementations of list algorithms using ilist_node_base.
class ilist_base {
typedef ilist_node_base node_base_type;

public:
static void insertBeforeImpl(ilist_node_base &Next, ilist_node_base &N) {
ilist_node_base &Prev = *Next.getPrev();
static void insertBeforeImpl(node_base_type &Next, node_base_type &N) {
node_base_type &Prev = *Next.getPrev();
N.setNext(&Next);
N.setPrev(&Prev);
Prev.setNext(&N);
Next.setPrev(&N);
}

static void removeImpl(ilist_node_base &N) {
ilist_node_base *Prev = N.getPrev();
ilist_node_base *Next = N.getNext();
static void removeImpl(node_base_type &N) {
node_base_type *Prev = N.getPrev();
node_base_type *Next = N.getNext();
Next->setPrev(Prev);
Prev->setNext(Next);

Expand All @@ -39,9 +41,9 @@ class ilist_base {
N.setNext(nullptr);
}

static void removeRangeImpl(ilist_node_base &First, ilist_node_base &Last) {
ilist_node_base *Prev = First.getPrev();
ilist_node_base *Final = Last.getPrev();
static void removeRangeImpl(node_base_type &First, node_base_type &Last) {
node_base_type *Prev = First.getPrev();
node_base_type *Final = Last.getPrev();
Last.setPrev(Prev);
Prev->setNext(&Last);

Expand All @@ -50,8 +52,8 @@ class ilist_base {
Final->setNext(nullptr);
}

static void transferBeforeImpl(ilist_node_base &Next, ilist_node_base &First,
ilist_node_base &Last) {
static void transferBeforeImpl(node_base_type &Next, node_base_type &First,
node_base_type &Last) {
if (&Next == &Last || &First == &Last)
return;

Expand All @@ -60,14 +62,14 @@ class ilist_base {
// Check for the most common mistake.
"Insertion point can't be one of the transferred nodes");

ilist_node_base &Final = *Last.getPrev();
node_base_type &Final = *Last.getPrev();

// Detach from old list/position.
First.getPrev()->setNext(&Last);
Last.setPrev(First.getPrev());

// Splice [First, Final] into its new list/position.
ilist_node_base &Prev = *Next.getPrev();
node_base_type &Prev = *Next.getPrev();
Final.setNext(&Next);
First.setPrev(&Prev);
Prev.setNext(&First);
Expand Down
11 changes: 6 additions & 5 deletions include/llvm/ADT/simple_ilist.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ namespace llvm {
/// because the extra semantic is otherwise non-obvious. They are equivalent
/// to calling \a std::for_each() on the range to be discarded.
template <typename T> class simple_ilist : ilist_base, ilist_node_access {
typedef ilist_base list_base_type;
ilist_sentinel<T> Sentinel;

public:
Expand Down Expand Up @@ -120,7 +121,7 @@ template <typename T> class simple_ilist : ilist_base, ilist_node_access {

/// Insert a node by reference; never copies.
iterator insert(iterator I, reference Node) {
ilist_base::insertBefore(*I.getNodePtr(), *this->getNodePtr(&Node));
list_base_type::insertBefore(*I.getNodePtr(), *this->getNodePtr(&Node));
return iterator(&Node);
}

Expand All @@ -135,7 +136,7 @@ template <typename T> class simple_ilist : ilist_base, ilist_node_access {
///
/// \see \a erase() for removing by iterator.
/// \see \a removeAndDispose() if the node should be deleted.
void remove(reference N) { ilist_base::remove(*this->getNodePtr(&N)); }
void remove(reference N) { list_base_type::remove(*this->getNodePtr(&N)); }

/// Remove a node by reference and dispose of it.
template <class Disposer>
Expand All @@ -158,7 +159,7 @@ template <typename T> class simple_ilist : ilist_base, ilist_node_access {
///
/// \see \a eraseAndDispose() if the nodes should be deleted.
iterator erase(iterator First, iterator Last) {
ilist_base::removeRange(*First.getNodePtr(), *Last.getNodePtr());
list_base_type::removeRange(*First.getNodePtr(), *Last.getNodePtr());
return Last;
}

Expand Down Expand Up @@ -201,8 +202,8 @@ template <typename T> class simple_ilist : ilist_base, ilist_node_access {

/// Splice in a range of nodes from another list.
void splice(iterator I, simple_ilist &, iterator First, iterator Last) {
ilist_base::transferBefore(*I.getNodePtr(), *First.getNodePtr(),
*Last.getNodePtr());
list_base_type::transferBefore(*I.getNodePtr(), *First.getNodePtr(),
*Last.getNodePtr());
}

/// Merge in another list.
Expand Down
53 changes: 28 additions & 25 deletions unittests/ADT/IListBaseTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,24 @@ using namespace llvm;

namespace {

typedef ilist_base list_base_type;
typedef ilist_node_base node_base_type;

TEST(IListBaseTest, insertBeforeImpl) {
ilist_node_base S, A, B;
node_base_type S, A, B;
// [S] <-> [S]
S.setPrev(&S);
S.setNext(&S);

// [S] <-> A <-> [S]
ilist_base::insertBeforeImpl(S, A);
list_base_type::insertBeforeImpl(S, A);
EXPECT_EQ(&A, S.getPrev());
EXPECT_EQ(&S, A.getPrev());
EXPECT_EQ(&A, S.getNext());
EXPECT_EQ(&S, A.getNext());

// [S] <-> A <-> B <-> [S]
ilist_base::insertBeforeImpl(S, B);
list_base_type::insertBeforeImpl(S, B);
EXPECT_EQ(&B, S.getPrev());
EXPECT_EQ(&A, B.getPrev());
EXPECT_EQ(&S, A.getPrev());
Expand All @@ -38,16 +41,16 @@ TEST(IListBaseTest, insertBeforeImpl) {
}

TEST(IListBaseTest, removeImpl) {
ilist_node_base S, A, B;
node_base_type S, A, B;

// [S] <-> A <-> B <-> [S]
S.setPrev(&S);
S.setNext(&S);
ilist_base::insertBeforeImpl(S, A);
ilist_base::insertBeforeImpl(S, B);
list_base_type::insertBeforeImpl(S, A);
list_base_type::insertBeforeImpl(S, B);

// [S] <-> B <-> [S]
ilist_base::removeImpl(A);
list_base_type::removeImpl(A);
EXPECT_EQ(&B, S.getPrev());
EXPECT_EQ(&S, B.getPrev());
EXPECT_EQ(&B, S.getNext());
Expand All @@ -56,26 +59,26 @@ TEST(IListBaseTest, removeImpl) {
EXPECT_EQ(nullptr, A.getNext());

// [S] <-> [S]
ilist_base::removeImpl(B);
list_base_type::removeImpl(B);
EXPECT_EQ(&S, S.getPrev());
EXPECT_EQ(&S, S.getNext());
EXPECT_EQ(nullptr, B.getPrev());
EXPECT_EQ(nullptr, B.getNext());
}

TEST(IListBaseTest, removeRangeImpl) {
ilist_node_base S, A, B, C, D;
node_base_type S, A, B, C, D;

// [S] <-> A <-> B <-> C <-> D <-> [S]
S.setPrev(&S);
S.setNext(&S);
ilist_base::insertBeforeImpl(S, A);
ilist_base::insertBeforeImpl(S, B);
ilist_base::insertBeforeImpl(S, C);
ilist_base::insertBeforeImpl(S, D);
list_base_type::insertBeforeImpl(S, A);
list_base_type::insertBeforeImpl(S, B);
list_base_type::insertBeforeImpl(S, C);
list_base_type::insertBeforeImpl(S, D);

// [S] <-> A <-> D <-> [S]
ilist_base::removeRangeImpl(B, D);
list_base_type::removeRangeImpl(B, D);
EXPECT_EQ(&D, S.getPrev());
EXPECT_EQ(&A, D.getPrev());
EXPECT_EQ(&S, A.getPrev());
Expand All @@ -87,40 +90,40 @@ TEST(IListBaseTest, removeRangeImpl) {
}

TEST(IListBaseTest, removeRangeImplAllButSentinel) {
ilist_node_base S, A, B;
node_base_type S, A, B;

// [S] <-> A <-> B <-> [S]
S.setPrev(&S);
S.setNext(&S);
ilist_base::insertBeforeImpl(S, A);
ilist_base::insertBeforeImpl(S, B);
list_base_type::insertBeforeImpl(S, A);
list_base_type::insertBeforeImpl(S, B);

// [S] <-> [S]
ilist_base::removeRangeImpl(A, S);
list_base_type::removeRangeImpl(A, S);
EXPECT_EQ(&S, S.getPrev());
EXPECT_EQ(&S, S.getNext());
EXPECT_EQ(nullptr, A.getPrev());
EXPECT_EQ(nullptr, B.getNext());
}

TEST(IListBaseTest, transferBeforeImpl) {
ilist_node_base S1, S2, A, B, C, D, E;
node_base_type S1, S2, A, B, C, D, E;

// [S1] <-> A <-> B <-> C <-> [S1]
S1.setPrev(&S1);
S1.setNext(&S1);
ilist_base::insertBeforeImpl(S1, A);
ilist_base::insertBeforeImpl(S1, B);
ilist_base::insertBeforeImpl(S1, C);
list_base_type::insertBeforeImpl(S1, A);
list_base_type::insertBeforeImpl(S1, B);
list_base_type::insertBeforeImpl(S1, C);

// [S2] <-> D <-> E <-> [S2]
S2.setPrev(&S2);
S2.setNext(&S2);
ilist_base::insertBeforeImpl(S2, D);
ilist_base::insertBeforeImpl(S2, E);
list_base_type::insertBeforeImpl(S2, D);
list_base_type::insertBeforeImpl(S2, E);

// [S1] <-> C <-> [S1]
ilist_base::transferBeforeImpl(D, A, C);
list_base_type::transferBeforeImpl(D, A, C);
EXPECT_EQ(&C, S1.getPrev());
EXPECT_EQ(&S1, C.getPrev());
EXPECT_EQ(&C, S1.getNext());
Expand Down

0 comments on commit 631d14e

Please sign in to comment.