ilist_base.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/ADT/ilist_base.h - Intrusive List Base --------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_ADT_ILIST_BASE_H
  14. #define LLVM_ADT_ILIST_BASE_H
  15. #include "llvm/ADT/ilist_node_base.h"
  16. #include <cassert>
  17. namespace llvm {
  18. /// Implementations of list algorithms using ilist_node_base.
  19. template <bool EnableSentinelTracking> class ilist_base {
  20. public:
  21. 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();
  24. N.setNext(&Next);
  25. N.setPrev(&Prev);
  26. Prev.setNext(&N);
  27. Next.setPrev(&N);
  28. }
  29. static void removeImpl(node_base_type &N) {
  30. node_base_type *Prev = N.getPrev();
  31. node_base_type *Next = N.getNext();
  32. Next->setPrev(Prev);
  33. Prev->setNext(Next);
  34. // Not strictly necessary, but helps catch a class of bugs.
  35. N.setPrev(nullptr);
  36. N.setNext(nullptr);
  37. }
  38. static void removeRangeImpl(node_base_type &First, node_base_type &Last) {
  39. node_base_type *Prev = First.getPrev();
  40. node_base_type *Final = Last.getPrev();
  41. Last.setPrev(Prev);
  42. Prev->setNext(&Last);
  43. // Not strictly necessary, but helps catch a class of bugs.
  44. First.setPrev(nullptr);
  45. Final->setNext(nullptr);
  46. }
  47. static void transferBeforeImpl(node_base_type &Next, node_base_type &First,
  48. node_base_type &Last) {
  49. if (&Next == &Last || &First == &Last)
  50. return;
  51. // Position cannot be contained in the range to be transferred.
  52. assert(&Next != &First &&
  53. // Check for the most common mistake.
  54. "Insertion point can't be one of the transferred nodes");
  55. node_base_type &Final = *Last.getPrev();
  56. // Detach from old list/position.
  57. First.getPrev()->setNext(&Last);
  58. Last.setPrev(First.getPrev());
  59. // Splice [First, Final] into its new list/position.
  60. node_base_type &Prev = *Next.getPrev();
  61. Final.setNext(&Next);
  62. First.setPrev(&Prev);
  63. Prev.setNext(&First);
  64. Next.setPrev(&Final);
  65. }
  66. template <class T> static void insertBefore(T &Next, T &N) {
  67. insertBeforeImpl(Next, N);
  68. }
  69. template <class T> static void remove(T &N) { removeImpl(N); }
  70. template <class T> static void removeRange(T &First, T &Last) {
  71. removeRangeImpl(First, Last);
  72. }
  73. template <class T> static void transferBefore(T &Next, T &First, T &Last) {
  74. transferBeforeImpl(Next, First, Last);
  75. }
  76. };
  77. } // end namespace llvm
  78. #endif // LLVM_ADT_ILIST_BASE_H
  79. #ifdef __GNUC__
  80. #pragma GCC diagnostic pop
  81. #endif