PPL  1.2
CO_Tree_defs.hh
Go to the documentation of this file.
1 /* CO_Tree class declaration.
2  Copyright (C) 2001-2010 Roberto Bagnara <bagnara@cs.unipr.it>
3  Copyright (C) 2010-2016 BUGSENG srl (http://bugseng.com)
4 
5 This file is part of the Parma Polyhedra Library (PPL).
6 
7 The PPL is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 The PPL is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software Foundation,
19 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1307, USA.
20 
21 For the most up-to-date information see the Parma Polyhedra Library
22 site: http://bugseng.com/products/ppl/ . */
23 
24 #ifndef PPL_CO_Tree_defs_hh
25 #define PPL_CO_Tree_defs_hh 1
26 
27 #include "CO_Tree_types.hh"
28 
29 #include "Coefficient_defs.hh"
30 #include <memory>
31 #include <cstddef>
32 
33 #ifndef PPL_CO_TREE_EXTRA_DEBUG
34 #ifdef PPL_ABI_BREAKING_EXTRA_DEBUG
35 #ifdef PPL_DOXYGEN_INCLUDE_IMPLEMENTATION_DETAILS
36 
45 #endif // defined(PPL_DOXYGEN_INCLUDE_IMPLEMENTATION_DETAILS)
46 #define PPL_CO_TREE_EXTRA_DEBUG 1
47 #else // !defined(PPL_ABI_BREAKING_EXTRA_DEBUG)
48 #define PPL_CO_TREE_EXTRA_DEBUG 0
49 #endif // !defined(PPL_ABI_BREAKING_EXTRA_DEBUG)
50 #endif // !defined(PPL_CO_TREE_EXTRA_DEBUG)
51 
52 
53 namespace Parma_Polyhedra_Library {
54 
55 #ifdef PPL_DOXYGEN_INCLUDE_IMPLEMENTATION_DETAILS
56 
102 #endif // defined(PPL_DOXYGEN_INCLUDE_IMPLEMENTATION_DETAILS)
103 class CO_Tree {
104 
105 public:
106  class const_iterator;
107  class iterator;
108 
109 private:
111  typedef unsigned height_t;
112 
114  >= sizeof_to_bits(sizeof(dimension_type)),
115  "height_t is too small to store depths.");
116 
117  class tree_iterator;
118 
119  // This must be declared here, because it is a friend of const_iterator.
122 
129 
130  // This must be declared here, because it is a friend of iterator.
133 
139  dimension_type dfs_index(iterator itr) const;
140 
141 public:
142 
144 
149  typedef Coefficient_traits::const_reference data_type_const_reference;
150 
152 
158  private:
159  public:
160 
161  typedef std::bidirectional_iterator_tag iterator_category;
162  typedef const data_type value_type;
163  typedef std::ptrdiff_t difference_type;
164  typedef value_type* pointer;
165  typedef data_type_const_reference reference;
166 
168 
171  explicit const_iterator();
172 
174 
180  explicit const_iterator(const CO_Tree& tree);
181 
183 
195  const_iterator(const CO_Tree& tree, dimension_type i);
196 
198 
204  const_iterator(const const_iterator& itr);
205 
207 
213  const_iterator(const iterator& itr);
214 
216 
222  void m_swap(const_iterator& itr);
223 
225 
232 
234 
240  const_iterator& operator=(const iterator& itr);
241 
243 
247 
249 
253 
255 
259 
261 
265 
267  data_type_const_reference operator*() const;
268 
270 
273  dimension_type index() const;
274 
276 
280  bool operator==(const const_iterator& x) const;
281 
283 
287  bool operator!=(const const_iterator& x) const;
288 
289  private:
291  bool OK() const;
292 
295 
297  const data_type* current_data;
298 
299 #if PPL_CO_TREE_EXTRA_DEBUG
300  const CO_Tree* tree;
302 #endif
303 
305  };
306 
308 
313  class iterator {
314  public:
315 
316  typedef std::bidirectional_iterator_tag iterator_category;
317  typedef data_type value_type;
318  typedef std::ptrdiff_t difference_type;
319  typedef value_type* pointer;
320  typedef value_type& reference;
321 
323 
326  iterator();
327 
329 
335  explicit iterator(CO_Tree& tree);
336 
338 
350  iterator(CO_Tree& tree, dimension_type i);
351 
353 
362  explicit iterator(const tree_iterator& itr);
363 
365 
371  iterator(const iterator& itr);
372 
374 
380  void m_swap(iterator& itr);
381 
383 
389  iterator& operator=(const iterator& itr);
390 
392 
398  iterator& operator=(const tree_iterator& itr);
399 
401 
404  iterator& operator++();
405 
407 
410  iterator& operator--();
411 
413 
416  iterator operator++(int);
417 
419 
422  iterator operator--(int);
423 
425  data_type& operator*();
426 
428  data_type_const_reference operator*() const;
429 
431 
434  dimension_type index() const;
435 
437 
441  bool operator==(const iterator& x) const;
442 
444 
448  bool operator!=(const iterator& x) const;
449 
450  private:
452  bool OK() const;
453 
456 
458  data_type* current_data;
459 
460 #if PPL_CO_TREE_EXTRA_DEBUG
461  CO_Tree* tree;
463 #endif
464 
466  friend dimension_type CO_Tree::dfs_index(iterator itr) const;
467  };
468 
470 
473  CO_Tree();
474 
476 
482  CO_Tree(const CO_Tree& y);
483 
485 
501  template <typename Iterator>
502  CO_Tree(Iterator i, dimension_type n);
503 
505 
511  CO_Tree& operator=(const CO_Tree& y);
512 
514 
517  void clear();
518 
520 
523  ~CO_Tree();
524 
526 
529  bool empty() const;
530 
532 
535  dimension_type size() const;
536 
538  static dimension_type max_size();
539 
541  void dump_tree() const;
542 
544 
548 
550 
567 
569 
587  iterator insert(dimension_type key, data_type_const_reference data);
588 
590 
616 
618 
646  data_type_const_reference data);
647 
649 
662 
664 
676  iterator erase(iterator itr);
677 
691 
693 
704 
707 
712  void fast_shift(dimension_type i, iterator itr);
713 
715 
723  void m_swap(CO_Tree& x);
724 
726 
729  iterator begin();
730 
732 
740  const iterator& end();
741 
743  const_iterator begin() const;
744 
746  const const_iterator& end() const;
747 
749 
752  const_iterator cbegin() const;
753 
755 
763  const const_iterator& cend() const;
764 
766 
778 
780 
792 
794 
816 
818 
840  dimension_type key) const;
841 
843 
861 
863 
881 
882 private:
883 
885 
906  dimension_type key) const;
907 
909 
930 
932 
955  data_type_const_reference data,
956  tree_iterator itr);
957 
959 
966  data_type_const_reference data,
967  tree_iterator itr);
968 
970 
985  data_type_const_reference data);
986 
988 
1001 
1003 
1009  void init(dimension_type n);
1010 
1012 
1018  void destroy();
1019 
1021  bool structure_OK() const;
1022 
1024  bool OK() const;
1025 
1027 
1033  static unsigned integer_log2(dimension_type n);
1034 
1036 
1051  static bool is_less_than_ratio(dimension_type numer, dimension_type denom,
1052  dimension_type ratio);
1053 
1055 
1071  static bool is_greater_than_ratio(dimension_type numer, dimension_type denom,
1072  dimension_type ratio);
1073 
1075 
1079  static void dump_subtree(tree_iterator itr);
1080 
1082 
1088  void rebuild_bigger_tree();
1089 
1091 
1100  void rebuild_smaller_tree();
1101 
1103 
1109  void refresh_cached_iterators();
1110 
1112 
1134  data_type_const_reference value);
1135 
1137 
1161  dimension_type last_in_subtree, dimension_type subtree_size,
1162  dimension_type key, data_type_const_reference value,
1163  bool add_element);
1164 
1166 
1192  dimension_type subtree_size,
1193  dimension_type last_used,
1194  dimension_type key,
1195  data_type_const_reference value,
1196  bool add_element);
1197 
1199 
1208  void move_data_from(CO_Tree& tree);
1209 
1211 
1221  void copy_data_from(const CO_Tree& tree);
1222 
1224 
1232 
1234 
1249  static void move_data_element(data_type& to, data_type& from);
1250 
1252 
1256 
1258 
1262 
1264 
1272 
1274 
1278 
1280 
1284 
1286 
1290 
1292  height_t max_depth;
1293 
1295 
1303 
1305  std::allocator<data_type> data_allocator;
1306 
1308 
1316  data_type* data;
1317 
1319 
1324 
1327 };
1328 
1330 
1331 public:
1332 
1341  explicit tree_iterator(CO_Tree& tree);
1342 
1344 
1354 
1356 
1365  tree_iterator(const iterator& itr, CO_Tree& tree);
1366 
1368 
1372  tree_iterator& operator=(const tree_iterator& itr);
1373 
1375 
1379  tree_iterator& operator=(const iterator& itr);
1380 
1382 
1386  bool operator==(const tree_iterator& itr) const;
1387 
1389 
1393  bool operator!=(const tree_iterator& itr) const;
1394 
1396 
1401  void get_root();
1402 
1404 
1407  void get_left_child();
1408 
1410 
1413  void get_right_child();
1414 
1416 
1419  void get_parent();
1420 
1434 
1442 
1450 
1452 
1455  bool is_root() const;
1456 
1458 
1461  bool is_right_child() const;
1462 
1464 
1467  bool is_leaf() const;
1468 
1470  data_type& operator*();
1471 
1473  Coefficient_traits::const_reference operator*() const;
1474 
1476 
1479  dimension_type& index();
1480 
1482 
1485  dimension_type index() const;
1486 
1488 
1492  dimension_type key() const;
1493 
1496 
1501  dimension_type dfs_index() const;
1502 
1512  dimension_type get_offset() const;
1513 
1515 
1518  height_t depth() const;
1519 
1520 private:
1522  bool OK() const;
1523 
1526 
1535 };
1536 
1537 #ifdef PPL_DOXYGEN_INCLUDE_IMPLEMENTATION_DETAILS
1538 
1540 #endif // defined(PPL_DOXYGEN_INCLUDE_IMPLEMENTATION_DETAILS)
1541 void swap(CO_Tree& x, CO_Tree& y);
1542 
1543 #ifdef PPL_DOXYGEN_INCLUDE_IMPLEMENTATION_DETAILS
1544 
1546 #endif // defined(PPL_DOXYGEN_INCLUDE_IMPLEMENTATION_DETAILS)
1548 
1549 #ifdef PPL_DOXYGEN_INCLUDE_IMPLEMENTATION_DETAILS
1550 
1552 #endif // defined(PPL_DOXYGEN_INCLUDE_IMPLEMENTATION_DETAILS)
1554 
1555 } // namespace Parma_Polyhedra_Library
1556 
1557 #include "CO_Tree_inlines.hh"
1558 #include "CO_Tree_templates.hh"
1559 
1560 #endif // !defined(PPL_CO_Tree_defs_hh)
dimension_type index() const
Returns the index of the element pointed to by *this.
void increase_keys_from(dimension_type key, dimension_type n)
Adds n to all keys greater than or equal to key.
Definition: CO_Tree.cc:196
bool is_root() const
Returns true if the pointed node is the root node.
void dump_tree() const
Dumps the tree to stdout, for debugging purposes.
std::bidirectional_iterator_tag iterator_category
bool operator!=(const iterator &x) const
Compares *this with x .
data_type & operator*()
Returns the key and value of the current node.
void m_swap(iterator &itr)
Swaps itr with *this.
void swap(CO_Tree &x, CO_Tree &y)
dimension_type key() const
Returns the index of the node pointed to by *this.
Coefficient data_type
The type of the data elements associated with keys.
void init(dimension_type n)
Initializes a tree with reserved size at least n .
Definition: CO_Tree.cc:607
size_t dimension_type
An unsigned integral type for representing space dimensions.
bool OK() const
Checks the internal invariants.
Definition: CO_Tree.cc:752
static void dump_subtree(tree_iterator itr)
Dumps the subtree rooted at itr to stdout, for debugging purposes.
Definition: CO_Tree.cc:798
static unsigned integer_log2(dimension_type n)
Returns the floor of the base-2 logarithm of n .
Definition: CO_Tree.cc:787
static bool is_greater_than_ratio(dimension_type numer, dimension_type denom, dimension_type ratio)
Compares the fractions numer/denom with ratio/100.
void destroy()
Deallocates the tree's dynamic arrays.
Definition: CO_Tree.cc:649
bool OK() const
Checks the internal invariant.
Definition: CO_Tree.cc:1401
dimension_type get_offset() const
Returns 2^h, with h the height of the current node in the tree, counting from 0.
dimension_type dfs_index() const
Returns the index of the current node in the DFS layout of the complete tree.
void get_parent()
Makes the iterator point to the parent of the current node.
static const dimension_type max_density_percent
The maximum density of used nodes.
iterator begin()
Returns an iterator that points at the first element.
data_type * data
The vector that contains the data of the keys in the tree.
bool operator==(const iterator &x) const
Compares *this with x .
bool is_right_child() const
Returns true if the pointed node has a parent and is its right child.
const dimension_type * current_index
A pointer to the corresponding element of the tree's indexes[] array.
bool is_leaf() const
Returns true if the pointed node is a leaf of the complete tree.
An iterator on the tree elements, ordered by key.
dimension_type * indexes
The vector that contains the keys in the tree.
static dimension_type max_size()
Returns the size() of the largest possible CO_Tree.
const_iterator & operator++()
Navigates to the next element.
const data_type * current_data
A pointer to the corresponding element of the tree's data[] array.
static const dimension_type min_leaf_density_percent
The minimum density at the leaves' depth.
void m_swap(const_iterator &itr)
Swaps itr with *this.
unsigned height_t
This is used for node heights and depths in the tree.
std::allocator< data_type > data_allocator
The allocator used to allocate/deallocate data.
tree_iterator insert_precise_aux(dimension_type key, data_type_const_reference data, tree_iterator itr)
Helper for insert_precise.
Definition: CO_Tree.cc:456
dimension_type size() const
Returns the number of elements stored in the tree.
void m_swap(CO_Tree &x)
Swaps x with *this.
iterator & operator=(const iterator &itr)
Assigns itr to *this .
const iterator & end()
Returns an iterator that points after the last element.
const_iterator & operator--()
Navigates to the previous element.
const_iterator()
Constructs an invalid const_iterator.
iterator bisect_in(iterator first, iterator last, dimension_type key)
Searches an element with key key in [first, last] using bisection.
void get_left_child()
Makes the iterator point to the left child of the current node.
CO_Tree & operator=(const CO_Tree &y)
The assignment operator.
tree_iterator rebalance(tree_iterator itr, dimension_type key, data_type_const_reference value)
Rebalances the tree.
Definition: CO_Tree.cc:868
bool empty() const
Returns true if the tree has no elements.
iterator insert(dimension_type key)
Inserts an element in the tree.
dimension_type reserved_size
The number of nodes in the complete tree.
const_iterator cbegin() const
Returns a const_iterator that points at the first element.
bool operator==(const const_iterator &x) const
Compares *this with x .
data_type & operator*()
Returns the current element.
#define sizeof_to_bits(size)
Definition: compiler.hh:80
tree_iterator & operator=(const tree_iterator &itr)
The assignment operator.
iterator cached_end
The iterator returned by end().
static const dimension_type min_density_percent
The minimum density of used nodes.
A cache-oblivious binary search tree of pairs.
bool operator==(const tree_iterator &itr) const
Compares *this with itr.
dimension_type offset
This is 2^h, with h the height of the current node in the tree, counting from 0.
iterator bisect(dimension_type key)
Searches an element with key key using bisection.
const_iterator cached_const_end
The iterator returned by the const version of end().
void follow_left_children_with_value()
Follows left children with a value, until it arrives at a leaf or at a node with no value...
tree_iterator(CO_Tree &tree)
Constructs a tree_iterator pointing at the root node of the specified tree.
dimension_type compact_elements_in_the_rightmost_end(dimension_type last_in_subtree, dimension_type subtree_size, dimension_type key, data_type_const_reference value, bool add_element)
Moves all elements of a subtree to the rightmost end.
Definition: CO_Tree.cc:951
bool OK() const
Checks the internal invariants, in debug mode only.
Definition: CO_Tree.cc:1359
const const_iterator & cend() const
Returns a const_iterator that points after the last element.
std::bidirectional_iterator_tag iterator_category
Coefficient value
Definition: PIP_Tree.cc:618
PPL_COEFFICIENT_TYPE Coefficient
An alias for easily naming the type of PPL coefficients.
dimension_type dfs_index(const_iterator itr) const
static bool is_less_than_ratio(dimension_type numer, dimension_type denom, dimension_type ratio)
Compares the fractions numer/denom with ratio/100.
bool OK() const
Checks the internal invariants, in debug mode only.
Definition: CO_Tree.cc:1317
iterator & operator++()
Navigates to the next element in the tree.
CO_Tree & tree
The tree containing the element pointed to by this iterator.
bool operator!=(const const_iterator &x) const
Compares *this with x .
dimension_type & index()
Returns a reference to the index of the element pointed to by *this.
bool operator!=(const tree_iterator &itr) const
Compares *this with itr.
tree_iterator insert_precise(dimension_type key, data_type_const_reference data, tree_iterator itr)
Inserts an element in the tree.
Definition: CO_Tree.cc:401
Coefficient_traits::const_reference data_type_const_reference
The entire library is confined to this namespace.
Definition: version.hh:61
A const iterator on the tree elements, ordered by key.
iterator()
Constructs an invalid iterator.
height_t depth() const
Returns the depth of the current node in the complete tree.
void rebuild_bigger_tree()
Increases the tree's reserved size.
Definition: CO_Tree.cc:819
data_type_const_reference operator*() const
Returns the current element.
void clear()
Removes all elements from the tree.
static void move_data_element(data_type &to, data_type &from)
Moves the value of from in to .
PPL_COMPILE_TIME_CHECK(C_Integer< height_t >::max >=sizeof_to_bits(sizeof(dimension_type)),"height_t is too small to store depths.")
void get_root()
Makes the iterator point to the root of tree.
void refresh_cached_iterators()
Re-initializes the cached iterators.
static dimension_type count_used_in_subtree(tree_iterator itr)
Counts the number of used elements in the subtree rooted at itr.
Definition: CO_Tree.cc:1294
dimension_type size_
The number of values stored in the tree.
void insert_in_empty_tree(dimension_type key, data_type_const_reference data)
Inserts an element in the tree.
bool structure_OK() const
Checks the internal invariants, but not the densities.
Definition: CO_Tree.cc:664
void follow_right_children_with_value()
Follows right children with a value, until it arrives at a leaf or at a node with no value...
const_iterator & operator=(const const_iterator &itr)
Assigns itr to *this .
iterator erase(dimension_type key)
Erases the element with key key from the tree.
void erase_element_and_shift_left(dimension_type key)
Removes the element with key key (if it exists) and decrements by 1 all elements' keys that were grea...
Definition: CO_Tree.cc:179
const dimension_type * current_index
A pointer to the corresponding element of the tree's indexes[] array.
iterator bisect_near(iterator hint, dimension_type key)
Searches an element with key key near hint.
dimension_type index() const
Returns the index of the element pointed to by *this.
void move_data_from(CO_Tree &tree)
Moves all data in the tree tree into *this.
Definition: CO_Tree.cc:1133
height_t max_depth
The depth of the leaves in the complete tree.
dimension_type external_memory_in_bytes() const
Returns the size in bytes of the memory managed by *this.
Definition: CO_Tree.cc:30
void redistribute_elements_in_subtree(dimension_type root_index, dimension_type subtree_size, dimension_type last_used, dimension_type key, data_type_const_reference value, bool add_element)
Redistributes the elements in the subtree rooted at root_index.
Definition: CO_Tree.cc:1038
void fast_shift(dimension_type i, iterator itr)
static const dimension_type unused_index
An index used as a marker for unused nodes in the tree.
iterator & operator--()
Navigates to the previous element in the tree.
void rebuild_smaller_tree()
Decreases the tree's reserved size.
void get_right_child()
Makes the iterator point to the right child of the current node.
void copy_data_from(const CO_Tree &tree)
Copies all data in the tree tree into *this.
Definition: CO_Tree.cc:1246
dimension_type i
The index of the current node in the DFS layout of the complete tree.
CO_Tree()
Constructs an empty tree.
data_type * current_data
A pointer to the corresponding element of the tree's data[] array.
void go_down_searching_key(dimension_type key)
Searches for an element with key key in the subtree rooted at *this.
Definition: CO_Tree.cc:1417