]> git.saurik.com Git - wxWidgets.git/blob - include/wx/vector.h
cleanup: indentation, line length, don't include unneeded headers
[wxWidgets.git] / include / wx / vector.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/vector.h
3 // Purpose: STL vector clone
4 // Author: Lindsay Mathieson
5 // Modified by: Vaclav Slavik - make it a template
6 // Created: 30.07.2001
7 // Copyright: (c) 2001 Lindsay Mathieson <lindsay@mathieson.org>,
8 // 2007 Vaclav Slavik <vslavik@fastmail.fm>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_VECTOR_H_
13 #define _WX_VECTOR_H_
14
15 #include "wx/defs.h"
16
17 #if 0 // wxUSE_STL
18
19 // FIXME: can't do this yet, wxVector::erase() is different (takes index,
20 // not iterator)
21 #include <vector>
22 #define wxVector std::vector
23
24 #else // !wxUSE_STL
25
26 template<typename T>
27 class wxVector
28 {
29 public:
30 typedef size_t size_type;
31 typedef T value_type;
32
33 wxVector() : m_allocsize(16), m_size(0), m_capacity(0), m_objects(0) {}
34
35 wxVector(const wxVector& c)
36 {
37 wxCHECK2(Copy(c), return);
38 }
39
40 ~wxVector()
41 {
42 clear();
43 }
44
45 void clear()
46 {
47 for (size_type i = 0; i < size(); i++)
48 delete m_objects[i];
49 free(m_objects);
50 m_objects = 0;
51 m_size = m_capacity = 0;
52 }
53
54 void reserve(size_type n)
55 {
56 if ( !Alloc(n) )
57 {
58 wxFAIL_MSG( _T("out of memory in wxVector::reserve()") );
59 }
60 }
61
62 size_type size() const
63 {
64 return m_size;
65 }
66
67 size_type capacity() const
68 {
69 return m_capacity;
70 }
71
72 bool empty() const
73 {
74 return size() == 0;
75 }
76
77 wxVector& operator=(const wxVector& vb)
78 {
79 wxCHECK(Copy(vb), *this);
80 return *this;
81 }
82
83 void push_back(const value_type& o)
84 {
85 wxCHECK2(Alloc(size() + 1), return);
86 Append(new value_type(o));
87 }
88
89 void pop_back()
90 {
91 RemoveAt(size() - 1);
92 }
93
94 const value_type& at(size_type idx) const
95 {
96 wxASSERT(idx < m_size);
97 return *m_objects[idx];
98 }
99
100 value_type& at(size_type idx)
101 {
102 wxASSERT(idx < m_size);
103 return *m_objects[idx];
104 }
105
106 const value_type& operator[](size_type idx) const { return at(idx); }
107 value_type& operator[](size_type idx) { return at(idx); }
108 const value_type& front() const { return at(0); }
109 value_type& front() { return at(0); }
110 const value_type& back() const { return at(size() - 1); }
111 value_type& back() { return at(size() - 1); }
112
113 size_type erase(size_type idx)
114 {
115 RemoveAt(idx);
116 return idx;
117 }
118
119 private:
120 bool Alloc(size_type sz)
121 {
122 // work in multiples of m_allocsize;
123 sz = (sz / m_allocsize + 1) * m_allocsize;
124 if (sz <= m_capacity)
125 return true;
126
127 // try to realloc
128 void *mem = realloc(m_objects, sizeof(value_type*) * sz);
129 if (! mem)
130 return false; // failed
131 // success
132 m_objects = (value_type **) mem;
133 m_capacity = sz;
134 return true;
135 }
136
137 void Append(value_type *obj)
138 {
139 wxASSERT(m_size < m_capacity);
140 m_objects[m_size] = obj;
141 m_size++;
142 }
143
144 void RemoveAt(size_type idx)
145 {
146 wxASSERT(idx < m_size);
147 delete m_objects[idx];
148 if (idx < m_size - 1)
149 memcpy(
150 m_objects + idx,
151 m_objects + idx + 1,
152 ( m_size - idx - 1 ) * sizeof(void*) );
153 m_size--;
154 }
155
156 bool Copy(const wxVector& vb)
157 {
158 clear();
159 if (! Alloc(vb.size()))
160 return false;
161
162 for (size_type i = 0; i < vb.size(); i++)
163 {
164 value_type *o = new value_type(vb.at(i));
165 if (! o)
166 return false;
167 Append(o);
168 }
169
170 return true;
171 }
172
173 private:
174 size_type m_allocsize;
175 size_type m_size,
176 m_capacity;
177 value_type **m_objects;
178 };
179
180 #endif // wxUSE_STL/!wxUSE_STL
181
182 #if WXWIN_COMPATIBILITY_2_8
183 #define WX_DECLARE_VECTORBASE(obj, cls) typedef wxVector<obj> cls
184 #define _WX_DECLARE_VECTOR(obj, cls, exp) WX_DECLARE_VECTORBASE(obj, cls)
185 #define WX_DECLARE_VECTOR(obj, cls) WX_DECLARE_VECTORBASE(obj, cls)
186 #endif // WXWIN_COMPATIBILITY_2_8
187
188 #endif // _WX_VECTOR_H_