Yggdrasill  0.2
list.hpp
1 //
2 // Created by lukas on 18.09.17.
3 //
4 
5 #ifndef YGG_LIST_HPP
6 #define YGG_LIST_HPP
7 
8 #include <cstddef>
9 #include <iterator>
10 #include <type_traits>
11 
12 #include "options.hpp"
13 #include "size_holder.hpp"
14 
15 namespace ygg {
16 
30 template <class Node, class Tag = int>
31 class ListNodeBase {
32 public:
33  Node * _l_prev;
34  Node * _l_next;
35 };
36 
49 template <class Node, class Options = DefaultOptions, class Tag = int>
50 class List {
51 public:
53 
54  static_assert(std::is_base_of<ListNodeBase<Node, Tag>, Node>::value,
55  "Node class not properly derived from ListNodeBase");
56 
58  template <class ConcreteIterator, class BaseType>
59  class IteratorBase {
60  public:
61  typedef ptrdiff_t difference_type;
62  typedef BaseType value_type;
63  typedef BaseType & reference;
64  typedef BaseType * pointer;
65  typedef std::input_iterator_tag iterator_category;
66 
67  IteratorBase();
68  IteratorBase(List<Node, Options, Tag> * l, BaseType * n);
69  IteratorBase(const ConcreteIterator & other);
70 
71  ConcreteIterator & operator=(const ConcreteIterator & other);
72  ConcreteIterator & operator=(ConcreteIterator && other);
73 
74  bool operator==(const ConcreteIterator & other) const;
75  bool operator!=(const ConcreteIterator & other) const;
76 
77  ConcreteIterator & operator++();
78  ConcreteIterator operator++(int);
79  ConcreteIterator & operator+=(size_t steps);
80  ConcreteIterator operator+(size_t steps) const;
81 
82  ConcreteIterator & operator--();
83  ConcreteIterator operator--(int);
84  ConcreteIterator & operator-=(size_t steps);
85  ConcreteIterator operator-(size_t steps) const;
86 
87  reference operator*() const;
88  pointer operator->() const;
89 
90  protected:
91  BaseType * n;
93  };
95 
96  class const_iterator; // forward for friendship
97 
101  class iterator : public IteratorBase<iterator, Node> {
102  public:
103  using IteratorBase<iterator, Node>::IteratorBase;
104  friend class const_iterator;
105  };
106 
110  class const_iterator : public IteratorBase<const_iterator, const Node> {
111  public:
112  using IteratorBase<const_iterator, const Node>::IteratorBase;
113  const_iterator(const iterator & other)
114  : IteratorBase<const_iterator, const Node>::IteratorBase(other.l,
115  other.n){};
116  };
117 
121  List();
122 
133  void insert(Node * next, Node * n);
134 
140  void remove(Node * n);
141 
147  iterator begin();
148  const_iterator begin() const;
149 
155  iterator end();
156  const_iterator end() const;
157 
163  iterator back();
164  const_iterator back() const;
165 
172  const_iterator iterator_to(const Node & n) const;
173  iterator iterator_to(const Node & n);
174 
184  size_t size() const;
185 
193  bool empty() const;
194 
200  void clear();
201 
202 private:
203  Node * head;
204  Node * tail;
205 
206  SizeHolder<Options::constant_time_size> s;
207 };
208 
209 } // namespace ygg
210 
211 #include "list.cpp"
212 
213 #endif // YGG_LIST_HPP
An intrusive doubly-linked list class.
Definition: list.hpp:50
Base class (template) to supply your node class with metainformation.
Definition: list.hpp:31
Const iterator over all elements in the list.
Definition: list.hpp:110
Iterator over all elements in the list.
Definition: list.hpp:101
Definition: rbtree.hpp:18