24 #include "ppl-config.h"
32 class Sparse_Row_from_Dense_Row_helper_iterator {
36 : row(r), sz(sz), idx(0) {
37 if (row.size() != 0 && row[0] == 0) {
42 Sparse_Row_from_Dense_Row_helper_iterator& operator++() {
45 while (idx < sz && row[idx] == 0) {
51 Sparse_Row_from_Dense_Row_helper_iterator operator++(
int) {
52 Sparse_Row_from_Dense_Row_helper_iterator tmp = *
this;
57 PPL::Coefficient_traits::const_reference
70 operator==(
const Sparse_Row_from_Dense_Row_helper_iterator& itr)
const {
71 PPL_ASSERT(&row == &(itr.row));
72 return idx == itr.idx;
76 operator!=(
const Sparse_Row_from_Dense_Row_helper_iterator& itr)
const {
77 return !(*
this == itr);
88 Sparse_Row_from_Dense_Row_helper_function(
const PPL::Dense_Row& row,
102 : tree(Sparse_Row_from_Dense_Row_helper_iterator(row, row.size()),
103 Sparse_Row_from_Dense_Row_helper_function(row, row.size())),
110 : tree(Sparse_Row_from_Dense_Row_helper_iterator(row, row.size()),
111 Sparse_Row_from_Dense_Row_helper_function(row, row.size())),
128 PPL_ASSERT(i < size_);
129 PPL_ASSERT(j < size_);
139 if (itr_i.
index() == i) {
140 if (itr_j.
index() == j) {
142 swap(*itr_i, *itr_j);
150 itr_j = tree.insert(j);
155 if (itr_j.
index() == j) {
161 itr_i = tree.insert(i);
176 PPL_ASSERT(last != end());
179 PPL_ASSERT(first.
index() <= j);
182 while (first.
index() < j) {
183 first = reset(first);
186 first = reset(first);
194 PPL_ASSERT(i < size_);
202 while (itr != itr_end) {
215 for ( ; i != i_end; ++i) {
216 Coefficient_traits::const_reference x_i = *i;
217 if (
const int x_i_sign =
sgn(x_i)) {
232 for (++i; i != i_end; ++i) {
233 Coefficient_traits::const_reference x_i = *i;
253 for (
iterator j = begin(), j_end = end(); j != j_end; ++j) {
263 class sparse_row_linear_combine_helper_iterator {
265 sparse_row_linear_combine_helper_iterator(
267 PPL::Coefficient_traits::const_reference coeff1_1,
268 PPL::Coefficient_traits::const_reference coeff2_1)
269 : coeff1(coeff1_1), coeff2(coeff2_1) {
274 update_current_data();
284 update_current_data();
287 PPL::Coefficient_traits::const_reference
operator*() {
288 return current_value;
292 return current_index;
296 void update_current_data() {
303 current_index = j.index();
305 current_value *= coeff2;
313 current_index = i.index();
315 current_value *= coeff1;
321 if (i.index() < j.index()) {
323 current_index = i.index();
325 current_value *= coeff1;
330 if (i.index() != j.index()) {
331 PPL_ASSERT(i.index() > j.index());
333 current_index = j.index();
335 current_value *= coeff2;
341 current_index = i.index();
343 current_value *= coeff1;
351 PPL_ASSERT(!from_i || i != i_end);
352 PPL_ASSERT(!from_j || j != j_end);
355 PPL::Coefficient_traits::const_reference coeff1;
356 PPL::Coefficient_traits::const_reference coeff2;
371 Coefficient_traits::const_reference coeff1,
372 Coefficient_traits::const_reference coeff2) {
373 PPL_ASSERT(coeff1 != 0);
374 PPL_ASSERT(coeff2 != 0);
375 PPL_ASSERT(
this != &y);
381 i = insert(i, j.
index());
399 PPL_ASSERT(i != i_end);
409 i = lower_bound(i, j.
index());
422 PPL_ASSERT(i == i_end || j == j_end);
423 for ( ; j != j_end; ++j) {
430 if (counter == 0 || counter < (7 * size()) / 64) {
439 while (i != i_end && j != j_end) {
458 i = insert(i, j.
index(), *j);
465 PPL_ASSERT(i == i_end || j == j_end);
466 for ( ; i != i_end; ++i) {
469 for ( ; j != j_end; ++j) {
470 i = insert(i, j.
index(), *j);
477 CO_Tree new_tree(sparse_row_linear_combine_helper_iterator(*
this, y,
480 counter + tree.size());
493 PPL_ASSERT(find(old_index) == end());
504 Coefficient_traits::const_reference coeff1,
505 Coefficient_traits::const_reference coeff2,
507 PPL_ASSERT(coeff1 != 0);
508 PPL_ASSERT(coeff2 != 0);
509 PPL_ASSERT(
this != &y);
517 i = insert(i, j.
index());
530 i = insert(i, j.
index());
542 i = insert(i, j.
index());
557 const iterator& i_end = this->end();
560 while (i != i_end && i.
index() < end && j != j_end) {
579 i = insert(i, j.
index(), *j);
585 PPL_ASSERT(i == i_end || j == j_end);
586 for ( ; i != i_end && i.
index() < end; ++i) {
589 for ( ; j != j_end; ++j) {
590 i = insert(i, j.
index(), *j);
601 const iterator& i_end = this->end();
604 while (i != i_end && i.
index() < end && j != j_end) {
623 i = insert(i, j.
index(), *j);
630 PPL_ASSERT(i == i_end || j == j_end);
631 for ( ; i != i_end && i.
index() < end; ++i) {
634 for ( ; j != j_end; ++j) {
635 i = insert(i, j.
index(), *j);
645 const iterator& i_end = this->end();
648 while (i != i_end && i.
index() < end && j != j_end) {
667 i = insert(i, j.
index(), *j);
674 PPL_ASSERT(i == i_end || j == j_end);
675 for ( ; i != i_end && i.
index() < end; ++i) {
678 for ( ; j != j_end; ++j) {
679 i = insert(i, j.
index(), *j);
686 s <<
"size " << size_ <<
' ';
688 for (
const_iterator i = begin(), i_end = end(); i != i_end; ++i) {
691 s <<
"elements " << n_elements <<
' ';
692 for (
const_iterator i = begin(), i_end = end(); i != i_end; ++i) {
693 s <<
"[ " << i.index() <<
" ]= " << *i <<
' ';
703 if (!(s >> str) || str !=
"size") {
711 if (!(s >> str) || str !=
"elements") {
716 if (!(s >> n_elements)) {
723 if (!(s >> str) || str !=
"[") {
726 if (!(s >> current_key)) {
729 if (!(s >> str) || str !=
"]=") {
732 if (!(s >> current_data)) {
735 tree.insert(current_key, current_data);
743 if (begin() == end()) {
748 return (last.
index() < size_);
766 while (i != i_end && j != j_end) {
790 for ( ; i != i_end; ++i) {
795 for ( ; j != j_end; ++j) {
817 if (itr != y.
end() && itr.
index() == i) {
848 Coefficient_traits::const_reference coeff1,
849 Coefficient_traits::const_reference coeff2) {
850 PPL_ASSERT(x.size() >= y.size());
851 PPL_ASSERT(coeff1 != 0);
852 PPL_ASSERT(coeff2 != 0);
857 itr = x.lower_bound(itr, i);
858 if (itr == x.end() || itr.index() != i) {
862 itr = x.insert(itr, i, y[i]);
864 PPL_ASSERT((*itr) != 0);
867 PPL_ASSERT(itr.index() == i);
879 Coefficient_traits::const_reference coeff1,
880 Coefficient_traits::const_reference coeff2,
882 PPL_ASSERT(coeff1 != 0);
883 PPL_ASSERT(coeff2 != 0);
884 PPL_ASSERT(start <= end);
885 PPL_ASSERT(end <= x.size());
886 PPL_ASSERT(end <= y.size());
893 PPL_ASSERT(itr == x.end() || itr.index() + 1 >= i);
894 if (itr != x.end() && itr.index() < i) {
897 PPL_ASSERT(itr == x.end() || itr.index() >= i);
898 if (itr == x.end() || itr.index() != i) {
902 itr = x.insert(itr, i, y[i]);
903 PPL_ASSERT((*itr) != 0);
906 PPL_ASSERT(itr.index() == i);
917 PPL_ASSERT(itr == x.end() || itr.index() + 1 >= i);
918 if (itr != x.end() && itr.index() < i) {
921 PPL_ASSERT(itr == x.end() || itr.index() >= i);
922 if (itr == x.end() || itr.index() != i) {
926 itr = x.insert(itr, i, y[i]);
928 PPL_ASSERT((*itr) != 0);
931 PPL_ASSERT(itr.index() == i);
941 PPL_ASSERT(itr == x.end() || itr.index() + 1 >= i);
942 if (itr != x.end() && itr.index() < i) {
945 PPL_ASSERT(itr == x.end() || itr.index() >= i);
946 if (itr == x.end() || itr.index() != i) {
950 itr = x.insert(itr, i, y[i]);
952 PPL_ASSERT((*itr) != 0);
955 PPL_ASSERT(itr.index() == i);
967 PPL_ASSERT(itr == x.end() || itr.index() + 1 >= i);
968 if (itr != x.end() && itr.index() < i) {
971 PPL_ASSERT(itr == x.end() || itr.index() >= i);
972 if (itr == x.end() || itr.index() != i) {
976 itr = x.insert(itr, i, y[i]);
977 PPL_ASSERT((*itr) != 0);
980 PPL_ASSERT(itr.index() == i);
992 PPL_ASSERT(itr == x.end() || itr.index() + 1 >= i);
993 if (itr != x.end() && itr.index() < i) {
996 PPL_ASSERT(itr == x.end() || itr.index() >= i);
997 if (itr == x.end() || itr.index() != i) {
1001 itr = x.insert(itr, i, y[i]);
1003 PPL_ASSERT((*itr) != 0);
1006 PPL_ASSERT(itr.index() == i);
1018 PPL_ASSERT(itr == x.end() || itr.index() + 1 >= i);
1019 if (itr != x.end() && itr.index() < i) {
1022 PPL_ASSERT(itr == x.end() || itr.index() >= i);
1023 if (itr == x.end() || itr.index() != i) {
1027 itr = x.insert(itr, i, y[i]);
1029 PPL_ASSERT((*itr) != 0);
1032 PPL_ASSERT(itr.index() == i);
1044 Coefficient_traits::const_reference coeff1,
1045 Coefficient_traits::const_reference coeff2) {
1046 PPL_ASSERT(x.size() >= y.size());
1049 i_end = y.end(); i != i_end; ++i) {
1060 itr = y.lower_bound(itr, i);
1062 if (itr == y.end() || itr.index() != i) {
1072 Coefficient_traits::const_reference coeff1,
1073 Coefficient_traits::const_reference coeff2,
1075 PPL_ASSERT(x.size() >= y.size());
1076 PPL_ASSERT(coeff1 != 0);
1077 PPL_ASSERT(coeff2 != 0);
1084 for ( ; itr != itr_end; ++itr) {
1085 x[itr.index()] += *itr;
1090 for ( ; itr != itr_end; ++itr) {
1091 x[itr.index()] -= *itr;
1095 for ( ; itr != itr_end; ++itr) {
1105 PPL_ASSERT(itr == y.end() || itr.index() + 1 >= i);
1106 if (itr != y.end() && itr.index() < i) {
1109 PPL_ASSERT(itr == y.end() || itr.index() >= i);
1111 if (itr == y.end() || itr.index() != i) {
1123 PPL_ASSERT(itr == y.end() || itr.index() + 1 >= i);
1124 if (itr != y.end() && itr.index() < i) {
1127 PPL_ASSERT(itr == y.end() || itr.index() >= i);
1129 if (itr == y.end() || itr.index() != i) {
1141 PPL_ASSERT(itr == y.end() || itr.index() + 1 >= i);
1142 if (itr != y.end() && itr.index() < i) {
1145 PPL_ASSERT(itr == y.end() || itr.index() >= i);
1147 if (itr == y.end() || itr.index() != i) {
1157 Coefficient_traits::const_reference coeff1,
1158 Coefficient_traits::const_reference coeff2) {
1159 x.linear_combine(y, coeff1, coeff2);
1164 Coefficient_traits::const_reference c1,
1165 Coefficient_traits::const_reference c2,
1167 x.linear_combine(y, c1, c2, start, end);
1176 swap(new_dense[i.index()], *i);
1185 swap(new_sparse, x);
Enable_If< Is_Singleton< T >::value, Interval< B, Info > >::type operator*(const Interval< B, Info > &x, const T &y)
dimension_type index() const
Returns the index of the element pointed to by *this.
void normalize()
Normalizes the modulo of coefficients so that they are mutually prime.
void ascii_dump() const
Writes to std::cerr an ASCII representation of *this.
bool operator!=(const Box< ITV > &x, const Box< ITV > &y)
void reset_after(dimension_type i)
Resets to zero the elements with index greater than or equal to i.
void swap(CO_Tree &x, CO_Tree &y)
void linear_combine(const Sparse_Row &y, Coefficient_traits::const_reference coeff1, Coefficient_traits::const_reference coeff2)
A finite sequence of coefficients.
size_t dimension_type
An unsigned integral type for representing space dimensions.
dimension_type size() const
Returns the size of the row.
void linear_combine(Dense_Row &x, const Dense_Row &y, Coefficient_traits::const_reference coeff1, Coefficient_traits::const_reference coeff2)
#define PPL_DIRTY_TEMP_COEFFICIENT(id)
Declare a local variable named id, of type Coefficient, and containing an unknown initial value...
iterator reset(iterator i)
Resets to zero the value pointed to by i.
CO_Tree::iterator iterator
An iterator on the row elements.
void add_mul_assign(GMP_Integer &x, const GMP_Integer &y, const GMP_Integer &z)
A finite sparse sequence of coefficients.
The standard C++ namespace.
An iterator on the tree elements, ordered by key.
Sparse_Row(dimension_type n=0)
Constructs a row with the specified size.
const iterator & end()
Returns an iterator that points after the last stored element.
void exact_div_assign(Checked_Number< T, Policy > &x, const Checked_Number< T, Policy > &y, const Checked_Number< T, Policy > &z)
void m_swap(CO_Tree &x)
Swaps x with *this.
dimension_type size() const
Gives the number of coefficients currently in use.
#define PPL_OUTPUT_DEFINITIONS_ASCII_ONLY(class_name)
A cache-oblivious binary search tree of pairs.
Enable_If< Is_Native_Or_Checked< T >::value, bool >::type ascii_load(std::istream &s, T &t)
PPL_COEFFICIENT_TYPE Coefficient
An alias for easily naming the type of PPL coefficients.
void neg_assign(GMP_Integer &x)
The entire library is confined to this namespace.
A const iterator on the tree elements, ordered by key.
iterator lower_bound(dimension_type i)
Lower bound of index i.
void swap_coefficients(dimension_type i, dimension_type j)
Swaps the i-th element with the j-th element.
iterator begin()
Returns an iterator that points at the first stored element.
void gcd_assign(GMP_Integer &x, const GMP_Integer &y, const GMP_Integer &z)
int sgn(Boundary_Type type, const T &x, const Info &info)
bool operator==(const Box< ITV > &x, const Box< ITV > &y)
dimension_type index() const
Returns the index of the element pointed to by *this.
Sparse_Row & operator=(const Dense_Row &row)
CO_Tree::const_iterator const_iterator
A const iterator on the row elements.
bool OK() const
Checks the invariant.