00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef PPL_DB_Row_inlines_hh
00024 #define PPL_DB_Row_inlines_hh 1
00025
00026 #include <cassert>
00027 #include <algorithm>
00028 #include <iostream>
00029 #include "checked.defs.hh"
00030
00031 namespace Parma_Polyhedra_Library {
00032
00033 template <typename T>
00034 inline void*
00035 DB_Row_Impl_Handler<T>::Impl::operator new(const size_t fixed_size,
00036 const dimension_type capacity) {
00037 #if PPL_CXX_SUPPORTS_FLEXIBLE_ARRAYS
00038 return ::operator new(fixed_size + capacity*sizeof(T));
00039 #else
00040 assert(capacity >= 1);
00041 return ::operator new(fixed_size + (capacity-1)*sizeof(T));
00042 #endif
00043 }
00044
00045 template <typename T>
00046 inline void
00047 DB_Row_Impl_Handler<T>::Impl::operator delete(void* p) {
00048 ::operator delete(p);
00049 }
00050
00051 template <typename T>
00052 inline void
00053 DB_Row_Impl_Handler<T>::Impl::operator delete(void* p, dimension_type) {
00054 ::operator delete(p);
00055 }
00056
00057 template <typename T>
00058 inline memory_size_type
00059 DB_Row_Impl_Handler<T>::Impl
00060 ::total_memory_in_bytes(dimension_type capacity) const {
00061 return
00062 sizeof(*this)
00063 + capacity*sizeof(T)
00064 #if !PPL_CXX_SUPPORTS_FLEXIBLE_ARRAYS
00065 - 1*sizeof(T)
00066 #endif
00067 + external_memory_in_bytes();
00068 }
00069
00070 template <typename T>
00071 inline memory_size_type
00072 DB_Row_Impl_Handler<T>::Impl::total_memory_in_bytes() const {
00073
00074
00075 return total_memory_in_bytes(size_);
00076 }
00077
00078 template <typename T>
00079 inline dimension_type
00080 DB_Row_Impl_Handler<T>::Impl::max_size() {
00081 return size_t(-1)/sizeof(T);
00082 }
00083
00084 template <typename T>
00085 inline dimension_type
00086 DB_Row_Impl_Handler<T>::Impl::size() const {
00087 return size_;
00088 }
00089
00090 template <typename T>
00091 inline void
00092 DB_Row_Impl_Handler<T>::Impl::set_size(const dimension_type new_sz) {
00093 size_ = new_sz;
00094 }
00095
00096 template <typename T>
00097 inline void
00098 DB_Row_Impl_Handler<T>::Impl::bump_size() {
00099 ++size_;
00100 }
00101
00102 template <typename T>
00103 inline
00104 DB_Row_Impl_Handler<T>::Impl::Impl()
00105 : size_(0) {
00106 }
00107
00108 template <typename T>
00109 inline
00110 DB_Row_Impl_Handler<T>::Impl::~Impl() {
00111 shrink(0);
00112 }
00113
00114 template <typename T>
00115 inline
00116 DB_Row_Impl_Handler<T>::DB_Row_Impl_Handler()
00117 : impl(0) {
00118 #if PPL_DB_ROW_EXTRA_DEBUG
00119 capacity_ = 0;
00120 #endif
00121 }
00122
00123 template <typename T>
00124 inline
00125 DB_Row_Impl_Handler<T>::~DB_Row_Impl_Handler() {
00126 delete impl;
00127 }
00128
00129 template <typename T>
00130 inline T&
00131 DB_Row_Impl_Handler<T>::Impl::operator[](const dimension_type k) {
00132 assert(k < size());
00133 return vec_[k];
00134 }
00135
00136 template <typename T>
00137 inline const T&
00138 DB_Row_Impl_Handler<T>::Impl::operator[](const dimension_type k) const {
00139 assert(k < size());
00140 return vec_[k];
00141 }
00142
00143 template <typename T>
00144 inline dimension_type
00145 DB_Row<T>::max_size() {
00146 return DB_Row_Impl_Handler<T>::Impl::max_size();
00147 }
00148
00149 template <typename T>
00150 inline dimension_type
00151 DB_Row<T>::size() const {
00152 return this->impl->size();
00153 }
00154
00155 #if PPL_DB_ROW_EXTRA_DEBUG
00156 template <typename T>
00157 inline dimension_type
00158 DB_Row<T>::capacity() const {
00159 return this->capacity_;
00160 }
00161 #endif // PPL_DB_ROW_EXTRA_DEBUG
00162
00163 template <typename T>
00164 inline
00165 DB_Row<T>::DB_Row()
00166 : DB_Row_Impl_Handler<T>() {
00167 }
00168
00169 template <typename T>
00170 inline void
00171 DB_Row<T>::allocate(
00172 #if PPL_CXX_SUPPORTS_FLEXIBLE_ARRAYS
00173 const
00174 #endif
00175 dimension_type capacity) {
00176 DB_Row<T>& x = *this;
00177 assert(capacity <= max_size());
00178 #if !PPL_CXX_SUPPORTS_FLEXIBLE_ARRAYS
00179 if (capacity == 0)
00180 ++capacity;
00181 #endif
00182 assert(x.impl == 0);
00183 x.impl = new (capacity) typename DB_Row_Impl_Handler<T>::Impl();
00184 #if PPL_DB_ROW_EXTRA_DEBUG
00185 assert(x.capacity_ == 0);
00186 x.capacity_ = capacity;
00187 #endif
00188 }
00189
00190 template <typename T>
00191 inline void
00192 DB_Row<T>::expand_within_capacity(const dimension_type new_size) {
00193 DB_Row<T>& x = *this;
00194 assert(x.impl);
00195 #if PPL_DB_ROW_EXTRA_DEBUG
00196 assert(new_size <= x.capacity_);
00197 #endif
00198 x.impl->expand_within_capacity(new_size);
00199 }
00200
00201 template <typename T>
00202 inline void
00203 DB_Row<T>::copy_construct_coefficients(const DB_Row& y) {
00204 DB_Row<T>& x = *this;
00205 assert(x.impl && y.impl);
00206 #if PPL_DB_ROW_EXTRA_DEBUG
00207 assert(y.size() <= x.capacity_);
00208 #endif
00209 x.impl->copy_construct_coefficients(*(y.impl));
00210 }
00211
00212 template <typename T>
00213 template <typename U>
00214 inline void
00215 DB_Row<T>::construct_upward_approximation(const DB_Row<U>& y,
00216 const dimension_type capacity) {
00217 DB_Row<T>& x = *this;
00218 assert(y.size() <= capacity && capacity <= max_size());
00219 allocate(capacity);
00220 assert(y.impl);
00221 x.impl->construct_upward_approximation(*(y.impl));
00222 }
00223
00224 template <typename T>
00225 inline void
00226 DB_Row<T>::construct(const dimension_type sz,
00227 const dimension_type capacity) {
00228 assert(sz <= capacity && capacity <= max_size());
00229 allocate(capacity);
00230 expand_within_capacity(sz);
00231 }
00232
00233 template <typename T>
00234 inline void
00235 DB_Row<T>::construct(const dimension_type sz) {
00236 construct(sz, sz);
00237 }
00238
00239 template <typename T>
00240 inline
00241 DB_Row<T>::DB_Row(const dimension_type sz,
00242 const dimension_type capacity)
00243 : DB_Row_Impl_Handler<T>() {
00244 construct(sz, capacity);
00245 }
00246
00247 template <typename T>
00248 inline
00249 DB_Row<T>::DB_Row(const dimension_type sz) {
00250 construct(sz);
00251 }
00252
00253 template <typename T>
00254 inline
00255 DB_Row<T>::DB_Row(const DB_Row& y)
00256 : DB_Row_Impl_Handler<T>() {
00257 if (y.impl) {
00258 allocate(compute_capacity(y.size(), max_size()));
00259 copy_construct_coefficients(y);
00260 }
00261 }
00262
00263 template <typename T>
00264 inline
00265 DB_Row<T>::DB_Row(const DB_Row& y,
00266 const dimension_type capacity)
00267 : DB_Row_Impl_Handler<T>() {
00268 assert(y.impl);
00269 assert(y.size() <= capacity && capacity <= max_size());
00270 allocate(capacity);
00271 copy_construct_coefficients(y);
00272 }
00273
00274 template <typename T>
00275 inline
00276 DB_Row<T>::DB_Row(const DB_Row& y,
00277 const dimension_type sz,
00278 const dimension_type capacity)
00279 : DB_Row_Impl_Handler<T>() {
00280 assert(y.impl);
00281 assert(y.size() <= sz && sz <= capacity && capacity <= max_size());
00282 allocate(capacity);
00283 copy_construct_coefficients(y);
00284 expand_within_capacity(sz);
00285 }
00286
00287 template <typename T>
00288 inline
00289 DB_Row<T>::~DB_Row() {
00290 }
00291
00292 template <typename T>
00293 inline void
00294 DB_Row<T>::shrink(const dimension_type new_size) {
00295 DB_Row<T>& x = *this;
00296 assert(x.impl);
00297 x.impl->shrink(new_size);
00298 }
00299
00300 template <typename T>
00301 inline void
00302 DB_Row<T>::swap(DB_Row& y) {
00303 DB_Row<T>& x = *this;
00304 std::swap(x.impl, y.impl);
00305 #if PPL_DB_ROW_EXTRA_DEBUG
00306 std::swap(x.capacity_, y.capacity_);
00307 #endif
00308 }
00309
00310 template <typename T>
00311 inline void
00312 DB_Row<T>::assign(DB_Row& y) {
00313 DB_Row<T>& x = *this;
00314 x.impl = y.impl;
00315 #if PPL_DB_ROW_EXTRA_DEBUG
00316 x.capacity_ = y.capacity_;
00317 #endif
00318 }
00319
00320 template <typename T>
00321 inline DB_Row<T>&
00322 DB_Row<T>::operator=(const DB_Row& y) {
00323
00324 DB_Row tmp(y);
00325
00326 swap(tmp);
00327
00328 return *this;
00329 }
00330
00331 template <typename T>
00332 inline T&
00333 DB_Row<T>::operator[](const dimension_type k) {
00334 DB_Row<T>& x = *this;
00335 return (*x.impl)[k];
00336 }
00337
00338 template <typename T>
00339 inline const T&
00340 DB_Row<T>::operator[](const dimension_type k) const {
00341 const DB_Row<T>& x = *this;
00342 return (*x.impl)[k];
00343 }
00344
00345 template <typename T>
00346 inline typename DB_Row<T>::iterator
00347 DB_Row<T>::begin() {
00348 DB_Row<T>& x = *this;
00349 return iterator(x.impl->vec_);
00350 }
00351
00352 template <typename T>
00353 inline typename DB_Row<T>::iterator
00354 DB_Row<T>::end() {
00355 DB_Row<T>& x = *this;
00356 return iterator(x.impl->vec_ + x.impl->size_);
00357 }
00358
00359 template <typename T>
00360 inline typename DB_Row<T>::const_iterator
00361 DB_Row<T>::begin() const {
00362 const DB_Row<T>& x = *this;
00363 return const_iterator(x.impl->vec_);
00364 }
00365
00366 template <typename T>
00367 inline typename DB_Row<T>::const_iterator
00368 DB_Row<T>::end() const {
00369 const DB_Row<T>& x = *this;
00370 return const_iterator(x.impl->vec_ + x.impl->size_);
00371 }
00372
00373 template <typename T>
00374 inline memory_size_type
00375 DB_Row<T>::external_memory_in_bytes(dimension_type capacity) const {
00376 const DB_Row<T>& x = *this;
00377 return x.impl->total_memory_in_bytes(capacity);
00378 }
00379
00380 template <typename T>
00381 inline memory_size_type
00382 DB_Row<T>::total_memory_in_bytes(dimension_type capacity) const {
00383 return sizeof(*this) + external_memory_in_bytes(capacity);
00384 }
00385
00386 template <typename T>
00387 inline memory_size_type
00388 DB_Row<T>::external_memory_in_bytes() const {
00389 const DB_Row<T>& x = *this;
00390 #if PPL_DB_ROW_EXTRA_DEBUG
00391 return x.impl->total_memory_in_bytes(x.capacity_);
00392 #else
00393 return x.impl->total_memory_in_bytes();
00394 #endif
00395 }
00396
00397 template <typename T>
00398 inline memory_size_type
00399 DB_Row<T>::total_memory_in_bytes() const {
00400 return sizeof(*this) + external_memory_in_bytes();
00401 }
00402
00404 template <typename T>
00405 inline bool
00406 operator!=(const DB_Row<T>& x, const DB_Row<T>& y) {
00407 return !(x == y);
00408 }
00409
00410 }
00411
00412
00413 namespace std {
00414
00416 template <typename T>
00417 inline void
00418 swap(Parma_Polyhedra_Library::DB_Row<T>& x,
00419 Parma_Polyhedra_Library::DB_Row<T>& y) {
00420 x.swap(y);
00421 }
00422
00424 template <typename T>
00425 inline void
00426 iter_swap(typename std::vector<Parma_Polyhedra_Library::DB_Row<T> >
00427 ::iterator x,
00428 typename std::vector<Parma_Polyhedra_Library::DB_Row<T> >
00429 ::iterator y) {
00430 swap(*x, *y);
00431 }
00432
00433 }
00434
00435 #endif // !defined(PPL_DB_Row_inlines_hh)