PPL  1.2
Swapping_Vector_inlines.hh
Go to the documentation of this file.
1 /* Swapping_Vector 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_Swapping_Vector_inlines_hh
25 #define PPL_Swapping_Vector_inlines_hh 1
26 
27 #include "assertions.hh"
28 
29 namespace Parma_Polyhedra_Library {
30 
31 template <typename T>
32 inline
34  : impl() {
35 }
36 
37 template <typename T>
38 inline
40  : impl() {
41  // NOTE: This is not the same as constructing impl as `impl(i)', because
42  // this implementation calls compute_capacity().
43  resize(i);
44 }
45 
46 template <typename T>
47 inline
49  : impl() {
50  resize(new_size, x);
51 }
52 
53 template <typename T>
54 inline void
56  impl.clear();
57 }
58 
59 template <typename T>
60 inline void
62  if (impl.capacity() < new_capacity) {
63  // Reallocation will take place.
64  std::vector<T> new_impl;
65 
66  new_impl.reserve(compute_capacity(new_capacity, max_num_rows()));
67  new_impl.resize(impl.size());
68 
69  using std::swap;
70 
71  // Steal the old elements.
72  for (dimension_type i = impl.size(); i-- > 0; ) {
73  swap(new_impl[i], impl[i]);
74  }
75 
76  // Put the new vector into place.
77  swap(impl, new_impl);
78  }
79 }
80 
81 template <typename T>
82 inline void
84  reserve(new_size);
85  impl.resize(new_size);
86 }
87 
88 template <typename T>
89 inline void
91  reserve(new_size);
92  impl.resize(new_size, x);
93 }
94 
95 template <typename T>
96 inline dimension_type
98  return impl.size();
99 }
100 
101 template <typename T>
102 inline dimension_type
104  return impl.capacity();
105 }
106 
107 template <typename T>
108 inline bool
110  return impl.empty();
111 }
112 
113 template <typename T>
114 inline void
116  using std::swap;
117  swap(impl, v.impl);
118 }
119 
120 template <typename T>
121 inline T&
123  return impl[i];
124 }
125 
126 template <typename T>
127 inline const T&
129  return impl[i];
130 }
131 
132 template <typename T>
133 inline T&
135  return impl.back();
136 }
137 
138 template <typename T>
139 inline const T&
141  return impl.back();
142 }
143 
144 template <typename T>
145 inline void
147  reserve(size() + 1);
148  impl.push_back(x);
149 }
150 
151 template <typename T>
152 inline void
154  impl.pop_back();
155 }
156 
157 template <typename T>
158 inline memory_size_type
160  // Estimate the size of vector.
161  memory_size_type n = impl.capacity() * sizeof(T);
162  for (const_iterator i = begin(), i_end = end(); i != i_end; ++i) {
163  n += i->external_memory_in_bytes();
164  }
165  return n;
166 }
167 
168 template <typename T>
169 inline typename Swapping_Vector<T>::iterator
171  return impl.begin();
172 }
173 
174 template <typename T>
175 inline typename Swapping_Vector<T>::iterator
177  return impl.end();
178 }
179 
180 template <typename T>
183  return impl.begin();
184 }
185 
186 template <typename T>
189  return impl.end();
190 }
191 
192 template <typename T>
193 inline typename Swapping_Vector<T>::iterator
195  PPL_ASSERT(itr >= begin());
196  PPL_ASSERT(itr < end());
197  const dimension_type old_i = itr - begin();
198  dimension_type i = old_i;
199  ++i;
200  while (i != size()) {
201  swap(impl[i-1], impl[i]);
202  }
203  impl.pop_back();
204  return begin() + old_i;
205 }
206 
207 template <typename T>
208 inline typename Swapping_Vector<T>::iterator
210  PPL_ASSERT(begin() <= first);
211  PPL_ASSERT(first <= last);
212  PPL_ASSERT(last <= end());
213  const iterator old_first = first;
214  typedef typename std::iterator_traits<iterator>::difference_type diff_t;
215  const diff_t k = last - first;
216  const dimension_type n = static_cast<dimension_type>(end() - last);
217  using std::swap;
218  for (dimension_type i = 0; i < n; ++i, ++first) {
219  swap(*first, *(first + k));
220  }
221  impl.erase(end() - k, end());
222  return old_first;
223 }
224 
225 template <typename T>
226 inline dimension_type
228  return impl.max_size();
229 }
230 
231 template <typename T>
232 inline void
234  vec1.m_swap(vec2);
235 }
236 
237 } // namespace Parma_Polyhedra_Library
238 
239 
240 #endif // !defined(PPL_Swapping_Vector_inlines_hh)
void swap(CO_Tree &x, CO_Tree &y)
size_t dimension_type
An unsigned integral type for representing space dimensions.
dimension_type compute_capacity(dimension_type requested_size, dimension_type maximum_size)
Speculative allocation function.
void swap(Swapping_Vector< T > &vec1, Swapping_Vector< T > &vec2)
void reserve(dimension_type new_capacity)
The entire library is confined to this namespace.
Definition: version.hh:61
std::vector< T >::const_iterator const_iterator
size_t memory_size_type
An unsigned integral type for representing memory size in bytes.