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
);
50 void Sort(CMPFUNCwxString function
) { wxArrayStringBase::Sort(function
); }
53 class WXDLLIMPEXP_BASE wxSortedArrayString
: public wxSortedArrayStringBase
56 wxSortedArrayString() : wxSortedArrayStringBase(wxStringSortAscending
)
58 wxSortedArrayString(const wxSortedArrayString
& array
)
59 : wxSortedArrayStringBase(array
)
61 wxSortedArrayString(const wxArrayString
& src
)
62 : wxSortedArrayStringBase(wxStringSortAscending
)
66 for ( size_t n
= 0; n
< src
.size(); n
++ )
70 int Index(const wxChar
* sz
, bool bCase
= true, bool bFromEnd
= false) const;
73 #else // if !wxUSE_STL
75 // ----------------------------------------------------------------------------
76 // The string array uses it's knowledge of internal structure of the wxString
77 // class to optimize string storage. Normally, we would store pointers to
78 // string, but as wxString is, in fact, itself a pointer (sizeof(wxString) is
79 // sizeof(char *)) we store these pointers instead. The cast to "wxString *" is
80 // really all we need to turn such pointer into a string!
82 // Of course, it can be called a dirty hack, but we use twice less memory and
83 // this approach is also more speed efficient, so it's probably worth it.
85 // Usage notes: when a string is added/inserted, a new copy of it is created,
86 // so the original string may be safely deleted. When a string is retrieved
87 // from the array (operator[] or Item() method), a reference is returned.
88 // ----------------------------------------------------------------------------
90 class WXDLLIMPEXP_BASE wxArrayString
93 // type of function used by wxArrayString::Sort()
94 typedef int (wxCMPFUNC_CONV
*CompareFunction
)(const wxString
& first
,
95 const wxString
& second
);
96 // type of function used by wxArrayString::Sort(), for compatibility with
98 typedef int (wxCMPFUNC_CONV
*CompareFunction2
)(wxString
* first
,
101 // constructors and destructor
103 wxArrayString() { Init(false); }
104 // if autoSort is true, the array is always sorted (in alphabetical order)
106 // NB: the reason for using int and not bool is that like this we can avoid
107 // using this ctor for implicit conversions from "const char *" (which
108 // we'd like to be implicitly converted to wxString instead!)
110 // of course, using explicit would be even better - if all compilers
112 wxArrayString(int autoSort
) { Init(autoSort
!= 0); }
114 wxArrayString(const wxArrayString
& array
);
115 // assignment operator
116 wxArrayString
& operator=(const wxArrayString
& src
);
117 // not virtual, this class should not be derived from
121 // empties the list, but doesn't release memory
123 // empties the list and releases memory
125 // preallocates memory for given number of items
126 void Alloc(size_t nCount
);
127 // minimzes the memory usage (by freeing all extra memory)
131 // number of elements in the array
132 size_t GetCount() const { return m_nCount
; }
134 bool IsEmpty() const { return m_nCount
== 0; }
135 // number of elements in the array (GetCount is preferred API)
136 size_t Count() const { return m_nCount
; }
138 // items access (range checking is done in debug version)
139 // get item at position uiIndex
140 wxString
& Item(size_t nIndex
) const
142 wxASSERT_MSG( nIndex
< m_nCount
,
143 _T("wxArrayString: index out of bounds") );
145 return *(wxString
*)&(m_pItems
[nIndex
]);
149 wxString
& operator[](size_t nIndex
) const { return Item(nIndex
); }
151 wxString
& Last() const
153 wxASSERT_MSG( !IsEmpty(),
154 _T("wxArrayString: index out of bounds") );
155 return Item(Count() - 1);
158 // return a wxString[], useful for the controls which
159 // take one in their ctor. You must delete[] it yourself
160 // once you are done with it. Will return NULL if the
161 // ArrayString was empty.
162 #if WXWIN_COMPATIBILITY_2_4
163 wxString
* GetStringArray() const;
167 // Search the element in the array, starting from the beginning if
168 // bFromEnd is false or from end otherwise. If bCase, comparison is case
169 // sensitive (default). Returns index of the first item matched or
171 int Index (const wxChar
*sz
, bool bCase
= true, bool bFromEnd
= false) const;
172 // add new element at the end (if the array is not sorted), return its
174 size_t Add(const wxString
& str
, size_t nInsert
= 1);
175 // add new element at given position
176 void Insert(const wxString
& str
, size_t uiIndex
, size_t nInsert
= 1);
177 // expand the array to have count elements
178 void SetCount(size_t count
);
179 // remove first item matching this value
180 void Remove(const wxChar
*sz
);
181 // remove item by index
182 #if WXWIN_COMPATIBILITY_2_4
183 void Remove(size_t nIndex
, size_t nRemove
= 1) { RemoveAt(nIndex
, nRemove
); }
185 void RemoveAt(size_t nIndex
, size_t nRemove
= 1);
188 // sort array elements in alphabetical order (or reversed alphabetical
189 // order if reverseOrder parameter is true)
190 void Sort(bool reverseOrder
= false);
191 // sort array elements using specified comparaison function
192 void Sort(CompareFunction compareFunction
);
193 void Sort(CompareFunction2 compareFunction
);
196 // compare two arrays case sensitively
197 bool operator==(const wxArrayString
& a
) const;
198 // compare two arrays case sensitively
199 bool operator!=(const wxArrayString
& a
) const { return !(*this == a
); }
201 // STL-like interface
202 typedef wxString value_type
;
203 typedef value_type
* pointer
;
204 typedef const value_type
* const_pointer
;
205 typedef value_type
* iterator
;
206 typedef const value_type
* const_iterator
;
207 typedef value_type
& reference
;
208 typedef const value_type
& const_reference
;
209 typedef int difference_type
;
210 typedef size_t size_type
;
212 // TODO: this code duplicates the one in dynarray.h
213 class reverse_iterator
215 typedef wxString value_type
;
216 typedef value_type
* pointer
;
217 typedef value_type
& reference
;
218 typedef reverse_iterator itor
;
219 friend itor
operator+(int o
, const itor
& it
);
220 friend itor
operator+(const itor
& it
, int o
);
221 friend itor
operator-(const itor
& it
, int o
);
222 friend difference_type
operator -(const itor
& i1
, const itor
& i2
);
225 reverse_iterator() : m_ptr(NULL
) { }
226 reverse_iterator(pointer ptr
) : m_ptr(ptr
) { }
227 reverse_iterator(const itor
& it
) : m_ptr(it
.m_ptr
) { }
228 reference
operator*() const { return *m_ptr
; }
229 pointer
operator->() const { return m_ptr
; }
230 itor
operator++() { --m_ptr
; return *this; }
232 { reverse_iterator tmp
= *this; --m_ptr
; return tmp
; }
233 itor
operator--() { ++m_ptr
; return *this; }
234 itor
operator--(int) { itor tmp
= *this; ++m_ptr
; return tmp
; }
235 bool operator ==(const itor
& it
) { return m_ptr
== it
.m_ptr
; }
236 bool operator !=(const itor
& it
) { return m_ptr
!= it
.m_ptr
; }
239 class const_reverse_iterator
241 typedef wxString value_type
;
242 typedef const value_type
* pointer
;
243 typedef const value_type
& reference
;
244 typedef const_reverse_iterator itor
;
245 friend itor
operator+(int o
, const itor
& it
);
246 friend itor
operator+(const itor
& it
, int o
);
247 friend itor
operator-(const itor
& it
, int o
);
248 friend difference_type
operator -(const itor
& i1
, const itor
& i2
);
251 const_reverse_iterator() : m_ptr(NULL
) { }
252 const_reverse_iterator(pointer ptr
) : m_ptr(ptr
) { }
253 const_reverse_iterator(const itor
& it
) : m_ptr(it
.m_ptr
) { }
254 const_reverse_iterator(const reverse_iterator
& it
) : m_ptr(it
.m_ptr
) { }
255 reference
operator*() const { return *m_ptr
; }
256 pointer
operator->() const { return m_ptr
; }
257 itor
operator++() { --m_ptr
; return *this; }
259 { itor tmp
= *this; --m_ptr
; return tmp
; }
260 itor
operator--() { ++m_ptr
; return *this; }
261 itor
operator--(int) { itor tmp
= *this; ++m_ptr
; return tmp
; }
262 bool operator ==(const itor
& it
) { return m_ptr
== it
.m_ptr
; }
263 bool operator !=(const itor
& it
) { return m_ptr
!= it
.m_ptr
; }
266 wxArrayString(const_iterator first
, const_iterator last
)
267 { Init(false); assign(first
, last
); }
268 wxArrayString(size_type n
, const_reference v
) { Init(false); assign(n
, v
); }
269 void assign(const_iterator first
, const_iterator last
);
270 void assign(size_type n
, const_reference v
)
271 { clear(); Add(v
, n
); }
272 reference
back() { return *(end() - 1); }
273 const_reference
back() const { return *(end() - 1); }
274 iterator
begin() { return (wxString
*)&(m_pItems
[0]); }
275 const_iterator
begin() const { return (wxString
*)&(m_pItems
[0]); }
276 size_type
capacity() const { return m_nSize
; }
277 void clear() { Clear(); }
278 bool empty() const { return IsEmpty(); }
279 iterator
end() { return begin() + GetCount(); }
280 const_iterator
end() const { return begin() + GetCount(); }
281 iterator
erase(iterator first
, iterator last
)
283 size_t idx
= first
- begin();
284 RemoveAt(idx
, last
- first
);
285 return begin() + idx
;
287 iterator
erase(iterator it
) { return erase(it
, it
+ 1); }
288 reference
front() { return *begin(); }
289 const_reference
front() const { return *begin(); }
290 void insert(iterator it
, size_type n
, const_reference v
)
291 { Insert(v
, it
- begin(), n
); }
292 iterator
insert(iterator it
, const_reference v
= value_type())
293 { size_t idx
= it
- begin(); Insert(v
, idx
); return begin() + idx
; }
294 void insert(iterator it
, const_iterator first
, const_iterator last
);
295 size_type
max_size() const { return INT_MAX
; }
296 void pop_back() { RemoveAt(GetCount() - 1); }
297 void push_back(const_reference v
) { Add(v
); }
298 reverse_iterator
rbegin() { return reverse_iterator(end() - 1); }
299 const_reverse_iterator
rbegin() const;
300 reverse_iterator
rend() { return reverse_iterator(begin() - 1); }
301 const_reverse_iterator
rend() const;
302 void reserve(size_type n
) /* base::reserve*/;
303 void resize(size_type n
, value_type v
= value_type());
304 size_type
size() const { return GetCount(); }
307 void Init(bool autoSort
); // common part of all ctors
308 void Copy(const wxArrayString
& src
); // copies the contents of another array
311 void Grow(size_t nIncrement
= 0); // makes array bigger if needed
312 void Free(); // free all the strings stored
314 void DoSort(); // common part of all Sort() variants
316 size_t m_nSize
, // current size of the array
317 m_nCount
; // current number of elements
319 wxChar
**m_pItems
; // pointer to data
321 bool m_autoSort
; // if true, keep the array always sorted
324 class WXDLLIMPEXP_BASE wxSortedArrayString
: public wxArrayString
327 wxSortedArrayString() : wxArrayString(true)
329 wxSortedArrayString(const wxArrayString
& array
) : wxArrayString(true)
335 // this class provides a temporary wxString* from a
337 class WXDLLIMPEXP_BASE wxCArrayString
340 wxCArrayString( const wxArrayString
& array
)
341 : m_array( array
), m_strings( NULL
)
343 ~wxCArrayString() { delete[] m_strings
; }
345 size_t GetCount() const { return m_array
.GetCount(); }
346 wxString
* GetStrings()
348 if( m_strings
) return m_strings
;
349 size_t count
= m_array
.GetCount();
350 m_strings
= new wxString
[count
];
351 for( size_t i
= 0; i
< count
; ++i
)
352 m_strings
[i
] = m_array
[i
];
356 const wxArrayString
& m_array
;