X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/7ed4fab7be1e5a4cc800ab023f4562caadee6c31..6443de026310552cacd68a6d0318e73d14929680:/src/common/dynarray.cpp?ds=sidebyside diff --git a/src/common/dynarray.cpp b/src/common/dynarray.cpp index a557970f6c..2d8e162378 100644 --- a/src/common/dynarray.cpp +++ b/src/common/dynarray.cpp @@ -182,8 +182,8 @@ int wxBaseArray::Index(long lItem, bool bFromEnd) const return wxNOT_FOUND; } -// search for an item in a sorted array (binary search) -int wxBaseArray::Index(long lItem, CMPFUNC fnCompare) const +// search for a place to insert an item into a sorted array (binary search) +size_t wxBaseArray::IndexForInsert(long lItem, CMPFUNC fnCompare) const { size_t i, lo = 0, @@ -198,12 +198,23 @@ int wxBaseArray::Index(long lItem, CMPFUNC fnCompare) const hi = i; else if ( res > 0 ) lo = i + 1; - else - return i; + else { + lo = i; + break; + } } - return wxNOT_FOUND; + return lo; } + +// search for an item in a sorted array (binary search) +int wxBaseArray::Index(long lItem, CMPFUNC fnCompare) const +{ + size_t n = IndexForInsert(lItem, fnCompare); + + return n < m_nCount && m_pItems[n] == lItem ? (int)n : wxNOT_FOUND; +} + // add item at the end void wxBaseArray::Add(long lItem) { @@ -214,28 +225,7 @@ void wxBaseArray::Add(long lItem) // add item assuming the array is sorted with fnCompare function void wxBaseArray::Add(long lItem, CMPFUNC fnCompare) { - size_t i, - lo = 0, - hi = m_nCount; - int res; - - while ( lo < hi ) { - i = (lo + hi)/2; - - res = (*fnCompare)((const void *)lItem, (const void *)m_pItems[i]); - if ( res < 0 ) - hi = i; - else if ( res > 0 ) - lo = i + 1; - else { - lo = hi = i; - break; - } - } - - wxASSERT( lo == hi ); // I hope I got binary search right :-) - - Insert(lItem, lo); + Insert(lItem, IndexForInsert(lItem, fnCompare)); } // add item at the given position