PPL
1.2
|
A cache-oblivious binary search tree of pairs. More...
#include <CO_Tree_defs.hh>
Classes | |
class | const_iterator |
A const iterator on the tree elements, ordered by key. More... | |
class | iterator |
An iterator on the tree elements, ordered by key. More... | |
class | tree_iterator |
Public Types | |
typedef Coefficient | data_type |
The type of the data elements associated with keys. More... | |
typedef Coefficient_traits::const_reference | data_type_const_reference |
Public Member Functions | |
CO_Tree () | |
Constructs an empty tree. More... | |
CO_Tree (const CO_Tree &y) | |
The copy constructor. More... | |
template<typename Iterator > | |
CO_Tree (Iterator i, dimension_type n) | |
A constructor from a sequence of n elements. More... | |
CO_Tree & | operator= (const CO_Tree &y) |
The assignment operator. More... | |
void | clear () |
Removes all elements from the tree. More... | |
~CO_Tree () | |
The destructor. More... | |
bool | empty () const |
Returns true if the tree has no elements. More... | |
dimension_type | size () const |
Returns the number of elements stored in the tree. More... | |
void | dump_tree () const |
Dumps the tree to stdout, for debugging purposes. More... | |
dimension_type | external_memory_in_bytes () const |
Returns the size in bytes of the memory managed by *this . More... | |
iterator | insert (dimension_type key) |
Inserts an element in the tree. More... | |
iterator | insert (dimension_type key, data_type_const_reference data) |
Inserts an element in the tree. More... | |
iterator | insert (iterator itr, dimension_type key) |
Inserts an element in the tree. More... | |
iterator | insert (iterator itr, dimension_type key, data_type_const_reference data) |
Inserts an element in the tree. More... | |
iterator | erase (dimension_type key) |
Erases the element with key key from the tree. More... | |
iterator | erase (iterator itr) |
Erases the element pointed to by itr from the tree. More... | |
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 greater than key . More... | |
void | increase_keys_from (dimension_type key, dimension_type n) |
Adds n to all keys greater than or equal to key . More... | |
void | fast_shift (dimension_type i, iterator itr) |
void | m_swap (CO_Tree &x) |
Swaps x with *this. More... | |
iterator | begin () |
Returns an iterator that points at the first element. More... | |
const iterator & | end () |
Returns an iterator that points after the last element. More... | |
const_iterator | begin () const |
Equivalent to cbegin(). More... | |
const const_iterator & | end () const |
Equivalent to cend(). More... | |
const_iterator | cbegin () const |
Returns a const_iterator that points at the first element. More... | |
const const_iterator & | cend () const |
Returns a const_iterator that points after the last element. More... | |
iterator | bisect (dimension_type key) |
Searches an element with key key using bisection. More... | |
const_iterator | bisect (dimension_type key) const |
Searches an element with key key using bisection. More... | |
iterator | bisect_in (iterator first, iterator last, dimension_type key) |
Searches an element with key key in [first, last] using bisection. More... | |
const_iterator | bisect_in (const_iterator first, const_iterator last, dimension_type key) const |
Searches an element with key key in [first, last] using bisection. More... | |
iterator | bisect_near (iterator hint, dimension_type key) |
Searches an element with key key near hint . More... | |
const_iterator | bisect_near (const_iterator hint, dimension_type key) const |
Searches an element with key key near hint . More... | |
Static Public Member Functions | |
static dimension_type | max_size () |
Returns the size() of the largest possible CO_Tree. More... | |
Private Types | |
typedef unsigned | height_t |
This is used for node heights and depths in the tree. More... | |
Private Member Functions | |
PPL_COMPILE_TIME_CHECK (C_Integer< height_t >::max >=sizeof_to_bits(sizeof(dimension_type)),"height_t is too small to store depths.") | |
dimension_type | dfs_index (const_iterator itr) const |
dimension_type | dfs_index (iterator itr) const |
dimension_type | bisect_in (dimension_type first, dimension_type last, dimension_type key) const |
Searches an element with key key in [first, last] using bisection. More... | |
dimension_type | bisect_near (dimension_type hint, dimension_type key) const |
Searches an element with key key near hint . More... | |
tree_iterator | insert_precise (dimension_type key, data_type_const_reference data, tree_iterator itr) |
Inserts an element in the tree. More... | |
tree_iterator | insert_precise_aux (dimension_type key, data_type_const_reference data, tree_iterator itr) |
Helper for insert_precise . More... | |
void | insert_in_empty_tree (dimension_type key, data_type_const_reference data) |
Inserts an element in the tree. More... | |
iterator | erase (tree_iterator itr) |
Erases from the tree the element pointed to by itr . More... | |
void | init (dimension_type n) |
Initializes a tree with reserved size at least n . More... | |
void | destroy () |
Deallocates the tree's dynamic arrays. More... | |
bool | structure_OK () const |
Checks the internal invariants, but not the densities. More... | |
bool | OK () const |
Checks the internal invariants. More... | |
void | rebuild_bigger_tree () |
Increases the tree's reserved size. More... | |
void | rebuild_smaller_tree () |
Decreases the tree's reserved size. More... | |
void | refresh_cached_iterators () |
Re-initializes the cached iterators. More... | |
tree_iterator | rebalance (tree_iterator itr, dimension_type key, data_type_const_reference value) |
Rebalances the tree. More... | |
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. More... | |
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 . More... | |
void | move_data_from (CO_Tree &tree) |
Moves all data in the tree tree into *this. More... | |
void | copy_data_from (const CO_Tree &tree) |
Copies all data in the tree tree into *this. More... | |
Static Private Member Functions | |
static unsigned | integer_log2 (dimension_type n) |
Returns the floor of the base-2 logarithm of n . More... | |
static bool | is_less_than_ratio (dimension_type numer, dimension_type denom, dimension_type ratio) |
Compares the fractions numer/denom with ratio/100. More... | |
static bool | is_greater_than_ratio (dimension_type numer, dimension_type denom, dimension_type ratio) |
Compares the fractions numer/denom with ratio/100. More... | |
static void | dump_subtree (tree_iterator itr) |
Dumps the subtree rooted at itr to stdout, for debugging purposes. More... | |
static dimension_type | count_used_in_subtree (tree_iterator itr) |
Counts the number of used elements in the subtree rooted at itr. More... | |
static void | move_data_element (data_type &to, data_type &from) |
Moves the value of from in to . More... | |
Private Attributes | |
iterator | cached_end |
The iterator returned by end(). More... | |
const_iterator | cached_const_end |
The iterator returned by the const version of end(). More... | |
height_t | max_depth |
The depth of the leaves in the complete tree. More... | |
dimension_type * | indexes |
The vector that contains the keys in the tree. More... | |
std::allocator< data_type > | data_allocator |
The allocator used to allocate/deallocate data. More... | |
data_type * | data |
The vector that contains the data of the keys in the tree. More... | |
dimension_type | reserved_size |
The number of nodes in the complete tree. More... | |
dimension_type | size_ |
The number of values stored in the tree. More... | |
Static Private Attributes | |
static const dimension_type | max_density_percent = 91 |
The maximum density of used nodes. More... | |
static const dimension_type | min_density_percent = 38 |
The minimum density of used nodes. More... | |
static const dimension_type | min_leaf_density_percent = 1 |
The minimum density at the leaves' depth. More... | |
static const dimension_type | unused_index = C_Integer<dimension_type>::max |
An index used as a marker for unused nodes in the tree. More... | |
Related Functions | |
(Note that these are not member functions.) | |
void | swap (CO_Tree &x, CO_Tree &y) |
Swaps x with y . More... | |
A cache-oblivious binary search tree of pairs.
This class implements a binary search tree with keys of dimension_type type and data of Coefficient type, laid out in a dynamically-sized array.
The array-based layout saves calls to new/delete (to insert elements only
allocations are performed) and, more importantly, is much more cache-friendly than a standard (pointer-based) tree, because the elements are stored sequentially in memory (leaving some holes to allow fast insertion of new elements). The downside of this representation is that all iterators are invalidated when an element is added or removed, because the array could have been enlarged or shrunk. This is partially addressed by providing references to internal end iterators that are updated when needed.
B-trees are cache-friendly too, but the cache size is fixed (usually at compile-time). This raises two problems: firstly the cache size must be known in advance and those data structures do not perform well with other cache sizes and, secondly, even if the cache size is known, the optimizations target only one level of cache. This kind of data structures are called cache aware. This implementation, instead, is cache oblivious: it performs well with every cache size, and thus exploits all of the available caches.
Assuming n
is the number of elements in the tree and B
is the number of (dimension_type, Coefficient) pairs that fit in a cache line, the time and cache misses complexities are the following:
The binary search tree is embedded in a (slightly bigger) complete tree, that is enlarged and shrunk when needed. The complete tree is laid out in an in-order DFS layout in two arrays: one for the keys and one for the associated data. The indexes and values are stored in different arrays to reduce cache-misses during key queries.
The tree can store up to elements. This limit allows faster density computations, but can be removed if needed.
Definition at line 103 of file CO_Tree_defs.hh.
The type of the data elements associated with keys.
If this is changed, occurrences of Coefficient_zero() in the CO_Tree implementation have to be replaced with constants of the correct type.
Definition at line 148 of file CO_Tree_defs.hh.
typedef Coefficient_traits::const_reference Parma_Polyhedra_Library::CO_Tree::data_type_const_reference |
Definition at line 149 of file CO_Tree_defs.hh.
|
private |
This is used for node heights and depths in the tree.
Definition at line 107 of file CO_Tree_defs.hh.
|
inline |
Constructs an empty tree.
This constructor takes time.
Definition at line 50 of file CO_Tree_inlines.hh.
Referenced by clear().
|
inline |
The copy constructor.
y | The tree that will be copied. |
This constructor takes time.
Definition at line 56 of file CO_Tree_inlines.hh.
References copy_data_from(), data_allocator, init(), OK(), and reserved_size.
Parma_Polyhedra_Library::CO_Tree::CO_Tree | ( | Iterator | i, |
dimension_type | n | ||
) |
A constructor from a sequence of n
elements.
i | An iterator that points to the first element of the sequence. |
n | The number of elements in the [i, i_end) sequence. |
i must be an input iterator on a sequence of data_type elements, sorted by index. Objects of Iterator type must have an index() method that returns the index with which the element pointed to by the iterator must be inserted.
This constructor takes time, so it is more efficient than the construction of an empty tree followed by n insertions, that would take
time.
Definition at line 30 of file CO_Tree_templates.hh.
References Parma_Polyhedra_Library::CO_Tree::tree_iterator::get_left_child(), Parma_Polyhedra_Library::CO_Tree::tree_iterator::get_parent(), Parma_Polyhedra_Library::CO_Tree::tree_iterator::get_right_child(), Parma_Polyhedra_Library::CO_Tree::tree_iterator::index(), init(), integer_log2(), is_greater_than_ratio(), max_density_percent, OK(), reserved_size, size_, sizeof_to_bits, and unused_index.
|
inline |
The destructor.
This destructor takes time.
Definition at line 80 of file CO_Tree_inlines.hh.
References destroy().
|
inline |
Returns an iterator that points at the first element.
This method takes time.
Definition at line 196 of file CO_Tree_inlines.hh.
Referenced by Parma_Polyhedra_Library::Sparse_Row::begin(), bisect(), and external_memory_in_bytes().
|
inline |
Equivalent to cbegin().
Definition at line 206 of file CO_Tree_inlines.hh.
|
inline |
Searches an element with key key
using bisection.
key | The key that will be searched for. |
If the element is found, an iterator pointing to that element is returned; otherwise, the returned iterator refers to the immediately preceding or succeeding value. If the tree is empty, end() is returned.
This method takes time.
Definition at line 226 of file CO_Tree_inlines.hh.
References begin(), bisect_in(), empty(), and end().
Referenced by bisect_near(), Parma_Polyhedra_Library::Sparse_Row::find(), and Parma_Polyhedra_Library::Sparse_Row::lower_bound().
|
inline |
Searches an element with key key
using bisection.
key | The key that will be searched for. |
If the element is found, an iterator pointing to that element is returned; otherwise, the returned iterator refers to the immediately preceding or succeeding value. If the tree is empty, end() is returned.
This method takes time.
Definition at line 236 of file CO_Tree_inlines.hh.
References begin(), bisect_in(), empty(), and end().
|
inline |
Searches an element with key key
in [first, last] using bisection.
first | An iterator pointing to the first element in the range. It must not be end(). |
last | An iterator pointing to the last element in the range. Note that this is included in the search. It must not be end(). |
key | The key that will be searched for. |
This method takes time.
Definition at line 246 of file CO_Tree_inlines.hh.
References dfs_index(), and end().
Referenced by bisect(), and bisect_in().
|
inline |
Searches an element with key key
in [first, last] using bisection.
first | An iterator pointing to the first element in the range. It must not be end(). |
last | An iterator pointing to the last element in the range. Note that this is included in the search. It must not be end(). |
key | The key that will be searched for. |
This method takes time.
Definition at line 255 of file CO_Tree_inlines.hh.
References bisect_in(), dfs_index(), and end().
|
private |
Searches an element with key key
in [first, last] using bisection.
first | The index of the first element in the range. It must be the index of an element with a value. |
last | The index of the last element in the range. It must be the index of an element with a value. Note that this is included in the search. |
key | The key that will be searched for. |
This method takes time.
Definition at line 215 of file CO_Tree.cc.
|
inline |
Searches an element with key key
near hint
.
hint | An iterator used as a hint. |
key | The key that will be searched for. |
If the element is found, the returned iterator points to that element; otherwise, it points to the immediately preceding or succeeding value. If the tree is empty, end() is returned.
The value of itr
does not affect the result of this method, as long it is a valid iterator for this tree. itr
may even be end().
This method takes time. If the distance between the returned position and
hint
is it takes
time.
Definition at line 265 of file CO_Tree_inlines.hh.
References bisect(), dfs_index(), and end().
Referenced by bisect_near(), Parma_Polyhedra_Library::Sparse_Row::find(), and Parma_Polyhedra_Library::Sparse_Row::lower_bound().
|
inline |
Searches an element with key key
near hint
.
hint | An iterator used as a hint. |
key | The key that will be searched for. |
If the element is found, the returned iterator points to that element; otherwise, it points to the immediately preceding or succeeding value. If the tree is empty, end() is returned.
The value of itr
does not affect the result of this method, as long it is a valid iterator for this tree. itr
may even be end().
This method takes time. If the distance between the returned position and
hint
is it takes
time.
Definition at line 275 of file CO_Tree_inlines.hh.
References bisect(), bisect_near(), dfs_index(), and end().
|
private |
Searches an element with key key
near hint
.
hint | An index used as a hint. It must be the index of an element with a value. |
key | The key that will be searched for. |
This uses a binary progression and then a bisection, so this method is , and it is
if the distance between the returned position and
hint
is .
This method takes time. If the distance between the returned position and
hint
is it takes
time.
Definition at line 262 of file CO_Tree.cc.
References Parma_Polyhedra_Library::swap().
|
inline |
Returns a const_iterator that points at the first element.
This method takes time.
Definition at line 216 of file CO_Tree_inlines.hh.
Referenced by Parma_Polyhedra_Library::Sparse_Row::begin(), and Parma_Polyhedra_Library::Sparse_Row::cbegin().
|
inline |
Returns a const_iterator that points after the last element.
This method always returns a reference to the same internal iterator, that is updated at each operation that modifies the structure. Client code can keep a const reference to that iterator instead of keep updating a local iterator.
This method takes time.
Definition at line 221 of file CO_Tree_inlines.hh.
References cached_const_end.
Referenced by Parma_Polyhedra_Library::Sparse_Row::cend(), and Parma_Polyhedra_Library::Sparse_Row::end().
|
inline |
Removes all elements from the tree.
This method takes time.
Definition at line 75 of file CO_Tree_inlines.hh.
References CO_Tree().
Referenced by Parma_Polyhedra_Library::Sparse_Row::clear().
|
private |
Moves all elements of a subtree to the rightmost end.
last_in_subtree | It is the index of the last element in the subtree. |
subtree_size | It is the number of valid elements in the subtree. It must be greater than zero. |
key | The key that may be added to the tree if add_element is true . |
value | The value that may be added to the tree if add_element is true . |
add_element | If it is true, it tries to add an element with key key and value value in the process (but it may not). |
This method takes time, where k is
subtree_size
.
Definition at line 951 of file CO_Tree.cc.
|
private |
Copies all data in the tree tree
into *this.
tree | The tree from which the element will be copied into *this. |
this must be empty and must have the same reserved size of tree
. this->OK() may return false before this method is called, but this->structure_OK() must return true.
This method takes time.
Definition at line 1246 of file CO_Tree.cc.
References data, indexes, reserved_size, and size_.
Referenced by CO_Tree(), and operator=().
|
staticprivate |
Counts the number of used elements in the subtree rooted at itr.
itr | An iterator pointing to the root of the desired subtree. |
This method takes time, where k is the number of elements in the subtree.
Definition at line 1294 of file CO_Tree.cc.
References Parma_Polyhedra_Library::CO_Tree::tree_iterator::dfs_index(), Parma_Polyhedra_Library::CO_Tree::tree_iterator::get_offset(), indexes, and Parma_Polyhedra_Library::CO_Tree::tree_iterator::tree.
|
private |
Deallocates the tree's dynamic arrays.
After this call, the tree fields are uninitialized, so init() must be called again before using the tree.
This method takes time.
Definition at line 649 of file CO_Tree.cc.
Referenced by operator=(), and ~CO_Tree().
|
inlineprivate |
Returns the index of the current element in the DFS layout of the complete tree.
itr | the iterator that points to the desired element. |
Definition at line 32 of file CO_Tree_inlines.hh.
References Parma_Polyhedra_Library::CO_Tree::const_iterator::current_index, indexes, and reserved_size.
Referenced by bisect_in(), bisect_near(), and fast_shift().
|
inlineprivate |
Returns the index of the current element in the DFS layout of the complete tree.
itr | the iterator that points to the desired element. |
Definition at line 41 of file CO_Tree_inlines.hh.
References Parma_Polyhedra_Library::CO_Tree::iterator::current_index, indexes, and reserved_size.
|
staticprivate |
Dumps the subtree rooted at itr
to stdout, for debugging purposes.
itr | A tree_iterator pointing to the root of the desired subtree. |
Definition at line 798 of file CO_Tree.cc.
References Parma_Polyhedra_Library::CO_Tree::tree_iterator::depth(), Parma_Polyhedra_Library::CO_Tree::tree_iterator::get_left_child(), Parma_Polyhedra_Library::CO_Tree::tree_iterator::get_parent(), Parma_Polyhedra_Library::CO_Tree::tree_iterator::get_right_child(), Parma_Polyhedra_Library::CO_Tree::tree_iterator::index(), and Parma_Polyhedra_Library::CO_Tree::tree_iterator::is_leaf().
Referenced by dump_tree().
|
inline |
Dumps the tree to stdout, for debugging purposes.
Definition at line 101 of file CO_Tree_inlines.hh.
References dump_subtree(), and empty().
|
inline |
Returns true
if the tree has no elements.
This method takes time.
Definition at line 86 of file CO_Tree_inlines.hh.
References size_.
Referenced by bisect(), Parma_Polyhedra_Library::CO_Tree::const_iterator::const_iterator(), dump_tree(), erase(), Parma_Polyhedra_Library::Sparse_Row::get(), insert(), insert_in_empty_tree(), and Parma_Polyhedra_Library::CO_Tree::iterator::iterator().
|
inline |
Returns an iterator that points after the last element.
This method always returns a reference to the same internal iterator, that is updated at each operation that modifies the structure. Client code can keep a const reference to that iterator instead of keep updating a local iterator.
This method takes time.
Definition at line 201 of file CO_Tree_inlines.hh.
References cached_end.
Referenced by bisect(), bisect_in(), bisect_near(), Parma_Polyhedra_Library::Sparse_Row::end(), erase(), external_memory_in_bytes(), and fast_shift().
|
inline |
Equivalent to cend().
Definition at line 211 of file CO_Tree_inlines.hh.
References cached_const_end.
|
inline |
Erases the element with key key
from the tree.
This operation invalidates existing iterators.
key
).key | The key of the element that will be erased from the tree. |
This method takes time if the element already exists, and
amortized time otherwise.
Definition at line 143 of file CO_Tree_inlines.hh.
References empty(), end(), Parma_Polyhedra_Library::CO_Tree::tree_iterator::go_down_searching_key(), Parma_Polyhedra_Library::CO_Tree::iterator::index(), Parma_Polyhedra_Library::CO_Tree::tree_iterator::index(), and unused_index.
Referenced by erase(), and Parma_Polyhedra_Library::Sparse_Row::reset().
|
inline |
Erases the element pointed to by itr
from the tree.
This operation invalidates existing iterators.
key
).itr | An iterator pointing to the element that will be erased from the tree. |
This method takes time if the element already exists, and
amortized time otherwise.
Definition at line 173 of file CO_Tree_inlines.hh.
References end(), and erase().
|
private |
Erases from the tree the element pointed to by itr
.
This operation invalidates existing iterators.
key
).itr | An iterator pointing to the element that will be erased. |
This method takes amortized time.
Definition at line 503 of file CO_Tree.cc.
References Parma_Polyhedra_Library::Coefficient_zero(), Parma_Polyhedra_Library::Implementation::BD_Shapes::empty, Parma_Polyhedra_Library::CO_Tree::tree_iterator::follow_left_children_with_value(), Parma_Polyhedra_Library::CO_Tree::tree_iterator::follow_right_children_with_value(), Parma_Polyhedra_Library::CO_Tree::tree_iterator::get_left_child(), Parma_Polyhedra_Library::CO_Tree::tree_iterator::get_offset(), Parma_Polyhedra_Library::CO_Tree::tree_iterator::get_parent(), Parma_Polyhedra_Library::CO_Tree::tree_iterator::get_right_child(), Parma_Polyhedra_Library::CO_Tree::tree_iterator::get_root(), Parma_Polyhedra_Library::CO_Tree::tree_iterator::go_down_searching_key(), Parma_Polyhedra_Library::CO_Tree::iterator::index(), Parma_Polyhedra_Library::CO_Tree::tree_iterator::index(), Parma_Polyhedra_Library::CO_Tree::tree_iterator::is_leaf(), and Parma_Polyhedra_Library::swap().
void Parma_Polyhedra_Library::CO_Tree::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 greater than key
.
key | The key of the element that will be erased from the tree. |
This operation invalidates existing iterators.
This method takes expected time, where k is the number of elements with keys greater than
key
.
Definition at line 179 of file CO_Tree.cc.
Referenced by Parma_Polyhedra_Library::Sparse_Row::delete_element_and_shift().
PPL::dimension_type Parma_Polyhedra_Library::CO_Tree::external_memory_in_bytes | ( | ) | const |
Returns the size in bytes of the memory managed by *this
.
This method takes time.
Definition at line 30 of file CO_Tree.cc.
References begin(), data, end(), Parma_Polyhedra_Library::external_memory_in_bytes(), indexes, and reserved_size.
Referenced by Parma_Polyhedra_Library::Sparse_Row::external_memory_in_bytes().
|
inline |
Sets to i
the key of *itr. Assumes that i<=itr.index() and that there are no elements with keys in [i,itr.index()).
All existing iterators remain valid.
This method takes time.
Definition at line 284 of file CO_Tree_inlines.hh.
References dfs_index(), end(), Parma_Polyhedra_Library::CO_Tree::iterator::index(), indexes, and OK().
Referenced by Parma_Polyhedra_Library::Sparse_Row::fast_swap().
void Parma_Polyhedra_Library::CO_Tree::increase_keys_from | ( | dimension_type | key, |
dimension_type | n | ||
) |
Adds n
to all keys greater than or equal to key
.
key | The key of the first element whose key will be increased. |
n | Specifies how much the keys will be increased. |
This method takes expected time, where k is the number of elements with keys greater than or equal to
key
.
Definition at line 196 of file CO_Tree.cc.
References Parma_Polyhedra_Library::Implementation::BD_Shapes::empty.
Referenced by Parma_Polyhedra_Library::Sparse_Row::add_zeroes_and_shift().
|
private |
Initializes a tree with reserved size at least n
.
n | A lower bound on the tree's desired reserved size. |
This method takes time.
Definition at line 607 of file CO_Tree.cc.
Referenced by CO_Tree(), operator=(), and rebuild_smaller_tree().
|
inline |
Inserts an element in the tree.
key | The key that will be inserted into the tree, associated with the default data. |
If such a pair already exists, an iterator pointing to that pair is returned.
This operation invalidates existing iterators.
This method takes time if the element already exists, and
amortized time otherwise.
Definition at line 111 of file CO_Tree_inlines.hh.
References Parma_Polyhedra_Library::Coefficient_zero(), empty(), Parma_Polyhedra_Library::CO_Tree::tree_iterator::go_down_searching_key(), Parma_Polyhedra_Library::CO_Tree::tree_iterator::index(), and insert_precise().
Referenced by Parma_Polyhedra_Library::Sparse_Row::insert().
|
inline |
Inserts an element in the tree.
key | The key that will be inserted into the tree.. |
data | The data that will be inserted into the tree. |
If an element with the specified key already exists, its associated data is set to data
and an iterator pointing to that pair is returned.
This operation invalidates existing iterators.
This method takes time if the element already exists, and
amortized time otherwise.amortized
Definition at line 128 of file CO_Tree_inlines.hh.
References empty(), Parma_Polyhedra_Library::CO_Tree::tree_iterator::go_down_searching_key(), Parma_Polyhedra_Library::CO_Tree::tree_iterator::index(), insert_in_empty_tree(), insert_precise(), and unused_index.
PPL::CO_Tree::iterator Parma_Polyhedra_Library::CO_Tree::insert | ( | iterator | itr, |
dimension_type | key | ||
) |
Inserts an element in the tree.
itr | The iterator used as hint |
key | The key that will be inserted into the tree, associated with the default data. |
This will be faster if itr
points near to the place where the new element will be inserted (or where is already stored). However, the value of itr
does not affect the result of this method, as long it is a valid iterator for this tree. itr
may even be end().
If an element with the specified key already exists, an iterator pointing to that pair is returned.
This operation invalidates existing iterators.
This method takes time if the element already exists, and
amortized time otherwise.
Definition at line 46 of file CO_Tree.cc.
References Parma_Polyhedra_Library::Coefficient_zero(), Parma_Polyhedra_Library::CO_Tree::tree_iterator::depth(), Parma_Polyhedra_Library::Implementation::BD_Shapes::empty, Parma_Polyhedra_Library::CO_Tree::tree_iterator::get_offset(), and Parma_Polyhedra_Library::CO_Tree::iterator::index().
PPL::CO_Tree::iterator Parma_Polyhedra_Library::CO_Tree::insert | ( | iterator | itr, |
dimension_type | key, | ||
data_type_const_reference | data | ||
) |
Inserts an element in the tree.
itr | The iterator used as hint |
key | The key that will be inserted into the tree. |
data | The data that will be inserted into the tree. |
This will be faster if itr
points near to the place where the new element will be inserted (or where is already stored). However, the value of itr
does not affect the result of this method, as long it is a valid iterator for this tree. itr
may even be end().
If an element with the specified key already exists, its associated data is set to data
and an iterator pointing to that pair is returned.
This operation invalidates existing iterators.
This method takes time if the element already exists, and
amortized time otherwise.
Definition at line 113 of file CO_Tree.cc.
References Parma_Polyhedra_Library::CO_Tree::tree_iterator::depth(), Parma_Polyhedra_Library::Implementation::BD_Shapes::empty, Parma_Polyhedra_Library::CO_Tree::tree_iterator::get_offset(), and Parma_Polyhedra_Library::CO_Tree::iterator::index().
|
inlineprivate |
Inserts an element in the tree.
key | The key that will be inserted into the tree. |
data | The data that will be associated with key . |
The tree must be empty.
This operation invalidates existing iterators.
This method takes time.
Definition at line 292 of file CO_Tree_inlines.hh.
References empty(), Parma_Polyhedra_Library::CO_Tree::tree_iterator::index(), OK(), rebuild_bigger_tree(), size_, and unused_index.
Referenced by insert().
|
private |
Inserts an element in the tree.
If there is already an element with key key
in the tree, its associated data is set to data
.
This operation invalidates existing iterators.
key | The key that will be inserted into the tree. |
data | The data that will be associated with key . |
itr | It must point to the element in the tree with key key or, if no such element exists, it must point to the node that would be his parent. |
This method takes time if the element already exists, and
amortized time otherwise.
Definition at line 401 of file CO_Tree.cc.
References Parma_Polyhedra_Library::Coefficient_zero(), Parma_Polyhedra_Library::Implementation::BD_Shapes::empty, Parma_Polyhedra_Library::CO_Tree::tree_iterator::go_down_searching_key(), Parma_Polyhedra_Library::CO_Tree::tree_iterator::index(), and Parma_Polyhedra_Library::swap().
Referenced by insert().
|
private |
Helper for insert_precise
.
This helper method takes the same arguments as insert_precise
, but besides assuming that itr
is a correct hint, it also assumes that key
and data
are not in the tree; namely, a proper insertion has to be done and the insertion can not invalidate data
.
Definition at line 456 of file CO_Tree.cc.
References Parma_Polyhedra_Library::Implementation::BD_Shapes::empty, Parma_Polyhedra_Library::CO_Tree::tree_iterator::get_left_child(), Parma_Polyhedra_Library::CO_Tree::tree_iterator::get_right_child(), Parma_Polyhedra_Library::CO_Tree::tree_iterator::get_root(), Parma_Polyhedra_Library::CO_Tree::tree_iterator::go_down_searching_key(), Parma_Polyhedra_Library::CO_Tree::tree_iterator::index(), and Parma_Polyhedra_Library::CO_Tree::tree_iterator::is_leaf().
|
staticprivate |
Returns the floor of the base-2 logarithm of n
.
n | It must be greater than zero. |
This method takes time.
Definition at line 787 of file CO_Tree.cc.
Referenced by CO_Tree(), and Parma_Polyhedra_Library::CO_Tree::tree_iterator::depth().
|
inlinestaticprivate |
Compares the fractions numer/denom with ratio/100.
ratio | It must be less than or equal to 100. |
numer | The numerator of the fraction. |
denom | The denominator of the fraction. |
This method takes time.
Definition at line 318 of file CO_Tree_inlines.hh.
References unused_index.
Referenced by CO_Tree().
|
inlinestaticprivate |
Compares the fractions numer/denom with ratio/100.
ratio | It must be less than or equal to 100. |
numer | The numerator of the fraction. |
denom | The denominator of the fraction. |
This method takes time.
Definition at line 308 of file CO_Tree_inlines.hh.
References unused_index.
|
inline |
Swaps x with *this.
x | The tree that will be swapped with *this. |
This operation invalidates existing iterators.
This method takes time.
Definition at line 179 of file CO_Tree_inlines.hh.
References data, data_allocator, indexes, max_depth, refresh_cached_iterators(), reserved_size, size_, structure_OK(), Parma_Polyhedra_Library::swap(), and swap().
Referenced by Parma_Polyhedra_Library::Sparse_Row::linear_combine(), rebuild_smaller_tree(), and Parma_Polyhedra_Library::swap().
|
inlinestatic |
Returns the size() of the largest possible CO_Tree.
Definition at line 96 of file CO_Tree_inlines.hh.
Referenced by Parma_Polyhedra_Library::Sparse_Row::max_size().
|
inlinestaticprivate |
Moves the value of from
in to
.
from | It must be a valid value. |
to | It must be a non-constructed chunk of memory. |
After the move, from
becomes a non-constructed chunk of memory and to
gets the value previously stored by from
.
The implementation of this method assumes that data_type values do not keep pointers to themselves nor to their fields.
This method takes time.
Definition at line 345 of file CO_Tree_inlines.hh.
|
private |
Moves all data in the tree tree
into *this.
tree | The tree from which the element will be moved into *this. |
this must be empty and big enough to contain all of tree's data without exceeding max_density.
This method takes time.
Definition at line 1133 of file CO_Tree.cc.
References data, Parma_Polyhedra_Library::CO_Tree::tree_iterator::get_left_child(), Parma_Polyhedra_Library::CO_Tree::tree_iterator::get_parent(), Parma_Polyhedra_Library::CO_Tree::tree_iterator::get_right_child(), Parma_Polyhedra_Library::CO_Tree::tree_iterator::index(), indexes, reserved_size, size_, sizeof_to_bits, and structure_OK().
Referenced by rebuild_smaller_tree().
|
private |
Checks the internal invariants.
Definition at line 752 of file CO_Tree.cc.
Referenced by CO_Tree(), Parma_Polyhedra_Library::CO_Tree::const_iterator::const_iterator(), fast_shift(), Parma_Polyhedra_Library::CO_Tree::tree_iterator::follow_left_children_with_value(), Parma_Polyhedra_Library::CO_Tree::tree_iterator::follow_right_children_with_value(), Parma_Polyhedra_Library::CO_Tree::tree_iterator::get_left_child(), Parma_Polyhedra_Library::CO_Tree::tree_iterator::get_parent(), Parma_Polyhedra_Library::CO_Tree::tree_iterator::get_right_child(), Parma_Polyhedra_Library::CO_Tree::tree_iterator::get_root(), Parma_Polyhedra_Library::CO_Tree::const_iterator::index(), Parma_Polyhedra_Library::CO_Tree::iterator::index(), insert_in_empty_tree(), Parma_Polyhedra_Library::CO_Tree::iterator::iterator(), Parma_Polyhedra_Library::CO_Tree::const_iterator::m_swap(), Parma_Polyhedra_Library::CO_Tree::iterator::m_swap(), Parma_Polyhedra_Library::CO_Tree::const_iterator::operator*(), Parma_Polyhedra_Library::CO_Tree::iterator::operator*(), Parma_Polyhedra_Library::CO_Tree::const_iterator::operator++(), Parma_Polyhedra_Library::CO_Tree::iterator::operator++(), Parma_Polyhedra_Library::CO_Tree::const_iterator::operator--(), Parma_Polyhedra_Library::CO_Tree::iterator::operator--(), Parma_Polyhedra_Library::CO_Tree::const_iterator::operator=(), Parma_Polyhedra_Library::CO_Tree::iterator::operator=(), Parma_Polyhedra_Library::CO_Tree::const_iterator::operator==(), and Parma_Polyhedra_Library::CO_Tree::iterator::operator==().
The assignment operator.
y | The tree that will be assigned to *this. |
This method takes time.
Definition at line 64 of file CO_Tree_inlines.hh.
References copy_data_from(), data_allocator, destroy(), init(), and reserved_size.
|
private |
|
private |
Rebalances the tree.
For insertions, it adds the pair (key, value) in the process.
This operation invalidates existing iterators that point to nodes in the rebalanced subtree.
itr | It points to the node where the new element has to be inserted or where an element has just been deleted. |
key | The index that will be inserted in the tree (for insertions only). |
value | The value that will be inserted in the tree (for insertions only). |
This method takes amortized time.
Definition at line 868 of file CO_Tree.cc.
References Parma_Polyhedra_Library::CO_Tree::tree_iterator::depth(), Parma_Polyhedra_Library::CO_Tree::tree_iterator::dfs_index(), Parma_Polyhedra_Library::CO_Tree::tree_iterator::get_left_child(), Parma_Polyhedra_Library::CO_Tree::tree_iterator::get_offset(), Parma_Polyhedra_Library::CO_Tree::tree_iterator::get_parent(), Parma_Polyhedra_Library::CO_Tree::tree_iterator::get_right_child(), Parma_Polyhedra_Library::CO_Tree::tree_iterator::index(), Parma_Polyhedra_Library::CO_Tree::tree_iterator::is_leaf(), Parma_Polyhedra_Library::CO_Tree::tree_iterator::is_right_child(), and value.
|
private |
Increases the tree's reserved size.
This is called when the density is about to exceed the maximum density (specified by max_density_percent).
This method takes time.
Definition at line 819 of file CO_Tree.cc.
Referenced by insert_in_empty_tree().
|
inlineprivate |
Decreases the tree's reserved size.
This is called when the density is about to become less than the minimum allowed density (specified by min_density_percent).
reserved_size
must be greater than 3 (otherwise the tree can just be cleared).
This method takes time.
Definition at line 328 of file CO_Tree_inlines.hh.
References init(), m_swap(), move_data_from(), reserved_size, and structure_OK().
|
private |
Redistributes the elements in the subtree rooted at root_index
.
The subtree's elements must be compacted to the rightmost end.
root_index | The index of the subtree's root node. |
subtree_size | It is the number of used elements in the subtree. It must be greater than zero. |
last_used | It points to the leftmost element with a value in the subtree. |
add_element | If it is true, this method adds an element with the specified key and value in the process. |
key | The key that will be added to the tree if add_element is true . |
value | The data that will be added to the tree if add_element is true . |
This method takes time, where k is
subtree_size
.
Definition at line 1038 of file CO_Tree.cc.
References sizeof_to_bits.
|
inlineprivate |
Re-initializes the cached iterators.
This method must be called when the indexes[] and data[] vector are reallocated.
This method takes time.
Definition at line 339 of file CO_Tree_inlines.hh.
References cached_const_end, cached_end, and reserved_size.
Referenced by m_swap().
|
inline |
Returns the number of elements stored in the tree.
This method takes time.
Definition at line 91 of file CO_Tree_inlines.hh.
References size_.
Referenced by Parma_Polyhedra_Library::Sparse_Row::num_stored_elements().
|
private |
Checks the internal invariants, but not the densities.
Definition at line 664 of file CO_Tree.cc.
References Parma_Polyhedra_Library::CO_Tree::const_iterator::index(), and Parma_Polyhedra_Library::CO_Tree::tree_iterator::index().
Referenced by m_swap(), move_data_from(), and rebuild_smaller_tree().
Swaps x
with y
.
Definition at line 873 of file CO_Tree_inlines.hh.
Referenced by Parma_Polyhedra_Library::CO_Tree::const_iterator::m_swap(), Parma_Polyhedra_Library::CO_Tree::iterator::m_swap(), and m_swap().
|
private |
The iterator returned by the const version of end().
It is updated when needed, to keep it valid.
Definition at line 1289 of file CO_Tree_defs.hh.
Referenced by cend(), end(), and refresh_cached_iterators().
|
private |
The iterator returned by end().
It is updated when needed, to keep it valid.
Definition at line 1283 of file CO_Tree_defs.hh.
Referenced by end(), and refresh_cached_iterators().
|
private |
The vector that contains the data of the keys in the tree.
If index[i] is unused_index
, data[i] is unused. Otherwise, data[i] contains the data associated to the indexes[i] key.
Its size is reserved_size + 1, because the first element is not used (to allow using the same index in both indexes[] and data[] instead of adding 1 to access data[]).
Definition at line 1316 of file CO_Tree_defs.hh.
Referenced by copy_data_from(), external_memory_in_bytes(), m_swap(), move_data_from(), and Parma_Polyhedra_Library::CO_Tree::iterator::operator=().
|
private |
The allocator used to allocate/deallocate data.
Definition at line 1305 of file CO_Tree_defs.hh.
Referenced by CO_Tree(), m_swap(), and operator=().
|
private |
The vector that contains the keys in the tree.
If an element of this vector is unused_index
, it means that that element and the corresponding element of data[] are not used.
Its size is reserved_size + 2, because the first and the last elements are used as markers for iterators.
Definition at line 1302 of file CO_Tree_defs.hh.
Referenced by Parma_Polyhedra_Library::CO_Tree::const_iterator::const_iterator(), copy_data_from(), count_used_in_subtree(), dfs_index(), external_memory_in_bytes(), fast_shift(), Parma_Polyhedra_Library::CO_Tree::iterator::iterator(), m_swap(), move_data_from(), and Parma_Polyhedra_Library::CO_Tree::iterator::operator=().
|
staticprivate |
The maximum density of used nodes.
This must be greater than or equal to 50 and lower than 100.
Definition at line 1255 of file CO_Tree_defs.hh.
Referenced by CO_Tree().
|
private |
The depth of the leaves in the complete tree.
Definition at line 1292 of file CO_Tree_defs.hh.
Referenced by m_swap().
|
staticprivate |
The minimum density of used nodes.
Must be strictly lower than the half of max_density_percent.
Definition at line 1261 of file CO_Tree_defs.hh.
|
staticprivate |
The minimum density at the leaves' depth.
Must be greater than zero and strictly lower than min_density_percent.
Increasing the value is safe but leads to time inefficiencies (measured against ppl_lpsol on 24 August 2010), because it forces trees to be more balanced, increasing the cost of rebalancing.
Definition at line 1271 of file CO_Tree_defs.hh.
|
private |
The number of nodes in the complete tree.
It is one less than a power of 2. If this is 0, data and indexes are set to NULL.
Definition at line 1323 of file CO_Tree_defs.hh.
Referenced by CO_Tree(), Parma_Polyhedra_Library::CO_Tree::const_iterator::const_iterator(), copy_data_from(), dfs_index(), external_memory_in_bytes(), Parma_Polyhedra_Library::CO_Tree::iterator::iterator(), m_swap(), move_data_from(), operator=(), rebuild_smaller_tree(), refresh_cached_iterators(), and Parma_Polyhedra_Library::CO_Tree::tree_iterator::tree_iterator().
|
private |
The number of values stored in the tree.
Definition at line 1326 of file CO_Tree_defs.hh.
Referenced by CO_Tree(), copy_data_from(), empty(), insert_in_empty_tree(), m_swap(), move_data_from(), and size().
|
staticprivate |
An index used as a marker for unused nodes in the tree.
This must not be used as a key.
Definition at line 1277 of file CO_Tree_defs.hh.
Referenced by CO_Tree(), Parma_Polyhedra_Library::CO_Tree::const_iterator::const_iterator(), erase(), Parma_Polyhedra_Library::CO_Tree::tree_iterator::follow_left_children_with_value(), Parma_Polyhedra_Library::CO_Tree::tree_iterator::follow_right_children_with_value(), insert(), insert_in_empty_tree(), is_greater_than_ratio(), is_less_than_ratio(), Parma_Polyhedra_Library::CO_Tree::iterator::iterator(), Parma_Polyhedra_Library::CO_Tree::const_iterator::operator++(), Parma_Polyhedra_Library::CO_Tree::iterator::operator++(), Parma_Polyhedra_Library::CO_Tree::const_iterator::operator--(), and Parma_Polyhedra_Library::CO_Tree::iterator::operator--().