CSortedList.C
//-----------------------------------------------------------------------------
// $Header: /asis/offline/ceres/cool/project/RCS/CSortedList.C,v 1.3 1997/06/26 18:54:13 messer Exp messer $
//
// COOL Program Library
// Copyright (C) CERES collaboration, 1996
//
// Implementation of template class CSortedList.
//
//-----------------------------------------------------------------------------
#ifndef RW_COMPILE_INSTANTIATE
#include "CSortedList.h"
#endif
//
// Binary search for the item that matches within the sort criterion to
// the one given as argument. For this comparism we can use the < operator.
// Note that we need the - operator defined here consistently with the < operator.
//
template<class T> size_t CSortedList<T>::_getIndexOfBestMatch(const T* item) const
{
if (items.length() < 1) return 0;
int leftBorder = 0;
int rightBorder = items.length() - 1;
int testPosition, jbestMatch;
//
// interval search for best match
//
while (rightBorder - leftBorder > 1) {
testPosition = leftBorder + (rightBorder - leftBorder) / 2;
if (*item < *(items(testPosition)))
rightBorder = testPosition;
else
leftBorder = testPosition;
}
if (*item-*(items(leftBorder)) < *item-*(items(rightBorder)))
jbestMatch = leftBorder;
else
jbestMatch = rightBorder;
return jbestMatch;
}