95.59% Lines (65/68) 100.00% Functions (11/11)
TLA Baseline Branch
Line Hits Code Line Hits Code
1   // 1   //
2   // Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com) 2   // Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com)
3   // 3   //
4   // Distributed under the Boost Software License, Version 1.0. (See accompanying 4   // Distributed under the Boost Software License, Version 1.0. (See accompanying
5   // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 5   // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6   // 6   //
7   // Official repository: https://github.com/cppalliance/corosio 7   // Official repository: https://github.com/cppalliance/corosio
8   // 8   //
9   9  
10   #ifndef BOOST_COROSIO_DETAIL_INTRUSIVE_HPP 10   #ifndef BOOST_COROSIO_DETAIL_INTRUSIVE_HPP
11   #define BOOST_COROSIO_DETAIL_INTRUSIVE_HPP 11   #define BOOST_COROSIO_DETAIL_INTRUSIVE_HPP
12   12  
13   namespace boost::corosio::detail { 13   namespace boost::corosio::detail {
14   14  
15   /** An intrusive doubly linked list. 15   /** An intrusive doubly linked list.
16   16  
17   This container provides O(1) push and pop operations for 17   This container provides O(1) push and pop operations for
18   elements that derive from @ref node. Elements are not 18   elements that derive from @ref node. Elements are not
19   copied or moved; they are linked directly into the list. 19   copied or moved; they are linked directly into the list.
20   20  
21   @tparam T The element type. Must derive from `intrusive_list<T>::node`. 21   @tparam T The element type. Must derive from `intrusive_list<T>::node`.
22   */ 22   */
23   template<class T> 23   template<class T>
24   class intrusive_list 24   class intrusive_list
25   { 25   {
26   public: 26   public:
27   /** Base class for list elements. 27   /** Base class for list elements.
28   28  
29   Derive from this class to make a type usable with 29   Derive from this class to make a type usable with
30   @ref intrusive_list. The `next_` and `prev_` pointers 30   @ref intrusive_list. The `next_` and `prev_` pointers
31   are private and accessible only to the list. 31   are private and accessible only to the list.
32   */ 32   */
33   class node 33   class node
34   { 34   {
35   friend class intrusive_list; 35   friend class intrusive_list;
36   36  
37   private: 37   private:
38   T* next_; 38   T* next_;
39   T* prev_; 39   T* prev_;
40   }; 40   };
41   41  
42   private: 42   private:
43   T* head_ = nullptr; 43   T* head_ = nullptr;
44   T* tail_ = nullptr; 44   T* tail_ = nullptr;
45   45  
46   public: 46   public:
HITCBC 47   10606 intrusive_list() = default; 47   10606 intrusive_list() = default;
48   48  
49   intrusive_list(intrusive_list&& other) noexcept 49   intrusive_list(intrusive_list&& other) noexcept
50   : head_(other.head_) 50   : head_(other.head_)
51   , tail_(other.tail_) 51   , tail_(other.tail_)
52   { 52   {
53   other.head_ = nullptr; 53   other.head_ = nullptr;
54   other.tail_ = nullptr; 54   other.tail_ = nullptr;
55   } 55   }
56   56  
57   intrusive_list(intrusive_list const&) = delete; 57   intrusive_list(intrusive_list const&) = delete;
58   intrusive_list& operator=(intrusive_list const&) = delete; 58   intrusive_list& operator=(intrusive_list const&) = delete;
59   intrusive_list& operator=(intrusive_list&&) = delete; 59   intrusive_list& operator=(intrusive_list&&) = delete;
60   60  
HITCBC 61   38 bool empty() const noexcept 61   38 bool empty() const noexcept
62   { 62   {
HITCBC 63   38 return head_ == nullptr; 63   38 return head_ == nullptr;
64   } 64   }
65   65  
66   /// Peek at the head element without removing it. 66   /// Peek at the head element without removing it.
67   T* front() const noexcept 67   T* front() const noexcept
68   { 68   {
69   return head_; 69   return head_;
70   } 70   }
71   71  
HITCBC 72   40992 void push_back(T* w) noexcept 72   40580 void push_back(T* w) noexcept
73   { 73   {
HITCBC 74   40992 auto* n = static_cast<node*>(w); 74   40580 auto* n = static_cast<node*>(w);
HITCBC 75   40992 n->next_ = nullptr; 75   40580 n->next_ = nullptr;
HITCBC 76   40992 n->prev_ = tail_; 76   40580 n->prev_ = tail_;
HITCBC 77   40992 if (tail_) 77   40580 if (tail_)
HITCBC 78   23470 static_cast<node*>(tail_)->next_ = w; 78   23208 static_cast<node*>(tail_)->next_ = w;
79   else 79   else
HITCBC 80   17522 head_ = w; 80   17372 head_ = w;
HITCBC 81   40992 tail_ = w; 81   40580 tail_ = w;
HITCBC 82   40992 } 82   40580 }
83   83  
84   void splice_back(intrusive_list& other) noexcept 84   void splice_back(intrusive_list& other) noexcept
85   { 85   {
86   if (other.empty()) 86   if (other.empty())
87   return; 87   return;
88   if (tail_) 88   if (tail_)
89   { 89   {
90   static_cast<node*>(tail_)->next_ = other.head_; 90   static_cast<node*>(tail_)->next_ = other.head_;
91   static_cast<node*>(other.head_)->prev_ = tail_; 91   static_cast<node*>(other.head_)->prev_ = tail_;
92   tail_ = other.tail_; 92   tail_ = other.tail_;
93   } 93   }
94   else 94   else
95   { 95   {
96   head_ = other.head_; 96   head_ = other.head_;
97   tail_ = other.tail_; 97   tail_ = other.tail_;
98   } 98   }
99   other.head_ = nullptr; 99   other.head_ = nullptr;
100   other.tail_ = nullptr; 100   other.tail_ = nullptr;
101   } 101   }
102   102  
HITCBC 103   257328 T* pop_front() noexcept 103   290538 T* pop_front() noexcept
104   { 104   {
HITCBC 105   257328 if (!head_) 105   290538 if (!head_)
HITCBC 106   240854 return nullptr; 106   274218 return nullptr;
HITCBC 107   16474 T* w = head_; 107   16320 T* w = head_;
HITCBC 108   16474 head_ = static_cast<node*>(head_)->next_; 108   16320 head_ = static_cast<node*>(head_)->next_;
HITCBC 109   16474 if (head_) 109   16320 if (head_)
HITCBC 110   44 static_cast<node*>(head_)->prev_ = nullptr; 110   40 static_cast<node*>(head_)->prev_ = nullptr;
111   else 111   else
HITCBC 112   16430 tail_ = nullptr; 112   16280 tail_ = nullptr;
113   // Defensive: clear stale linkage so remove() on a 113   // Defensive: clear stale linkage so remove() on a
114   // popped node cannot corrupt the list. 114   // popped node cannot corrupt the list.
HITCBC 115   16474 auto* n = static_cast<node*>(w); 115   16320 auto* n = static_cast<node*>(w);
HITCBC 116   16474 n->next_ = nullptr; 116   16320 n->next_ = nullptr;
HITCBC 117   16474 n->prev_ = nullptr; 117   16320 n->prev_ = nullptr;
HITCBC 118   16474 return w; 118   16320 return w;
119   } 119   }
120   120  
HITCBC 121   24518 void remove(T* w) noexcept 121   24260 void remove(T* w) noexcept
122   { 122   {
HITCBC 123   24518 auto* n = static_cast<node*>(w); 123   24260 auto* n = static_cast<node*>(w);
124   // Already detached — nothing to do. 124   // Already detached — nothing to do.
HITCBC 125   24518 if (!n->next_ && !n->prev_ && head_ != w && tail_ != w) 125   24260 if (!n->next_ && !n->prev_ && head_ != w && tail_ != w)
MISUBC 126   return; 126   return;
HITCBC 127   24518 if (n->prev_) 127   24260 if (n->prev_)
HITCBC 128   7812 static_cast<node*>(n->prev_)->next_ = n->next_; 128   7727 static_cast<node*>(n->prev_)->next_ = n->next_;
129   else 129   else
HITCBC 130   16706 head_ = n->next_; 130   16533 head_ = n->next_;
HITCBC 131   24518 if (n->next_) 131   24260 if (n->next_)
HITCBC 132   15661 static_cast<node*>(n->next_)->prev_ = n->prev_; 132   15488 static_cast<node*>(n->next_)->prev_ = n->prev_;
133   else 133   else
HITCBC 134   8857 tail_ = n->prev_; 134   8772 tail_ = n->prev_;
HITCBC 135   24518 n->next_ = nullptr; 135   24260 n->next_ = nullptr;
HITCBC 136   24518 n->prev_ = nullptr; 136   24260 n->prev_ = nullptr;
137   } 137   }
138   138  
139   /// Invoke @p f for each element in the list. 139   /// Invoke @p f for each element in the list.
140   template<class F> 140   template<class F>
HITCBC 141   122 void for_each(F f) 141   122 void for_each(F f)
142   { 142   {
HITCBC 143   122 for (T* p = head_; p; p = static_cast<node*>(p)->next_) 143   122 for (T* p = head_; p; p = static_cast<node*>(p)->next_)
MISUBC 144   f(p); 144   f(p);
HITCBC 145   122 } 145   122 }
146   }; 146   };
147   147  
148   /** An intrusive singly linked FIFO queue. 148   /** An intrusive singly linked FIFO queue.
149   149  
150   This container provides O(1) push and pop operations for 150   This container provides O(1) push and pop operations for
151   elements that derive from @ref node. Elements are not 151   elements that derive from @ref node. Elements are not
152   copied or moved; they are linked directly into the queue. 152   copied or moved; they are linked directly into the queue.
153   153  
154   Unlike @ref intrusive_list, this uses only a single `next_` 154   Unlike @ref intrusive_list, this uses only a single `next_`
155   pointer per node, saving memory at the cost of not supporting 155   pointer per node, saving memory at the cost of not supporting
156   O(1) removal of arbitrary elements. 156   O(1) removal of arbitrary elements.
157   157  
158   @tparam T The element type. Must derive from `intrusive_queue<T>::node`. 158   @tparam T The element type. Must derive from `intrusive_queue<T>::node`.
159   */ 159   */
160   template<class T> 160   template<class T>
161   class intrusive_queue 161   class intrusive_queue
162   { 162   {
163   public: 163   public:
164   /** Base class for queue elements. 164   /** Base class for queue elements.
165   165  
166   Derive from this class to make a type usable with 166   Derive from this class to make a type usable with
167   @ref intrusive_queue. The `next_` pointer is private 167   @ref intrusive_queue. The `next_` pointer is private
168   and accessible only to the queue. 168   and accessible only to the queue.
169   */ 169   */
170   class node 170   class node
171   { 171   {
172   friend class intrusive_queue; 172   friend class intrusive_queue;
173   173  
174   private: 174   private:
175   T* next_; 175   T* next_;
176   }; 176   };
177   177  
178   private: 178   private:
179   T* head_ = nullptr; 179   T* head_ = nullptr;
180   T* tail_ = nullptr; 180   T* tail_ = nullptr;
181   181  
182   public: 182   public:
HITCBC 183   2730 intrusive_queue() = default; 183   2731 intrusive_queue() = default;
184   184  
185   intrusive_queue(intrusive_queue&& other) noexcept 185   intrusive_queue(intrusive_queue&& other) noexcept
186   : head_(other.head_) 186   : head_(other.head_)
187   , tail_(other.tail_) 187   , tail_(other.tail_)
188   { 188   {
189   other.head_ = nullptr; 189   other.head_ = nullptr;
190   other.tail_ = nullptr; 190   other.tail_ = nullptr;
191   } 191   }
192   192  
193   intrusive_queue(intrusive_queue const&) = delete; 193   intrusive_queue(intrusive_queue const&) = delete;
194   intrusive_queue& operator=(intrusive_queue const&) = delete; 194   intrusive_queue& operator=(intrusive_queue const&) = delete;
195   intrusive_queue& operator=(intrusive_queue&&) = delete; 195   intrusive_queue& operator=(intrusive_queue&&) = delete;
196   196  
HITCBC 197   1984159 bool empty() const noexcept 197   2351362 bool empty() const noexcept
198   { 198   {
HITCBC 199   1984159 return head_ == nullptr; 199   2351362 return head_ == nullptr;
200   } 200   }
201   201  
HITCBC 202   611697 void push(T* w) noexcept 202   724914 void push(T* w) noexcept
203   { 203   {
HITCBC 204   611697 w->next_ = nullptr; 204   724914 w->next_ = nullptr;
HITCBC 205   611697 if (tail_) 205   724914 if (tail_)
HITCBC 206   268204 tail_->next_ = w; 206   313269 tail_->next_ = w;
207   else 207   else
HITCBC 208   343493 head_ = w; 208   411645 head_ = w;
HITCBC 209   611697 tail_ = w; 209   724914 tail_ = w;
HITCBC 210   611697 } 210   724914 }
211   211  
HITCBC 212   320472 void splice(intrusive_queue& other) noexcept 212   388243 void splice(intrusive_queue& other) noexcept
213   { 213   {
HITCBC 214   320472 if (other.empty()) 214   388243 if (other.empty())
MISUBC 215   return; 215   return;
HITCBC 216   320472 if (tail_) 216   388243 if (tail_)
HITCBC 217   123086 tail_->next_ = other.head_; 217   161258 tail_->next_ = other.head_;
218   else 218   else
HITCBC 219   197386 head_ = other.head_; 219   226985 head_ = other.head_;
HITCBC 220   320472 tail_ = other.tail_; 220   388243 tail_ = other.tail_;
HITCBC 221   320472 other.head_ = nullptr; 221   388243 other.head_ = nullptr;
HITCBC 222   320472 other.tail_ = nullptr; 222   388243 other.tail_ = nullptr;
223   } 223   }
224   224  
HITCBC 225   834102 T* pop() noexcept 225   990179 T* pop() noexcept
226   { 226   {
HITCBC 227   834102 if (!head_) 227   990179 if (!head_)
HITCBC 228   222405 return nullptr; 228   265265 return nullptr;
HITCBC 229   611697 T* w = head_; 229   724914 T* w = head_;
HITCBC 230   611697 head_ = head_->next_; 230   724914 head_ = head_->next_;
HITCBC 231   611697 if (!head_) 231   724914 if (!head_)
HITCBC 232   220407 tail_ = nullptr; 232   250387 tail_ = nullptr;
233   // Defensive: clear stale linkage on popped node. 233   // Defensive: clear stale linkage on popped node.
HITCBC 234   611697 w->next_ = nullptr; 234   724914 w->next_ = nullptr;
HITCBC 235   611697 return w; 235   724914 return w;
236   } 236   }
237   }; 237   };
238   238  
239   } // namespace boost::corosio::detail 239   } // namespace boost::corosio::detail
240   240  
241   #endif 241   #endif