Initial Commit
This commit is contained in:
57
common/BINARRAY.TPP
Normal file
57
common/BINARRAY.TPP
Normal file
@@ -0,0 +1,57 @@
|
||||
#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
|
||||
Reference in New Issue
Block a user