1 //===- llvm/ADT/ilist_base.h - Intrusive List Base --------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_ADT_ILIST_BASE_H
10 #define LLVM_ADT_ILIST_BASE_H
12 #include "llvm/ADT/ilist_node_base.h"
17 /// Implementations of list algorithms using ilist_node_base.
18 template <bool EnableSentinelTracking
> class ilist_base
{
20 using node_base_type
= ilist_node_base
<EnableSentinelTracking
>;
22 static void insertBeforeImpl(node_base_type
&Next
, node_base_type
&N
) {
23 node_base_type
&Prev
= *Next
.getPrev();
30 static void removeImpl(node_base_type
&N
) {
31 node_base_type
*Prev
= N
.getPrev();
32 node_base_type
*Next
= N
.getNext();
36 // Not strictly necessary, but helps catch a class of bugs.
41 static void removeRangeImpl(node_base_type
&First
, node_base_type
&Last
) {
42 node_base_type
*Prev
= First
.getPrev();
43 node_base_type
*Final
= Last
.getPrev();
47 // Not strictly necessary, but helps catch a class of bugs.
48 First
.setPrev(nullptr);
49 Final
->setNext(nullptr);
52 static void transferBeforeImpl(node_base_type
&Next
, node_base_type
&First
,
53 node_base_type
&Last
) {
54 if (&Next
== &Last
|| &First
== &Last
)
57 // Position cannot be contained in the range to be transferred.
58 assert(&Next
!= &First
&&
59 // Check for the most common mistake.
60 "Insertion point can't be one of the transferred nodes");
62 node_base_type
&Final
= *Last
.getPrev();
64 // Detach from old list/position.
65 First
.getPrev()->setNext(&Last
);
66 Last
.setPrev(First
.getPrev());
68 // Splice [First, Final] into its new list/position.
69 node_base_type
&Prev
= *Next
.getPrev();
76 template <class T
> static void insertBefore(T
&Next
, T
&N
) {
77 insertBeforeImpl(Next
, N
);
80 template <class T
> static void remove(T
&N
) { removeImpl(N
); }
81 template <class T
> static void removeRange(T
&First
, T
&Last
) {
82 removeRangeImpl(First
, Last
);
85 template <class T
> static void transferBefore(T
&Next
, T
&First
, T
&Last
) {
86 transferBeforeImpl(Next
, First
, Last
);
90 } // end namespace llvm
92 #endif // LLVM_ADT_ILIST_BASE_H