[go: up one dir, main page]

Menu

[r135]: / lpi_pvector.h  Maximize  Restore  History

Download this file

253 lines (201 with data), 7.5 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
Copyright (c) 2005-2008 Lode Vandevenne
All rights reserved.
This file is part of Lode's Programming Interface.
Lode's Programming Interface is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Lode's Programming Interface is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Lode's Programming Interface. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LPI_PVECTOR_H_INCLUDED
#define LPI_PVECTOR_H_INCLUDED
#include <vector>
/*
lpi::pvector is a class similar to std::vector with one difference:
Whenever an element is removed from the pvector (including at the pvector's destruction),
the delete operator is called on it.
So a pvector can only contain pointers that were allocated with new, and it deletes them automatically.
This is handy for arrays of polymorphic objects.
Due to the deleting, some functionality of the regular std::vector had to be left out:
-operations that duplicate pointers to another vector, such as operator=
-operations that create multiple times the same pointer in 1 vector, such as assign, resize
-operations that allow the user to modify the pointer without the vector's knowledge. For this are disabled:
any non-const iterator or reference, the non-const operator[] and at()
-erase and insert still work, but with const_iterators instead of iterators
The following operations of pvector will result in calling delete on elements:
-clear
-destructor
-erase
-pop_back
*/
namespace lpi
{
template<typename T, typename A = std::allocator<T> >
class pvector
{
protected:
typedef std::vector<T, A> vector_type;
std::vector<T, A> v;
void deleteAll()
{
for(size_type i = 0; i < v.size(); i++) delete v[i];
}
typedef typename vector_type::iterator iterator;
public:
typedef T value_type;
typedef A allocator_type;
typedef typename vector_type::const_pointer const_pointer;
typedef typename vector_type::const_reference const_reference;
typedef typename vector_type::const_iterator const_iterator;
typedef typename vector_type::const_reverse_iterator const_reverse_iterator;
typedef typename vector_type::size_type size_type;
typedef typename vector_type::difference_type difference_type;
public:
explicit pvector(const A& a = A()) : v(a) {}
~pvector()
{
deleteAll();
}
const_iterator begin() const { return v.begin(); }
const_iterator end() const { return v.end(); }
const_reverse_iterator rbegin() const { return v.rbegin(); }
const_reverse_iterator rend() const { return v.rend(); }
size_type size() const { return v.size(); }
size_type max_size() const { return v.max_size(); }
bool empty() const { return v.empty(); }
size_type capacity() const { return v.capacity(); }
void reserve(size_type n) { v.reserve(n); }
const_reference operator[](size_type n) const { return v[n]; }
const_reference at(size_type n) const { return v.at(n); }
const_reference front() const { return v.front(); }
const_reference back() const { return v.back(); }
void push_back(const T& x) { v.push_back(x); }
void pop_back()
{
delete v.back();
v.pop_back();
}
const_iterator insert(const_iterator position, const T& x)
{
iterator it = v.begin() + (position - v.begin());
it = v.insert(it, x);
return v.begin() + (it - v.begin());
}
const_iterator erase(const_iterator position)
{
delete *position;
iterator it = v.begin() + (position - v.begin());
it = v.erase(it);
return v.begin() + (it - v.begin());
}
const_iterator erase(const_iterator first, const_iterator last)
{
for(const_iterator i = first; i != last; ++i) delete *i;
iterator it_first = v.begin() + (first - v.begin());
iterator it_last = v.begin() + (last - v.begin());
it_last = v.erase(it_first, it_last);
return v.begin() + (it_last - v.begin());
}
void swap(pvector& x)
{
v.swap(x.v);
}
void clear()
{
deleteAll();
v.clear();
}
/*
Public members of std::vector that may not be in pvector due to allowing duplicating a pointer or changing a pointer without delete
typedef typename vector_type::pointer pointer;
typedef typename vector_type::reference reference;
typedef typename vector_type::iterator iterator;
typedef typename vector_type::reverse_iterator reverse_iterator;
explicit pvector(size_type n, const T& value = T(), const A& a = A()) : v(n, value, a)
pvector(const pvector& x) : v(x.v)
template<typename _InputIterator> vector(_InputIterator first, _InputIterator last, const A& a = A()) : v(first, last, a)
pvector& operator=(const pvector& x); //disallowed for pvector
void assign(size_type n, const T& val)
iterator begin() { return v.begin(); }
iterator end() { return v.end(); }
reverse_iterator rbegin() { return v.rbegin(); }
reverse_iterator rend() { return v.rend(); }
void resize(size_type new_size, T x = T())
reference operator[](size_type n)
reference at(size_type n)
reference front() { return *begin(); }
reference back() { return *(end() - 1); }
iterator insert(iterator position, const T& x);
void insert(iterator position, size_type n, const T& x)
template<typename _InputIterator> void insert(iterator position, _InputIterator first, _InputIterator last)
iterator erase(iterator position);
iterator erase(iterator first, iterator last);
*/
};
template<typename T, typename A>
inline bool operator==(const pvector<T, A>& x, const pvector<T, A>& y)
{
return x.v == y.v;
}
template<typename T, typename A>
inline bool operator<(const pvector<T, A>& x, const pvector<T, A>& y)
{
return x.v < y.v;
}
template<typename T, typename A>
inline bool operator!=(const pvector<T, A>& x, const pvector<T, A>& y)
{
return x.v != y.v;
}
template<typename T, typename A>
inline bool operator>(const pvector<T, A>& x, const pvector<T, A>& y)
{
return x.v > y.v;
}
template<typename T, typename A>
inline bool operator<=(const pvector<T, A>& x, const pvector<T, A>& y)
{
return x.v <= y.v;
}
template<typename T, typename A>
inline bool operator>=(const pvector<T, A>& x, const pvector<T, A>& y)
{
return x.v >= y.v;
}
template<typename T, typename A>
inline void swap(pvector<T, A>& x, pvector<T, A>& y)
{
return x.v.swap(y.v);
}
////////////////////////////////////////////////////////////////////////////////
/*
VectorGuard is a class that will delete all elements of a vector in its destructor.
This makes it easier to use a regular std::vector where you want all its elements
to be deleted when it goes out of scope.
*/
template<typename T>
class VectorGuard
{
private:
T& v; //the std::vector
public:
VectorGuard(T& v)
: v(v)
{
}
~VectorGuard()
{
for(size_t i = 0; i < v.size(); i++)
{
delete v[i];
}
}
};
} //namespace lpi
#endif