X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/a7ea63e21fda950c658a477d59d0818aab993fc9..a4ea083bbdf710d3234dcbd2eafb5d0e078f8348:/src/common/arrstr.cpp?ds=sidebyside diff --git a/src/common/arrstr.cpp b/src/common/arrstr.cpp index bfc4d88981..f66fcdd4ee 100644 --- a/src/common/arrstr.cpp +++ b/src/common/arrstr.cpp @@ -4,7 +4,6 @@ // Author: Vadim Zeitlin // Modified by: // Created: 29/01/98 -// RCS-ID: $Id$ // Copyright: (c) 1998 Vadim Zeitlin // Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// @@ -22,15 +21,27 @@ #include "wx/arrstr.h" +#include "wx/beforestd.h" +#include +#include +#include "wx/afterstd.h" + // ============================================================================ // ArrayString // ============================================================================ -#include "wx/arrstr.h" +wxArrayString::wxArrayString(size_t sz, const char** a) +{ +#if !wxUSE_STD_CONTAINERS + Init(false); +#endif + for (size_t i=0; i < sz; i++) + Add(a[i]); +} -wxArrayString::wxArrayString(size_t sz, const wxChar** a) +wxArrayString::wxArrayString(size_t sz, const wchar_t** a) { -#if !wxUSE_STL +#if !wxUSE_STD_CONTAINERS Init(false); #endif for (size_t i=0; i < sz; i++) @@ -39,14 +50,14 @@ wxArrayString::wxArrayString(size_t sz, const wxChar** a) wxArrayString::wxArrayString(size_t sz, const wxString* a) { -#if !wxUSE_STL +#if !wxUSE_STD_CONTAINERS Init(false); #endif for (size_t i=0; i < sz; i++) Add(a[i]); } -#if !wxUSE_STL +#if !wxUSE_STD_CONTAINERS // size increment = min(50% of current size, ARRAY_MAXSIZE_INCREMENT) #define ARRAY_MAXSIZE_INCREMENT 4096 @@ -130,7 +141,7 @@ void wxArrayString::Grow(size_t nIncrement) pNew[j] = m_pItems[j]; // delete old memory (but do not release the strings!) - wxDELETEA(m_pItems); + delete [] m_pItems; m_pItems = pNew; } @@ -155,7 +166,7 @@ void wxArrayString::Clear() // dtor wxArrayString::~wxArrayString() { - wxDELETEA(m_pItems); + delete [] m_pItems; } void wxArrayString::reserve(size_t nSize) @@ -194,11 +205,12 @@ void wxArrayString::Shrink() pNew[j] = m_pItems[j]; delete [] m_pItems; m_pItems = pNew; + m_nSize = m_nCount; } } // searches the array for an item (forward or backwards) -int wxArrayString::Index(const wxChar *sz, bool bCase, bool bFromEnd) const +int wxArrayString::Index(const wxString& str, bool bCase, bool bFromEnd) const { if ( m_autoSort ) { // use binary search in the sorted array @@ -212,7 +224,7 @@ int wxArrayString::Index(const wxChar *sz, bool bCase, bool bFromEnd) const while ( lo < hi ) { i = (lo + hi)/2; - res = wxStrcmp(sz, m_pItems[i]); + res = str.compare(m_pItems[i]); if ( res < 0 ) hi = i; else if ( res > 0 ) @@ -229,7 +241,7 @@ int wxArrayString::Index(const wxChar *sz, bool bCase, bool bFromEnd) const if ( m_nCount > 0 ) { size_t ui = m_nCount; do { - if ( m_pItems[--ui].IsSameAs(sz, bCase) ) + if ( m_pItems[--ui].IsSameAs(str, bCase) ) return ui; } while ( ui != 0 ); @@ -237,7 +249,7 @@ int wxArrayString::Index(const wxChar *sz, bool bCase, bool bFromEnd) const } else { for( size_t ui = 0; ui < m_nCount; ui++ ) { - if( m_pItems[ui].IsSameAs(sz, bCase) ) + if( m_pItems[ui].IsSameAs(str, bCase) ) return ui; } } @@ -332,6 +344,14 @@ wxArrayString::insert(iterator it, const_iterator first, const_iterator last) } } +void wxArrayString::resize(size_type n, value_type v) +{ + if ( n < m_nCount ) + m_nCount = n; + else if ( n > m_nCount ) + Add(v, n - m_nCount); +} + // expand the array void wxArrayString::SetCount(size_t count) { @@ -356,7 +376,7 @@ void wxArrayString::RemoveAt(size_t nIndex, size_t nRemove) } // removes item from array (by value) -void wxArrayString::Remove(const wxChar *sz) +void wxArrayString::Remove(const wxString& sz) { int iIndex = Index(sz); @@ -366,85 +386,65 @@ void wxArrayString::Remove(const wxChar *sz) RemoveAt(iIndex); } -void wxArrayString::assign(const_iterator first, const_iterator last) -{ - reserve(last - first); - for(; first != last; ++first) - push_back(*first); -} - // ---------------------------------------------------------------------------- // sorting // ---------------------------------------------------------------------------- -// we can only sort one array at a time with the quick-sort based -// implementation -#if wxUSE_THREADS - // need a critical section to protect access to gs_compareFunction and - // gs_sortAscending variables - static wxCriticalSection gs_critsectStringSort; -#endif // wxUSE_THREADS - -// function to use for string comparaison -static wxArrayString::CompareFunction gs_compareFunction = NULL; - -// if we don't use the compare function, this flag tells us if we sort the -// array in ascending or descending order -static bool gs_sortAscending = true; - -// function which is called by quick sort -extern "C" int wxC_CALLING_CONV // LINKAGEMODE -wxStringCompareFunction(const void *first, const void *second) +// we need an adaptor as our predicates use qsort() convention and so return +// negative, null or positive value depending on whether the first item is less +// than, equal to or greater than the other one while we need a real boolean +// predicate now that we use std::sort() +struct wxSortPredicateAdaptor { - wxString *strFirst = (wxString *)first; - wxString *strSecond = (wxString *)second; + wxSortPredicateAdaptor(wxArrayString::CompareFunction compareFunction) + : m_compareFunction(compareFunction) + { + } - if ( gs_compareFunction ) { - return gs_compareFunction(*strFirst, *strSecond); - } - else { - // maybe we should use wxStrcoll - int result = strFirst->Cmp(*strSecond); + bool operator()(const wxString& first, const wxString& second) const + { + return (*m_compareFunction)(first, second) < 0; + } - return gs_sortAscending ? result : -result; - } -} + wxArrayString::CompareFunction m_compareFunction; +}; -// sort array elements using passed comparaison function void wxArrayString::Sort(CompareFunction compareFunction) { - wxCRIT_SECT_LOCKER(lockCmpFunc, gs_critsectStringSort); - - wxASSERT( !gs_compareFunction ); // must have been reset to NULL - gs_compareFunction = compareFunction; + wxCHECK_RET( !m_autoSort, wxT("can't use this method with sorted arrays") ); - DoSort(); - - // reset it to NULL so that Sort(bool) will work the next time - gs_compareFunction = NULL; + std::sort(m_pItems, m_pItems + m_nCount, + wxSortPredicateAdaptor(compareFunction)); } -extern "C" +struct wxSortPredicateAdaptor2 { - typedef int (wxC_CALLING_CONV * wxStringCompareFn)(const void *first, - const void *second); -} + wxSortPredicateAdaptor2(wxArrayString::CompareFunction2 compareFunction) + : m_compareFunction(compareFunction) + { + } + + bool operator()(const wxString& first, const wxString& second) const + { + return (*m_compareFunction)(const_cast(&first), + const_cast(&second)) < 0; + } + + wxArrayString::CompareFunction2 m_compareFunction; +}; void wxArrayString::Sort(CompareFunction2 compareFunction) { - qsort(m_pItems, m_nCount, sizeof(wxString), (wxStringCompareFn)compareFunction); + std::sort(m_pItems, m_pItems + m_nCount, + wxSortPredicateAdaptor2(compareFunction)); } void wxArrayString::Sort(bool reverseOrder) { - Sort(reverseOrder ? wxStringSortDescending : wxStringSortAscending); -} - -void wxArrayString::DoSort() -{ - wxCHECK_RET( !m_autoSort, wxT("can't use this method with sorted arrays") ); - - qsort(m_pItems, m_nCount, sizeof(wxString), wxStringCompareFunction); + if ( reverseOrder ) + std::sort(m_pItems, m_pItems + m_nCount, std::greater()); + else // normal sort + std::sort(m_pItems, m_pItems + m_nCount); } bool wxArrayString::operator==(const wxArrayString& a) const @@ -461,19 +461,7 @@ bool wxArrayString::operator==(const wxArrayString& a) const return true; } -#endif // !wxUSE_STL - -int wxCMPFUNC_CONV wxStringSortAscending(wxString* s1, wxString* s2) -{ - return s1->Cmp(*s2); -} - -int wxCMPFUNC_CONV wxStringSortDescending(wxString* s1, wxString* s2) -{ - return -s1->Cmp(*s2); -} - - +#endif // !wxUSE_STD_CONTAINERS // =========================================================================== // wxJoin and wxSplit