Files
Work/common/BINARRAY.TPP
2024-08-07 09:09:36 -04:00

58 lines
1.4 KiB
C++

#ifndef _COMMON_BINARYSEARCHARRAY_TPP_
#define _COMMON_BINARYSEARCHARRAY_TPP_
template <class T>
WORD BinarySearchArray<T>::searchItem(const T &desiredItem,T &foundItem)
{
LONG itemIndex;
return searchItem(desiredItem,foundItem,itemIndex);
}
template <class T>
WORD BinarySearchArray<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 BinarySearchArray<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