intrusive_linked_list-inl.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #pragma once
  2. #ifndef INTRUSIVE_LINKED_LIST_INL_H_
  3. #error "Direct inclusion of this file is not allowed, include intrusive_linked_list.h"
  4. // For the sake of sane code completion.
  5. #include "intrusive_linked_list.h"
  6. #endif
  7. #include <library/cpp/yt/assert/assert.h>
  8. namespace NYT {
  9. ////////////////////////////////////////////////////////////////////////////////
  10. template <class TItem, class TItemToNode>
  11. TIntrusiveLinkedList<TItem, TItemToNode>::TIntrusiveLinkedList(TItemToNode itemToNode)
  12. : ItemToNode_(itemToNode)
  13. { }
  14. template <class TItem, class TItemToNode>
  15. TItem* TIntrusiveLinkedList<TItem, TItemToNode>::GetFront() const
  16. {
  17. return Front_;
  18. }
  19. template <class TItem, class TItemToNode>
  20. TItem* TIntrusiveLinkedList<TItem, TItemToNode>::GetBack() const
  21. {
  22. return Back_;
  23. }
  24. template <class TItem, class TItemToNode>
  25. int TIntrusiveLinkedList<TItem, TItemToNode>::GetSize() const
  26. {
  27. return Size_;
  28. }
  29. template <class TItem, class TItemToNode>
  30. void TIntrusiveLinkedList<TItem, TItemToNode>::PushBack(TItem* item)
  31. {
  32. auto* node = ItemToNode_(item);
  33. if (Back_) {
  34. ItemToNode_(Back_)->Next = item;
  35. } else {
  36. Front_ = item;
  37. }
  38. node->Next = nullptr;
  39. node->Prev = Back_;
  40. Back_ = item;
  41. ++Size_;
  42. }
  43. template <class TItem, class TItemToNode>
  44. void TIntrusiveLinkedList<TItem, TItemToNode>::PopBack()
  45. {
  46. Y_ASSERT(Back_);
  47. if (Front_ == Back_) {
  48. Front_ = Back_ = nullptr;
  49. } else {
  50. Back_ = ItemToNode_(Back_)->Prev;
  51. ItemToNode_(Back_)->Next = nullptr;
  52. }
  53. --Size_;
  54. }
  55. template <class TItem, class TItemToNode>
  56. void TIntrusiveLinkedList<TItem, TItemToNode>::PushFront(TItem* item)
  57. {
  58. auto* node = ItemToNode_(item);
  59. if (Front_) {
  60. ItemToNode_(Front_)->Prev = item;
  61. } else {
  62. Back_ = item;
  63. }
  64. node->Next = Front_;
  65. node->Prev = nullptr;
  66. Front_ = item;
  67. ++Size_;
  68. }
  69. template <class TItem, class TItemToNode>
  70. void TIntrusiveLinkedList<TItem, TItemToNode>::PopFront()
  71. {
  72. Y_ASSERT(Front_);
  73. if (Front_ == Back_) {
  74. Front_ = Back_ = nullptr;
  75. } else {
  76. Front_ = ItemToNode_(Front_)->Next;
  77. ItemToNode_(Front_)->Prev = nullptr;
  78. }
  79. --Size_;
  80. }
  81. template <class TItem, class TItemToNode>
  82. void TIntrusiveLinkedList<TItem, TItemToNode>::Remove(TItem* item)
  83. {
  84. YT_ASSERT(Front_);
  85. auto* node = ItemToNode_(item);
  86. if (node->Next) {
  87. ItemToNode_(node->Next)->Prev = node->Prev;
  88. }
  89. if (node->Prev) {
  90. ItemToNode_(node->Prev)->Next = node->Next;
  91. }
  92. if (Front_ == item) {
  93. Front_ = node->Next;
  94. }
  95. if (Back_ == item) {
  96. Back_ = node->Prev;
  97. }
  98. --Size_;
  99. }
  100. template <class TItem, class TItemToNode>
  101. void TIntrusiveLinkedList<TItem, TItemToNode>::Clear()
  102. {
  103. Front_ = Back_ = nullptr;
  104. Size_ = 0;
  105. }
  106. ////////////////////////////////////////////////////////////////////////////////
  107. } // namespace NYT