PPL  1.2
Sparse_Row_inlines.hh
Go to the documentation of this file.
1 /* Sparse_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_Sparse_Row_inlines_hh
25 #define PPL_Sparse_Row_inlines_hh 1
26 
27 #include <algorithm>
28 
29 namespace Parma_Polyhedra_Library {
30 
31 inline
33  : size_(n) {
34  PPL_ASSERT(OK());
35 }
36 
37 inline
39  : size_(n) {
40  PPL_ASSERT(OK());
41 }
42 
43 inline
45  : tree(y.tree), size_(y.size_) {
46 }
47 
48 inline
50  : tree(y.begin(),
51  std::distance(y.begin(), y.lower_bound(std::min(y.size(), sz)))),
52  size_(sz) {
53  PPL_ASSERT(OK());
54 }
55 
56 inline void
58  using std::swap;
59  swap(tree, x.tree);
60  swap(size_, x.size_);
61  PPL_ASSERT(OK());
62  PPL_ASSERT(x.OK());
63 }
64 
65 inline dimension_type
67  return size_;
68 }
69 
70 inline dimension_type
72  return tree.size();
73 }
74 
75 inline void
77  if (n < size_) {
78  reset_after(n);
79  }
80  size_ = n;
81  PPL_ASSERT(OK());
82 }
83 
84 inline void
86  PPL_ASSERT(size() >= n);
87  resize(n);
88 }
89 
90 inline void
92  PPL_ASSERT(size() <= n);
93  resize(n);
94 }
95 
96 inline void
98  PPL_ASSERT(i < size_);
100  --size_;
101  PPL_ASSERT(OK());
102 }
103 
104 inline void
106  PPL_ASSERT(i <= size_);
107  tree.increase_keys_from(i, n);
108  size_ += n;
109  PPL_ASSERT(OK());
110 }
111 
114  return tree.begin();
115 }
116 
117 inline const Sparse_Row::iterator&
119  return tree.end();
120 }
121 
124  return tree.cbegin();
125 }
126 
127 inline const Sparse_Row::const_iterator&
129  return tree.cend();
130 }
131 
134  return tree.cbegin();
135 }
136 
137 inline const Sparse_Row::const_iterator&
139  return tree.cend();
140 }
141 
142 inline dimension_type
144  return CO_Tree::max_size();
145 }
146 
147 inline void
149  tree.clear();
150 }
151 
152 inline Coefficient&
154  PPL_ASSERT(i < size_);
155  iterator itr = insert(i);
156  return *itr;
157 }
158 
159 inline Coefficient_traits::const_reference
161  return get(i);
162 }
163 
164 inline Coefficient_traits::const_reference
166  PPL_ASSERT(i < size_);
167  if (tree.empty()) {
168  return Coefficient_zero();
169  }
170  const_iterator itr = find(i);
171  if (itr != end()) {
172  return *itr;
173  }
174  else {
175  return Coefficient_zero();
176  }
177 }
178 
181  PPL_ASSERT(i < size());
182 
183  iterator itr = tree.bisect(i);
184 
185  if (itr != end() && itr.index() == i) {
186  return itr;
187  }
188  return end();
189 }
190 
193  PPL_ASSERT(i < size());
194 
195  iterator itr = tree.bisect_near(hint, i);
196 
197  if (itr != end() && itr.index() == i) {
198  return itr;
199  }
200  return end();
201 }
202 
205  PPL_ASSERT(i < size());
206 
207  const_iterator itr = tree.bisect(i);
208 
209  if (itr != end() && itr.index() == i) {
210  return itr;
211  }
212 
213  return end();
214 }
215 
218  PPL_ASSERT(i < size());
219 
220  const_iterator itr = tree.bisect_near(hint, i);
221 
222  if (itr != end() && itr.index() == i) {
223  return itr;
224  }
225  return end();
226 }
227 
230  PPL_ASSERT(i <= size());
231 
232  iterator itr = tree.bisect(i);
233 
234  if (itr == end()) {
235  return end();
236  }
237 
238  if (itr.index() < i) {
239  ++itr;
240  }
241 
242  PPL_ASSERT(itr == end() || itr.index() >= i);
243 
244  return itr;
245 }
246 
249  PPL_ASSERT(i <= size());
250 
251  iterator itr = tree.bisect_near(hint, i);
252 
253  if (itr == end()) {
254  return end();
255  }
256 
257  if (itr.index() < i) {
258  ++itr;
259  }
260 
261  PPL_ASSERT(itr == end() || itr.index() >= i);
262 
263  return itr;
264 }
265 
268  PPL_ASSERT(i <= size());
269 
270  const_iterator itr = tree.bisect(i);
271 
272  if (itr == end()) {
273  return end();
274  }
275 
276  if (itr.index() < i) {
277  ++itr;
278  }
279 
280  PPL_ASSERT(itr == end() || itr.index() >= i);
281 
282  return itr;
283 }
284 
287  PPL_ASSERT(i <= size());
288 
289  const_iterator itr = tree.bisect_near(hint, i);
290 
291  if (itr == end()) {
292  return end();
293  }
294 
295  if (itr.index() < i) {
296  ++itr;
297  }
298 
299  PPL_ASSERT(itr == end() || itr.index() >= i);
300 
301  return itr;
302 }
303 
305 Sparse_Row::insert(dimension_type i, Coefficient_traits::const_reference x) {
306  PPL_ASSERT(i < size_);
307  return tree.insert(i, x);
308 }
309 
312  Coefficient_traits::const_reference x) {
313  PPL_ASSERT(i < size_);
314  return tree.insert(itr, i, x);
315 }
316 
319  PPL_ASSERT(i < size_);
320  return tree.insert(i);
321 }
322 
325  PPL_ASSERT(i < size_);
326  return tree.insert(itr, i);
327 }
328 
329 inline void
331  PPL_ASSERT(i != end());
332  PPL_ASSERT(j != end());
333  using std::swap;
334  swap(*i, *j);
335  PPL_ASSERT(OK());
336 }
337 
338 inline void
340  PPL_ASSERT(lower_bound(i) == itr);
341  PPL_ASSERT(itr != end());
342  tree.fast_shift(i, itr);
343  PPL_ASSERT(OK());
344 }
345 
348  iterator res = tree.erase(i);
349  PPL_ASSERT(OK());
350  return res;
351 }
352 
353 inline void
355  PPL_ASSERT(i < size());
356 
357  tree.erase(i);
358  PPL_ASSERT(OK());
359 }
360 
361 inline memory_size_type
364 }
365 
366 inline memory_size_type
368  return external_memory_in_bytes();
369 }
370 
371 inline memory_size_type
373  return external_memory_in_bytes() + sizeof(*this);
374 }
375 
376 inline memory_size_type
378  return total_memory_in_bytes();
379 }
380 
381 #ifdef PPL_DOXYGEN_INCLUDE_IMPLEMENTATION_DETAILS
382 
383 #endif // defined(PPL_DOXYGEN_INCLUDE_IMPLEMENTATION_DETAILS)
384 inline void
386  x.m_swap(y);
387 }
388 
389 } // namespace Parma_Polyhedra_Library
390 
391 #endif // !defined(PPL_Sparse_Row_inlines_hh)
dimension_type index() const
Returns the index of the element pointed to by *this.
void increase_keys_from(dimension_type key, dimension_type n)
Adds n to all keys greater than or equal to key.
Definition: CO_Tree.cc:196
iterator find(dimension_type i)
Looks for an element with index i.
void delete_element_and_shift(dimension_type i)
Deletes the i-th element from the row, shifting the next elements to the left.
void reset_after(dimension_type i)
Resets to zero the elements with index greater than or equal to i.
Definition: Sparse_Row.cc:193
void swap(CO_Tree &x, CO_Tree &y)
size_t dimension_type
An unsigned integral type for representing space dimensions.
dimension_type size() const
Returns the size of the row.
void m_swap(Sparse_Row &x)
Swaps *this and x.
void resize(dimension_type n)
Resizes the row to the specified size.
iterator reset(iterator i)
Resets to zero the value pointed to by i.
iterator begin()
Returns an iterator that points at the first element.
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.
static dimension_type max_size()
Returns the size() of the largest possible CO_Tree.
dimension_type size() const
Returns the number of elements stored in the tree.
const iterator & end()
Returns an iterator that points after the last element.
bool empty() const
Returns true if the tree has no elements.
iterator insert(dimension_type key)
Inserts an element in the tree.
void expand_within_capacity(dimension_type n)
Resizes the row to size n.
Coefficient & operator[](dimension_type i)
Gets a reference to the i-th element.
const_iterator cbegin() const
Returns a const_iterator that points at the first element.
void clear()
Resets all the elements of this row.
void shrink(dimension_type n)
Resizes the row to size n.
void fast_swap(dimension_type i, iterator itr)
iterator bisect(dimension_type key)
Searches an element with key key using bisection.
const const_iterator & cend() const
Returns a const_iterator that points after the last element.
PPL_COEFFICIENT_TYPE Coefficient
An alias for easily naming the type of PPL coefficients.
memory_size_type total_memory_in_bytes() const
Returns the size in bytes of the memory managed by *this.
CO_Tree tree
The tree used to store the elements.
iterator insert(dimension_type i, Coefficient_traits::const_reference x)
Equivalent to (*this)[i] = x; find(i), but faster.
Coefficient_traits::const_reference Coefficient_zero()
Returns a const reference to a Coefficient with value 0.
The entire library is confined to this namespace.
Definition: version.hh:61
memory_size_type external_memory_in_bytes() const
Returns the size in bytes of the memory managed by *this.
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.
Definition: Sparse_Row.cc:127
void clear()
Removes all elements from the tree.
dimension_type size_
The size of the row.
dimension_type num_stored_elements() const
Returns the number of elements explicitly stored in the row.
iterator begin()
Returns an iterator that points at the first stored element.
const const_iterator & cend() const
Returns an iterator that points after the last element.
void swap(Sparse_Row &x, Sparse_Row &y)
size_t memory_size_type
An unsigned integral type for representing memory size in bytes.
iterator erase(dimension_type key)
Erases the element with key key from the tree.
void add_zeroes_and_shift(dimension_type n, dimension_type i)
Adds n zeroes before index i.
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 grea...
Definition: CO_Tree.cc:179
iterator bisect_near(iterator hint, dimension_type key)
Searches an element with key key near hint.
dimension_type index() const
Returns the index of the element pointed to by *this.
dimension_type external_memory_in_bytes() const
Returns the size in bytes of the memory managed by *this.
Definition: CO_Tree.cc:30
static dimension_type max_size()
Returns the size() of the largest possible Sparse_Row.
void swap(Parma_Polyhedra_Library::Sparse_Row &x, Parma_Polyhedra_Library::Sparse_Row &y)
Swaps x with y.
void fast_shift(dimension_type i, iterator itr)
const_iterator cbegin() const
Returns an iterator that points at the first element.
Coefficient_traits::const_reference get(dimension_type i) const
Gets the i-th element in the sequence.
bool OK() const
Checks the invariant.
Definition: Sparse_Row.cc:742