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