]> git.saurik.com Git - wxWidgets.git/blame - include/wx/arrstr.h
added wxColour(RGBColor) ctor and use it insteaf of constructing wxColour from RGBCol...
[wxWidgets.git] / include / wx / arrstr.h
CommitLineData
df5168c4
MB
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>
65571936 9// Licence: wxWindows licence
df5168c4
MB
10///////////////////////////////////////////////////////////////////////////////
11
12#ifndef _WX_ARRSTR_H
13#define _WX_ARRSTR_H
14
15#include "wx/defs.h"
16#include "wx/string.h"
17
eae4425d
JS
18WXDLLIMPEXP_BASE int wxCMPFUNC_CONV wxStringSortAscending(wxString*, wxString*);
19WXDLLIMPEXP_BASE int wxCMPFUNC_CONV wxStringSortDescending(wxString*, wxString*);
df5168c4
MB
20
21#if wxUSE_STL
22
23#include "wx/dynarray.h"
24
39318636 25typedef int (wxCMPFUNC_CONV *CMPFUNCwxString)(wxString*, wxString*);
f700f98c
MB
26typedef wxString _wxArraywxBaseArrayStringBase;
27_WX_DECLARE_BASEARRAY_2(_wxArraywxBaseArrayStringBase, wxBaseArrayStringBase,
28 wxArray_SortFunction<wxString>,
29 class WXDLLIMPEXP_BASE);
4629016d 30WX_DEFINE_USER_EXPORTED_TYPEARRAY(wxString, wxArrayStringBase,
116270c6 31 wxBaseArrayStringBase, WXDLLIMPEXP_BASE);
4629016d 32_WX_DEFINE_SORTED_TYPEARRAY_2(wxString, wxSortedArrayStringBase,
df5168c4
MB
33 wxBaseArrayStringBase, = wxStringSortAscending,
34 class WXDLLIMPEXP_BASE, CMPFUNCwxString);
35
36class WXDLLIMPEXP_BASE wxArrayString : public wxArrayStringBase
37{
38public:
4e75b65f
MB
39 // type of function used by wxArrayString::Sort()
40 typedef int (wxCMPFUNC_CONV *CompareFunction)(const wxString& first,
41 const wxString& second);
42
df5168c4
MB
43 wxArrayString() { }
44 wxArrayString(const wxArrayString& a) : wxArrayStringBase(a) { }
d44d0cbd
JS
45 wxArrayString(size_t sz, const wxChar** a);
46 wxArrayString(size_t sz, const wxString* a);
2da2f941
MB
47
48 int Index(const wxChar* sz, bool bCase = true, bool bFromEnd = false) const;
4e75b65f
MB
49
50 void Sort(bool reverseOrder = false);
51 void Sort(CompareFunction function);
14baddc9 52 void Sort(CMPFUNCwxString function) { wxArrayStringBase::Sort(function); }
555d0d7e
MB
53
54 size_t Add(const wxString& string, size_t copies = 1)
55 {
56 wxArrayStringBase::Add(string, copies);
57 return size() - copies;
58 }
df5168c4
MB
59};
60
61class WXDLLIMPEXP_BASE wxSortedArrayString : public wxSortedArrayStringBase
62{
63public:
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 }
2da2f941
MB
77
78 int Index(const wxChar* sz, bool bCase = true, bool bFromEnd = false) const;
df5168c4
MB
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
98class WXDLLIMPEXP_BASE wxArrayString
99{
100public:
101 // type of function used by wxArrayString::Sort()
39318636 102 typedef int (wxCMPFUNC_CONV *CompareFunction)(const wxString& first,
df5168c4
MB
103 const wxString& second);
104 // type of function used by wxArrayString::Sort(), for compatibility with
105 // wxArray
39318636 106 typedef int (wxCMPFUNC_CONV *CompareFunction2)(wxString* first,
df5168c4
MB
107 wxString* second);
108
109 // constructors and destructor
110 // default ctor
584ad2a3
MB
111 wxArrayString() { Init(false); }
112 // if autoSort is true, the array is always sorted (in alphabetical order)
df5168c4
MB
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...
584ad2a3 120 wxArrayString(int autoSort) { Init(autoSort != 0); }
d44d0cbd
JS
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);
df5168c4
MB
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
df5168c4
MB
170
171 // item management
172 // Search the element in the array, starting from the beginning if
584ad2a3 173 // bFromEnd is false or from end otherwise. If bCase, comparison is case
df5168c4
MB
174 // sensitive (default). Returns index of the first item matched or
175 // wxNOT_FOUND
584ad2a3 176 int Index (const wxChar *sz, bool bCase = true, bool bFromEnd = false) const;
df5168c4
MB
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
df5168c4
MB
187 void RemoveAt(size_t nIndex, size_t nRemove = 1);
188
189 // sorting
190 // sort array elements in alphabetical order (or reversed alphabetical
584ad2a3
MB
191 // order if reverseOrder parameter is true)
192 void Sort(bool reverseOrder = false);
df5168c4
MB
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
3c7896fd 214 // TODO: this code duplicates the one in dynarray.h
df5168c4
MB
215 class reverse_iterator
216 {
37589419
MB
217 typedef wxString value_type;
218 typedef value_type* pointer;
219 typedef value_type& reference;
df5168c4
MB
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; }
60d8e886
VZ
232 itor& operator++() { --m_ptr; return *this; }
233 const itor operator++(int)
df5168c4 234 { reverse_iterator tmp = *this; --m_ptr; return tmp; }
60d8e886
VZ
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; }
df5168c4
MB
239 };
240
241 class const_reverse_iterator
242 {
37589419
MB
243 typedef wxString value_type;
244 typedef const value_type* pointer;
245 typedef const value_type& reference;
df5168c4
MB
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; }
60d8e886
VZ
259 itor& operator++() { --m_ptr; return *this; }
260 const itor operator++(int)
df5168c4 261 { itor tmp = *this; --m_ptr; return tmp; }
60d8e886
VZ
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; }
df5168c4
MB
266 };
267
584ad2a3
MB
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); }
df5168c4
MB
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
308protected:
309 void Init(bool autoSort); // common part of all ctors
310 void Copy(const wxArrayString& src); // copies the contents of another array
311
312private:
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
001ad728 321 const wxChar **m_pItems; // pointer to data
df5168c4 322
584ad2a3 323 bool m_autoSort; // if true, keep the array always sorted
df5168c4
MB
324};
325
326class WXDLLIMPEXP_BASE wxSortedArrayString : public wxArrayString
327{
328public:
584ad2a3 329 wxSortedArrayString() : wxArrayString(true)
df5168c4 330 { }
584ad2a3 331 wxSortedArrayString(const wxArrayString& array) : wxArrayString(true)
df5168c4
MB
332 { Copy(array); }
333};
334
335#endif // !wxUSE_STL
336
584ad2a3
MB
337// this class provides a temporary wxString* from a
338// wxArrayString
339class WXDLLIMPEXP_BASE wxCArrayString
340{
341public:
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 }
357private:
358 const wxArrayString& m_array;
359 wxString* m_strings;
360};
361
df5168c4 362#endif