/// 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);
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);
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;
// 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);
/// 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:
/// 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);
}
///
/// \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>
///
/// \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;
}
/// 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.
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());
}
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());
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());
}
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());
}
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());
}
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());