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
Swapping_Vector()33 Swapping_Vector<T>::Swapping_Vector()
34 : impl() {
35 }
36
37 template <typename T>
38 inline
Swapping_Vector(dimension_type i)39 Swapping_Vector<T>::Swapping_Vector(dimension_type i)
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
Swapping_Vector(dimension_type new_size,const T & x)48 Swapping_Vector<T>::Swapping_Vector(dimension_type new_size, const T& x)
49 : impl() {
50 resize(new_size, x);
51 }
52
53 template <typename T>
54 inline void
clear()55 Swapping_Vector<T>::clear() {
56 impl.clear();
57 }
58
59 template <typename T>
60 inline void
reserve(dimension_type new_capacity)61 Swapping_Vector<T>::reserve(dimension_type new_capacity) {
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
resize(dimension_type new_size)83 Swapping_Vector<T>::resize(dimension_type new_size) {
84 reserve(new_size);
85 impl.resize(new_size);
86 }
87
88 template <typename T>
89 inline void
resize(dimension_type new_size,const T & x)90 Swapping_Vector<T>::resize(dimension_type new_size, const T& x) {
91 reserve(new_size);
92 impl.resize(new_size, x);
93 }
94
95 template <typename T>
96 inline dimension_type
size() const97 Swapping_Vector<T>::size() const {
98 return impl.size();
99 }
100
101 template <typename T>
102 inline dimension_type
capacity() const103 Swapping_Vector<T>::capacity() const {
104 return impl.capacity();
105 }
106
107 template <typename T>
108 inline bool
empty() const109 Swapping_Vector<T>::empty() const {
110 return impl.empty();
111 }
112
113 template <typename T>
114 inline void
m_swap(Swapping_Vector & v)115 Swapping_Vector<T>::m_swap(Swapping_Vector& v) {
116 using std::swap;
117 swap(impl, v.impl);
118 }
119
120 template <typename T>
121 inline T&
operator [](dimension_type i)122 Swapping_Vector<T>::operator[](dimension_type i) {
123 return impl[i];
124 }
125
126 template <typename T>
127 inline const T&
operator [](dimension_type i) const128 Swapping_Vector<T>::operator[](dimension_type i) const {
129 return impl[i];
130 }
131
132 template <typename T>
133 inline T&
back()134 Swapping_Vector<T>::back() {
135 return impl.back();
136 }
137
138 template <typename T>
139 inline const T&
back() const140 Swapping_Vector<T>::back() const {
141 return impl.back();
142 }
143
144 template <typename T>
145 inline void
push_back(const T & x)146 Swapping_Vector<T>::push_back(const T& x) {
147 reserve(size() + 1);
148 impl.push_back(x);
149 }
150
151 template <typename T>
152 inline void
pop_back()153 Swapping_Vector<T>::pop_back() {
154 impl.pop_back();
155 }
156
157 template <typename T>
158 inline memory_size_type
external_memory_in_bytes() const159 Swapping_Vector<T>::external_memory_in_bytes() const {
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
begin()170 Swapping_Vector<T>::begin() {
171 return impl.begin();
172 }
173
174 template <typename T>
175 inline typename Swapping_Vector<T>::iterator
end()176 Swapping_Vector<T>::end() {
177 return impl.end();
178 }
179
180 template <typename T>
181 inline typename Swapping_Vector<T>::const_iterator
begin() const182 Swapping_Vector<T>::begin() const {
183 return impl.begin();
184 }
185
186 template <typename T>
187 inline typename Swapping_Vector<T>::const_iterator
end() const188 Swapping_Vector<T>::end() const {
189 return impl.end();
190 }
191
192 template <typename T>
193 inline typename Swapping_Vector<T>::iterator
erase(iterator itr)194 Swapping_Vector<T>::erase(iterator itr) {
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
erase(iterator first,iterator last)209 Swapping_Vector<T>::erase(iterator first, iterator last) {
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
max_num_rows()227 Swapping_Vector<T>::max_num_rows() {
228 return impl.max_size();
229 }
230
231 template <typename T>
232 inline void
swap(Swapping_Vector<T> & vec1,Swapping_Vector<T> & vec2)233 swap(Swapping_Vector<T>& vec1, Swapping_Vector<T>& vec2) {
234 vec1.m_swap(vec2);
235 }
236
237 } // namespace Parma_Polyhedra_Library
238
239
240 #endif // !defined(PPL_Swapping_Vector_inlines_hh)
241