llvm.org GIT mirror llvm / 631d14e
ADT: Use typedefs for ilist_base and ilist_node_base, NFC 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 Duncan P. N. Exon Smith 3 years ago
3 changed file(s) with 48 addition(s) and 42 deletion(s). Raw diff Collapse all Expand all
1818
1919 /// Implementations of list algorithms using ilist_node_base.
2020 class ilist_base {
21 typedef ilist_node_base node_base_type;
22
2123 public:
22 static void insertBeforeImpl(ilist_node_base &Next, ilist_node_base &N) {
23 ilist_node_base &Prev = *Next.getPrev();
24 static void insertBeforeImpl(node_base_type &Next, node_base_type &N) {
25 node_base_type &Prev = *Next.getPrev();
2426 N.setNext(&Next);
2527 N.setPrev(&Prev);
2628 Prev.setNext(&N);
2729 Next.setPrev(&N);
2830 }
2931
30 static void removeImpl(ilist_node_base &N) {
31 ilist_node_base *Prev = N.getPrev();
32 ilist_node_base *Next = N.getNext();
32 static void removeImpl(node_base_type &N) {
33 node_base_type *Prev = N.getPrev();
34 node_base_type *Next = N.getNext();
3335 Next->setPrev(Prev);
3436 Prev->setNext(Next);
3537
3840 N.setNext(nullptr);
3941 }
4042
41 static void removeRangeImpl(ilist_node_base &First, ilist_node_base &Last) {
42 ilist_node_base *Prev = First.getPrev();
43 ilist_node_base *Final = Last.getPrev();
43 static void removeRangeImpl(node_base_type &First, node_base_type &Last) {
44 node_base_type *Prev = First.getPrev();
45 node_base_type *Final = Last.getPrev();
4446 Last.setPrev(Prev);
4547 Prev->setNext(&Last);
4648
4951 Final->setNext(nullptr);
5052 }
5153
52 static void transferBeforeImpl(ilist_node_base &Next, ilist_node_base &First,
53 ilist_node_base &Last) {
54 static void transferBeforeImpl(node_base_type &Next, node_base_type &First,
55 node_base_type &Last) {
5456 if (&Next == &Last || &First == &Last)
5557 return;
5658
5961 // Check for the most common mistake.
6062 "Insertion point can't be one of the transferred nodes");
6163
62 ilist_node_base &Final = *Last.getPrev();
64 node_base_type &Final = *Last.getPrev();
6365
6466 // Detach from old list/position.
6567 First.getPrev()->setNext(&Last);
6668 Last.setPrev(First.getPrev());
6769
6870 // Splice [First, Final] into its new list/position.
69 ilist_node_base &Prev = *Next.getPrev();
71 node_base_type &Prev = *Next.getPrev();
7072 Final.setNext(&Next);
7173 First.setPrev(&Prev);
7274 Prev.setNext(&First);
4646 /// because the extra semantic is otherwise non-obvious. They are equivalent
4747 /// to calling \a std::for_each() on the range to be discarded.
4848 template class simple_ilist : ilist_base, ilist_node_access {
49 typedef ilist_base list_base_type;
4950 ilist_sentinel Sentinel;
5051
5152 public:
119120
120121 /// Insert a node by reference; never copies.
121122 iterator insert(iterator I, reference Node) {
122 ilist_base::insertBefore(*I.getNodePtr(), *this->getNodePtr(&Node));
123 list_base_type::insertBefore(*I.getNodePtr(), *this->getNodePtr(&Node));
123124 return iterator(&Node);
124125 }
125126
134135 ///
135136 /// \see \a erase() for removing by iterator.
136137 /// \see \a removeAndDispose() if the node should be deleted.
137 void remove(reference N) { ilist_base::remove(*this->getNodePtr(&N)); }
138 void remove(reference N) { list_base_type::remove(*this->getNodePtr(&N)); }
138139
139140 /// Remove a node by reference and dispose of it.
140141 template
157158 ///
158159 /// \see \a eraseAndDispose() if the nodes should be deleted.
159160 iterator erase(iterator First, iterator Last) {
160 ilist_base::removeRange(*First.getNodePtr(), *Last.getNodePtr());
161 list_base_type::removeRange(*First.getNodePtr(), *Last.getNodePtr());
161162 return Last;
162163 }
163164
200201
201202 /// Splice in a range of nodes from another list.
202203 void splice(iterator I, simple_ilist &, iterator First, iterator Last) {
203 ilist_base::transferBefore(*I.getNodePtr(), *First.getNodePtr(),
204 *Last.getNodePtr());
204 list_base_type::transferBefore(*I.getNodePtr(), *First.getNodePtr(),
205 *Last.getNodePtr());
205206 }
206207
207208 /// Merge in another list.
1313
1414 namespace {
1515
16 typedef ilist_base list_base_type;
17 typedef ilist_node_base node_base_type;
18
1619 TEST(IListBaseTest, insertBeforeImpl) {
17 ilist_node_base S, A, B;
20 node_base_type S, A, B;
1821 // [S] <-> [S]
1922 S.setPrev(&S);
2023 S.setNext(&S);
2124
2225 // [S] <-> A <-> [S]
23 ilist_base::insertBeforeImpl(S, A);
26 list_base_type::insertBeforeImpl(S, A);
2427 EXPECT_EQ(&A, S.getPrev());
2528 EXPECT_EQ(&S, A.getPrev());
2629 EXPECT_EQ(&A, S.getNext());
2730 EXPECT_EQ(&S, A.getNext());
2831
2932 // [S] <-> A <-> B <-> [S]
30 ilist_base::insertBeforeImpl(S, B);
33 list_base_type::insertBeforeImpl(S, B);
3134 EXPECT_EQ(&B, S.getPrev());
3235 EXPECT_EQ(&A, B.getPrev());
3336 EXPECT_EQ(&S, A.getPrev());
3740 }
3841
3942 TEST(IListBaseTest, removeImpl) {
40 ilist_node_base S, A, B;
43 node_base_type S, A, B;
4144
4245 // [S] <-> A <-> B <-> [S]
4346 S.setPrev(&S);
4447 S.setNext(&S);
45 ilist_base::insertBeforeImpl(S, A);
46 ilist_base::insertBeforeImpl(S, B);
48 list_base_type::insertBeforeImpl(S, A);
49 list_base_type::insertBeforeImpl(S, B);
4750
4851 // [S] <-> B <-> [S]
49 ilist_base::removeImpl(A);
52 list_base_type::removeImpl(A);
5053 EXPECT_EQ(&B, S.getPrev());
5154 EXPECT_EQ(&S, B.getPrev());
5255 EXPECT_EQ(&B, S.getNext());
5558 EXPECT_EQ(nullptr, A.getNext());
5659
5760 // [S] <-> [S]
58 ilist_base::removeImpl(B);
61 list_base_type::removeImpl(B);
5962 EXPECT_EQ(&S, S.getPrev());
6063 EXPECT_EQ(&S, S.getNext());
6164 EXPECT_EQ(nullptr, B.getPrev());
6366 }
6467
6568 TEST(IListBaseTest, removeRangeImpl) {
66 ilist_node_base S, A, B, C, D;
69 node_base_type S, A, B, C, D;
6770
6871 // [S] <-> A <-> B <-> C <-> D <-> [S]
6972 S.setPrev(&S);
7073 S.setNext(&S);
71 ilist_base::insertBeforeImpl(S, A);
72 ilist_base::insertBeforeImpl(S, B);
73 ilist_base::insertBeforeImpl(S, C);
74 ilist_base::insertBeforeImpl(S, D);
74 list_base_type::insertBeforeImpl(S, A);
75 list_base_type::insertBeforeImpl(S, B);
76 list_base_type::insertBeforeImpl(S, C);
77 list_base_type::insertBeforeImpl(S, D);
7578
7679 // [S] <-> A <-> D <-> [S]
77 ilist_base::removeRangeImpl(B, D);
80 list_base_type::removeRangeImpl(B, D);
7881 EXPECT_EQ(&D, S.getPrev());
7982 EXPECT_EQ(&A, D.getPrev());
8083 EXPECT_EQ(&S, A.getPrev());
8689 }
8790
8891 TEST(IListBaseTest, removeRangeImplAllButSentinel) {
89 ilist_node_base S, A, B;
92 node_base_type S, A, B;
9093
9194 // [S] <-> A <-> B <-> [S]
9295 S.setPrev(&S);
9396 S.setNext(&S);
94 ilist_base::insertBeforeImpl(S, A);
95 ilist_base::insertBeforeImpl(S, B);
97 list_base_type::insertBeforeImpl(S, A);
98 list_base_type::insertBeforeImpl(S, B);
9699
97100 // [S] <-> [S]
98 ilist_base::removeRangeImpl(A, S);
101 list_base_type::removeRangeImpl(A, S);
99102 EXPECT_EQ(&S, S.getPrev());
100103 EXPECT_EQ(&S, S.getNext());
101104 EXPECT_EQ(nullptr, A.getPrev());
103106 }
104107
105108 TEST(IListBaseTest, transferBeforeImpl) {
106 ilist_node_base S1, S2, A, B, C, D, E;
109 node_base_type S1, S2, A, B, C, D, E;
107110
108111 // [S1] <-> A <-> B <-> C <-> [S1]
109112 S1.setPrev(&S1);
110113 S1.setNext(&S1);
111 ilist_base::insertBeforeImpl(S1, A);
112 ilist_base::insertBeforeImpl(S1, B);
113 ilist_base::insertBeforeImpl(S1, C);
114 list_base_type::insertBeforeImpl(S1, A);
115 list_base_type::insertBeforeImpl(S1, B);
116 list_base_type::insertBeforeImpl(S1, C);
114117
115118 // [S2] <-> D <-> E <-> [S2]
116119 S2.setPrev(&S2);
117120 S2.setNext(&S2);
118 ilist_base::insertBeforeImpl(S2, D);
119 ilist_base::insertBeforeImpl(S2, E);
121 list_base_type::insertBeforeImpl(S2, D);
122 list_base_type::insertBeforeImpl(S2, E);
120123
121124 // [S1] <-> C <-> [S1]
122 ilist_base::transferBeforeImpl(D, A, C);
125 list_base_type::transferBeforeImpl(D, A, C);
123126 EXPECT_EQ(&C, S1.getPrev());
124127 EXPECT_EQ(&S1, C.getPrev());
125128 EXPECT_EQ(&C, S1.getNext());