Yggdrasill  0.2
intervaltree.hpp
1 #ifndef INTERVALTREE_HPP
2 #define INTERVALTREE_HPP
3 
4 #include "rbtree.hpp"
5 
6 #include <algorithm>
7 #include <iostream>
8 #include <string>
9 
10 namespace ygg {
11 namespace intervaltree_internal {
12 template <class Node, class INB, class NodeTraits, bool skipfirst,
13  class Comparable>
14 Node * find_next_overlapping(Node * cur, const Comparable & q);
15 
16 template <class KeyType>
17 class DummyRange : public std::pair<KeyType, KeyType> {
18 public:
19  DummyRange(KeyType lower, KeyType upper);
20 };
21 
22 template <class Node, class NodeTraits>
24 public:
25  template <class T1, class T2>
26  bool operator()(const T1 & lhs, const T2 & rhs) const;
27 };
28 
29 // TODO add a possibility for bulk updates
30 template <class Node, class INB, class NodeTraits>
31 class ExtendedNodeTraits : public NodeTraits {
32 public:
33  // TODO these can probably made more efficient
34  template <class BaseTree>
35  static void leaf_inserted(Node & node, BaseTree & t);
36 
37  static void fix_node(Node & node);
38  template <class BaseTree>
39  static void rotated_left(Node & node, BaseTree & t);
40  template <class BaseTree>
41  static void rotated_right(Node & node, BaseTree & t);
42  template <class BaseTree>
43  static void deleted_below(Node & node, BaseTree & t);
44  template <class BaseTree>
45  static void
46  delete_leaf(Node & node, BaseTree & t)
47  {
48  (void)node;
49  (void)t;
50  }
51  template <class BaseTree>
52  static void swapped(Node & n1, Node & n2, BaseTree & t);
53 
54  // Make our DummyRange comparable
55  static typename NodeTraits::key_type get_lower(
57  range);
58  static typename NodeTraits::key_type get_upper(
60  range);
61 };
62 } // namespace intervaltree_internal
63 
64 template <class Node, class NodeTraits, class Options = DefaultOptions,
65  class Tag = int>
66 class ITreeNodeBase : public RBTreeNodeBase<Node, Options, Tag> {
67 public:
68  typename NodeTraits::key_type _it_max_upper;
69 };
70 
79 template <class Node>
81 public:
87  using key_type = void;
88 
96  static key_type get_lower(const Node & n) = delete;
97 
105  static key_type get_upper(const Node & n) = delete;
106 };
107 
123 template <class Node, class NodeTraits, class Options = DefaultOptions,
124  class Tag = int>
126  : private RBTree<
127  Node,
128  intervaltree_internal::ExtendedNodeTraits<
129  Node, ITreeNodeBase<Node, NodeTraits, Options, Tag>, NodeTraits>,
130  Options, Tag,
131  intervaltree_internal::IntervalCompare<Node, NodeTraits>> {
132 public:
133  using Key = typename NodeTraits::key_type;
135 
137  static_assert(std::is_base_of<INB, Node>::value,
138  "Node class not properly derived from ITreeNodeBase!");
139 
140  static_assert(std::is_base_of<ITreeNodeTraits<Node>, NodeTraits>::value,
141  "NodeTraits not properly derived from ITreeNodeTraits!");
142 
143  using ENodeTraits =
145  using BaseTree = RBTree<
148 
149  IntervalTree();
150 
151  bool verify_integrity() const;
152  void dump_to_dot(const std::string & filename) const;
153 
154  /* Import some of RBTree's methods into the public namespace */
155  using BaseTree::empty;
156  using BaseTree::insert;
157  using BaseTree::remove;
158 
159  // Iteration of sets of intervals
160  template <class Comparable>
161  class QueryResult {
162  public:
164  public:
165  typedef ptrdiff_t difference_type;
166  typedef Node value_type;
167  typedef const Node & const_reference;
168  typedef const Node * const_pointer;
169  typedef std::input_iterator_tag iterator_category;
170 
171  const_iterator(Node * n, const Comparable & q);
172  const_iterator(const const_iterator & other);
173  ~const_iterator();
174 
175  const_iterator & operator=(const const_iterator & other);
176 
177  bool operator==(const const_iterator & other) const;
178  bool operator!=(const const_iterator & other) const;
179 
180  const_iterator & operator++();
181  const_iterator operator++(int);
182 
183  const_reference operator*() const;
184  const_pointer operator->() const;
185 
186  private:
187  Node * n;
188  Comparable q;
189  };
190 
191  QueryResult(Node * n, const Comparable & q);
192 
193  const_iterator begin() const;
194  const_iterator end() const;
195 
196  private:
197  Node * n;
198  Comparable q;
199  };
200 
217  template <class Comparable>
218  QueryResult<Comparable> query(const Comparable & q) const;
219 
220  template <class Comparable>
221  typename BaseTree::template const_iterator<false>
222  interval_upper_bound(const Comparable & query_range) const;
223 
224  // TODO FIXME this is actually very specific?
225  void fixup_maxima(Node & lowest);
226 
227  // Iterating the events should still be possible
228  using BaseTree::begin;
229  using BaseTree::end;
230 
231 private:
232  bool verify_maxima(Node * n) const;
233 };
234 
235 } // namespace ygg
236 
237 #include "intervaltree.cpp"
238 
239 #endif // INTERVALTREE_HPP
Abstract base class for the Node Traits that need to be implemented.
Definition: intervaltree.hpp:80
Definition: intervaltree.hpp:23
Stores an Interval Tree.
Definition: intervaltree.hpp:125
Definition: intervaltree.hpp:66
Definition: intervaltree.hpp:17
Base class (template) to supply your node class with metainformation.
Definition: rbtree.hpp:92
Definition: intervaltree.hpp:161
Definition: intervaltree.hpp:31
void key_type
The type of your interval bounds. This is the type that get_lower() and get_upper() must return...
Definition: intervaltree.hpp:87
The Red-Black Tree.
Definition: rbtree.hpp:194
Definition: intervaltree.hpp:163
Definition: rbtree.hpp:18