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:
Vadim Zeitlin
2014-06-23 01:08:50 +00:00
parent 8161cf19e9
commit e2e79bd000
4 changed files with 36 additions and 2 deletions

View File

@@ -35,6 +35,7 @@ All:
- Implement wxThread::SetPriority() for pthreads (Luca Bacci). - Implement wxThread::SetPriority() for pthreads (Luca Bacci).
- Add wxInt64 support to wxText{Input,Output}Stream (Alexander Bezzubikov). - Add wxInt64 support to wxText{Input,Output}Stream (Alexander Bezzubikov).
- Define wxOVERRIDE as override for supporting compilers (Thomas Goyne). - Define wxOVERRIDE as override for supporting compilers (Thomas Goyne).
- Allow specifying custom comparator for wxSortedArrayString (Catalin Raceanu).
All (GUI): All (GUI):

View File

@@ -85,6 +85,9 @@ public:
for ( size_t n = 0; n < src.size(); n++ ) for ( size_t n = 0; n < src.size(); n++ )
Add(src[n]); Add(src[n]);
} }
wxEXPLICIT wxSortedArrayString(wxArrayString::CompareFunction compareFunction)
: wxSortedArrayStringBase(compareFunction)
{ }
int Index(const wxString& str, bool bCase = true, bool bFromEnd = false) const; 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 Init(bool autoSort); // common part of all ctors
void Copy(const wxArrayString& src); // copies the contents of another array void Copy(const wxArrayString& src); // copies the contents of another array
CompareFunction m_compareFunction; // set only from wxSortedArrayString
private: private:
void Grow(size_t nIncrement = 0); // makes array bigger if needed void Grow(size_t nIncrement = 0); // makes array bigger if needed
@@ -360,6 +365,10 @@ public:
{ } { }
wxSortedArrayString(const wxArrayString& array) : wxArrayString(true) wxSortedArrayString(const wxArrayString& array) : wxArrayString(true)
{ Copy(array); } { Copy(array); }
wxEXPLICIT wxSortedArrayString(CompareFunction compareFunction)
: wxArrayString(true)
{ m_compareFunction = compareFunction; }
}; };
#endif // !wxUSE_STD_CONTAINERS #endif // !wxUSE_STD_CONTAINERS

View File

@@ -54,7 +54,15 @@ class wxArrayString : public wxArray
{ {
public: 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); typedef int (*CompareFunction)(const wxString& first, const wxString& second);
@@ -280,6 +288,21 @@ public:
class wxSortedArrayString : public wxArray class wxSortedArrayString : public wxArray
{ {
public: 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. Conversion constructor.

View File

@@ -72,6 +72,7 @@ void wxArrayString::Init(bool autoSort)
m_nSize = m_nSize =
m_nCount = 0; m_nCount = 0;
m_pItems = NULL; m_pItems = NULL;
m_compareFunction = NULL;
m_autoSort = autoSort; m_autoSort = autoSort;
} }
@@ -270,7 +271,7 @@ size_t wxArrayString::Add(const wxString& str, size_t nInsert)
while ( lo < hi ) { while ( lo < hi ) {
i = (lo + hi)/2; 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 ) if ( res < 0 )
hi = i; hi = i;
else if ( res > 0 ) else if ( res > 0 )