Added compatibility functions for wxUSE_STL = 1:
[wxWidgets.git] / include / wx / arrstr.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: include/wx/arrstr.h
3 // Purpose: wxArrayString class
4 // Author: Mattia Barbon and Vadim Zeitlin
5 // Modified by:
6 // Created: 07/07/03
7 // RCS-ID: $Id$
8 // Copyright: (c) 2003 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_ARRSTR_H
13 #define _WX_ARRSTR_H
14
15 #include "wx/defs.h"
16 #include "wx/string.h"
17
18 WXDLLIMPEXP_BASE int wxCMPFUNC_CONV wxStringSortAscending(wxString*, wxString*);
19 WXDLLIMPEXP_BASE int wxCMPFUNC_CONV wxStringSortDescending(wxString*, wxString*);
20
21 #if wxUSE_STL
22
23 #include "wx/dynarray.h"
24
25 typedef int (wxCMPFUNC_CONV *CMPFUNCwxString)(wxString*, wxString*);
26 typedef wxString _wxArraywxBaseArrayStringBase;
27 _WX_DECLARE_BASEARRAY_2(_wxArraywxBaseArrayStringBase, wxBaseArrayStringBase,
28 wxArray_SortFunction<wxString>,
29 class WXDLLIMPEXP_BASE);
30 WX_DEFINE_USER_EXPORTED_TYPEARRAY(wxString, wxArrayStringBase,
31 wxBaseArrayStringBase, WXDLLIMPEXP_BASE);
32 _WX_DEFINE_SORTED_TYPEARRAY_2(wxString, wxSortedArrayStringBase,
33 wxBaseArrayStringBase, = wxStringSortAscending,
34 class WXDLLIMPEXP_BASE, CMPFUNCwxString);
35
36 class WXDLLIMPEXP_BASE wxArrayString : public wxArrayStringBase
37 {
38 public:
39 // type of function used by wxArrayString::Sort()
40 typedef int (wxCMPFUNC_CONV *CompareFunction)(const wxString& first,
41 const wxString& second);
42
43 wxArrayString() { }
44 wxArrayString(const wxArrayString& a) : wxArrayStringBase(a) { }
45
46 int Index(const wxChar* sz, bool bCase = true, bool bFromEnd = false) const;
47
48 void Sort(bool reverseOrder = false);
49 void Sort(CompareFunction function);
50 };
51
52 class WXDLLIMPEXP_BASE wxSortedArrayString : public wxSortedArrayStringBase
53 {
54 public:
55 wxSortedArrayString() : wxSortedArrayStringBase(wxStringSortAscending)
56 { }
57 wxSortedArrayString(const wxSortedArrayString& array)
58 : wxSortedArrayStringBase(array)
59 { }
60 wxSortedArrayString(const wxArrayString& src)
61 : wxSortedArrayStringBase(wxStringSortAscending)
62 {
63 reserve(src.size());
64
65 for ( size_t n = 0; n < src.size(); n++ )
66 Add(src[n]);
67 }
68
69 int Index(const wxChar* sz, bool bCase = true, bool bFromEnd = false) const;
70 };
71
72 #else // if !wxUSE_STL
73
74 // ----------------------------------------------------------------------------
75 // The string array uses it's knowledge of internal structure of the wxString
76 // class to optimize string storage. Normally, we would store pointers to
77 // string, but as wxString is, in fact, itself a pointer (sizeof(wxString) is
78 // sizeof(char *)) we store these pointers instead. The cast to "wxString *" is
79 // really all we need to turn such pointer into a string!
80 //
81 // Of course, it can be called a dirty hack, but we use twice less memory and
82 // this approach is also more speed efficient, so it's probably worth it.
83 //
84 // Usage notes: when a string is added/inserted, a new copy of it is created,
85 // so the original string may be safely deleted. When a string is retrieved
86 // from the array (operator[] or Item() method), a reference is returned.
87 // ----------------------------------------------------------------------------
88
89 class WXDLLIMPEXP_BASE wxArrayString
90 {
91 public:
92 // type of function used by wxArrayString::Sort()
93 typedef int (wxCMPFUNC_CONV *CompareFunction)(const wxString& first,
94 const wxString& second);
95 // type of function used by wxArrayString::Sort(), for compatibility with
96 // wxArray
97 typedef int (wxCMPFUNC_CONV *CompareFunction2)(wxString* first,
98 wxString* second);
99
100 // constructors and destructor
101 // default ctor
102 wxArrayString() { Init(false); }
103 // if autoSort is true, the array is always sorted (in alphabetical order)
104 //
105 // NB: the reason for using int and not bool is that like this we can avoid
106 // using this ctor for implicit conversions from "const char *" (which
107 // we'd like to be implicitly converted to wxString instead!)
108 //
109 // of course, using explicit would be even better - if all compilers
110 // supported it...
111 wxArrayString(int autoSort) { Init(autoSort != 0); }
112 // copy ctor
113 wxArrayString(const wxArrayString& array);
114 // assignment operator
115 wxArrayString& operator=(const wxArrayString& src);
116 // not virtual, this class should not be derived from
117 ~wxArrayString();
118
119 // memory management
120 // empties the list, but doesn't release memory
121 void Empty();
122 // empties the list and releases memory
123 void Clear();
124 // preallocates memory for given number of items
125 void Alloc(size_t nCount);
126 // minimzes the memory usage (by freeing all extra memory)
127 void Shrink();
128
129 // simple accessors
130 // number of elements in the array
131 size_t GetCount() const { return m_nCount; }
132 // is it empty?
133 bool IsEmpty() const { return m_nCount == 0; }
134 // number of elements in the array (GetCount is preferred API)
135 size_t Count() const { return m_nCount; }
136
137 // items access (range checking is done in debug version)
138 // get item at position uiIndex
139 wxString& Item(size_t nIndex) const
140 {
141 wxASSERT_MSG( nIndex < m_nCount,
142 _T("wxArrayString: index out of bounds") );
143
144 return *(wxString *)&(m_pItems[nIndex]);
145 }
146
147 // same as Item()
148 wxString& operator[](size_t nIndex) const { return Item(nIndex); }
149 // get last item
150 wxString& Last() const
151 {
152 wxASSERT_MSG( !IsEmpty(),
153 _T("wxArrayString: index out of bounds") );
154 return Item(Count() - 1);
155 }
156
157 // return a wxString[], useful for the controls which
158 // take one in their ctor. You must delete[] it yourself
159 // once you are done with it. Will return NULL if the
160 // ArrayString was empty.
161 #if WXWIN_COMPATIBILITY_2_4
162 wxString* GetStringArray() const;
163 #endif
164
165 // item management
166 // Search the element in the array, starting from the beginning if
167 // bFromEnd is false or from end otherwise. If bCase, comparison is case
168 // sensitive (default). Returns index of the first item matched or
169 // wxNOT_FOUND
170 int Index (const wxChar *sz, bool bCase = true, bool bFromEnd = false) const;
171 // add new element at the end (if the array is not sorted), return its
172 // index
173 size_t Add(const wxString& str, size_t nInsert = 1);
174 // add new element at given position
175 void Insert(const wxString& str, size_t uiIndex, size_t nInsert = 1);
176 // expand the array to have count elements
177 void SetCount(size_t count);
178 // remove first item matching this value
179 void Remove(const wxChar *sz);
180 // remove item by index
181 #if WXWIN_COMPATIBILITY_2_4
182 void Remove(size_t nIndex, size_t nRemove = 1) { RemoveAt(nIndex, nRemove); }
183 #endif
184 void RemoveAt(size_t nIndex, size_t nRemove = 1);
185
186 // sorting
187 // sort array elements in alphabetical order (or reversed alphabetical
188 // order if reverseOrder parameter is true)
189 void Sort(bool reverseOrder = false);
190 // sort array elements using specified comparaison function
191 void Sort(CompareFunction compareFunction);
192 void Sort(CompareFunction2 compareFunction);
193
194 // comparison
195 // compare two arrays case sensitively
196 bool operator==(const wxArrayString& a) const;
197 // compare two arrays case sensitively
198 bool operator!=(const wxArrayString& a) const { return !(*this == a); }
199
200 // STL-like interface
201 typedef wxString value_type;
202 typedef value_type* pointer;
203 typedef const value_type* const_pointer;
204 typedef value_type* iterator;
205 typedef const value_type* const_iterator;
206 typedef value_type& reference;
207 typedef const value_type& const_reference;
208 typedef int difference_type;
209 typedef size_t size_type;
210
211 // FIXME: same in dynarray.h
212 class reverse_iterator
213 {
214 typedef wxString value_type;
215 typedef value_type* pointer;
216 typedef value_type& reference;
217 typedef reverse_iterator itor;
218 friend itor operator+(int o, const itor& it);
219 friend itor operator+(const itor& it, int o);
220 friend itor operator-(const itor& it, int o);
221 friend difference_type operator -(const itor& i1, const itor& i2);
222 public:
223 pointer m_ptr;
224 reverse_iterator() : m_ptr(NULL) { }
225 reverse_iterator(pointer ptr) : m_ptr(ptr) { }
226 reverse_iterator(const itor& it) : m_ptr(it.m_ptr) { }
227 reference operator*() const { return *m_ptr; }
228 pointer operator->() const { return m_ptr; }
229 itor operator++() { --m_ptr; return *this; }
230 itor operator++(int)
231 { reverse_iterator tmp = *this; --m_ptr; return tmp; }
232 itor operator--() { ++m_ptr; return *this; }
233 itor operator--(int) { itor tmp = *this; ++m_ptr; return tmp; }
234 bool operator ==(const itor& it) { return m_ptr == it.m_ptr; }
235 bool operator !=(const itor& it) { return m_ptr != it.m_ptr; }
236 };
237
238 class const_reverse_iterator
239 {
240 typedef wxString value_type;
241 typedef const value_type* pointer;
242 typedef const value_type& reference;
243 typedef const_reverse_iterator itor;
244 friend itor operator+(int o, const itor& it);
245 friend itor operator+(const itor& it, int o);
246 friend itor operator-(const itor& it, int o);
247 friend difference_type operator -(const itor& i1, const itor& i2);
248 public:
249 pointer m_ptr;
250 const_reverse_iterator() : m_ptr(NULL) { }
251 const_reverse_iterator(pointer ptr) : m_ptr(ptr) { }
252 const_reverse_iterator(const itor& it) : m_ptr(it.m_ptr) { }
253 const_reverse_iterator(const reverse_iterator& it) : m_ptr(it.m_ptr) { }
254 reference operator*() const { return *m_ptr; }
255 pointer operator->() const { return m_ptr; }
256 itor operator++() { --m_ptr; return *this; }
257 itor operator++(int)
258 { itor tmp = *this; --m_ptr; return tmp; }
259 itor operator--() { ++m_ptr; return *this; }
260 itor operator--(int) { itor tmp = *this; ++m_ptr; return tmp; }
261 bool operator ==(const itor& it) { return m_ptr == it.m_ptr; }
262 bool operator !=(const itor& it) { return m_ptr != it.m_ptr; }
263 };
264
265 wxArrayString(const_iterator first, const_iterator last)
266 { Init(false); assign(first, last); }
267 wxArrayString(size_type n, const_reference v) { Init(false); assign(n, v); }
268 void assign(const_iterator first, const_iterator last);
269 void assign(size_type n, const_reference v)
270 { clear(); Add(v, n); }
271 reference back() { return *(end() - 1); }
272 const_reference back() const { return *(end() - 1); }
273 iterator begin() { return (wxString *)&(m_pItems[0]); }
274 const_iterator begin() const { return (wxString *)&(m_pItems[0]); }
275 size_type capacity() const { return m_nSize; }
276 void clear() { Clear(); }
277 bool empty() const { return IsEmpty(); }
278 iterator end() { return begin() + GetCount(); }
279 const_iterator end() const { return begin() + GetCount(); }
280 iterator erase(iterator first, iterator last)
281 {
282 size_t idx = first - begin();
283 RemoveAt(idx, last - first);
284 return begin() + idx;
285 }
286 iterator erase(iterator it) { return erase(it, it + 1); }
287 reference front() { return *begin(); }
288 const_reference front() const { return *begin(); }
289 void insert(iterator it, size_type n, const_reference v)
290 { Insert(v, it - begin(), n); }
291 iterator insert(iterator it, const_reference v = value_type())
292 { size_t idx = it - begin(); Insert(v, idx); return begin() + idx; }
293 void insert(iterator it, const_iterator first, const_iterator last);
294 size_type max_size() const { return INT_MAX; }
295 void pop_back() { RemoveAt(GetCount() - 1); }
296 void push_back(const_reference v) { Add(v); }
297 reverse_iterator rbegin() { return reverse_iterator(end() - 1); }
298 const_reverse_iterator rbegin() const;
299 reverse_iterator rend() { return reverse_iterator(begin() - 1); }
300 const_reverse_iterator rend() const;
301 void reserve(size_type n) /* base::reserve*/;
302 void resize(size_type n, value_type v = value_type());
303 size_type size() const { return GetCount(); }
304
305 protected:
306 void Init(bool autoSort); // common part of all ctors
307 void Copy(const wxArrayString& src); // copies the contents of another array
308
309 private:
310 void Grow(size_t nIncrement = 0); // makes array bigger if needed
311 void Free(); // free all the strings stored
312
313 void DoSort(); // common part of all Sort() variants
314
315 size_t m_nSize, // current size of the array
316 m_nCount; // current number of elements
317
318 wxChar **m_pItems; // pointer to data
319
320 bool m_autoSort; // if true, keep the array always sorted
321 };
322
323 class WXDLLIMPEXP_BASE wxSortedArrayString : public wxArrayString
324 {
325 public:
326 wxSortedArrayString() : wxArrayString(true)
327 { }
328 wxSortedArrayString(const wxArrayString& array) : wxArrayString(true)
329 { Copy(array); }
330 };
331
332 #endif // !wxUSE_STL
333
334 // this class provides a temporary wxString* from a
335 // wxArrayString
336 class WXDLLIMPEXP_BASE wxCArrayString
337 {
338 public:
339 wxCArrayString( const wxArrayString& array )
340 : m_array( array ), m_strings( NULL )
341 { }
342 ~wxCArrayString() { delete[] m_strings; }
343
344 size_t GetCount() const { return m_array.GetCount(); }
345 wxString* GetStrings()
346 {
347 if( m_strings ) return m_strings;
348 size_t count = m_array.GetCount();
349 m_strings = new wxString[count];
350 for( size_t i = 0; i < count; ++i )
351 m_strings[i] = m_array[i];
352 return m_strings;
353 }
354 private:
355 const wxArrayString& m_array;
356 wxString* m_strings;
357 };
358
359 #endif