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 // ----------------------------------------------------------------------------
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!
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.
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 // ----------------------------------------------------------------------------
98 class WXDLLIMPEXP_BASE wxArrayString
101 // type of function used by wxArrayString::Sort()
102 typedef int (wxCMPFUNC_CONV
*CompareFunction
)(const wxString
& first
,
103 const wxString
& second
);
104 // type of function used by wxArrayString::Sort(), for compatibility with
106 typedef int (wxCMPFUNC_CONV
*CompareFunction2
)(wxString
* first
,
109 // constructors and destructor
111 wxArrayString() { Init(false); }
112 // if autoSort is true, the array is always sorted (in alphabetical order)
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!)
118 // of course, using explicit would be even better - if all compilers
120 wxArrayString(int autoSort
) { Init(autoSort
!= 0); }
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
);
126 wxArrayString(const wxArrayString
& array
);
127 // assignment operator
128 wxArrayString
& operator=(const wxArrayString
& src
);
129 // not virtual, this class should not be derived from
133 // empties the list, but doesn't release memory
135 // empties the list and releases memory
137 // preallocates memory for given number of items
138 void Alloc(size_t nCount
);
139 // minimzes the memory usage (by freeing all extra memory)
143 // number of elements in the array
144 size_t GetCount() const { return m_nCount
; }
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
; }
150 // items access (range checking is done in debug version)
151 // get item at position uiIndex
152 wxString
& Item(size_t nIndex
) const
154 wxASSERT_MSG( nIndex
< m_nCount
,
155 _T("wxArrayString: index out of bounds") );
157 return *(wxString
*)&(m_pItems
[nIndex
]);
161 wxString
& operator[](size_t nIndex
) const { return Item(nIndex
); }
163 wxString
& Last() const
165 wxASSERT_MSG( !IsEmpty(),
166 _T("wxArrayString: index out of bounds") );
167 return Item(Count() - 1);
172 // Search the element in the array, starting from the beginning if
173 // bFromEnd is false or from end otherwise. If bCase, comparison is case
174 // sensitive (default). Returns index of the first item matched or
176 int Index (const wxChar
*sz
, bool bCase
= true, bool bFromEnd
= false) const;
177 // add new element at the end (if the array is not sorted), return its
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
187 void RemoveAt(size_t nIndex
, size_t nRemove
= 1);
190 // sort array elements in alphabetical order (or reversed alphabetical
191 // order if reverseOrder parameter is true)
192 void Sort(bool reverseOrder
= false);
193 // sort array elements using specified comparaison function
194 void Sort(CompareFunction compareFunction
);
195 void Sort(CompareFunction2 compareFunction
);
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
); }
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
;
214 // TODO: this code duplicates the one in dynarray.h
215 class reverse_iterator
217 typedef wxString value_type
;
218 typedef value_type
* pointer
;
219 typedef value_type
& reference
;
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
);
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
; }
232 itor
& operator++() { --m_ptr
; return *this; }
233 const itor
operator++(int)
234 { reverse_iterator tmp
= *this; --m_ptr
; return tmp
; }
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
; }
241 class const_reverse_iterator
243 typedef wxString value_type
;
244 typedef const value_type
* pointer
;
245 typedef const value_type
& reference
;
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
);
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
; }
259 itor
& operator++() { --m_ptr
; return *this; }
260 const itor
operator++(int)
261 { itor tmp
= *this; --m_ptr
; return tmp
; }
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
; }
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
); }
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
)
285 size_t idx
= first
- begin();
286 RemoveAt(idx
, last
- first
);
287 return begin() + idx
;
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(); }
309 void Init(bool autoSort
); // common part of all ctors
310 void Copy(const wxArrayString
& src
); // copies the contents of another array
313 void Grow(size_t nIncrement
= 0); // makes array bigger if needed
314 void Free(); // free all the strings stored
316 void DoSort(); // common part of all Sort() variants
318 size_t m_nSize
, // current size of the array
319 m_nCount
; // current number of elements
321 const wxChar
**m_pItems
; // pointer to data
323 bool m_autoSort
; // if true, keep the array always sorted
326 class WXDLLIMPEXP_BASE wxSortedArrayString
: public wxArrayString
329 wxSortedArrayString() : wxArrayString(true)
331 wxSortedArrayString(const wxArrayString
& array
) : wxArrayString(true)
337 // this class provides a temporary wxString* from a
339 class WXDLLIMPEXP_BASE wxCArrayString
342 wxCArrayString( const wxArrayString
& array
)
343 : m_array( array
), m_strings( NULL
)
345 ~wxCArrayString() { delete[] m_strings
; }
347 size_t GetCount() const { return m_array
.GetCount(); }
348 wxString
* GetStrings()
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
];
358 const wxArrayString
& m_array
;