58 lines
1.4 KiB
C++
58 lines
1.4 KiB
C++
#ifndef _COMMON_BINSRCH_TPP_
|
|
#define _COMMON_BINSRCH_TPP_
|
|
|
|
template <class T>
|
|
WORD BinarySearch<T>::searchItem(const T &desiredItem,T &foundItem)
|
|
{
|
|
LONG itemIndex;
|
|
return searchItem(desiredItem,foundItem,itemIndex);
|
|
}
|
|
|
|
template <class T>
|
|
WORD BinarySearch<T>::searchItem(const T &desiredItem,Block<T> &foundItems)
|
|
{
|
|
LONG foundIndex;
|
|
LONG startIndex;
|
|
LONG endIndex;
|
|
T foundItem;
|
|
|
|
foundItems.remove();
|
|
if(!searchItem(desiredItem,foundItem,foundIndex))return FALSE;
|
|
startIndex=endIndex=foundIndex;
|
|
for(;startIndex>=0&&foundItem==mSortedVector[startIndex];startIndex--);
|
|
startIndex++;
|
|
for(;endIndex<itemCount()&&foundItem==mSortedVector[endIndex];endIndex++);
|
|
endIndex--;
|
|
while(startIndex<=endIndex)foundItems.insert(&mSortedVector[startIndex++]);
|
|
return foundItems.size();
|
|
}
|
|
|
|
template <class T>
|
|
WORD BinarySearch<T>::searchItem(const T &desiredItem,T &foundItem,LONG &foundIndex)
|
|
{
|
|
LONG lowerBound;
|
|
LONG upperBound;
|
|
LONG itemIndex;
|
|
WORD returnCode(FALSE);
|
|
|
|
if(!itemCount())return FALSE;
|
|
lowerBound=0L;
|
|
upperBound=mItemCount-1L;
|
|
while(TRUE)
|
|
{
|
|
itemIndex=(lowerBound+upperBound)/2L;
|
|
if(desiredItem>mSortedVector[itemIndex])lowerBound=itemIndex+1L;
|
|
else upperBound=itemIndex-1;
|
|
if(mSortedVector[itemIndex]==desiredItem)
|
|
{
|
|
foundItem=mSortedVector[itemIndex];
|
|
foundIndex=itemIndex;
|
|
returnCode=TRUE;
|
|
break;
|
|
}
|
|
if(lowerBound>upperBound)break;
|
|
}
|
|
return returnCode;
|
|
}
|
|
#endif
|