diff --git a/include/wx/arrstr.h b/include/wx/arrstr.h index bed1098d74..951dd16d1b 100644 --- a/include/wx/arrstr.h +++ b/include/wx/arrstr.h @@ -398,8 +398,10 @@ private: // (if the old buffer is big enough, just return NULL). wxString *Grow(size_t nIncrement); - // Binary search in the sorted array - size_t BinarySearch(const wxString& str, bool equal = false) const; + // Binary search in the sorted array: return the index of the string if it's + // present, otherwise, if lowerBound is true, return the position at which + // the string should be inserted and if it's false return wxNOT_FOUND. + size_t BinarySearch(const wxString& str, bool lowerBound) const; size_t m_nSize, // current size of the array m_nCount; // current number of elements diff --git a/src/common/arrstr.cpp b/src/common/arrstr.cpp index 3611bc4276..65317bb1e8 100644 --- a/src/common/arrstr.cpp +++ b/src/common/arrstr.cpp @@ -379,7 +379,7 @@ void wxArrayString::Shrink() } // Binary search in the sorted array -size_t wxArrayString::BinarySearch(const wxString& str, bool equal) const +size_t wxArrayString::BinarySearch(const wxString& str, bool lowerBound) const { size_t lo = 0, @@ -398,7 +398,7 @@ size_t wxArrayString::BinarySearch(const wxString& str, bool equal) const return i; } wxASSERT_MSG(lo == hi, wxT("binary search broken")); - return equal ? wxNOT_FOUND : lo; + return lowerBound ? lo : wxNOT_FOUND; } // searches the array for an item (forward or backwards) @@ -408,7 +408,7 @@ int wxArrayString::Index(const wxString& str, bool bCase, bool bFromEnd) const // use binary search in the sorted array wxASSERT_MSG( bCase && !bFromEnd, wxT("search parameters ignored for auto sorted array") ); - return BinarySearch(str, true); + return BinarySearch(str, false /* not lower bound */); } else { // use linear search in unsorted array @@ -438,7 +438,7 @@ size_t wxArrayString::Add(const wxString& str, size_t nInsert) { if ( m_autoSort ) { // insert the string at the correct position to keep the array sorted - size_t nIndex = BinarySearch(str); + size_t nIndex = BinarySearch(str, true /* return lower bound */); Insert(str, nIndex, nInsert); return nIndex; }