1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: include/wx/arrstr.h
3 // Purpose: wxArrayString class
4 // Author: Mattia Barbon and Vadim Zeitlin
8 // Copyright: (c) 2003 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
16 #include "wx/string.h"
18 WXDLLIMPEXP_BASE
int wxCMPFUNC_CONV
wxStringSortAscending(wxString
*, wxString
*);
19 WXDLLIMPEXP_BASE
int wxCMPFUNC_CONV
wxStringSortDescending(wxString
*, wxString
*);
23 #include "wx/dynarray.h"
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
);
36 class WXDLLIMPEXP_BASE wxArrayString
: public wxArrayStringBase
39 // type of function used by wxArrayString::Sort()
40 typedef int (wxCMPFUNC_CONV
*CompareFunction
)(const wxString
& first
,
41 const wxString
& second
);
44 wxArrayString(const wxArrayString
& a
) : wxArrayStringBase(a
) { }
46 int Index(const wxChar
* sz
, bool bCase
= true, bool bFromEnd
= false) const;
48 void Sort(bool reverseOrder
= false);
49 void Sort(CompareFunction function
);
52 class WXDLLIMPEXP_BASE wxSortedArrayString
: public wxSortedArrayStringBase
55 wxSortedArrayString() : wxSortedArrayStringBase(wxStringSortAscending
)
57 wxSortedArrayString(const wxSortedArrayString
& array
)
58 : wxSortedArrayStringBase(array
)
60 wxSortedArrayString(const wxArrayString
& src
)
61 : wxSortedArrayStringBase(wxStringSortAscending
)
65 for ( size_t n
= 0; n
< src
.size(); n
++ )
69 int Index(const wxChar
* sz
, bool bCase
= true, bool bFromEnd
= false) const;
72 #else // if !wxUSE_STL
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!
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.
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 // ----------------------------------------------------------------------------
89 class WXDLLIMPEXP_BASE wxArrayString
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
97 typedef int (wxCMPFUNC_CONV
*CompareFunction2
)(wxString
* first
,
100 // constructors and destructor
102 wxArrayString() { Init(false); }
103 // if autoSort is true, the array is always sorted (in alphabetical order)
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!)
109 // of course, using explicit would be even better - if all compilers
111 wxArrayString(int autoSort
) { Init(autoSort
!= 0); }
113 wxArrayString(const wxArrayString
& array
);
114 // assignment operator
115 wxArrayString
& operator=(const wxArrayString
& src
);
116 // not virtual, this class should not be derived from
120 // empties the list, but doesn't release memory
122 // empties the list and releases memory
124 // preallocates memory for given number of items
125 void Alloc(size_t nCount
);
126 // minimzes the memory usage (by freeing all extra memory)
130 // number of elements in the array
131 size_t GetCount() const { return m_nCount
; }
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
; }
137 // items access (range checking is done in debug version)
138 // get item at position uiIndex
139 wxString
& Item(size_t nIndex
) const
141 wxASSERT_MSG( nIndex
< m_nCount
,
142 _T("wxArrayString: index out of bounds") );
144 return *(wxString
*)&(m_pItems
[nIndex
]);
148 wxString
& operator[](size_t nIndex
) const { return Item(nIndex
); }
150 wxString
& Last() const
152 wxASSERT_MSG( !IsEmpty(),
153 _T("wxArrayString: index out of bounds") );
154 return Item(Count() - 1);
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;
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
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
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
); }
184 void RemoveAt(size_t nIndex
, size_t nRemove
= 1);
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
);
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
); }
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
;
211 // FIXME: same in dynarray.h
212 class reverse_iterator
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
);
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; }
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
; }
238 class const_reverse_iterator
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
);
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; }
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
; }
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
)
282 size_t idx
= first
- begin();
283 RemoveAt(idx
, last
- first
);
284 return begin() + idx
;
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(); }
306 void Init(bool autoSort
); // common part of all ctors
307 void Copy(const wxArrayString
& src
); // copies the contents of another array
310 void Grow(size_t nIncrement
= 0); // makes array bigger if needed
311 void Free(); // free all the strings stored
313 void DoSort(); // common part of all Sort() variants
315 size_t m_nSize
, // current size of the array
316 m_nCount
; // current number of elements
318 wxChar
**m_pItems
; // pointer to data
320 bool m_autoSort
; // if true, keep the array always sorted
323 class WXDLLIMPEXP_BASE wxSortedArrayString
: public wxArrayString
326 wxSortedArrayString() : wxArrayString(true)
328 wxSortedArrayString(const wxArrayString
& array
) : wxArrayString(true)
334 // this class provides a temporary wxString* from a
336 class WXDLLIMPEXP_BASE wxCArrayString
339 wxCArrayString( const wxArrayString
& array
)
340 : m_array( array
), m_strings( NULL
)
342 ~wxCArrayString() { delete[] m_strings
; }
344 size_t GetCount() const { return m_array
.GetCount(); }
345 wxString
* GetStrings()
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
];
355 const wxArrayString
& m_array
;