STXXL  1.4-dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
swap_vector.h
Go to the documentation of this file.
1 /***************************************************************************
2  * include/stxxl/bits/common/swap_vector.h
3  *
4  * Part of the STXXL. See http://stxxl.sourceforge.net
5  *
6  * Copyright (C) 2014 Thomas Keh <[email protected]>
7  *
8  * Distributed under the Boost Software License, Version 1.0.
9  * (See accompanying file LICENSE_1_0.txt or copy at
10  * http://www.boost.org/LICENSE_1_0.txt)
11  **************************************************************************/
12 
13 #ifndef STXXL_COMMON_SWAP_VECTOR_HEADER
14 #define STXXL_COMMON_SWAP_VECTOR_HEADER
15 
16 #include <algorithm>
17 #include <cassert>
18 #include <stxxl/bits/verbose.h>
19 #include <stxxl/bits/noncopyable.h>
20 
22 
23 //! \addtogroup support
24 //! \{
25 
26 /*!
27  * Vector that avoids copying of ValueType objects in push_back() (here:
28  * swap_back()) and resize() operations. Values are swapped with
29  * default-constructed instances instead. Important: A template spezialization
30  * for std::swap(ValueType&,ValueType&) must be provided. Make shure the swap
31  * implementation is located above these lines.
32  */
33 template <typename ValueType>
34 class swap_vector : private noncopyable
35 {
36 public:
37  typedef ValueType value_type;
38  typedef size_t size_type;
39 
40 protected:
41  //! size of vector
43 
44  //! size of allocated memory
46 
47  //! pointer to allocated memory area
49 
50 public:
51  // *** simple pointer iterators
52 
53  typedef value_type* iterator;
54  typedef const value_type* const_iterator;
56  typedef const value_type& const_reference;
57 
58 public:
59  //! Create an empty vector.
61  : m_size(0), m_capacity(0), m_array(NULL)
62  { }
63  //! Create a vector with the spezified size.
65  : m_size(size), m_capacity(size), m_array(NULL)
66  {
67  if (m_size > 0)
68  m_array = new value_type[m_size];
69  }
70  //! Create a vector with the spezified size and reserve (possibly more)
71  //! space.
73  : m_size(size), m_capacity(std::max(size, capacity)), m_array(NULL)
74  {
75  if (m_capacity > 0)
76  m_array = new value_type[m_capacity];
77  }
78  //! Swap the vector with another one.
79  void swap(swap_vector& obj)
80  {
81  using std::swap;
82  swap(m_size, obj.m_size);
83  swap(m_capacity, obj.m_capacity);
84  swap(m_array, obj.m_array);
85  }
86  //! Delete the vector.
88  {
89  delete[] m_array;
90  }
91  //! Return the vector size.
92  size_type size() const
93  {
94  return m_size;
95  }
96  //! Return the vector size.
97  bool empty() const
98  {
99  return (m_size == 0);
100  }
101  //! Return the size of the underlaying array.
103  {
104  return m_capacity;
105  }
106  //! Return iterator to the beginning of vector.
108  {
109  return m_array;
110  }
111  //! Return iterator to the beginning of vector.
113  {
114  return m_array;
115  }
116  //! Return mutable iterator to the first element.
118  {
119  return m_array;
120  }
121  //! Return constant iterator to the first element.
123  {
124  return m_array;
125  }
126  //! Return constant iterator to the first element.
128  {
129  return begin();
130  }
131  //! Return mutable iterator beyond the last element.
133  {
134  return m_array + m_size;
135  }
136  //! Return constant iterator beyond the last element.
138  {
139  return m_array + m_size;
140  }
141  //! Return constant iterator beyond the last element.
143  {
144  return end();
145  }
146  //! Return the i-th position of the vector.
147  reference operator [] (size_type i)
148  {
149  assert(i < m_size);
150  return *(begin() + i);
151  }
152  //! Return constant reference to the i-th position of the vector.
153  const_reference operator [] (size_type i) const
154  {
155  assert(i < m_size);
156  return *(begin() + i);
157  }
158  //! Return reference to first element.
160  {
161  assert(m_size > 0);
162  return *(m_array);
163  }
164  //! Return constant reference to first element.
166  {
167  assert(m_size > 0);
168  return *(m_array);
169  }
170  //! Return reference to last element.
172  {
173  assert(m_size > 0);
174  return *(m_array + m_size - 1);
175  }
176  //! Return reference to last element.
178  {
179  assert(m_size > 0);
180  return *(m_array + m_size - 1);
181  }
182  //! Resize the underlaying array to contain at least newsize items.
183  void reserve(size_type newsize)
184  {
185  if (newsize > m_capacity)
186  {
187  if (m_array)
188  {
189  value_type* tmp = m_array;
190  m_array = new value_type[newsize];
191  for (unsigned i = 0; i < m_size; ++i)
192  {
193  using std::swap;
194  swap(tmp[i], m_array[i]);
195  }
196  delete[] tmp;
197  }
198  else
199  {
200  m_array = new value_type[newsize];
201  }
202  m_capacity = newsize;
203  }
204  }
205  //! Resize the vector to contain at least newsize items.
206  void resize(size_type newsize)
207  {
208  reserve(newsize);
209  m_size = newsize;
210  }
211  //! Create a new value_type object at the end of the vector and then swap
212  //! it with val.
214  {
215  if (m_size + 1 > m_capacity)
216  {
217  reserve(std::max((size_type)2, 2 * m_capacity));
218  }
219  using std::swap;
220  swap(m_array[m_size], val);
221  ++m_size;
222  }
223  //! Clear the vector. The capacity doesn't change.
224  void clear()
225  {
226  m_size = 0;
227  }
228  //! Erase the element at the given position by swapping it to the and and
229  //! then reducing the vector size.
231  {
232  return erase(position, position + 1);
233  }
234  //! Erase the elements at in the range [begin,last) by swapping them to the
235  //! and and then reducing the vector size.
237  {
238  assert(first >= begin());
239  assert(last <= end());
240  if (last < m_array + m_size - 1)
241  {
242  // iteratively swap forward the elements behind last
243  iterator f = first;
244  iterator l = last;
245  while (l < end())
246  {
247  using std::swap;
248  swap(*f, *l);
249  ++f;
250  ++l;
251  }
252  }
253  m_size -= (last - first);
254  return first;
255  }
256 };
257 
258 /*!
259  * Transforms the range [first,last) into a range with all the elements for
260  * which pred returns true removed, and returns an iterator to the new end of
261  * that range.
262  *
263  * The function is compatible to std::remove_if, but uses std::swap instead of
264  * copy-assignment (resp. move-assignment in C++11).
265  */
266 template <class ForwardIterator, class UnaryPredicate>
267 ForwardIterator swap_remove_if(ForwardIterator first, ForwardIterator last, UnaryPredicate pred)
268 {
269  ForwardIterator result = first;
270  while (first != last)
271  {
272  if (!pred(*first))
273  {
274  using std::swap;
275  swap(*first, *result);
276  ++result;
277  }
278  ++first;
279  }
280  return result;
281 }
282 
283 // \}
284 
286 
287 namespace std {
288 
289 template <class ValueType>
290 void swap(stxxl::swap_vector<ValueType>& a,
292 {
293  a.swap(b);
294 }
295 
296 } // namespace std
297 
298 #endif // !STXXL_COMMON_SWAP_VECTOR_HEADER
void swap(swap_vector &obj)
Swap the vector with another one.
Definition: swap_vector.h:79
swap_vector()
Create an empty vector.
Definition: swap_vector.h:60
const value_type & const_reference
Definition: swap_vector.h:56
value_type * iterator
Definition: swap_vector.h:53
ValueType value_type
Definition: swap_vector.h:37
size_type m_capacity
size of allocated memory
Definition: swap_vector.h:45
iterator erase(iterator position)
Erase the element at the given position by swapping it to the and and then reducing the vector size...
Definition: swap_vector.h:230
value_type & reference
Definition: swap_vector.h:55
ForwardIterator swap_remove_if(ForwardIterator first, ForwardIterator last, UnaryPredicate pred)
Transforms the range [first,last) into a range with all the elements for which pred returns true remo...
Definition: swap_vector.h:267
swap_vector(size_type size)
Create a vector with the spezified size.
Definition: swap_vector.h:64
const_iterator end() const
Return constant iterator beyond the last element.
Definition: swap_vector.h:137
void swap_back(reference val)
Create a new value_type object at the end of the vector and then swap it with val.
Definition: swap_vector.h:213
reference front()
Return reference to first element.
Definition: swap_vector.h:159
value_type * m_array
pointer to allocated memory area
Definition: swap_vector.h:48
const_iterator cend() const
Return constant iterator beyond the last element.
Definition: swap_vector.h:142
const_iterator begin() const
Return constant iterator to the first element.
Definition: swap_vector.h:122
const value_type * const_iterator
Definition: swap_vector.h:54
void reserve(size_type newsize)
Resize the underlaying array to contain at least newsize items.
Definition: swap_vector.h:183
size_type capacity() const
Return the size of the underlaying array.
Definition: swap_vector.h:102
size_type size() const
Return the vector size.
Definition: swap_vector.h:92
void clear()
Clear the vector. The capacity doesn&#39;t change.
Definition: swap_vector.h:224
iterator data()
Return iterator to the beginning of vector.
Definition: swap_vector.h:107
size_type m_size
size of vector
Definition: swap_vector.h:42
bool empty() const
Return the vector size.
Definition: swap_vector.h:97
~swap_vector()
Delete the vector.
Definition: swap_vector.h:87
iterator end()
Return mutable iterator beyond the last element.
Definition: swap_vector.h:132
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
static uint_pair max()
return an uint_pair instance containing the largest value possible
Definition: uint_types.h:241
const_reference back() const
Return reference to last element.
Definition: swap_vector.h:177
const_iterator cbegin() const
Return constant iterator to the first element.
Definition: swap_vector.h:127
const_reference front() const
Return constant reference to first element.
Definition: swap_vector.h:165
swap_vector(size_type size, size_type capacity)
Create a vector with the spezified size and reserve (possibly more) space.
Definition: swap_vector.h:72
iterator begin()
Return mutable iterator to the first element.
Definition: swap_vector.h:117
Vector that avoids copying of ValueType objects in push_back() (here: swap_back()) and resize() opera...
Definition: swap_vector.h:34
iterator erase(iterator first, iterator last)
Erase the elements at in the range [begin,last) by swapping them to the and and then reducing the vec...
Definition: swap_vector.h:236
const_iterator data() const
Return iterator to the beginning of vector.
Definition: swap_vector.h:112
void resize(size_type newsize)
Resize the vector to contain at least newsize items.
Definition: swap_vector.h:206
reference back()
Return reference to last element.
Definition: swap_vector.h:171
#define STXXL_END_NAMESPACE
Definition: namespace.h:17