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
) { }
45 wxArrayString(size_t sz
, const wxChar
** a
);
46 wxArrayString(size_t sz
, const wxString
* a
);
48 int Index(const wxChar
* sz
, bool bCase
= true, bool bFromEnd
= false) const;
50 void Sort(bool reverseOrder
= false);
51 void Sort(CompareFunction function
);
52 void Sort(CMPFUNCwxString function
) { wxArrayStringBase::Sort(function
); }
54 size_t Add(const wxString
& string
, size_t copies
= 1)
56 wxArrayStringBase::Add(string
, copies
);
57 return size() - copies
;
61 class WXDLLIMPEXP_BASE wxSortedArrayString
: public wxSortedArrayStringBase
64 wxSortedArrayString() : wxSortedArrayStringBase(wxStringSortAscending
)
66 wxSortedArrayString(const wxSortedArrayString
& array
)
67 : wxSortedArrayStringBase(array
)
69 wxSortedArrayString(const wxArrayString
& src
)
70 : wxSortedArrayStringBase(wxStringSortAscending
)
74 for ( size_t n
= 0; n
< src
.size(); n
++ )
78 int Index(const wxChar
* sz
, bool bCase
= true, bool bFromEnd
= false) const;
81 #else // if !wxUSE_STL
83 class WXDLLIMPEXP_BASE wxArrayString
86 // type of function used by wxArrayString::Sort()
87 typedef int (wxCMPFUNC_CONV
*CompareFunction
)(const wxString
& first
,
88 const wxString
& second
);
89 // type of function used by wxArrayString::Sort(), for compatibility with
91 typedef int (wxCMPFUNC_CONV
*CompareFunction2
)(wxString
* first
,
94 // constructors and destructor
96 wxArrayString() { Init(false); }
97 // if autoSort is true, the array is always sorted (in alphabetical order)
99 // NB: the reason for using int and not bool is that like this we can avoid
100 // using this ctor for implicit conversions from "const char *" (which
101 // we'd like to be implicitly converted to wxString instead!)
103 // of course, using explicit would be even better - if all compilers
105 wxArrayString(int autoSort
) { Init(autoSort
!= 0); }
106 // C string array ctor
107 wxArrayString(size_t sz
, const wxChar
** a
);
108 // wxString string array ctor
109 wxArrayString(size_t sz
, const wxString
* a
);
111 wxArrayString(const wxArrayString
& array
);
112 // assignment operator
113 wxArrayString
& operator=(const wxArrayString
& src
);
114 // not virtual, this class should not be derived from
118 // empties the list, but doesn't release memory
120 // empties the list and releases memory
122 // preallocates memory for given number of items
123 void Alloc(size_t nCount
);
124 // minimzes the memory usage (by freeing all extra memory)
128 // number of elements in the array
129 size_t GetCount() const { return m_nCount
; }
131 bool IsEmpty() const { return m_nCount
== 0; }
132 // number of elements in the array (GetCount is preferred API)
133 size_t Count() const { return m_nCount
; }
135 // items access (range checking is done in debug version)
136 // get item at position uiIndex
137 wxString
& Item(size_t nIndex
) const
139 wxASSERT_MSG( nIndex
< m_nCount
,
140 _T("wxArrayString: index out of bounds") );
142 return m_pItems
[nIndex
];
146 wxString
& operator[](size_t nIndex
) const { return Item(nIndex
); }
148 wxString
& Last() const
150 wxASSERT_MSG( !IsEmpty(),
151 _T("wxArrayString: index out of bounds") );
152 return Item(Count() - 1);
157 // Search the element in the array, starting from the beginning if
158 // bFromEnd is false or from end otherwise. If bCase, comparison is case
159 // sensitive (default). Returns index of the first item matched or
161 int Index (const wxChar
*sz
, bool bCase
= true, bool bFromEnd
= false) const;
162 // add new element at the end (if the array is not sorted), return its
164 size_t Add(const wxString
& str
, size_t nInsert
= 1);
165 // add new element at given position
166 void Insert(const wxString
& str
, size_t uiIndex
, size_t nInsert
= 1);
167 // expand the array to have count elements
168 void SetCount(size_t count
);
169 // remove first item matching this value
170 void Remove(const wxChar
*sz
);
171 // remove item by index
172 void RemoveAt(size_t nIndex
, size_t nRemove
= 1);
175 // sort array elements in alphabetical order (or reversed alphabetical
176 // order if reverseOrder parameter is true)
177 void Sort(bool reverseOrder
= false);
178 // sort array elements using specified comparaison function
179 void Sort(CompareFunction compareFunction
);
180 void Sort(CompareFunction2 compareFunction
);
183 // compare two arrays case sensitively
184 bool operator==(const wxArrayString
& a
) const;
185 // compare two arrays case sensitively
186 bool operator!=(const wxArrayString
& a
) const { return !(*this == a
); }
188 // STL-like interface
189 typedef wxString value_type
;
190 typedef value_type
* pointer
;
191 typedef const value_type
* const_pointer
;
192 typedef value_type
* iterator
;
193 typedef const value_type
* const_iterator
;
194 typedef value_type
& reference
;
195 typedef const value_type
& const_reference
;
196 typedef int difference_type
;
197 typedef size_t size_type
;
199 // TODO: this code duplicates the one in dynarray.h
200 class reverse_iterator
202 typedef wxString value_type
;
203 typedef value_type
* pointer
;
204 typedef value_type
& reference
;
205 typedef reverse_iterator itor
;
206 friend itor
operator+(int o
, const itor
& it
);
207 friend itor
operator+(const itor
& it
, int o
);
208 friend itor
operator-(const itor
& it
, int o
);
209 friend difference_type
operator -(const itor
& i1
, const itor
& i2
);
212 reverse_iterator() : m_ptr(NULL
) { }
213 reverse_iterator(pointer ptr
) : m_ptr(ptr
) { }
214 reverse_iterator(const itor
& it
) : m_ptr(it
.m_ptr
) { }
215 reference
operator*() const { return *m_ptr
; }
216 pointer
operator->() const { return m_ptr
; }
217 itor
& operator++() { --m_ptr
; return *this; }
218 const itor
operator++(int)
219 { reverse_iterator tmp
= *this; --m_ptr
; return tmp
; }
220 itor
& operator--() { ++m_ptr
; return *this; }
221 const itor
operator--(int) { itor tmp
= *this; ++m_ptr
; return tmp
; }
222 bool operator ==(const itor
& it
) const { return m_ptr
== it
.m_ptr
; }
223 bool operator !=(const itor
& it
) const { return m_ptr
!= it
.m_ptr
; }
226 class const_reverse_iterator
228 typedef wxString value_type
;
229 typedef const value_type
* pointer
;
230 typedef const value_type
& reference
;
231 typedef const_reverse_iterator itor
;
232 friend itor
operator+(int o
, const itor
& it
);
233 friend itor
operator+(const itor
& it
, int o
);
234 friend itor
operator-(const itor
& it
, int o
);
235 friend difference_type
operator -(const itor
& i1
, const itor
& i2
);
238 const_reverse_iterator() : m_ptr(NULL
) { }
239 const_reverse_iterator(pointer ptr
) : m_ptr(ptr
) { }
240 const_reverse_iterator(const itor
& it
) : m_ptr(it
.m_ptr
) { }
241 const_reverse_iterator(const reverse_iterator
& it
) : m_ptr(it
.m_ptr
) { }
242 reference
operator*() const { return *m_ptr
; }
243 pointer
operator->() const { return m_ptr
; }
244 itor
& operator++() { --m_ptr
; return *this; }
245 const itor
operator++(int)
246 { itor tmp
= *this; --m_ptr
; return tmp
; }
247 itor
& operator--() { ++m_ptr
; return *this; }
248 const itor
operator--(int) { itor tmp
= *this; ++m_ptr
; return tmp
; }
249 bool operator ==(const itor
& it
) const { return m_ptr
== it
.m_ptr
; }
250 bool operator !=(const itor
& it
) const { return m_ptr
!= it
.m_ptr
; }
253 wxArrayString(const_iterator first
, const_iterator last
)
254 { Init(false); assign(first
, last
); }
255 wxArrayString(size_type n
, const_reference v
) { Init(false); assign(n
, v
); }
256 void assign(const_iterator first
, const_iterator last
);
257 void assign(size_type n
, const_reference v
)
258 { clear(); Add(v
, n
); }
259 reference
back() { return *(end() - 1); }
260 const_reference
back() const { return *(end() - 1); }
261 iterator
begin() { return m_pItems
; }
262 const_iterator
begin() const { return m_pItems
; }
263 size_type
capacity() const { return m_nSize
; }
264 void clear() { Clear(); }
265 bool empty() const { return IsEmpty(); }
266 iterator
end() { return begin() + GetCount(); }
267 const_iterator
end() const { return begin() + GetCount(); }
268 iterator
erase(iterator first
, iterator last
)
270 size_t idx
= first
- begin();
271 RemoveAt(idx
, last
- first
);
272 return begin() + idx
;
274 iterator
erase(iterator it
) { return erase(it
, it
+ 1); }
275 reference
front() { return *begin(); }
276 const_reference
front() const { return *begin(); }
277 void insert(iterator it
, size_type n
, const_reference v
)
278 { Insert(v
, it
- begin(), n
); }
279 iterator
insert(iterator it
, const_reference v
= value_type())
280 { size_t idx
= it
- begin(); Insert(v
, idx
); return begin() + idx
; }
281 void insert(iterator it
, const_iterator first
, const_iterator last
);
282 size_type
max_size() const { return INT_MAX
; }
283 void pop_back() { RemoveAt(GetCount() - 1); }
284 void push_back(const_reference v
) { Add(v
); }
285 reverse_iterator
rbegin() { return reverse_iterator(end() - 1); }
286 const_reverse_iterator
rbegin() const;
287 reverse_iterator
rend() { return reverse_iterator(begin() - 1); }
288 const_reverse_iterator
rend() const;
289 void reserve(size_type n
) /* base::reserve*/;
290 void resize(size_type n
, value_type v
= value_type());
291 size_type
size() const { return GetCount(); }
294 void Init(bool autoSort
); // common part of all ctors
295 void Copy(const wxArrayString
& src
); // copies the contents of another array
298 void Grow(size_t nIncrement
= 0); // makes array bigger if needed
300 void DoSort(); // common part of all Sort() variants
302 size_t m_nSize
, // current size of the array
303 m_nCount
; // current number of elements
305 wxString
*m_pItems
; // pointer to data
307 bool m_autoSort
; // if true, keep the array always sorted
310 class WXDLLIMPEXP_BASE wxSortedArrayString
: public wxArrayString
313 wxSortedArrayString() : wxArrayString(true)
315 wxSortedArrayString(const wxArrayString
& array
) : wxArrayString(true)
321 // this class provides a temporary wxString* from a
323 class WXDLLIMPEXP_BASE wxCArrayString
326 wxCArrayString( const wxArrayString
& array
)
327 : m_array( array
), m_strings( NULL
)
329 ~wxCArrayString() { delete[] m_strings
; }
331 size_t GetCount() const { return m_array
.GetCount(); }
332 wxString
* GetStrings()
334 if( m_strings
) return m_strings
;
335 size_t count
= m_array
.GetCount();
336 m_strings
= new wxString
[count
];
337 for( size_t i
= 0; i
< count
; ++i
)
338 m_strings
[i
] = m_array
[i
];
342 const wxArrayString
& m_array
;