From: Mattia Barbon Date: Thu, 22 Jul 2004 19:08:21 +0000 (+0000) Subject: Added compatibility functions for wxUSE_STL = 1: X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/4e75b65f7c801f98fd75e0863d29d59e4b12b6fb Added compatibility functions for wxUSE_STL = 1: void wxArrayString::Sort(CompareFunction function) void wxArrayString::Sort(bool reverseOrder). git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@28404 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/include/wx/arrstr.h b/include/wx/arrstr.h index 9643b64e76..11f32a466f 100644 --- a/include/wx/arrstr.h +++ b/include/wx/arrstr.h @@ -36,10 +36,17 @@ _WX_DEFINE_SORTED_TYPEARRAY_2(wxString, wxSortedArrayStringBase, class WXDLLIMPEXP_BASE wxArrayString : public wxArrayStringBase { public: + // type of function used by wxArrayString::Sort() + typedef int (wxCMPFUNC_CONV *CompareFunction)(const wxString& first, + const wxString& second); + wxArrayString() { } wxArrayString(const wxArrayString& a) : wxArrayStringBase(a) { } int Index(const wxChar* sz, bool bCase = true, bool bFromEnd = false) const; + + void Sort(bool reverseOrder = false); + void Sort(CompareFunction function); }; class WXDLLIMPEXP_BASE wxSortedArrayString : public wxSortedArrayStringBase diff --git a/src/common/dynarray.cpp b/src/common/dynarray.cpp index f55c594a3b..738e9813a3 100644 --- a/src/common/dynarray.cpp +++ b/src/common/dynarray.cpp @@ -470,28 +470,53 @@ int wxArrayString::Index(const wxChar* sz, bool bCase, bool WXUNUSED(bFromEnd)) return it == end() ? wxNOT_FOUND : it - begin(); } +template class wxStringCompareLess { public: - typedef int (wxCMPFUNC_CONV * fnc)(const wxChar*, const wxChar*); -public: - wxStringCompareLess(fnc f) : m_f(f) { } + wxStringCompareLess(F f) : m_f(f) { } bool operator()(const wxChar* s1, const wxChar* s2) { return m_f(s1, s2) < 0; } + bool operator()(const wxString& s1, const wxString& s2) + { return m_f(s1, s2) < 0; } private: - fnc m_f; + F m_f; }; +template +wxStringCompareLess wxStringCompare(F f) +{ + return wxStringCompareLess(f); +} + +void wxArrayString::Sort(CompareFunction function) +{ + std::sort(begin(), end(), wxStringCompare(function)); +} + +void wxArrayString::Sort(bool reverseOrder) +{ + if (reverseOrder) + { + std::sort(begin(), end(), std::greater()); + } + else + { + std::sort(begin(), end()); + } +} + int wxSortedArrayString::Index(const wxChar* sz, bool bCase, bool WXUNUSED(bFromEnd)) const { wxSortedArrayString::const_iterator it; + wxString s(sz); if (bCase) - it = std::lower_bound(begin(), end(), sz, - wxStringCompareLess(wxStrcmpCppWrapper)); + it = std::lower_bound(begin(), end(), s, + wxStringCompare(wxStrcmpCppWrapper)); else - it = std::lower_bound(begin(), end(), sz, - wxStringCompareLess(wxStricmpCppWrapper)); + it = std::lower_bound(begin(), end(), s, + wxStringCompare(wxStricmpCppWrapper)); if (it == end()) return wxNOT_FOUND;