rippled
List.h
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of Beast: https://github.com/vinniefalco/Beast
4  Copyright 2013, Vinnie Falco <vinnie.falco@gmail.com>
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #ifndef BEAST_INTRUSIVE_LIST_H_INCLUDED
21 #define BEAST_INTRUSIVE_LIST_H_INCLUDED
22 
23 #include <iterator>
24 #include <type_traits>
25 
26 namespace beast {
27 
28 template <typename, typename>
29 class List;
30 
31 namespace detail {
32 
35 template <typename T, typename U>
36 struct CopyConst
37 {
38  explicit CopyConst() = default;
39 
40  using type = typename std::remove_const<U>::type;
41 };
42 
43 template <typename T, typename U>
44 struct CopyConst<T const, U>
45 {
46  explicit CopyConst() = default;
47 
48  using type = typename std::remove_const<U>::type const;
49 };
52 // This is the intrusive portion of the doubly linked list.
53 // One derivation per list that the object may appear on
54 // concurrently is required.
55 //
56 template <typename T, typename Tag>
57 class ListNode
58 {
59 private:
60  using value_type = T;
61 
62  friend class List<T, Tag>;
63 
64  template <typename>
65  friend class ListIterator;
66 
69 };
70 
71 //------------------------------------------------------------------------------
72 
73 template <typename N>
75  : public std::iterator<std::bidirectional_iterator_tag, std::size_t>
76 {
77 public:
78  using value_type =
80  using pointer = value_type*;
83 
84  ListIterator(N* node = nullptr) noexcept : m_node(node)
85  {
86  }
87 
88  template <typename M>
89  ListIterator(ListIterator<M> const& other) noexcept : m_node(other.m_node)
90  {
91  }
92 
93  template <typename M>
94  bool
95  operator==(ListIterator<M> const& other) const noexcept
96  {
97  return m_node == other.m_node;
98  }
99 
100  template <typename M>
101  bool
102  operator!=(ListIterator<M> const& other) const noexcept
103  {
104  return !((*this) == other);
105  }
106 
107  reference
108  operator*() const noexcept
109  {
110  return dereference();
111  }
112 
113  pointer
114  operator->() const noexcept
115  {
116  return &dereference();
117  }
118 
119  ListIterator&
120  operator++() noexcept
121  {
122  increment();
123  return *this;
124  }
125 
127  operator++(int) noexcept
128  {
129  ListIterator result(*this);
130  increment();
131  return result;
132  }
133 
134  ListIterator&
135  operator--() noexcept
136  {
137  decrement();
138  return *this;
139  }
140 
142  operator--(int) noexcept
143  {
144  ListIterator result(*this);
145  decrement();
146  return result;
147  }
148 
149 private:
150  reference
151  dereference() const noexcept
152  {
153  return static_cast<reference>(*m_node);
154  }
155 
156  void
157  increment() noexcept
158  {
159  m_node = m_node->m_next;
160  }
161 
162  void
163  decrement() noexcept
164  {
165  m_node = m_node->m_prev;
166  }
167 
168  N* m_node;
169 };
170 
171 } // namespace detail
172 
277 template <typename T, typename Tag = void>
278 class List
279 {
280 public:
281  using Node = typename detail::ListNode<T, Tag>;
282 
283  using value_type = T;
284  using pointer = value_type*;
286  using const_pointer = value_type const*;
287  using const_reference = value_type const&;
290 
293 
296  {
297  m_head.m_prev = nullptr; // identifies the head
298  m_tail.m_next = nullptr; // identifies the tail
299  clear();
300  }
301 
302  List(List const&) = delete;
303  List&
304  operator=(List const&) = delete;
305 
309  bool
310  empty() const noexcept
311  {
312  return size() == 0;
313  }
314 
316  size_type
317  size() const noexcept
318  {
319  return m_size;
320  }
321 
326  reference
327  front() noexcept
328  {
329  return element_from(m_head.m_next);
330  }
331 
337  front() const noexcept
338  {
339  return element_from(m_head.m_next);
340  }
341 
346  reference
347  back() noexcept
348  {
349  return element_from(m_tail.m_prev);
350  }
351 
357  back() const noexcept
358  {
359  return element_from(m_tail.m_prev);
360  }
361 
365  iterator
366  begin() noexcept
367  {
368  return iterator(m_head.m_next);
369  }
370 
375  begin() const noexcept
376  {
377  return const_iterator(m_head.m_next);
378  }
379 
384  cbegin() const noexcept
385  {
386  return const_iterator(m_head.m_next);
387  }
388 
392  iterator
393  end() noexcept
394  {
395  return iterator(&m_tail);
396  }
397 
402  end() const noexcept
403  {
404  return const_iterator(&m_tail);
405  }
406 
411  cend() const noexcept
412  {
413  return const_iterator(&m_tail);
414  }
415 
419  void
420  clear() noexcept
421  {
422  m_head.m_next = &m_tail;
423  m_tail.m_prev = &m_head;
424  m_size = 0;
425  }
426 
433  iterator
434  insert(iterator pos, T& element) noexcept
435  {
436  Node* node = static_cast<Node*>(&element);
437  node->m_next = &*pos;
438  node->m_prev = node->m_next->m_prev;
439  node->m_next->m_prev = node;
440  node->m_prev->m_next = node;
441  ++m_size;
442  return iterator(node);
443  }
444 
450  void
451  insert(iterator pos, List& other) noexcept
452  {
453  if (!other.empty())
454  {
455  Node* before = &*pos;
456  other.m_head.m_next->m_prev = before->m_prev;
457  before->m_prev->m_next = other.m_head.m_next;
458  other.m_tail.m_prev->m_next = before;
459  before->m_prev = other.m_tail.m_prev;
460  m_size += other.m_size;
461  other.clear();
462  }
463  }
464 
470  iterator
471  erase(iterator pos) noexcept
472  {
473  Node* node = &*pos;
474  ++pos;
475  node->m_next->m_prev = node->m_prev;
476  node->m_prev->m_next = node->m_next;
477  --m_size;
478  return pos;
479  }
480 
485  iterator
486  push_front(T& element) noexcept
487  {
488  return insert(begin(), element);
489  }
490 
495  T&
496  pop_front() noexcept
497  {
498  T& element(front());
499  erase(begin());
500  return element;
501  }
502 
507  iterator
508  push_back(T& element) noexcept
509  {
510  return insert(end(), element);
511  }
512 
517  T&
518  pop_back() noexcept
519  {
520  T& element(back());
521  erase(--end());
522  return element;
523  }
524 
526  void
527  swap(List& other) noexcept
528  {
529  List temp;
530  temp.append(other);
531  other.append(*this);
532  append(temp);
533  }
534 
539  iterator
540  prepend(List& list) noexcept
541  {
542  return insert(begin(), list);
543  }
544 
549  iterator
550  append(List& list) noexcept
551  {
552  return insert(end(), list);
553  }
554 
560  iterator
561  iterator_to(T& element) const noexcept
562  {
563  return iterator(static_cast<Node*>(&element));
564  }
565 
572  const_iterator_to(T const& element) const noexcept
573  {
574  return const_iterator(static_cast<Node const*>(&element));
575  }
576 
577 private:
578  reference
579  element_from(Node* node) noexcept
580  {
581  return *(static_cast<pointer>(node));
582  }
583 
585  element_from(Node const* node) const noexcept
586  {
587  return *(static_cast<const_pointer>(node));
588  }
589 
590 private:
594 };
595 
596 } // namespace beast
597 
598 #endif
beast::detail::ListIterator::ListIterator
ListIterator(N *node=nullptr) noexcept
Definition: List.h:84
beast::List::front
const_reference front() const noexcept
Obtain a const reference to the first element.
Definition: List.h:337
beast::List::size
size_type size() const noexcept
Returns the number of elements in the list.
Definition: List.h:317
beast::List::operator=
List & operator=(List const &)=delete
beast::detail::ListIterator
Definition: List.h:74
beast::detail::ListIterator::decrement
void decrement() noexcept
Definition: List.h:163
beast::detail::ListIterator::increment
void increment() noexcept
Definition: List.h:157
beast::List::const_iterator
detail::ListIterator< Node const > const_iterator
Definition: List.h:292
beast::detail::ListIterator::reference
value_type & reference
Definition: List.h:81
beast::List::prepend
iterator prepend(List &list) noexcept
Insert another list at the beginning of this list.
Definition: List.h:540
beast::detail::ListIterator::ListIterator
ListIterator(ListIterator< M > const &other) noexcept
Definition: List.h:89
beast::detail::CopyConst::type
typename std::remove_const< U >::type type
Definition: List.h:40
beast::List::iterator_to
iterator iterator_to(T &element) const noexcept
Obtain an iterator from an element.
Definition: List.h:561
beast::List::end
iterator end() noexcept
Obtain a iterator to the end of the list.
Definition: List.h:393
beast::List::size_type
std::size_t size_type
Definition: List.h:288
beast::List::const_iterator_to
const_iterator const_iterator_to(T const &element) const noexcept
Obtain a const iterator from an element.
Definition: List.h:572
iterator
beast::List::empty
bool empty() const noexcept
Determine if the list is empty.
Definition: List.h:310
beast::insight::detail::StatsDMetricBase
Definition: StatsDCollector.cpp:52
beast::List::iterator
detail::ListIterator< Node > iterator
Definition: List.h:291
beast::List::cend
const_iterator cend() const noexcept
Obtain a const iterator to the end of the list.
Definition: List.h:411
beast::List::clear
void clear() noexcept
Clear the list.
Definition: List.h:420
beast::List::push_front
iterator push_front(T &element) noexcept
Insert an element at the beginning of the list.
Definition: List.h:486
beast::detail::CopyConst< T const, U >::type
typename std::remove_const< U >::type const type
Definition: List.h:48
beast::detail::ListIterator::operator!=
bool operator!=(ListIterator< M > const &other) const noexcept
Definition: List.h:102
beast::detail::ListIterator::operator++
ListIterator & operator++() noexcept
Definition: List.h:120
beast::List::insert
void insert(iterator pos, List &other) noexcept
Insert another list into this one.
Definition: List.h:451
beast::List::m_head
Node m_head
Definition: List.h:592
beast::List::cbegin
const_iterator cbegin() const noexcept
Obtain a const iterator to the beginning of the list.
Definition: List.h:384
beast::detail::ListIterator::operator--
ListIterator & operator--() noexcept
Definition: List.h:135
beast::List::begin
iterator begin() noexcept
Obtain an iterator to the beginning of the list.
Definition: List.h:366
beast::List::insert
iterator insert(iterator pos, T &element) noexcept
Insert an element.
Definition: List.h:434
beast::detail::ListIterator::m_node
N * m_node
Definition: List.h:168
beast::detail::ListIterator::operator--
ListIterator operator--(int) noexcept
Definition: List.h:142
beast::detail::ListIterator::operator++
ListIterator operator++(int) noexcept
Definition: List.h:127
beast::List< beast::insight::detail::StatsDMetricBase >::Node
typename detail::ListNode< beast::insight::detail::StatsDMetricBase, void > Node
Definition: List.h:281
beast::List::element_from
reference element_from(Node *node) noexcept
Definition: List.h:579
beast::List::m_size
size_type m_size
Definition: List.h:591
beast::List::pop_front
T & pop_front() noexcept
Remove the element at the beginning of the list.
Definition: List.h:496
beast::List::element_from
const_reference element_from(Node const *node) const noexcept
Definition: List.h:585
beast::List< beast::insight::detail::StatsDMetricBase >::const_reference
value_type const & const_reference
Definition: List.h:287
beast::detail::ListNode
Definition: List.h:57
beast::detail::CopyConst::CopyConst
CopyConst()=default
beast::detail::ListNode::m_next
ListNode * m_next
Definition: List.h:67
beast::List::front
reference front() noexcept
Obtain a reference to the first element.
Definition: List.h:327
beast::detail::ListIterator::pointer
value_type * pointer
Definition: List.h:80
beast::detail::ListIterator::dereference
reference dereference() const noexcept
Definition: List.h:151
beast::detail::ListIterator::operator==
bool operator==(ListIterator< M > const &other) const noexcept
Definition: List.h:95
beast::detail::ListIterator::operator*
reference operator*() const noexcept
Definition: List.h:108
beast::List::swap
void swap(List &other) noexcept
Swap contents with another list.
Definition: List.h:527
std::ptrdiff_t
std::remove_const
beast::detail::CopyConst
Copy const attribute from T to U if present.
Definition: List.h:36
std::size_t
beast::List::append
iterator append(List &list) noexcept
Append another list at the end of this list.
Definition: List.h:550
beast::List::begin
const_iterator begin() const noexcept
Obtain a const iterator to the beginning of the list.
Definition: List.h:375
beast::List::erase
iterator erase(iterator pos) noexcept
Remove an element.
Definition: List.h:471
beast::detail::ListNode::m_prev
ListNode * m_prev
Definition: List.h:68
beast::detail::ListIterator::operator->
pointer operator->() const noexcept
Definition: List.h:114
beast::List::end
const_iterator end() const noexcept
Obtain a const iterator to the end of the list.
Definition: List.h:402
beast::List::m_tail
Node m_tail
Definition: List.h:593
beast::List::back
const_reference back() const noexcept
Obtain a const reference to the last element.
Definition: List.h:357
beast::List::List
List()
Create an empty list.
Definition: List.h:295
type_traits
beast::List::push_back
iterator push_back(T &element) noexcept
Append an element at the end of the list.
Definition: List.h:508
beast::detail::ListIterator::value_type
typename beast::detail::CopyConst< N, typename N::value_type >::type value_type
Definition: List.h:79
beast::List< beast::insight::detail::StatsDMetricBase >::const_pointer
value_type const * const_pointer
Definition: List.h:286
beast::List
Intrusive doubly linked list.
Definition: List.h:29
beast::List::pop_back
T & pop_back() noexcept
Remove the element at the end of the list.
Definition: List.h:518
beast::PropertyStream::Item
Definition: PropertyStream.h:182
beast::List::back
reference back() noexcept
Obtain a reference to the last element.
Definition: List.h:347
beast::List::reference
value_type & reference
Definition: List.h:285
beast
Definition: base_uint.h:585