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 {
76 public:
78  using value_type =
81  using pointer = value_type*;
84 
85  ListIterator(N* node = nullptr) noexcept : m_node(node)
86  {
87  }
88 
89  template <typename M>
90  ListIterator(ListIterator<M> const& other) noexcept : m_node(other.m_node)
91  {
92  }
93 
94  template <typename M>
95  bool
96  operator==(ListIterator<M> const& other) const noexcept
97  {
98  return m_node == other.m_node;
99  }
100 
101  template <typename M>
102  bool
103  operator!=(ListIterator<M> const& other) const noexcept
104  {
105  return !((*this) == other);
106  }
107 
108  reference
109  operator*() const noexcept
110  {
111  return dereference();
112  }
113 
114  pointer
115  operator->() const noexcept
116  {
117  return &dereference();
118  }
119 
120  ListIterator&
121  operator++() noexcept
122  {
123  increment();
124  return *this;
125  }
126 
128  operator++(int) noexcept
129  {
130  ListIterator result(*this);
131  increment();
132  return result;
133  }
134 
135  ListIterator&
136  operator--() noexcept
137  {
138  decrement();
139  return *this;
140  }
141 
143  operator--(int) noexcept
144  {
145  ListIterator result(*this);
146  decrement();
147  return result;
148  }
149 
150 private:
151  reference
152  dereference() const noexcept
153  {
154  return static_cast<reference>(*m_node);
155  }
156 
157  void
158  increment() noexcept
159  {
160  m_node = m_node->m_next;
161  }
162 
163  void
164  decrement() noexcept
165  {
166  m_node = m_node->m_prev;
167  }
168 
169  N* m_node;
170 };
171 
172 } // namespace detail
173 
278 template <typename T, typename Tag = void>
279 class List
280 {
281 public:
282  using Node = typename detail::ListNode<T, Tag>;
283 
284  using value_type = T;
285  using pointer = value_type*;
287  using const_pointer = value_type const*;
288  using const_reference = value_type const&;
291 
294 
297  {
298  m_head.m_prev = nullptr; // identifies the head
299  m_tail.m_next = nullptr; // identifies the tail
300  clear();
301  }
302 
303  List(List const&) = delete;
304  List&
305  operator=(List const&) = delete;
306 
310  bool
311  empty() const noexcept
312  {
313  return size() == 0;
314  }
315 
317  size_type
318  size() const noexcept
319  {
320  return m_size;
321  }
322 
327  reference
328  front() noexcept
329  {
330  return element_from(m_head.m_next);
331  }
332 
338  front() const noexcept
339  {
340  return element_from(m_head.m_next);
341  }
342 
347  reference
348  back() noexcept
349  {
350  return element_from(m_tail.m_prev);
351  }
352 
358  back() const noexcept
359  {
360  return element_from(m_tail.m_prev);
361  }
362 
366  iterator
367  begin() noexcept
368  {
369  return iterator(m_head.m_next);
370  }
371 
376  begin() const noexcept
377  {
378  return const_iterator(m_head.m_next);
379  }
380 
385  cbegin() const noexcept
386  {
387  return const_iterator(m_head.m_next);
388  }
389 
393  iterator
394  end() noexcept
395  {
396  return iterator(&m_tail);
397  }
398 
403  end() const noexcept
404  {
405  return const_iterator(&m_tail);
406  }
407 
412  cend() const noexcept
413  {
414  return const_iterator(&m_tail);
415  }
416 
420  void
421  clear() noexcept
422  {
423  m_head.m_next = &m_tail;
424  m_tail.m_prev = &m_head;
425  m_size = 0;
426  }
427 
434  iterator
435  insert(iterator pos, T& element) noexcept
436  {
437  Node* node = static_cast<Node*>(&element);
438  node->m_next = &*pos;
439  node->m_prev = node->m_next->m_prev;
440  node->m_next->m_prev = node;
441  node->m_prev->m_next = node;
442  ++m_size;
443  return iterator(node);
444  }
445 
451  void
452  insert(iterator pos, List& other) noexcept
453  {
454  if (!other.empty())
455  {
456  Node* before = &*pos;
457  other.m_head.m_next->m_prev = before->m_prev;
458  before->m_prev->m_next = other.m_head.m_next;
459  other.m_tail.m_prev->m_next = before;
460  before->m_prev = other.m_tail.m_prev;
461  m_size += other.m_size;
462  other.clear();
463  }
464  }
465 
471  iterator
472  erase(iterator pos) noexcept
473  {
474  Node* node = &*pos;
475  ++pos;
476  node->m_next->m_prev = node->m_prev;
477  node->m_prev->m_next = node->m_next;
478  --m_size;
479  return pos;
480  }
481 
486  iterator
487  push_front(T& element) noexcept
488  {
489  return insert(begin(), element);
490  }
491 
496  T&
497  pop_front() noexcept
498  {
499  T& element(front());
500  erase(begin());
501  return element;
502  }
503 
508  iterator
509  push_back(T& element) noexcept
510  {
511  return insert(end(), element);
512  }
513 
518  T&
519  pop_back() noexcept
520  {
521  T& element(back());
522  erase(--end());
523  return element;
524  }
525 
527  void
528  swap(List& other) noexcept
529  {
530  List temp;
531  temp.append(other);
532  other.append(*this);
533  append(temp);
534  }
535 
540  iterator
541  prepend(List& list) noexcept
542  {
543  return insert(begin(), list);
544  }
545 
550  iterator
551  append(List& list) noexcept
552  {
553  return insert(end(), list);
554  }
555 
561  iterator
562  iterator_to(T& element) const noexcept
563  {
564  return iterator(static_cast<Node*>(&element));
565  }
566 
573  const_iterator_to(T const& element) const noexcept
574  {
575  return const_iterator(static_cast<Node const*>(&element));
576  }
577 
578 private:
579  reference
580  element_from(Node* node) noexcept
581  {
582  return *(static_cast<pointer>(node));
583  }
584 
586  element_from(Node const* node) const noexcept
587  {
588  return *(static_cast<const_pointer>(node));
589  }
590 
591 private:
595 };
596 
597 } // namespace beast
598 
599 #endif
beast::detail::ListIterator::ListIterator
ListIterator(N *node=nullptr) noexcept
Definition: List.h:85
beast::List::front
const_reference front() const noexcept
Obtain a const reference to the first element.
Definition: List.h:338
beast::List::size
size_type size() const noexcept
Returns the number of elements in the list.
Definition: List.h:318
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:164
beast::detail::ListIterator::increment
void increment() noexcept
Definition: List.h:158
beast::List::const_iterator
detail::ListIterator< Node const > const_iterator
Definition: List.h:293
beast::detail::ListIterator::reference
value_type & reference
Definition: List.h:82
beast::List::prepend
iterator prepend(List &list) noexcept
Insert another list at the beginning of this list.
Definition: List.h:541
beast::detail::ListIterator::ListIterator
ListIterator(ListIterator< M > const &other) noexcept
Definition: List.h:90
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:562
beast::List::end
iterator end() noexcept
Obtain a iterator to the end of the list.
Definition: List.h:394
beast::List::size_type
std::size_t size_type
Definition: List.h:289
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:573
iterator
beast::List::empty
bool empty() const noexcept
Determine if the list is empty.
Definition: List.h:311
std::bidirectional_iterator_tag
beast::insight::detail::StatsDMetricBase
Definition: StatsDCollector.cpp:52
beast::List::iterator
detail::ListIterator< Node > iterator
Definition: List.h:292
beast::List::cend
const_iterator cend() const noexcept
Obtain a const iterator to the end of the list.
Definition: List.h:412
beast::List::clear
void clear() noexcept
Clear the list.
Definition: List.h:421
beast::List::push_front
iterator push_front(T &element) noexcept
Insert an element at the beginning of the list.
Definition: List.h:487
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:103
beast::detail::ListIterator::operator++
ListIterator & operator++() noexcept
Definition: List.h:121
beast::List::insert
void insert(iterator pos, List &other) noexcept
Insert another list into this one.
Definition: List.h:452
beast::List::m_head
Node m_head
Definition: List.h:593
beast::List::cbegin
const_iterator cbegin() const noexcept
Obtain a const iterator to the beginning of the list.
Definition: List.h:385
beast::detail::ListIterator::operator--
ListIterator & operator--() noexcept
Definition: List.h:136
beast::List::begin
iterator begin() noexcept
Obtain an iterator to the beginning of the list.
Definition: List.h:367
beast::List::insert
iterator insert(iterator pos, T &element) noexcept
Insert an element.
Definition: List.h:435
beast::detail::ListIterator::m_node
N * m_node
Definition: List.h:169
beast::detail::ListIterator::operator--
ListIterator operator--(int) noexcept
Definition: List.h:143
beast::detail::ListIterator::operator++
ListIterator operator++(int) noexcept
Definition: List.h:128
beast::List< beast::insight::detail::StatsDMetricBase >::Node
typename detail::ListNode< beast::insight::detail::StatsDMetricBase, void > Node
Definition: List.h:282
beast::List::element_from
reference element_from(Node *node) noexcept
Definition: List.h:580
beast::List::m_size
size_type m_size
Definition: List.h:592
beast::List::pop_front
T & pop_front() noexcept
Remove the element at the beginning of the list.
Definition: List.h:497
beast::List::element_from
const_reference element_from(Node const *node) const noexcept
Definition: List.h:586
beast::List< beast::insight::detail::StatsDMetricBase >::const_reference
value_type const & const_reference
Definition: List.h:288
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:328
beast::detail::ListIterator::pointer
value_type * pointer
Definition: List.h:81
beast::detail::ListIterator::dereference
reference dereference() const noexcept
Definition: List.h:152
beast::detail::ListIterator::operator==
bool operator==(ListIterator< M > const &other) const noexcept
Definition: List.h:96
beast::detail::ListIterator::operator*
reference operator*() const noexcept
Definition: List.h:109
beast::List::swap
void swap(List &other) noexcept
Swap contents with another list.
Definition: List.h:528
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:551
beast::List::begin
const_iterator begin() const noexcept
Obtain a const iterator to the beginning of the list.
Definition: List.h:376
beast::List::erase
iterator erase(iterator pos) noexcept
Remove an element.
Definition: List.h:472
beast::detail::ListNode::m_prev
ListNode * m_prev
Definition: List.h:68
beast::detail::ListIterator::operator->
pointer operator->() const noexcept
Definition: List.h:115
beast::List::end
const_iterator end() const noexcept
Obtain a const iterator to the end of the list.
Definition: List.h:403
beast::List::m_tail
Node m_tail
Definition: List.h:594
beast::List::back
const_reference back() const noexcept
Obtain a const reference to the last element.
Definition: List.h:358
beast::List::List
List()
Create an empty list.
Definition: List.h:296
type_traits
beast::List::push_back
iterator push_back(T &element) noexcept
Append an element at the end of the list.
Definition: List.h:509
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:287
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:519
beast::PropertyStream::Item
Definition: PropertyStream.h:170
beast::List::back
reference back() noexcept
Obtain a reference to the last element.
Definition: List.h:348
beast::List::reference
value_type & reference
Definition: List.h:286
beast
Definition: base_uint.h:641