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