made wxArrayString::assign(iterator, iterator) a template function; also fixed a...
[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 // these functions are only used in STL build now but we define them in any
19 // case for compatibility with the existing code outside of the library which
20 // could be using them
21 inline int wxCMPFUNC_CONV wxStringSortAscending(wxString* s1, wxString* s2)
22 {
23 return s1->Cmp(*s2);
24 }
25
26 inline int wxCMPFUNC_CONV wxStringSortDescending(wxString* s1, wxString* s2)
27 {
28 return wxStringSortAscending(s2, s1);
29 }
30
31 #if wxUSE_STL
32
33 #include "wx/dynarray.h"
34
35 typedef int (wxCMPFUNC_CONV *CMPFUNCwxString)(wxString*, wxString*);
36 typedef wxString _wxArraywxBaseArrayStringBase;
37 _WX_DECLARE_BASEARRAY_2(_wxArraywxBaseArrayStringBase, wxBaseArrayStringBase,
38 wxArray_SortFunction<wxString>,
39 class WXDLLIMPEXP_BASE);
40 WX_DEFINE_USER_EXPORTED_TYPEARRAY(wxString, wxArrayStringBase,
41 wxBaseArrayStringBase, WXDLLIMPEXP_BASE);
42 _WX_DEFINE_SORTED_TYPEARRAY_2(wxString, wxSortedArrayStringBase,
43 wxBaseArrayStringBase, = wxStringSortAscending,
44 class WXDLLIMPEXP_BASE, CMPFUNCwxString);
45
46 class WXDLLIMPEXP_BASE wxArrayString : public wxArrayStringBase
47 {
48 public:
49 // type of function used by wxArrayString::Sort()
50 typedef int (wxCMPFUNC_CONV *CompareFunction)(const wxString& first,
51 const wxString& second);
52
53 wxArrayString() { }
54 wxArrayString(const wxArrayString& a) : wxArrayStringBase(a) { }
55 wxArrayString(size_t sz, const char** a);
56 wxArrayString(size_t sz, const wchar_t** a);
57 wxArrayString(size_t sz, const wxString* a);
58
59 int Index(const wxString& str, bool bCase = true, bool bFromEnd = false) const;
60
61 void Sort(bool reverseOrder = false);
62 void Sort(CompareFunction function);
63 void Sort(CMPFUNCwxString function) { wxArrayStringBase::Sort(function); }
64
65 size_t Add(const wxString& string, size_t copies = 1)
66 {
67 wxArrayStringBase::Add(string, copies);
68 return size() - copies;
69 }
70 };
71
72 class WXDLLIMPEXP_BASE wxSortedArrayString : public wxSortedArrayStringBase
73 {
74 public:
75 wxSortedArrayString() : wxSortedArrayStringBase(wxStringSortAscending)
76 { }
77 wxSortedArrayString(const wxSortedArrayString& array)
78 : wxSortedArrayStringBase(array)
79 { }
80 wxSortedArrayString(const wxArrayString& src)
81 : wxSortedArrayStringBase(wxStringSortAscending)
82 {
83 reserve(src.size());
84
85 for ( size_t n = 0; n < src.size(); n++ )
86 Add(src[n]);
87 }
88
89 int Index(const wxString& str, bool bCase = true, bool bFromEnd = false) const;
90 };
91
92 #else // if !wxUSE_STL
93
94 // this shouldn't be defined for compilers not supporting template methods or
95 // without std::distance() -- and if all of the currently supported compilers
96 // do have it, then it can just be removed and wxHAS_VECTOR_TEMPLATE_ASSIGN
97 // code always used
98 #define wxHAS_VECTOR_TEMPLATE_ASSIGN
99
100 #ifdef wxHAS_VECTOR_TEMPLATE_ASSIGN
101 #include "wx/beforestd.h"
102 #include <iterator>
103 #include "wx/afterstd.h"
104 #endif // wxHAS_VECTOR_TEMPLATE_ASSIGN
105
106 class WXDLLIMPEXP_BASE wxArrayString
107 {
108 public:
109 // type of function used by wxArrayString::Sort()
110 typedef int (wxCMPFUNC_CONV *CompareFunction)(const wxString& first,
111 const wxString& second);
112 // type of function used by wxArrayString::Sort(), for compatibility with
113 // wxArray
114 typedef int (wxCMPFUNC_CONV *CompareFunction2)(wxString* first,
115 wxString* second);
116
117 // constructors and destructor
118 // default ctor
119 wxArrayString() { Init(false); }
120 // if autoSort is true, the array is always sorted (in alphabetical order)
121 //
122 // NB: the reason for using int and not bool is that like this we can avoid
123 // using this ctor for implicit conversions from "const char *" (which
124 // we'd like to be implicitly converted to wxString instead!)
125 //
126 // of course, using explicit would be even better - if all compilers
127 // supported it...
128 wxArrayString(int autoSort) { Init(autoSort != 0); }
129 // C string array ctor
130 wxArrayString(size_t sz, const char** a);
131 wxArrayString(size_t sz, const wchar_t** a);
132 // wxString string array ctor
133 wxArrayString(size_t sz, const wxString* a);
134 // copy ctor
135 wxArrayString(const wxArrayString& array);
136 // assignment operator
137 wxArrayString& operator=(const wxArrayString& src);
138 // not virtual, this class should not be derived from
139 ~wxArrayString();
140
141 // memory management
142 // empties the list, but doesn't release memory
143 void Empty();
144 // empties the list and releases memory
145 void Clear();
146 // preallocates memory for given number of items
147 void Alloc(size_t nCount);
148 // minimzes the memory usage (by freeing all extra memory)
149 void Shrink();
150
151 // simple accessors
152 // number of elements in the array
153 size_t GetCount() const { return m_nCount; }
154 // is it empty?
155 bool IsEmpty() const { return m_nCount == 0; }
156 // number of elements in the array (GetCount is preferred API)
157 size_t Count() const { return m_nCount; }
158
159 // items access (range checking is done in debug version)
160 // get item at position uiIndex
161 wxString& Item(size_t nIndex) const
162 {
163 wxASSERT_MSG( nIndex < m_nCount,
164 _T("wxArrayString: index out of bounds") );
165
166 return m_pItems[nIndex];
167 }
168
169 // same as Item()
170 wxString& operator[](size_t nIndex) const { return Item(nIndex); }
171 // get last item
172 wxString& Last() const
173 {
174 wxASSERT_MSG( !IsEmpty(),
175 _T("wxArrayString: index out of bounds") );
176 return Item(GetCount() - 1);
177 }
178
179
180 // item management
181 // Search the element in the array, starting from the beginning if
182 // bFromEnd is false or from end otherwise. If bCase, comparison is case
183 // sensitive (default). Returns index of the first item matched or
184 // wxNOT_FOUND
185 int Index (const wxString& str, bool bCase = true, bool bFromEnd = false) const;
186 // add new element at the end (if the array is not sorted), return its
187 // index
188 size_t Add(const wxString& str, size_t nInsert = 1);
189 // add new element at given position
190 void Insert(const wxString& str, size_t uiIndex, size_t nInsert = 1);
191 // expand the array to have count elements
192 void SetCount(size_t count);
193 // remove first item matching this value
194 void Remove(const wxString& sz);
195 // remove item by index
196 void RemoveAt(size_t nIndex, size_t nRemove = 1);
197
198 // sorting
199 // sort array elements in alphabetical order (or reversed alphabetical
200 // order if reverseOrder parameter is true)
201 void Sort(bool reverseOrder = false);
202 // sort array elements using specified comparison function
203 void Sort(CompareFunction compareFunction);
204 void Sort(CompareFunction2 compareFunction);
205
206 // comparison
207 // compare two arrays case sensitively
208 bool operator==(const wxArrayString& a) const;
209 // compare two arrays case sensitively
210 bool operator!=(const wxArrayString& a) const { return !(*this == a); }
211
212 // STL-like interface
213 typedef wxString value_type;
214 typedef value_type* pointer;
215 typedef const value_type* const_pointer;
216 typedef value_type* iterator;
217 typedef const value_type* const_iterator;
218 typedef value_type& reference;
219 typedef const value_type& const_reference;
220 typedef int difference_type;
221 typedef size_t size_type;
222
223 // TODO: this code duplicates the one in dynarray.h
224 class reverse_iterator
225 {
226 typedef wxString value_type;
227 typedef value_type* pointer;
228 typedef value_type& reference;
229 typedef reverse_iterator itor;
230 friend itor operator+(int o, const itor& it);
231 friend itor operator+(const itor& it, int o);
232 friend itor operator-(const itor& it, int o);
233 friend difference_type operator -(const itor& i1, const itor& i2);
234 public:
235 pointer m_ptr;
236 reverse_iterator() : m_ptr(NULL) { }
237 reverse_iterator(pointer ptr) : m_ptr(ptr) { }
238 reverse_iterator(const itor& it) : m_ptr(it.m_ptr) { }
239 reference operator*() const { return *m_ptr; }
240 pointer operator->() const { return m_ptr; }
241 itor& operator++() { --m_ptr; return *this; }
242 const itor operator++(int)
243 { reverse_iterator tmp = *this; --m_ptr; return tmp; }
244 itor& operator--() { ++m_ptr; return *this; }
245 const itor operator--(int) { itor tmp = *this; ++m_ptr; return tmp; }
246 bool operator ==(const itor& it) const { return m_ptr == it.m_ptr; }
247 bool operator !=(const itor& it) const { return m_ptr != it.m_ptr; }
248 };
249
250 class const_reverse_iterator
251 {
252 typedef wxString value_type;
253 typedef const value_type* pointer;
254 typedef const value_type& reference;
255 typedef const_reverse_iterator itor;
256 friend itor operator+(int o, const itor& it);
257 friend itor operator+(const itor& it, int o);
258 friend itor operator-(const itor& it, int o);
259 friend difference_type operator -(const itor& i1, const itor& i2);
260 public:
261 pointer m_ptr;
262 const_reverse_iterator() : m_ptr(NULL) { }
263 const_reverse_iterator(pointer ptr) : m_ptr(ptr) { }
264 const_reverse_iterator(const itor& it) : m_ptr(it.m_ptr) { }
265 const_reverse_iterator(const reverse_iterator& it) : m_ptr(it.m_ptr) { }
266 reference operator*() const { return *m_ptr; }
267 pointer operator->() const { return m_ptr; }
268 itor& operator++() { --m_ptr; return *this; }
269 const itor operator++(int)
270 { itor tmp = *this; --m_ptr; return tmp; }
271 itor& operator--() { ++m_ptr; return *this; }
272 const itor operator--(int) { itor tmp = *this; ++m_ptr; return tmp; }
273 bool operator ==(const itor& it) const { return m_ptr == it.m_ptr; }
274 bool operator !=(const itor& it) const { return m_ptr != it.m_ptr; }
275 };
276
277 wxArrayString(const_iterator first, const_iterator last)
278 { Init(false); assign(first, last); }
279 wxArrayString(size_type n, const_reference v) { Init(false); assign(n, v); }
280
281 #ifdef wxHAS_VECTOR_TEMPLATE_ASSIGN
282 template <class Iterator>
283 void assign(Iterator first, Iterator last)
284 {
285 clear();
286 reserve(std::distance(first, last));
287 for(; first != last; ++first)
288 push_back(*first);
289 }
290 #else // !wxHAS_VECTOR_TEMPLATE_ASSIGN
291 void assign(const_iterator first, const_iterator last)
292 {
293 clear();
294 reserve(last - first);
295 for(; first != last; ++first)
296 push_back(*first);
297 }
298 #endif // wxHAS_VECTOR_TEMPLATE_ASSIGN/!wxHAS_VECTOR_TEMPLATE_ASSIGN
299
300 void assign(size_type n, const_reference v)
301 { clear(); Add(v, n); }
302 reference back() { return *(end() - 1); }
303 const_reference back() const { return *(end() - 1); }
304 iterator begin() { return m_pItems; }
305 const_iterator begin() const { return m_pItems; }
306 size_type capacity() const { return m_nSize; }
307 void clear() { Clear(); }
308 bool empty() const { return IsEmpty(); }
309 iterator end() { return begin() + GetCount(); }
310 const_iterator end() const { return begin() + GetCount(); }
311 iterator erase(iterator first, iterator last)
312 {
313 size_t idx = first - begin();
314 RemoveAt(idx, last - first);
315 return begin() + idx;
316 }
317 iterator erase(iterator it) { return erase(it, it + 1); }
318 reference front() { return *begin(); }
319 const_reference front() const { return *begin(); }
320 void insert(iterator it, size_type n, const_reference v)
321 { Insert(v, it - begin(), n); }
322 iterator insert(iterator it, const_reference v = value_type())
323 { size_t idx = it - begin(); Insert(v, idx); return begin() + idx; }
324 void insert(iterator it, const_iterator first, const_iterator last);
325 size_type max_size() const { return INT_MAX; }
326 void pop_back() { RemoveAt(GetCount() - 1); }
327 void push_back(const_reference v) { Add(v); }
328 reverse_iterator rbegin() { return reverse_iterator(end() - 1); }
329 const_reverse_iterator rbegin() const
330 { return const_reverse_iterator(end() - 1); }
331 reverse_iterator rend() { return reverse_iterator(begin() - 1); }
332 const_reverse_iterator rend() const
333 { return const_reverse_iterator(begin() - 1); }
334 void reserve(size_type n) /* base::reserve*/;
335 void resize(size_type n, value_type v = value_type());
336 size_type size() const { return GetCount(); }
337 void swap(wxArrayString& other)
338 {
339 wxSwap(m_nSize, other.m_nSize);
340 wxSwap(m_nCount, other.m_nCount);
341 wxSwap(m_pItems, other.m_pItems);
342 wxSwap(m_autoSort, other.m_autoSort);
343 }
344
345 protected:
346 void Init(bool autoSort); // common part of all ctors
347 void Copy(const wxArrayString& src); // copies the contents of another array
348
349 private:
350 void Grow(size_t nIncrement = 0); // makes array bigger if needed
351
352 size_t m_nSize, // current size of the array
353 m_nCount; // current number of elements
354
355 wxString *m_pItems; // pointer to data
356
357 bool m_autoSort; // if true, keep the array always sorted
358 };
359
360 class WXDLLIMPEXP_BASE wxSortedArrayString : public wxArrayString
361 {
362 public:
363 wxSortedArrayString() : wxArrayString(true)
364 { }
365 wxSortedArrayString(const wxArrayString& array) : wxArrayString(true)
366 { Copy(array); }
367 };
368
369 #endif // !wxUSE_STL
370
371 // this class provides a temporary wxString* from a
372 // wxArrayString
373 class WXDLLIMPEXP_BASE wxCArrayString
374 {
375 public:
376 wxCArrayString( const wxArrayString& array )
377 : m_array( array ), m_strings( NULL )
378 { }
379 ~wxCArrayString() { delete[] m_strings; }
380
381 size_t GetCount() const { return m_array.GetCount(); }
382 wxString* GetStrings()
383 {
384 if( m_strings ) return m_strings;
385 size_t count = m_array.GetCount();
386 m_strings = new wxString[count];
387 for( size_t i = 0; i < count; ++i )
388 m_strings[i] = m_array[i];
389 return m_strings;
390 }
391
392 wxString* Release()
393 {
394 wxString *r = GetStrings();
395 m_strings = NULL;
396 return r;
397 }
398
399 private:
400 const wxArrayString& m_array;
401 wxString* m_strings;
402 };
403
404
405 // ----------------------------------------------------------------------------
406 // helper functions for working with arrays
407 // ----------------------------------------------------------------------------
408
409 // by default, these functions use the escape character to escape the
410 // separators occuring inside the string to be joined, this can be disabled by
411 // passing '\0' as escape
412
413 WXDLLIMPEXP_BASE wxString wxJoin(const wxArrayString& arr,
414 const wxChar sep,
415 const wxChar escape = wxT('\\'));
416
417 WXDLLIMPEXP_BASE wxArrayString wxSplit(const wxString& str,
418 const wxChar sep,
419 const wxChar escape = wxT('\\'));
420
421
422 // ----------------------------------------------------------------------------
423 // This helper class allows to pass both C array of wxStrings or wxArrayString
424 // using the same interface.
425 //
426 // Use it when you have two methods taking wxArrayString or (int, wxString[]),
427 // that do the same thing. This class lets you iterate over input data in the
428 // same way whether it is a raw array of strings or wxArrayString.
429 //
430 // The object does not take ownership of the data -- internally it keeps
431 // pointers to the data, therefore the data must be disposed of by user
432 // and only after this object is destroyed. Usually it is not a problem as
433 // only temporary objects of this class are used.
434 // ----------------------------------------------------------------------------
435
436 class wxArrayStringsAdapter
437 {
438 public:
439 // construct an adapter from a wxArrayString
440 wxArrayStringsAdapter(const wxArrayString& strings)
441 : m_type(wxSTRING_ARRAY), m_size(strings.size())
442 {
443 m_data.array = &strings;
444 }
445
446 // construct an adapter from a wxString[]
447 wxArrayStringsAdapter(unsigned int n, const wxString *strings)
448 : m_type(wxSTRING_POINTER), m_size(n)
449 {
450 m_data.ptr = strings;
451 }
452
453 // construct an adapter from a single wxString
454 wxArrayStringsAdapter(const wxString& s)
455 : m_type(wxSTRING_POINTER), m_size(1)
456 {
457 m_data.ptr = &s;
458 }
459
460 // default copy constructor is ok
461
462 // iteration interface
463 size_t GetCount() const { return m_size; }
464 bool IsEmpty() const { return GetCount() == 0; }
465 const wxString& operator[] (unsigned int i) const
466 {
467 wxASSERT_MSG( i < GetCount(), wxT("index out of bounds") );
468 if(m_type == wxSTRING_POINTER)
469 return m_data.ptr[i];
470 return m_data.array->Item(i);
471 }
472 wxArrayString AsArrayString() const
473 {
474 if(m_type == wxSTRING_ARRAY)
475 return *m_data.array;
476 return wxArrayString(GetCount(), m_data.ptr);
477 }
478
479 private:
480 // type of the data being held
481 enum wxStringContainerType
482 {
483 wxSTRING_ARRAY, // wxArrayString
484 wxSTRING_POINTER // wxString[]
485 };
486
487 wxStringContainerType m_type;
488 size_t m_size;
489 union
490 {
491 const wxString * ptr;
492 const wxArrayString * array;
493 } m_data;
494
495 wxDECLARE_NO_ASSIGN_CLASS(wxArrayStringsAdapter);
496 };
497
498 #endif // _WX_ARRSTR_H