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
40 wxArrayString(const wxArrayString
& a
) : wxArrayStringBase(a
) { }
42 int Index(const wxChar
* sz
, bool bCase
= true, bool bFromEnd
= false) const;
45 class WXDLLIMPEXP_BASE wxSortedArrayString
: public wxSortedArrayStringBase
48 wxSortedArrayString() : wxSortedArrayStringBase(wxStringSortAscending
)
50 wxSortedArrayString(const wxSortedArrayString
& array
)
51 : wxSortedArrayStringBase(array
)
53 wxSortedArrayString(const wxArrayString
& src
)
54 : wxSortedArrayStringBase(wxStringSortAscending
)
58 for ( size_t n
= 0; n
< src
.size(); n
++ )
62 int Index(const wxChar
* sz
, bool bCase
= true, bool bFromEnd
= false) const;
65 #else // if !wxUSE_STL
67 // ----------------------------------------------------------------------------
68 // The string array uses it's knowledge of internal structure of the wxString
69 // class to optimize string storage. Normally, we would store pointers to
70 // string, but as wxString is, in fact, itself a pointer (sizeof(wxString) is
71 // sizeof(char *)) we store these pointers instead. The cast to "wxString *" is
72 // really all we need to turn such pointer into a string!
74 // Of course, it can be called a dirty hack, but we use twice less memory and
75 // this approach is also more speed efficient, so it's probably worth it.
77 // Usage notes: when a string is added/inserted, a new copy of it is created,
78 // so the original string may be safely deleted. When a string is retrieved
79 // from the array (operator[] or Item() method), a reference is returned.
80 // ----------------------------------------------------------------------------
82 class WXDLLIMPEXP_BASE wxArrayString
85 // type of function used by wxArrayString::Sort()
86 typedef int (wxCMPFUNC_CONV
*CompareFunction
)(const wxString
& first
,
87 const wxString
& second
);
88 // type of function used by wxArrayString::Sort(), for compatibility with
90 typedef int (wxCMPFUNC_CONV
*CompareFunction2
)(wxString
* first
,
93 // constructors and destructor
96 : m_nSize(0), m_nCount(0), m_pItems(NULL
), m_autoSort(FALSE
)
98 // if autoSort is TRUE, the array is always sorted (in alphabetical order)
100 // NB: the reason for using int and not bool is that like this we can avoid
101 // using this ctor for implicit conversions from "const char *" (which
102 // we'd like to be implicitly converted to wxString instead!)
104 // of course, using explicit would be even better - if all compilers
106 wxArrayString(int autoSort
)
107 : m_nSize(0), m_nCount(0), m_pItems(NULL
), m_autoSort(FALSE
)
108 { Init(autoSort
!= 0); }
110 wxArrayString(const wxArrayString
& array
);
111 // assignment operator
112 wxArrayString
& operator=(const wxArrayString
& src
);
113 // not virtual, this class should not be derived from
117 // empties the list, but doesn't release memory
119 // empties the list and releases memory
121 // preallocates memory for given number of items
122 void Alloc(size_t nCount
);
123 // minimzes the memory usage (by freeing all extra memory)
127 // number of elements in the array
128 size_t GetCount() const { return m_nCount
; }
130 bool IsEmpty() const { return m_nCount
== 0; }
131 // number of elements in the array (GetCount is preferred API)
132 size_t Count() const { return m_nCount
; }
134 // items access (range checking is done in debug version)
135 // get item at position uiIndex
136 wxString
& Item(size_t nIndex
) const
138 wxASSERT_MSG( nIndex
< m_nCount
,
139 _T("wxArrayString: index out of bounds") );
141 return *(wxString
*)&(m_pItems
[nIndex
]);
145 wxString
& operator[](size_t nIndex
) const { return Item(nIndex
); }
147 wxString
& Last() const
149 wxASSERT_MSG( !IsEmpty(),
150 _T("wxArrayString: index out of bounds") );
151 return Item(Count() - 1);
154 #if WXWIN_COMPATIBILITY_2_4
155 // return a wxString[], useful for the controls which
156 // take one in their ctor. You must delete[] it yourself
157 // once you are done with it. Will return NULL if the
158 // ArrayString was empty.
159 wxString
* GetStringArray() const;
163 // Search the element in the array, starting from the beginning if
164 // bFromEnd is FALSE or from end otherwise. If bCase, comparison is case
165 // sensitive (default). Returns index of the first item matched or
167 int Index (const wxChar
*sz
, bool bCase
= TRUE
, bool bFromEnd
= FALSE
) const;
168 // add new element at the end (if the array is not sorted), return its
170 size_t Add(const wxString
& str
, size_t nInsert
= 1);
171 // add new element at given position
172 void Insert(const wxString
& str
, size_t uiIndex
, size_t nInsert
= 1);
173 // expand the array to have count elements
174 void SetCount(size_t count
);
175 // remove first item matching this value
176 void Remove(const wxChar
*sz
);
177 // remove item by index
178 #if WXWIN_COMPATIBILITY_2_4
179 void Remove(size_t nIndex
, size_t nRemove
= 1) { RemoveAt(nIndex
, nRemove
); }
181 void RemoveAt(size_t nIndex
, size_t nRemove
= 1);
184 // sort array elements in alphabetical order (or reversed alphabetical
185 // order if reverseOrder parameter is TRUE)
186 void Sort(bool reverseOrder
= FALSE
);
187 // sort array elements using specified comparaison function
188 void Sort(CompareFunction compareFunction
);
189 void Sort(CompareFunction2 compareFunction
);
192 // compare two arrays case sensitively
193 bool operator==(const wxArrayString
& a
) const;
194 // compare two arrays case sensitively
195 bool operator!=(const wxArrayString
& a
) const { return !(*this == a
); }
197 // STL-like interface
198 typedef wxString value_type
;
199 typedef value_type
* pointer
;
200 typedef const value_type
* const_pointer
;
201 typedef value_type
* iterator
;
202 typedef const value_type
* const_iterator
;
203 typedef value_type
& reference
;
204 typedef const value_type
& const_reference
;
205 typedef int difference_type
;
206 typedef size_t size_type
;
208 // FIXME: same in dynarray.h
209 class reverse_iterator
211 typedef wxString value_type
;
212 typedef value_type
* pointer
;
213 typedef value_type
& reference
;
214 typedef reverse_iterator itor
;
215 friend itor
operator+(int o
, const itor
& it
);
216 friend itor
operator+(const itor
& it
, int o
);
217 friend itor
operator-(const itor
& it
, int o
);
218 friend difference_type
operator -(const itor
& i1
, const itor
& i2
);
221 reverse_iterator() : m_ptr(NULL
) { }
222 reverse_iterator(pointer ptr
) : m_ptr(ptr
) { }
223 reverse_iterator(const itor
& it
) : m_ptr(it
.m_ptr
) { }
224 reference
operator*() const { return *m_ptr
; }
225 pointer
operator->() const { return m_ptr
; }
226 itor
operator++() { --m_ptr
; return *this; }
228 { reverse_iterator tmp
= *this; --m_ptr
; return tmp
; }
229 itor
operator--() { ++m_ptr
; return *this; }
230 itor
operator--(int) { itor tmp
= *this; ++m_ptr
; return tmp
; }
231 bool operator ==(const itor
& it
) { return m_ptr
== it
.m_ptr
; }
232 bool operator !=(const itor
& it
) { return m_ptr
!= it
.m_ptr
; }
235 class const_reverse_iterator
237 typedef wxString value_type
;
238 typedef const value_type
* pointer
;
239 typedef const value_type
& reference
;
240 typedef const_reverse_iterator itor
;
241 friend itor
operator+(int o
, const itor
& it
);
242 friend itor
operator+(const itor
& it
, int o
);
243 friend itor
operator-(const itor
& it
, int o
);
244 friend difference_type
operator -(const itor
& i1
, const itor
& i2
);
247 const_reverse_iterator() : m_ptr(NULL
) { }
248 const_reverse_iterator(pointer ptr
) : m_ptr(ptr
) { }
249 const_reverse_iterator(const itor
& it
) : m_ptr(it
.m_ptr
) { }
250 const_reverse_iterator(const reverse_iterator
& it
) : m_ptr(it
.m_ptr
) { }
251 reference
operator*() const { return *m_ptr
; }
252 pointer
operator->() const { return m_ptr
; }
253 itor
operator++() { --m_ptr
; return *this; }
255 { itor tmp
= *this; --m_ptr
; return tmp
; }
256 itor
operator--() { ++m_ptr
; return *this; }
257 itor
operator--(int) { itor tmp
= *this; ++m_ptr
; return tmp
; }
258 bool operator ==(const itor
& it
) { return m_ptr
== it
.m_ptr
; }
259 bool operator !=(const itor
& it
) { return m_ptr
!= it
.m_ptr
; }
262 void assign(const_iterator first
, const_iterator last
);
263 void assign(size_type n
, const_reference v
)
264 { clear(); Add(v
, n
); }
265 reference
back() { return *(end() - 1); }
266 const_reference
back() const { return *(end() - 1); }
267 iterator
begin() { return (wxString
*)&(m_pItems
[0]); }
268 const_iterator
begin() const { return (wxString
*)&(m_pItems
[0]); }
269 size_type
capacity() const { return m_nSize
; }
270 void clear() { Clear(); }
271 bool empty() const { return IsEmpty(); }
272 iterator
end() { return begin() + GetCount(); }
273 const_iterator
end() const { return begin() + GetCount(); }
274 iterator
erase(iterator first
, iterator last
)
276 size_t idx
= first
- begin();
277 RemoveAt(idx
, last
- first
);
278 return begin() + idx
;
280 iterator
erase(iterator it
) { return erase(it
, it
+ 1); }
281 reference
front() { return *begin(); }
282 const_reference
front() const { return *begin(); }
283 void insert(iterator it
, size_type n
, const_reference v
)
284 { Insert(v
, it
- begin(), n
); }
285 iterator
insert(iterator it
, const_reference v
= value_type())
286 { size_t idx
= it
- begin(); Insert(v
, idx
); return begin() + idx
; }
287 void insert(iterator it
, const_iterator first
, const_iterator last
);
288 size_type
max_size() const { return INT_MAX
; }
289 void pop_back() { RemoveAt(GetCount() - 1); }
290 void push_back(const_reference v
) { Add(v
); }
291 reverse_iterator
rbegin() { return reverse_iterator(end() - 1); }
292 const_reverse_iterator
rbegin() const;
293 reverse_iterator
rend() { return reverse_iterator(begin() - 1); }
294 const_reverse_iterator
rend() const;
295 void reserve(size_type n
) /* base::reserve*/;
296 void resize(size_type n
, value_type v
= value_type());
297 size_type
size() const { return GetCount(); }
300 void Init(bool autoSort
); // common part of all ctors
301 void Copy(const wxArrayString
& src
); // copies the contents of another array
304 void Grow(size_t nIncrement
= 0); // makes array bigger if needed
305 void Free(); // free all the strings stored
307 void DoSort(); // common part of all Sort() variants
309 size_t m_nSize
, // current size of the array
310 m_nCount
; // current number of elements
312 wxChar
**m_pItems
; // pointer to data
314 bool m_autoSort
; // if TRUE, keep the array always sorted
317 class WXDLLIMPEXP_BASE wxSortedArrayString
: public wxArrayString
320 wxSortedArrayString() : wxArrayString(TRUE
)
322 wxSortedArrayString(const wxArrayString
& array
) : wxArrayString(TRUE
)