]>
git.saurik.com Git - wxWidgets.git/blob - include/wx/vector.h
1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: STL vector clone
4 // Author: Lindsay Mathieson
5 // Modified by: Vaclav Slavik - make it a template
7 // Copyright: (c) 2001 Lindsay Mathieson <lindsay@mathieson.org>,
8 // 2007 Vaclav Slavik <vslavik@fastmail.fm>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
17 #if wxUSE_STD_CONTAINERS
22 #define wxVector std::vector
24 inline void wxVectorSort(wxVector
<T
>& v
)
26 std::sort(v
.begin(), v
.end());
29 #else // !wxUSE_STD_CONTAINERS
31 #include "wx/scopeguard.h"
32 #include "wx/meta/movable.h"
33 #include "wx/meta/if.h"
35 #include "wx/beforestd.h"
36 #include <new> // for placement new
37 #include "wx/afterstd.h"
39 // wxQsort is declared in wx/utils.h, but can't include that file here,
40 // it indirectly includes this file. Just lovely...
41 typedef int (*wxSortCallback
)(const void* pItem1
,
43 const void* user_data
);
44 WXDLLIMPEXP_BASE
void wxQsort(void* pbase
, size_t total_elems
,
45 size_t size
, wxSortCallback cmp
,
46 const void* user_data
);
51 // These templates encapsulate memory operations for use by wxVector; there are
52 // two implementations, both in generic way for any C++ types and as an
53 // optimized version for "movable" types that uses realloc() and memmove().
55 // version for movable types:
57 struct wxVectorMemOpsMovable
59 static void Free(T
* array
)
62 static T
* Realloc(T
* old
, size_t newCapacity
, size_t WXUNUSED(occupiedSize
))
63 { return (T
*)realloc(old
, newCapacity
* sizeof(T
)); }
65 static void MemmoveBackward(T
* dest
, T
* source
, size_t count
)
66 { memmove(dest
, source
, count
* sizeof(T
)); }
68 static void MemmoveForward(T
* dest
, T
* source
, size_t count
)
69 { memmove(dest
, source
, count
* sizeof(T
)); }
72 // generic version for non-movable types:
74 struct wxVectorMemOpsGeneric
76 static void Free(T
* array
)
77 { ::operator delete(array
); }
79 static T
* Realloc(T
* old
, size_t newCapacity
, size_t occupiedSize
)
81 T
*mem
= (T
*)::operator new(newCapacity
* sizeof(T
));
82 for ( size_t i
= 0; i
< occupiedSize
; i
++ )
84 ::new(mem
+ i
) T(old
[i
]);
87 ::operator delete(old
);
91 static void MemmoveBackward(T
* dest
, T
* source
, size_t count
)
93 wxASSERT( dest
< source
);
95 T
* sourceptr
= source
;
96 for ( size_t i
= count
; i
> 0; --i
, ++destptr
, ++sourceptr
)
98 ::new(destptr
) T(*sourceptr
);
103 static void MemmoveForward(T
* dest
, T
* source
, size_t count
)
105 wxASSERT( dest
> source
);
106 T
* destptr
= dest
+ count
- 1;
107 T
* sourceptr
= source
+ count
- 1;
108 for ( size_t i
= count
; i
> 0; --i
, --destptr
, --sourceptr
)
110 ::new(destptr
) T(*sourceptr
);
117 } // namespace wxPrivate
123 // This cryptic expression means "typedef Ops to wxVectorMemOpsMovable if
124 // type T is movable type, otherwise to wxVectorMemOpsGeneric".
126 // Note that we use typedef instead of privately deriving from this (which
127 // would allowed us to omit "Ops::" prefixes below) to keep VC6 happy,
128 // it can't compile code that derives from wxIf<...>::value.
130 // Note that bcc needs the extra parentheses for non-type template
131 // arguments to compile this expression.
132 typedef typename wxIf
< (wxIsMovable
<T
>::value
),
133 wxPrivate::wxVectorMemOpsMovable
<T
>,
134 wxPrivate::wxVectorMemOpsGeneric
<T
> >::value
138 typedef size_t size_type
;
139 typedef size_t difference_type
;
140 typedef T value_type
;
141 typedef value_type
* pointer
;
142 typedef value_type
* iterator
;
143 typedef const value_type
* const_iterator
;
144 typedef value_type
& reference
;
146 class reverse_iterator
149 reverse_iterator() : m_ptr(NULL
) { }
150 wxEXPLICIT
reverse_iterator(iterator it
) : m_ptr(it
) { }
151 reverse_iterator(const reverse_iterator
& it
) : m_ptr(it
.m_ptr
) { }
153 reference
operator*() const { return *m_ptr
; }
154 pointer
operator->() const { return m_ptr
; }
156 iterator
base() const { return m_ptr
; }
158 reverse_iterator
& operator++()
159 { --m_ptr
; return *this; }
160 reverse_iterator
operator++(int)
161 { reverse_iterator tmp
= *this; --m_ptr
; return tmp
; }
162 reverse_iterator
& operator--()
163 { ++m_ptr
; return *this; }
164 reverse_iterator
operator--(int)
165 { reverse_iterator tmp
= *this; ++m_ptr
; return tmp
; }
167 reverse_iterator
operator+(difference_type n
) const
168 { return reverse_iterator(m_ptr
- n
); }
169 reverse_iterator
& operator+=(difference_type n
)
170 { m_ptr
-= n
; return *this; }
171 reverse_iterator
operator-(difference_type n
) const
172 { return reverse_iterator(m_ptr
+ n
); }
173 reverse_iterator
& operator-=(difference_type n
)
174 { m_ptr
+= n
; return *this; }
176 reference
operator[](difference_type n
) const
177 { return *(*this + n
); }
179 bool operator ==(const reverse_iterator
& it
) const
180 { return m_ptr
== it
.m_ptr
; }
181 bool operator !=(const reverse_iterator
& it
) const
182 { return m_ptr
!= it
.m_ptr
; }
188 wxVector() : m_size(0), m_capacity(0), m_values(NULL
) {}
190 wxVector(size_type p_size
)
191 : m_size(0), m_capacity(0), m_values(NULL
)
194 for ( size_t n
= 0; n
< p_size
; n
++ )
195 push_back(value_type());
198 wxVector(size_type p_size
, const value_type
& v
)
199 : m_size(0), m_capacity(0), m_values(NULL
)
202 for ( size_t n
= 0; n
< p_size
; n
++ )
206 wxVector(const wxVector
& c
) : m_size(0), m_capacity(0), m_values(NULL
)
216 void assign(size_type p_size
, const value_type
& v
)
220 for ( size_t n
= 0; n
< p_size
; n
++ )
224 void swap(wxVector
& v
)
226 wxSwap(m_size
, v
.m_size
);
227 wxSwap(m_capacity
, v
.m_capacity
);
228 wxSwap(m_values
, v
.m_values
);
233 // call destructors of stored objects:
234 for ( size_type i
= 0; i
< m_size
; i
++ )
245 void reserve(size_type n
)
247 if ( n
<= m_capacity
)
250 // increase the size twice, unless we're already too big or unless
253 // NB: casts to size_type are needed to suppress warnings about
254 // mixing enumeral and non-enumeral type in conditional expression
255 const size_type increment
= m_size
> 0
256 ? m_size
< ALLOC_MAX_SIZE
258 : (size_type
)ALLOC_MAX_SIZE
259 : (size_type
)ALLOC_INITIAL_SIZE
;
260 if ( m_capacity
+ increment
> n
)
261 n
= m_capacity
+ increment
;
263 m_values
= Ops::Realloc(m_values
, n
* sizeof(value_type
), m_size
);
267 void resize(size_type n
)
271 else if ( n
> m_size
)
272 Extend(n
, value_type());
275 void resize(size_type n
, const value_type
& v
)
279 else if ( n
> m_size
)
283 size_type
size() const
288 size_type
capacity() const
298 wxVector
& operator=(const wxVector
& vb
)
308 void push_back(const value_type
& v
)
312 // use placement new to initialize new object in preallocated place in
313 // m_values and store 'v' in it:
314 void* const place
= m_values
+ m_size
;
315 ::new(place
) value_type(v
);
317 // only increase m_size if the ctor didn't throw an exception; notice
318 // that if it _did_ throw, everything is OK, because we only increased
319 // vector's capacity so far and possibly written some data to
320 // uninitialized memory at the end of m_values
329 const value_type
& at(size_type idx
) const
331 wxASSERT(idx
< m_size
);
332 return m_values
[idx
];
335 value_type
& at(size_type idx
)
337 wxASSERT(idx
< m_size
);
338 return m_values
[idx
];
341 const value_type
& operator[](size_type idx
) const { return at(idx
); }
342 value_type
& operator[](size_type idx
) { return at(idx
); }
343 const value_type
& front() const { return at(0); }
344 value_type
& front() { return at(0); }
345 const value_type
& back() const { return at(size() - 1); }
346 value_type
& back() { return at(size() - 1); }
348 const_iterator
begin() const { return m_values
; }
349 iterator
begin() { return m_values
; }
350 const_iterator
end() const { return m_values
+ size(); }
351 iterator
end() { return m_values
+ size(); }
353 reverse_iterator
rbegin() { return reverse_iterator(end() - 1); }
354 reverse_iterator
rend() { return reverse_iterator(begin() - 1); }
356 iterator
insert(iterator it
, const value_type
& v
= value_type())
358 // NB: this must be done before reserve(), because reserve()
359 // invalidates iterators!
360 const size_t idx
= it
- begin();
361 const size_t after
= end() - it
;
365 // the place where the new element is going to be inserted
366 value_type
* const place
= m_values
+ idx
;
368 // unless we're inserting at the end, move following elements out of
371 Ops::MemmoveForward(place
+ 1, place
, after
);
373 // if the ctor called below throws an exception, we need to move all
374 // the elements back to their original positions in m_values
375 wxScopeGuard moveBack
= wxMakeGuard(
376 Ops::MemmoveBackward
, place
, place
+ 1, after
);
380 // use placement new to initialize new object in preallocated place in
381 // m_values and store 'v' in it:
382 ::new(place
) value_type(v
);
384 // now that we did successfully add the new element, increment the size
385 // and disable moving the items back
389 return begin() + idx
;
392 iterator
erase(iterator it
)
394 return erase(it
, it
+ 1);
397 iterator
erase(iterator first
, iterator last
)
401 wxASSERT( first
< end() && last
<= end() );
403 const size_type idx
= first
- begin();
404 const size_type count
= last
- first
;
405 const size_type after
= end() - last
;
407 // erase elements by calling their destructors:
408 for ( iterator i
= first
; i
< last
; ++i
)
411 // once that's done, move following elements over to the freed space:
414 Ops::MemmoveBackward(m_values
+ idx
, m_values
+ idx
+ count
, after
);
419 return begin() + idx
;
422 #if WXWIN_COMPATIBILITY_2_8
423 wxDEPRECATED( size_type
erase(size_type n
) );
424 #endif // WXWIN_COMPATIBILITY_2_8
427 // VC6 can't compile static const int members
428 enum { ALLOC_INITIAL_SIZE
= 16 };
429 enum { ALLOC_MAX_SIZE
= 4096 };
431 void Copy(const wxVector
& vb
)
435 for ( const_iterator i
= vb
.begin(); i
!= vb
.end(); ++i
)
440 void Shrink(size_type n
)
442 for ( size_type i
= n
; i
< m_size
; i
++ )
447 void Extend(size_type n
, const value_type
& v
)
450 for ( size_type i
= m_size
; i
< n
; i
++ )
456 value_type
*m_values
;
459 #if WXWIN_COMPATIBILITY_2_8
461 inline typename wxVector
<T
>::size_type wxVector
<T
>::erase(size_type n
)
466 #endif // WXWIN_COMPATIBILITY_2_8
473 // This is a helper for the wxVectorSort function, and should not be used
474 // directly in user's code.
476 struct wxVectorComparator
479 Compare(const void* pitem1
, const void* pitem2
, const void* )
481 const T
& item1
= *reinterpret_cast<const T
*>(pitem1
);
482 const T
& item2
= *reinterpret_cast<const T
*>(pitem2
);
486 else if (item2
< item1
)
493 } // namespace wxPrivate
498 void wxVectorSort(wxVector
<T
>& v
)
500 wxQsort(v
.begin(), v
.size(), sizeof(T
),
501 wxPrivate::wxVectorComparator
<T
>::Compare
, NULL
);
506 #endif // wxUSE_STD_CONTAINERS/!wxUSE_STD_CONTAINERS
508 #if WXWIN_COMPATIBILITY_2_8
509 #define WX_DECLARE_VECTORBASE(obj, cls) typedef wxVector<obj> cls
510 #define _WX_DECLARE_VECTOR(obj, cls, exp) WX_DECLARE_VECTORBASE(obj, cls)
511 #define WX_DECLARE_VECTOR(obj, cls) WX_DECLARE_VECTORBASE(obj, cls)
512 #endif // WXWIN_COMPATIBILITY_2_8
514 #endif // _WX_VECTOR_H_