Allow specifying custom comparator for wxSortedArrayString.
Add a possibility to order wxSortedArrayString in some order different from the default alphabetical one. Closes #16330. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@76751 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
@@ -35,6 +35,7 @@ All:
|
||||
- Implement wxThread::SetPriority() for pthreads (Luca Bacci).
|
||||
- Add wxInt64 support to wxText{Input,Output}Stream (Alexander Bezzubikov).
|
||||
- Define wxOVERRIDE as override for supporting compilers (Thomas Goyne).
|
||||
- Allow specifying custom comparator for wxSortedArrayString (Catalin Raceanu).
|
||||
|
||||
All (GUI):
|
||||
|
||||
|
@@ -85,6 +85,9 @@ public:
|
||||
for ( size_t n = 0; n < src.size(); n++ )
|
||||
Add(src[n]);
|
||||
}
|
||||
wxEXPLICIT wxSortedArrayString(wxArrayString::CompareFunction compareFunction)
|
||||
: wxSortedArrayStringBase(compareFunction)
|
||||
{ }
|
||||
|
||||
int Index(const wxString& str, bool bCase = true, bool bFromEnd = false) const;
|
||||
|
||||
@@ -342,6 +345,8 @@ protected:
|
||||
void Init(bool autoSort); // common part of all ctors
|
||||
void Copy(const wxArrayString& src); // copies the contents of another array
|
||||
|
||||
CompareFunction m_compareFunction; // set only from wxSortedArrayString
|
||||
|
||||
private:
|
||||
void Grow(size_t nIncrement = 0); // makes array bigger if needed
|
||||
|
||||
@@ -360,6 +365,10 @@ public:
|
||||
{ }
|
||||
wxSortedArrayString(const wxArrayString& array) : wxArrayString(true)
|
||||
{ Copy(array); }
|
||||
|
||||
wxEXPLICIT wxSortedArrayString(CompareFunction compareFunction)
|
||||
: wxArrayString(true)
|
||||
{ m_compareFunction = compareFunction; }
|
||||
};
|
||||
|
||||
#endif // !wxUSE_STD_CONTAINERS
|
||||
|
@@ -54,7 +54,15 @@ class wxArrayString : public wxArray
|
||||
{
|
||||
public:
|
||||
/**
|
||||
The function type used with wxArrayString::Sort function.
|
||||
The function type used with wxArrayString::Sort().
|
||||
|
||||
This function uses the same conventions as the standard @c qsort()
|
||||
comparison function, that is it should return a negative value if the
|
||||
first argument is less than the second one, a positive value if the
|
||||
first argument is greater than the second one and 0 if the arguments
|
||||
are equal.
|
||||
|
||||
@since 3.1.0
|
||||
*/
|
||||
typedef int (*CompareFunction)(const wxString& first, const wxString& second);
|
||||
|
||||
@@ -280,6 +288,21 @@ public:
|
||||
class wxSortedArrayString : public wxArray
|
||||
{
|
||||
public:
|
||||
/**
|
||||
Default constructor.
|
||||
|
||||
The elements of the array are kept sorted in alphabetical order.
|
||||
*/
|
||||
wxSortedArrayString();
|
||||
|
||||
/**
|
||||
Constructs a sorted array using the specified @a compareFunction for
|
||||
item comparison.
|
||||
|
||||
@since 3.1.0
|
||||
*/
|
||||
wxSortedArrayString(CompareFunction compareFunction);
|
||||
|
||||
/**
|
||||
Conversion constructor.
|
||||
|
||||
|
@@ -72,6 +72,7 @@ void wxArrayString::Init(bool autoSort)
|
||||
m_nSize =
|
||||
m_nCount = 0;
|
||||
m_pItems = NULL;
|
||||
m_compareFunction = NULL;
|
||||
m_autoSort = autoSort;
|
||||
}
|
||||
|
||||
@@ -270,7 +271,7 @@ size_t wxArrayString::Add(const wxString& str, size_t nInsert)
|
||||
while ( lo < hi ) {
|
||||
i = (lo + hi)/2;
|
||||
|
||||
res = str.Cmp(m_pItems[i]);
|
||||
res = m_compareFunction ? m_compareFunction(str, m_pItems[i]) : str.Cmp(m_pItems[i]);
|
||||
if ( res < 0 )
|
||||
hi = i;
|
||||
else if ( res > 0 )
|
||||
|
Reference in New Issue
Block a user