PPL  1.2
DB_Row_inlines.hh
Go to the documentation of this file.
1 /* DB_Row class implementation: inline functions.
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_DB_Row_inlines_hh
25 #define PPL_DB_Row_inlines_hh 1
26 
27 #include "checked_defs.hh"
28 #include "assertions.hh"
29 #include <cstddef>
30 #include <limits>
31 #include <algorithm>
32 #include <iostream>
33 
34 namespace Parma_Polyhedra_Library {
35 
36 template <typename T>
37 inline void*
38 DB_Row_Impl_Handler<T>::Impl::operator new(const size_t fixed_size,
39  const dimension_type capacity) {
40 #if PPL_CXX_SUPPORTS_ZERO_LENGTH_ARRAYS
41  return ::operator new(fixed_size + capacity*sizeof(T));
42 #else
43  PPL_ASSERT(capacity >= 1);
44  return ::operator new(fixed_size + (capacity-1)*sizeof(T));
45 #endif
46 }
47 
48 template <typename T>
49 inline void
51  ::operator delete(p);
52 }
53 
54 template <typename T>
55 inline void
57  ::operator delete(p);
58 }
59 
60 template <typename T>
61 inline memory_size_type
64  return
65  sizeof(*this)
66  + capacity*sizeof(T)
67 #if !PPL_CXX_SUPPORTS_ZERO_LENGTH_ARRAYS
68  - 1*sizeof(T)
69 #endif
71 }
72 
73 template <typename T>
74 inline memory_size_type
76  // In general, this is a lower bound, as the capacity of *this
77  // may be strictly greater than `size_'
79 }
80 
81 template <typename T>
82 inline dimension_type
84  return std::numeric_limits<size_t>::max() / sizeof(T);
85 }
86 
87 template <typename T>
88 inline dimension_type
90  return size_;
91 }
92 
93 template <typename T>
94 inline void
96  size_ = new_sz;
97 }
98 
99 template <typename T>
100 inline void
102  ++size_;
103 }
104 
105 template <typename T>
106 inline
108  : size_(0) {
109 }
110 
111 template <typename T>
112 inline
114  shrink(0);
115 }
116 
117 template <typename T>
118 inline
120  : impl(0) {
121 #if PPL_DB_ROW_EXTRA_DEBUG
122  capacity_ = 0;
123 #endif
124 }
125 
126 template <typename T>
127 inline
129  delete impl;
130 }
131 
132 template <typename T>
133 inline T&
135  PPL_ASSERT(k < size());
136  return vec_[k];
137 }
138 
139 template <typename T>
140 inline const T&
142  PPL_ASSERT(k < size());
143  return vec_[k];
144 }
145 
146 template <typename T>
147 inline dimension_type
150 }
151 
152 template <typename T>
153 inline dimension_type
155  return this->impl->size();
156 }
157 
158 #if PPL_DB_ROW_EXTRA_DEBUG
159 template <typename T>
160 inline dimension_type
161 DB_Row<T>::capacity() const {
162  return this->capacity_;
163 }
164 #endif // PPL_DB_ROW_EXTRA_DEBUG
165 
166 template <typename T>
167 inline
169  : DB_Row_Impl_Handler<T>() {
170 }
171 
172 template <typename T>
173 inline void
175 #if PPL_CXX_SUPPORTS_ZERO_LENGTH_ARRAYS
176  const
177 #endif
178  dimension_type capacity) {
179  DB_Row<T>& x = *this;
180  PPL_ASSERT(capacity <= max_size());
181 #if !PPL_CXX_SUPPORTS_ZERO_LENGTH_ARRAYS
182  if (capacity == 0) {
183  ++capacity;
184  }
185 #endif
186  PPL_ASSERT(x.impl == 0);
187  x.impl = new(capacity) typename DB_Row_Impl_Handler<T>::Impl();
188 #if PPL_DB_ROW_EXTRA_DEBUG
189  PPL_ASSERT(x.capacity_ == 0);
190  x.capacity_ = capacity;
191 #endif
192 }
193 
194 template <typename T>
195 inline void
197  DB_Row<T>& x = *this;
198  PPL_ASSERT(x.impl);
199 #if PPL_DB_ROW_EXTRA_DEBUG
200  PPL_ASSERT(new_size <= x.capacity_);
201 #endif
202  x.impl->expand_within_capacity(new_size);
203 }
204 
205 template <typename T>
206 inline void
208  DB_Row<T>& x = *this;
209  PPL_ASSERT(x.impl && y.impl);
210 #if PPL_DB_ROW_EXTRA_DEBUG
211  PPL_ASSERT(y.size() <= x.capacity_);
212 #endif
213  x.impl->copy_construct_coefficients(*(y.impl));
214 }
215 
216 template <typename T>
217 template <typename U>
218 inline void
220  const dimension_type capacity) {
221  DB_Row<T>& x = *this;
222  PPL_ASSERT(y.size() <= capacity && capacity <= max_size());
223  allocate(capacity);
224  PPL_ASSERT(y.impl);
225  x.impl->construct_upward_approximation(*(y.impl));
226 }
227 
228 template <typename T>
229 inline void
231  const dimension_type capacity) {
232  PPL_ASSERT(sz <= capacity && capacity <= max_size());
233  allocate(capacity);
234  expand_within_capacity(sz);
235 }
236 
237 template <typename T>
238 inline void
240  construct(sz, sz);
241 }
242 
243 template <typename T>
244 inline
246  const dimension_type capacity)
247  : DB_Row_Impl_Handler<T>() {
248  construct(sz, capacity);
249 }
250 
251 template <typename T>
252 inline
254  construct(sz);
255 }
256 
257 template <typename T>
258 inline
260  : DB_Row_Impl_Handler<T>() {
261  if (y.impl != 0) {
264  }
265 }
266 
267 template <typename T>
268 inline
270  const dimension_type capacity)
271  : DB_Row_Impl_Handler<T>() {
272  PPL_ASSERT(y.impl);
273  PPL_ASSERT(y.size() <= capacity && capacity <= max_size());
274  allocate(capacity);
276 }
277 
278 template <typename T>
279 inline
281  const dimension_type sz,
282  const dimension_type capacity)
283  : DB_Row_Impl_Handler<T>() {
284  PPL_ASSERT(y.impl);
285  PPL_ASSERT(y.size() <= sz && sz <= capacity && capacity <= max_size());
286  allocate(capacity);
289 }
290 
291 template <typename T>
292 inline
294 }
295 
296 template <typename T>
297 inline void
299  DB_Row<T>& x = *this;
300  PPL_ASSERT(x.impl);
301  x.impl->shrink(new_size);
302 }
303 
304 template <typename T>
305 inline void
307  using std::swap;
308  DB_Row<T>& x = *this;
309  swap(x.impl, y.impl);
310 #if PPL_DB_ROW_EXTRA_DEBUG
311  swap(x.capacity_, y.capacity_);
312 #endif
313 }
314 
315 template <typename T>
316 inline void
318  DB_Row<T>& x = *this;
319  x.impl = y.impl;
320 #if PPL_DB_ROW_EXTRA_DEBUG
321  x.capacity_ = y.capacity_;
322 #endif
323 }
324 
325 template <typename T>
326 inline DB_Row<T>&
328  DB_Row tmp(y);
329  m_swap(tmp);
330  return *this;
331 }
332 
333 template <typename T>
334 inline T&
336  DB_Row<T>& x = *this;
337  return (*x.impl)[k];
338 }
339 
340 template <typename T>
341 inline const T&
343  const DB_Row<T>& x = *this;
344  return (*x.impl)[k];
345 }
346 
347 template <typename T>
348 inline typename DB_Row<T>::iterator
350  DB_Row<T>& x = *this;
351  return iterator(x.impl->vec_);
352 }
353 
354 template <typename T>
355 inline typename DB_Row<T>::iterator
357  DB_Row<T>& x = *this;
358  return iterator(x.impl->vec_ + x.impl->size_);
359 }
360 
361 template <typename T>
362 inline typename DB_Row<T>::const_iterator
364  const DB_Row<T>& x = *this;
365  return const_iterator(x.impl->vec_);
366 }
367 
368 template <typename T>
369 inline typename DB_Row<T>::const_iterator
370 DB_Row<T>::end() const {
371  const DB_Row<T>& x = *this;
372  return const_iterator(x.impl->vec_ + x.impl->size_);
373 }
374 
375 template <typename T>
376 inline memory_size_type
378  const DB_Row<T>& x = *this;
379  return x.impl->total_memory_in_bytes(capacity);
380 }
381 
382 template <typename T>
383 inline memory_size_type
385  return sizeof(*this) + external_memory_in_bytes(capacity);
386 }
387 
388 template <typename T>
389 inline memory_size_type
391  const DB_Row<T>& x = *this;
392 #if PPL_DB_ROW_EXTRA_DEBUG
393  return x.impl->total_memory_in_bytes(x.capacity_);
394 #else
395  return x.impl->total_memory_in_bytes();
396 #endif
397 }
398 
399 template <typename T>
400 inline memory_size_type
402  return sizeof(*this) + external_memory_in_bytes();
403 }
404 
406 template <typename T>
407 inline bool
408 operator!=(const DB_Row<T>& x, const DB_Row<T>& y) {
409  return !(x == y);
410 }
411 
413 template <typename T>
414 inline void
416  x.m_swap(y);
417 }
418 
420 template <typename T>
421 inline void
422 iter_swap(typename std::vector<DB_Row<T> >::iterator x,
423  typename std::vector<DB_Row<T> >::iterator y) {
424  swap(*x, *y);
425 }
426 
427 } // namespace Parma_Polyhedra_Library
428 
429 #endif // !defined(PPL_DB_Row_inlines_hh)
void set_size(dimension_type new_sz)
Sets to new_sz the actual size of *this.
void iter_swap(typename std::vector< DB_Row< T > >::iterator x, typename std::vector< DB_Row< T > >::iterator y)
void assign(DB_Row &y)
Assigns the implementation of y to *this.
T vec_[ 1]
The vector of coefficients.
Definition: DB_Row_defs.hh:455
void swap(CO_Tree &x, CO_Tree &y)
memory_size_type total_memory_in_bytes() const
Returns a lower bound to the total size in bytes of the memory occupied by *this. ...
void m_swap(DB_Row &y)
Swaps *this with y.
size_t dimension_type
An unsigned integral type for representing space dimensions.
void swap(DB_Row< T > &x, DB_Row< T > &y)
A class to define STL const and non-const iterators from pointer types.
void bump_size()
Increments the size of *this by 1.
memory_size_type total_memory_in_bytes() const
Returns a lower bound to the total size in bytes of the memory occupied by *this. ...
DB_Row & operator=(const DB_Row &y)
Assignment operator.
void expand_within_capacity(dimension_type new_size)
Expands the row to size new_size.
T & operator[](dimension_type k)
Returns a reference to the element of the row indexed by k.
void allocate(dimension_type capacity)
Allocates memory for a default constructed DB_Row object, allowing for capacity coefficients at most...
dimension_type compute_capacity(dimension_type requested_size, dimension_type maximum_size)
Speculative allocation function.
The base class for the single rows of matrices.
Definition: DB_Row_defs.hh:120
Enable_If< Is_Native< T >::value, memory_size_type >::type external_memory_in_bytes(const T &)
For native types, returns the size in bytes of the memory managed by the type of the (unused) paramet...
dimension_type size_
The number of coefficients in the row.
Definition: DB_Row_defs.hh:446
The real implementation of a DB_Row object.
Definition: DB_Row_defs.hh:352
void shrink(dimension_type new_size)
Shrinks the row by erasing elements at the end.
iterator end()
Returns the past-the-end iterator.
The entire library is confined to this namespace.
Definition: version.hh:61
T & operator[](dimension_type k)
Returns a reference to the element of *this indexed by k.
void construct_upward_approximation(const DB_Row< U > &y, dimension_type capacity)
Constructs properly a conservative approximation of y.
iterator begin()
Returns the const iterator pointing to the first element, if *this is not empty; otherwise, returns the past-the-end const iterator.
dimension_type size() const
Gives the number of coefficients currently in use.
DB_Row()
Pre-constructs a row: construction must be completed by construct().
static dimension_type max_size()
Returns the size() of the largest possible DB_Row.
size_t memory_size_type
An unsigned integral type for representing memory size in bytes.
memory_size_type external_memory_in_bytes() const
Returns a lower bound to the size in bytes of the memory managed by *this.
dimension_type size() const
Returns the actual size of this.
Enable_If< Is_Native_Or_Checked< To >::value &&Is_Special< From >::value, Result >::type construct(To &to, const From &, Rounding_Dir dir)
The handler of the actual DB_Row implementation.
Definition: DB_Row_defs.hh:58
static dimension_type max_size()
Returns the size() of the largest possible Impl.
bool operator!=(const DB_Row< T > &x, const DB_Row< T > &y)
void copy_construct_coefficients(const DB_Row &y)
Exception-safe copy construction mechanism for coefficients.
void construct(dimension_type sz)
Constructs properly a default-constructed element.
Impl * impl
A pointer to the actual implementation.
Definition: DB_Row_defs.hh:66