1 |
786 |
skrzyp |
// This file is part of the uSTL library, an STL implementation.
|
2 |
|
|
//
|
3 |
|
|
// Copyright (c) 2005-2009 by Mike Sharov <msharov@users.sourceforge.net>
|
4 |
|
|
// This file is free software, distributed under the MIT License.
|
5 |
|
|
|
6 |
|
|
#ifndef UVECTOR_H_00BB13AF082BEB7829C031B265518169
|
7 |
|
|
#define UVECTOR_H_00BB13AF082BEB7829C031B265518169
|
8 |
|
|
|
9 |
|
|
#include "memblock.h"
|
10 |
|
|
#include "umemory.h"
|
11 |
|
|
|
12 |
|
|
namespace ustl {
|
13 |
|
|
|
14 |
|
|
/// \class vector uvector.h ustl.h
|
15 |
|
|
/// \ingroup Sequences
|
16 |
|
|
///
|
17 |
|
|
/// \brief STL vector equivalent.
|
18 |
|
|
///
|
19 |
|
|
/// Provides a typed array-like interface to a managed memory block, including
|
20 |
|
|
/// element access, iteration, modification, resizing, and serialization. In
|
21 |
|
|
/// this design elements frequently undergo bitwise move, so don't put it in
|
22 |
|
|
/// here if it doesn't support it. This mostly means having no self-pointers.
|
23 |
|
|
///
|
24 |
|
|
template <typename T>
|
25 |
|
|
class vector {
|
26 |
|
|
public:
|
27 |
|
|
typedef T value_type;
|
28 |
|
|
typedef value_type* pointer;
|
29 |
|
|
typedef const value_type* const_pointer;
|
30 |
|
|
typedef value_type& reference;
|
31 |
|
|
typedef const value_type& const_reference;
|
32 |
|
|
typedef pointer iterator;
|
33 |
|
|
typedef const_pointer const_iterator;
|
34 |
|
|
typedef memblock::size_type size_type;
|
35 |
|
|
typedef memblock::written_size_type written_size_type;
|
36 |
|
|
typedef memblock::difference_type difference_type;
|
37 |
|
|
typedef ::ustl::reverse_iterator<iterator> reverse_iterator;
|
38 |
|
|
typedef ::ustl::reverse_iterator<const_iterator> const_reverse_iterator;
|
39 |
|
|
public:
|
40 |
|
|
inline vector (void);
|
41 |
|
|
inline explicit vector (size_type n);
|
42 |
|
|
vector (size_type n, const T& v);
|
43 |
|
|
vector (const vector<T>& v);
|
44 |
|
|
vector (const_iterator i1, const_iterator i2);
|
45 |
|
|
inline ~vector (void) throw();
|
46 |
|
|
inline const vector<T>& operator= (const vector<T>& v);
|
47 |
|
|
inline bool operator== (const vector<T>& v) const { return (m_Data == v.m_Data); }
|
48 |
|
|
inline operator cmemlink (void) const { return (cmemlink (m_Data)); }
|
49 |
|
|
inline operator cmemlink (void) { return (cmemlink (m_Data)); }
|
50 |
|
|
inline operator memlink (void) { return (memlink (m_Data)); }
|
51 |
|
|
inline void reserve (size_type n, bool bExact = true);
|
52 |
|
|
inline void resize (size_type n, bool bExact = true);
|
53 |
|
|
inline size_type capacity (void) const { return (m_Data.capacity() / sizeof(T)); }
|
54 |
|
|
inline size_type size (void) const { return (m_Data.size() / sizeof(T)); }
|
55 |
|
|
inline size_type max_size (void) const { return (m_Data.max_size() / sizeof(T)); }
|
56 |
|
|
inline bool empty (void) const { return (m_Data.empty()); }
|
57 |
|
|
inline iterator begin (void) { return (iterator (m_Data.begin())); }
|
58 |
|
|
inline const_iterator begin (void) const { return (const_iterator (m_Data.begin())); }
|
59 |
|
|
inline iterator end (void) { return (iterator (m_Data.end())); }
|
60 |
|
|
inline const_iterator end (void) const { return (const_iterator (m_Data.end())); }
|
61 |
|
|
inline reverse_iterator rbegin (void) { return (reverse_iterator (end())); }
|
62 |
|
|
inline const_reverse_iterator rbegin (void) const { return (const_reverse_iterator (end())); }
|
63 |
|
|
inline reverse_iterator rend (void) { return (reverse_iterator (begin())); }
|
64 |
|
|
inline const_reverse_iterator rend (void) const { return (const_reverse_iterator (begin())); }
|
65 |
|
|
inline iterator iat (size_type i) { assert (i <= size()); return (begin() + i); }
|
66 |
|
|
inline const_iterator iat (size_type i) const { assert (i <= size()); return (begin() + i); }
|
67 |
|
|
inline reference at (size_type i) { assert (i < size()); return (begin()[i]); }
|
68 |
|
|
inline const_reference at (size_type i) const { assert (i < size()); return (begin()[i]); }
|
69 |
|
|
inline reference operator[] (size_type i) { return (at (i)); }
|
70 |
|
|
inline const_reference operator[] (size_type i) const { return (at (i)); }
|
71 |
|
|
inline reference front (void) { return (at(0)); }
|
72 |
|
|
inline const_reference front (void) const { return (at(0)); }
|
73 |
|
|
inline reference back (void) { assert (!empty()); return (end()[-1]); }
|
74 |
|
|
inline const_reference back (void) const { assert (!empty()); return (end()[-1]); }
|
75 |
|
|
inline void push_back (const T& v = T());
|
76 |
|
|
inline void pop_back (void) { m_Data.memlink::resize (m_Data.size() - sizeof(T)); }
|
77 |
|
|
inline void clear (void) { m_Data.clear(); }
|
78 |
|
|
inline void deallocate (void) throw();
|
79 |
|
|
inline void assign (const_iterator i1, const_iterator i2);
|
80 |
|
|
inline void assign (size_type n, const T& v);
|
81 |
|
|
inline void swap (vector<T>& v) { m_Data.swap (v.m_Data); }
|
82 |
|
|
inline iterator insert (iterator ip, const T& v = T());
|
83 |
|
|
inline iterator insert (iterator ip, size_type n, const T& v);
|
84 |
|
|
inline iterator insert (iterator ip, const_iterator i1, const_iterator i2);
|
85 |
|
|
inline iterator erase (iterator ep, size_type n = 1);
|
86 |
|
|
inline iterator erase (iterator ep1, iterator ep2);
|
87 |
|
|
inline void manage (pointer p, size_type n) { m_Data.manage (p, n * sizeof(T)); }
|
88 |
|
|
inline bool is_linked (void) const { return (m_Data.is_linked()); }
|
89 |
|
|
inline void unlink (void) { m_Data.unlink(); }
|
90 |
|
|
inline void copy_link (void) { m_Data.copy_link(); }
|
91 |
|
|
inline void link (const_pointer p, size_type n) { m_Data.link (p, n * sizeof(T)); }
|
92 |
|
|
inline void link (pointer p, size_type n) { m_Data.link (p, n * sizeof(T)); }
|
93 |
|
|
inline void link (const vector<T>& v) { m_Data.link (v); }
|
94 |
|
|
inline void link (vector<T>& v) { m_Data.link (v); }
|
95 |
|
|
inline void link (const_pointer first, const_pointer last) { m_Data.link (first, last); }
|
96 |
|
|
inline void link (pointer first, pointer last) { m_Data.link (first, last); }
|
97 |
|
|
inline void read (istream& is) { container_read (is, *this); }
|
98 |
|
|
inline void write (ostream& os) const { container_write (os, *this); }
|
99 |
|
|
inline void text_write (ostringstream& os) const { container_text_write (os, *this); }
|
100 |
|
|
inline size_t stream_size (void) const { return (container_stream_size (*this)); }
|
101 |
|
|
protected:
|
102 |
|
|
inline iterator insert_space (iterator ip, size_type n);
|
103 |
|
|
private:
|
104 |
|
|
memblock m_Data; ///< Raw element data, consecutively stored.
|
105 |
|
|
};
|
106 |
|
|
|
107 |
|
|
/// Allocates space for at least \p n elements.
|
108 |
|
|
template <typename T>
|
109 |
|
|
inline void vector<T>::reserve (size_type n, bool bExact)
|
110 |
|
|
{
|
111 |
|
|
const size_type oldCapacity = m_Data.capacity() - m_Data.capacity() % sizeof(T);
|
112 |
|
|
m_Data.reserve (n * sizeof(T), bExact);
|
113 |
|
|
construct (iterator (m_Data.begin() + oldCapacity), iterator (m_Data.begin() + m_Data.capacity()));
|
114 |
|
|
}
|
115 |
|
|
|
116 |
|
|
/// Resizes the vector to contain \p n elements.
|
117 |
|
|
template <typename T>
|
118 |
|
|
inline void vector<T>::resize (size_type n, bool bExact)
|
119 |
|
|
{
|
120 |
|
|
const size_type nb = n * sizeof(T);
|
121 |
|
|
if (m_Data.capacity() < nb)
|
122 |
|
|
reserve (n, bExact);
|
123 |
|
|
m_Data.memlink::resize (nb);
|
124 |
|
|
}
|
125 |
|
|
|
126 |
|
|
/// Calls element destructors and frees storage.
|
127 |
|
|
template <typename T>
|
128 |
|
|
inline void vector<T>::deallocate (void) throw()
|
129 |
|
|
{
|
130 |
|
|
destroy (begin(), iterator (m_Data.begin() + m_Data.capacity()));
|
131 |
|
|
m_Data.deallocate();
|
132 |
|
|
}
|
133 |
|
|
|
134 |
|
|
/// Initializes empty vector.
|
135 |
|
|
template <typename T>
|
136 |
|
|
inline vector<T>::vector (void)
|
137 |
|
|
: m_Data ()
|
138 |
|
|
{
|
139 |
|
|
}
|
140 |
|
|
|
141 |
|
|
/// Initializes a vector of size \p n.
|
142 |
|
|
template <typename T>
|
143 |
|
|
inline vector<T>::vector (size_type n)
|
144 |
|
|
: m_Data ()
|
145 |
|
|
{
|
146 |
|
|
resize (n);
|
147 |
|
|
}
|
148 |
|
|
|
149 |
|
|
/// Copies \p n elements from \p v.
|
150 |
|
|
template <typename T>
|
151 |
|
|
vector<T>::vector (size_type n, const T& v)
|
152 |
|
|
: m_Data ()
|
153 |
|
|
{
|
154 |
|
|
resize (n);
|
155 |
|
|
::ustl::fill (begin(), end(), v);
|
156 |
|
|
}
|
157 |
|
|
|
158 |
|
|
/// Copies \p v.
|
159 |
|
|
template <typename T>
|
160 |
|
|
vector<T>::vector (const vector<T>& v)
|
161 |
|
|
: m_Data ()
|
162 |
|
|
{
|
163 |
|
|
resize (v.size());
|
164 |
|
|
::ustl::copy (v.begin(), v.end(), begin());
|
165 |
|
|
}
|
166 |
|
|
|
167 |
|
|
/// Copies range [\p i1, \p i2]
|
168 |
|
|
template <typename T>
|
169 |
|
|
vector<T>::vector (const_iterator i1, const_iterator i2)
|
170 |
|
|
: m_Data ()
|
171 |
|
|
{
|
172 |
|
|
resize (distance (i1, i2));
|
173 |
|
|
::ustl::copy (i1, i2, begin());
|
174 |
|
|
}
|
175 |
|
|
|
176 |
|
|
/// Destructor
|
177 |
|
|
template <typename T>
|
178 |
|
|
inline vector<T>::~vector (void) throw()
|
179 |
|
|
{
|
180 |
|
|
destroy (begin(), iterator (m_Data.begin() + m_Data.capacity()));
|
181 |
|
|
}
|
182 |
|
|
|
183 |
|
|
/// Copies the range [\p i1, \p i2]
|
184 |
|
|
template <typename T>
|
185 |
|
|
inline void vector<T>::assign (const_iterator i1, const_iterator i2)
|
186 |
|
|
{
|
187 |
|
|
assert (i1 <= i2);
|
188 |
|
|
resize (distance (i1, i2));
|
189 |
|
|
::ustl::copy (i1, i2, begin());
|
190 |
|
|
}
|
191 |
|
|
|
192 |
|
|
/// Copies \p n elements with value \p v.
|
193 |
|
|
template <typename T>
|
194 |
|
|
inline void vector<T>::assign (size_type n, const T& v)
|
195 |
|
|
{
|
196 |
|
|
resize (n);
|
197 |
|
|
::ustl::fill (begin(), end(), v);
|
198 |
|
|
}
|
199 |
|
|
|
200 |
|
|
/// Copies contents of \p v.
|
201 |
|
|
template <typename T>
|
202 |
|
|
inline const vector<T>& vector<T>::operator= (const vector<T>& v)
|
203 |
|
|
{
|
204 |
|
|
assign (v.begin(), v.end());
|
205 |
|
|
return (*this);
|
206 |
|
|
}
|
207 |
|
|
|
208 |
|
|
/// Inserts \p n uninitialized elements at \p ip.
|
209 |
|
|
template <typename T>
|
210 |
|
|
inline typename vector<T>::iterator vector<T>::insert_space (iterator ip, size_type n)
|
211 |
|
|
{
|
212 |
|
|
const uoff_t ipmi = distance (m_Data.begin(), memblock::iterator(ip));
|
213 |
|
|
reserve (size() + n, false);
|
214 |
|
|
return (iterator (m_Data.insert (m_Data.iat(ipmi), n * sizeof(T))));
|
215 |
|
|
}
|
216 |
|
|
|
217 |
|
|
/// Inserts \p n elements with value \p v at offsets \p ip.
|
218 |
|
|
template <typename T>
|
219 |
|
|
inline typename vector<T>::iterator vector<T>::insert (iterator ip, size_type n, const T& v)
|
220 |
|
|
{
|
221 |
|
|
ip = insert_space (ip, n);
|
222 |
|
|
::ustl::fill (ip, ip + n, v);
|
223 |
|
|
return (ip);
|
224 |
|
|
}
|
225 |
|
|
|
226 |
|
|
/// Inserts value \p v at offset \p ip.
|
227 |
|
|
template <typename T>
|
228 |
|
|
inline typename vector<T>::iterator vector<T>::insert (iterator ip, const T& v)
|
229 |
|
|
{
|
230 |
|
|
*(ip = insert_space (ip, 1)) = v;
|
231 |
|
|
return (ip);
|
232 |
|
|
}
|
233 |
|
|
|
234 |
|
|
/// Inserts range [\p i1, \p i2] at offset \p ip.
|
235 |
|
|
template <typename T>
|
236 |
|
|
inline typename vector<T>::iterator vector<T>::insert (iterator ip, const_iterator i1, const_iterator i2)
|
237 |
|
|
{
|
238 |
|
|
assert (i1 <= i2);
|
239 |
|
|
ip = insert_space (ip, distance (i1, i2));
|
240 |
|
|
::ustl::copy (i1, i2, ip);
|
241 |
|
|
return (ip);
|
242 |
|
|
}
|
243 |
|
|
|
244 |
|
|
/// Removes \p count elements at offset \p ep.
|
245 |
|
|
template <typename T>
|
246 |
|
|
inline typename vector<T>::iterator vector<T>::erase (iterator ep, size_type n)
|
247 |
|
|
{
|
248 |
|
|
return (iterator (m_Data.erase (memblock::iterator(ep), n * sizeof(T))));
|
249 |
|
|
}
|
250 |
|
|
|
251 |
|
|
/// Removes elements from \p ep1 to \p ep2.
|
252 |
|
|
template <typename T>
|
253 |
|
|
inline typename vector<T>::iterator vector<T>::erase (iterator ep1, iterator ep2)
|
254 |
|
|
{
|
255 |
|
|
assert (ep1 <= ep2);
|
256 |
|
|
return (erase (ep1, distance(ep1, ep2)));
|
257 |
|
|
}
|
258 |
|
|
|
259 |
|
|
/// Inserts value \p v at the end of the vector.
|
260 |
|
|
template <typename T>
|
261 |
|
|
inline void vector<T>::push_back (const T& v)
|
262 |
|
|
{
|
263 |
|
|
resize (size() + 1, false);
|
264 |
|
|
back() = v;
|
265 |
|
|
}
|
266 |
|
|
|
267 |
|
|
/// Use with vector classes to allocate and link to stack space. \p n is in elements.
|
268 |
|
|
#define typed_alloca_link(m,T,n) (m).link ((T*) alloca ((n) * sizeof(T)), (n))
|
269 |
|
|
|
270 |
|
|
} // namespace ustl
|
271 |
|
|
|
272 |
|
|
#endif
|