00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef PPL_DB_Matrix_inlines_hh
00025 #define PPL_DB_Matrix_inlines_hh 1
00026
00027 #include "globals.defs.hh"
00028 #include "Checked_Number.defs.hh"
00029 #include "distances.defs.hh"
00030 #include "assert.hh"
00031 #include <iostream>
00032
00033 namespace Parma_Polyhedra_Library {
00034
00035 template <typename T>
00036 inline void
00037 DB_Matrix<T>::swap(DB_Matrix& y) {
00038 std::swap(rows, y.rows);
00039 std::swap(row_size, y.row_size);
00040 std::swap(row_capacity, y.row_capacity);
00041 }
00042
00043 template <typename T>
00044 inline dimension_type
00045 DB_Matrix<T>::max_num_rows() {
00046 return std::vector<DB_Row<T> >().max_size();
00047 }
00048
00049 template <typename T>
00050 inline dimension_type
00051 DB_Matrix<T>::max_num_columns() {
00052 return DB_Row<T>::max_size();
00053 }
00054
00055 template <typename T>
00056 inline memory_size_type
00057 DB_Matrix<T>::total_memory_in_bytes() const {
00058 return sizeof(*this) + external_memory_in_bytes();
00059 }
00060
00061 template <typename T>
00062 inline
00063 DB_Matrix<T>::const_iterator::const_iterator()
00064 : i(Iter()) {
00065 }
00066
00067 template <typename T>
00068 inline
00069 DB_Matrix<T>::const_iterator::const_iterator(const Iter& b)
00070 : i(b) {
00071 }
00072
00073 template <typename T>
00074 inline
00075 DB_Matrix<T>::const_iterator::const_iterator(const const_iterator& y)
00076 : i(y.i) {
00077 }
00078
00079 template <typename T>
00080 inline typename DB_Matrix<T>::const_iterator&
00081 DB_Matrix<T>::const_iterator::operator=(const const_iterator& y) {
00082 i = y.i;
00083 return *this;
00084 }
00085
00086 template <typename T>
00087 inline typename DB_Matrix<T>::const_iterator::reference
00088 DB_Matrix<T>::const_iterator::operator*() const {
00089 return *i;
00090 }
00091
00092 template <typename T>
00093 inline typename DB_Matrix<T>::const_iterator::pointer
00094 DB_Matrix<T>::const_iterator::operator->() const {
00095 return &*i;
00096 }
00097
00098 template <typename T>
00099 inline typename DB_Matrix<T>::const_iterator&
00100 DB_Matrix<T>::const_iterator::operator++() {
00101 ++i;
00102 return *this;
00103 }
00104
00105 template <typename T>
00106 inline typename DB_Matrix<T>::const_iterator
00107 DB_Matrix<T>::const_iterator::operator++(int) {
00108 return const_iterator(i++);
00109 }
00110
00111 template <typename T>
00112 inline bool
00113 DB_Matrix<T>::const_iterator::operator==(const const_iterator& y) const {
00114 return i == y.i;
00115 }
00116
00117 template <typename T>
00118 inline bool
00119 DB_Matrix<T>::const_iterator::operator!=(const const_iterator& y) const {
00120 return !operator==(y);
00121 }
00122
00123 template <typename T>
00124 inline typename DB_Matrix<T>::const_iterator
00125 DB_Matrix<T>::begin() const {
00126 return const_iterator(rows.begin());
00127 }
00128
00129 template <typename T>
00130 inline typename DB_Matrix<T>::const_iterator
00131 DB_Matrix<T>::end() const {
00132 return const_iterator(rows.end());
00133 }
00134
00135 template <typename T>
00136 inline
00137 DB_Matrix<T>::DB_Matrix()
00138 : rows(),
00139 row_size(0),
00140 row_capacity(0) {
00141 }
00142
00143 template <typename T>
00144 inline
00145 DB_Matrix<T>::~DB_Matrix() {
00146 }
00147
00148 template <typename T>
00149 inline DB_Row<T>&
00150 DB_Matrix<T>::operator[](const dimension_type k) {
00151 PPL_ASSERT(k < rows.size());
00152 return rows[k];
00153 }
00154
00155 template <typename T>
00156 inline const DB_Row<T>&
00157 DB_Matrix<T>::operator[](const dimension_type k) const {
00158 PPL_ASSERT(k < rows.size());
00159 return rows[k];
00160 }
00161
00162 template <typename T>
00163 inline dimension_type
00164 DB_Matrix<T>::num_rows() const {
00165 return rows.size();
00166 }
00167
00168 #ifdef PPL_DOXYGEN_INCLUDE_IMPLEMENTATION_DETAILS
00169
00170 #endif // defined(PPL_DOXYGEN_INCLUDE_IMPLEMENTATION_DETAILS)
00171 template <typename T>
00172 inline bool
00173 operator!=(const DB_Matrix<T>& x, const DB_Matrix<T>& y) {
00174 return !(x == y);
00175 }
00176
00177 template <typename T>
00178 inline
00179 DB_Matrix<T>::DB_Matrix(const DB_Matrix& y)
00180 : rows(y.rows),
00181 row_size(y.row_size),
00182 row_capacity(compute_capacity(y.row_size, max_num_columns())) {
00183 }
00184
00185 template <typename T>
00186 inline DB_Matrix<T>&
00187 DB_Matrix<T>::operator=(const DB_Matrix& y) {
00188
00189
00190
00191
00192 if (this != &y) {
00193
00194 rows = y.rows;
00195 row_size = y.row_size;
00196
00197
00198 row_capacity = compute_capacity(y.row_size, max_num_columns());
00199 }
00200 return *this;
00201 }
00202
00203 #ifdef PPL_DOXYGEN_INCLUDE_IMPLEMENTATION_DETAILS
00204
00205 #endif // defined(PPL_DOXYGEN_INCLUDE_IMPLEMENTATION_DETAILS)
00206 template <typename Specialization, typename Temp, typename To, typename T>
00207 inline bool
00208 l_m_distance_assign(Checked_Number<To, Extended_Number_Policy>& r,
00209 const DB_Matrix<T>& x,
00210 const DB_Matrix<T>& y,
00211 const Rounding_Dir dir,
00212 Temp& tmp0,
00213 Temp& tmp1,
00214 Temp& tmp2) {
00215 const dimension_type x_num_rows = x.num_rows();
00216 if (x_num_rows != y.num_rows())
00217 return false;
00218 assign_r(tmp0, 0, ROUND_NOT_NEEDED);
00219 for (dimension_type i = x_num_rows; i-- > 0; ) {
00220 const DB_Row<T>& x_i = x[i];
00221 const DB_Row<T>& y_i = y[i];
00222 for (dimension_type j = x_num_rows; j-- > 0; ) {
00223 const T& x_i_j = x_i[j];
00224 const T& y_i_j = y_i[j];
00225 if (is_plus_infinity(x_i_j)) {
00226 if (is_plus_infinity(y_i_j))
00227 continue;
00228 else {
00229 pinf:
00230 assign_r(r, PLUS_INFINITY, ROUND_NOT_NEEDED);
00231 return true;
00232 }
00233 }
00234 else if (is_plus_infinity(y_i_j))
00235 goto pinf;
00236
00237 const Temp* tmp1p;
00238 const Temp* tmp2p;
00239 if (x_i_j > y_i_j) {
00240 maybe_assign(tmp1p, tmp1, x_i_j, dir);
00241 maybe_assign(tmp2p, tmp2, y_i_j, inverse(dir));
00242 }
00243 else {
00244 maybe_assign(tmp1p, tmp1, y_i_j, dir);
00245 maybe_assign(tmp2p, tmp2, x_i_j, inverse(dir));
00246 }
00247 sub_assign_r(tmp1, *tmp1p, *tmp2p, dir);
00248 PPL_ASSERT(sgn(tmp1) >= 0);
00249 Specialization::combine(tmp0, tmp1, dir);
00250 }
00251 }
00252 Specialization::finalize(tmp0, dir);
00253 assign_r(r, tmp0, dir);
00254 return true;
00255 }
00256
00257 #ifdef PPL_DOXYGEN_INCLUDE_IMPLEMENTATION_DETAILS
00258
00259 #endif // defined(PPL_DOXYGEN_INCLUDE_IMPLEMENTATION_DETAILS)
00260 template <typename Temp, typename To, typename T>
00261 inline bool
00262 rectilinear_distance_assign(Checked_Number<To, Extended_Number_Policy>& r,
00263 const DB_Matrix<T>& x,
00264 const DB_Matrix<T>& y,
00265 const Rounding_Dir dir,
00266 Temp& tmp0,
00267 Temp& tmp1,
00268 Temp& tmp2) {
00269 return
00270 l_m_distance_assign<Rectilinear_Distance_Specialization<Temp> >(r, x, y,
00271 dir,
00272 tmp0,
00273 tmp1,
00274 tmp2);
00275 }
00276
00277
00278 #ifdef PPL_DOXYGEN_INCLUDE_IMPLEMENTATION_DETAILS
00279
00280 #endif // defined(PPL_DOXYGEN_INCLUDE_IMPLEMENTATION_DETAILS)
00281 template <typename Temp, typename To, typename T>
00282 inline bool
00283 euclidean_distance_assign(Checked_Number<To, Extended_Number_Policy>& r,
00284 const DB_Matrix<T>& x,
00285 const DB_Matrix<T>& y,
00286 const Rounding_Dir dir,
00287 Temp& tmp0,
00288 Temp& tmp1,
00289 Temp& tmp2) {
00290 return
00291 l_m_distance_assign<Euclidean_Distance_Specialization<Temp> >(r, x, y,
00292 dir,
00293 tmp0,
00294 tmp1,
00295 tmp2);
00296 }
00297
00298 #ifdef PPL_DOXYGEN_INCLUDE_IMPLEMENTATION_DETAILS
00299
00300 #endif // defined(PPL_DOXYGEN_INCLUDE_IMPLEMENTATION_DETAILS)
00301 template <typename Temp, typename To, typename T>
00302 inline bool
00303 l_infinity_distance_assign(Checked_Number<To, Extended_Number_Policy>& r,
00304 const DB_Matrix<T>& x,
00305 const DB_Matrix<T>& y,
00306 const Rounding_Dir dir,
00307 Temp& tmp0,
00308 Temp& tmp1,
00309 Temp& tmp2) {
00310 return
00311 l_m_distance_assign<L_Infinity_Distance_Specialization<Temp> >(r, x, y,
00312 dir,
00313 tmp0,
00314 tmp1,
00315 tmp2);
00316 }
00317
00318 }
00319
00320 namespace std {
00321
00322 #ifdef PPL_DOXYGEN_INCLUDE_IMPLEMENTATION_DETAILS
00323
00324 #endif // defined(PPL_DOXYGEN_INCLUDE_IMPLEMENTATION_DETAILS)
00325 template <typename T>
00326 inline void
00327 swap(Parma_Polyhedra_Library::DB_Matrix<T>& x,
00328 Parma_Polyhedra_Library::DB_Matrix<T>& y) {
00329 x.swap(y);
00330 }
00331
00332 }
00333
00334 #endif // !defined(PPL_DB_Matrix_inlines_hh)