From e2e79bd000181384eb56081e365e137867f608cd Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Mon, 23 Jun 2014 01:08:50 +0000 Subject: [PATCH] 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 --- docs/changes.txt | 1 + include/wx/arrstr.h | 9 +++++++++ interface/wx/arrstr.h | 25 ++++++++++++++++++++++++- src/common/arrstr.cpp | 3 ++- 4 files changed, 36 insertions(+), 2 deletions(-) diff --git a/docs/changes.txt b/docs/changes.txt index f3899f8ba5..a1a75ab8ce 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -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): diff --git a/include/wx/arrstr.h b/include/wx/arrstr.h index 3877ba158e..d0d614a1ea 100644 --- a/include/wx/arrstr.h +++ b/include/wx/arrstr.h @@ -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 diff --git a/interface/wx/arrstr.h b/interface/wx/arrstr.h index d22f7a0e2c..3937ecbb94 100644 --- a/interface/wx/arrstr.h +++ b/interface/wx/arrstr.h @@ -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. diff --git a/src/common/arrstr.cpp b/src/common/arrstr.cpp index f66fcdd4ee..03441eef3e 100644 --- a/src/common/arrstr.cpp +++ b/src/common/arrstr.cpp @@ -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 )