original function:
void
CDataServer::GetDatasetPointData(WORD datasetId,
const CGetGraphPointDataInfo& GetInfo,
CGetGraphPointDataResultInfo& ReturnedResultInfo)const
{
CGraphDataServerAutoLock AutoLock(this, datasetId);
{
CPointDouble PreviousInRangeDataPoint(0, 0);
bool previousInRangePointValid = false;
if(GetInfo.m_xMaxPeripheral && ReturnedResultInfo.m_found
&& !ReturnedResultInfo.m_foundPeripheral)
{
PreviousInRangeDataPoint = ReturnedResultInfo.m_DataPoint;
previousInRangePointValid = true;
}
bool previousPointFoundWasMinPeripheral = false;
{
if(GetInfo.m_xMinPeripheral && ReturnedResultInfo.m_found
&& ReturnedResultInfo.m_foundPeripheral)
{
previousPointFoundWasMinPeripheral = true;
}
}
ReturnedResultInfo.m_found = false;
ReturnedResultInfo.m_foundPeripheral = false;
const CDataset* Dataset = GetDatasetById(datasetId);
if(Dataset)
{
int searchIndex = 0;
const int increment = 1;
{
if(Dataset->IsValidIndex(GetInfo.m_nextIterationHint))
{
// Start searching at this index.
searchIndex = GetInfo.m_nextIterationHint;
}
}
CPointDouble PreviousOutOfRangeDataPoint(0, 0);
bool previousOutOfRangePointValid = false;
int previousRangeArea = -2;
while(Dataset->IsValidIndex(searchIndex))
{
CPointDouble DataPoint(0, 0);
Dataset->GetDataPoint(searchIndex, DataPoint);
if(!GetInfo.IsYValueInRange(DataPoint.y))
{
searchIndex += increment;
}
else
{
int rangeArea = GetInfo.GetXRangeArea(DataPoint.x);
if(rangeArea == 0)
{
// Got it.
if(GetInfo.m_xMinPeripheral && previousOutOfRangePointValid)
{
// But return the peripheral point.
ReturnedResultInfo.m_DataPoint = PreviousOutOfRangeDataPoint;
ReturnedResultInfo.m_found = true;
ReturnedResultInfo.m_foundPeripheral = true;
ReturnedResultInfo.m_currentIterationHint = searchIndex - 1;
ReturnedResultInfo.m_nextIterationHint = searchIndex;
}
else
{
// Return a point in range.
ReturnedResultInfo.m_DataPoint = DataPoint;
ReturnedResultInfo.m_found = true;
ReturnedResultInfo.m_currentIterationHint = searchIndex;
ReturnedResultInfo.m_nextIterationHint = searchIndex + increment;
}
break;
}
else
{
searchIndex += increment;
if(GetInfo.m_xMaxPeripheral)
{
if(rangeArea == 1)
{
// Value was too high.
if(previousRangeArea == -1 &&
GetInfo.m_xMinPeripheral && previousOutOfRangePointValid &&
!previousPointFoundWasMinPeripheral)
{
// There we no points in range (we went from too low to too
// high).
// Return a minimal peripheral point.
ReturnedResultInfo.m_DataPoint = PreviousOutOfRangeDataPoint;
ReturnedResultInfo.m_found = true;
ReturnedResultInfo.m_foundPeripheral = true;
ReturnedResultInfo.m_currentIterationHint = searchIndex;
ReturnedResultInfo.m_nextIterationHint = searchIndex + 1;
}
else
{
// Return a maximal peripheral point.
ReturnedResultInfo.m_DataPoint = DataPoint;
ReturnedResultInfo.m_found = true;
ReturnedResultInfo.m_foundPeripheral = true;
ReturnedResultInfo.m_currentIterationHint = searchIndex;
ReturnedResultInfo.m_nextIterationHint = searchIndex + increment;
}
break;
}
if(GetInfo.m_xMinPeripheral)
{
PreviousOutOfRangeDataPoint = DataPoint;
previousOutOfRangePointValid = true;
}
}
previousRangeArea = rangeArea;
}
}
}
}
}
}
i selected the code inside the while loop and did a refactor. and got:
void
CDataServer::GetDatasetPointData(WORD datasetId,
const CGetGraphPointDataInfo& GetInfo,
CGetGraphPointDataResultInfo& ReturnedResultInfo)const
{
CGraphDataServerAutoLock AutoLock(this, datasetId);
{
CPointDouble PreviousInRangeDataPoint(0, 0);
bool previousInRangePointValid = false;
if(GetInfo.m_xMaxPeripheral && ReturnedResultInfo.m_found
&& !ReturnedResultInfo.m_foundPeripheral)
{
PreviousInRangeDataPoint = ReturnedResultInfo.m_DataPoint;
previousInRangePointValid = true;
}
bool previousPointFoundWasMinPeripheral = false;
{
if(GetInfo.m_xMinPeripheral && ReturnedResultInfo.m_found
&& ReturnedResultInfo.m_foundPeripheral)
{
previousPointFoundWasMinPeripheral = true;
}
}
ReturnedResultInfo.m_found = false;
ReturnedResultInfo.m_foundPeripheral = false;
const CDataset* Dataset = GetDatasetById(datasetId);
if(Dataset)
{
int searchIndex = 0;
const int increment = 1;
{
if(Dataset->IsValidIndex(GetInfo.m_nextIterationHint))
{
// Start searching at this index.
searchIndex = GetInfo.m_nextIterationHint;
}
}
CPointDouble PreviousOutOfRangeDataPoint(0, 0);
bool previousOutOfRangePointValid = false;
int previousRangeArea = -2;
while(Dataset->IsValidIndex(searchIndex))
{
Refactored(Dataset, searchIndex, GetInfo, increment, previousOutOfRangePointValid, ReturnedResultInfo, PreviousOutOfRangeDataPoint, previousRangeArea, previousPointFoundWasMinPeripheral);
return;
}
}
}
}
and in the header:
void Refactored( const CDataset* Dataset, int &searchIndex, const CGetGraphPointDataInfo &GetInfo, const int increment, bool &previousOutOfRangePointValid, CGetGraphPointDataResultInfo &ReturnedResultInfo, CPointDouble &PreviousOutOfRangeDataPoint, int &previousRangeArea, bool previousPointFoundWasMinPeripheral )
{
CPointDouble DataPoint(0, 0);
Dataset->GetDataPoint(searchIndex, DataPoint);
if(!GetInfo.IsYValueInRange(DataPoint.y))
{
searchIndex += increment;
}
else
{
int rangeArea = GetInfo.GetXRangeArea(DataPoint.x);
if(rangeArea == 0)
{
// Got it.
if(GetInfo.m_xMinPeripheral && previousOutOfRangePointValid)
{
// But return the peripheral point.
ReturnedResultInfo.m_DataPoint = PreviousOutOfRangeDataPoint;
ReturnedResultInfo.m_found = true;
ReturnedResultInfo.m_foundPeripheral = true;
ReturnedResultInfo.m_currentIterationHint = searchIndex - 1;
ReturnedResultInfo.m_nextIterationHint = searchIndex;
}
else
{
// Return a point in range.
ReturnedResultInfo.m_DataPoint = DataPoint;
ReturnedResultInfo.m_found = true;
ReturnedResultInfo.m_currentIterationHint = searchIndex;
ReturnedResultInfo.m_nextIterationHint = searchIndex + increment;
}
break;
}
else
{
searchIndex += increment;
if(GetInfo.m_xMaxPeripheral)
{
if(rangeArea == 1)
{
// Value was too high.
if(previousRangeArea == -1 &&
GetInfo.m_xMinPeripheral && previousOutOfRangePointValid &&
!previousPointFoundWasMinPeripheral)
{
// There we no points in range (we went from too low to too
// high).
// Return a minimal peripheral point.
ReturnedResultInfo.m_DataPoint = PreviousOutOfRangeDataPoint;
ReturnedResultInfo.m_found = true;
ReturnedResultInfo.m_foundPeripheral = true;
ReturnedResultInfo.m_currentIterationHint = searchIndex;
ReturnedResultInfo.m_nextIterationHint = searchIndex + 1;
}
else
{
// Return a maximal peripheral point.
ReturnedResultInfo.m_DataPoint = DataPoint;
ReturnedResultInfo.m_found = true;
ReturnedResultInfo.m_foundPeripheral = true;
ReturnedResultInfo.m_currentIterationHint = searchIndex;
ReturnedResultInfo.m_nextIterationHint = searchIndex + increment;
}
break;
}
if(GetInfo.m_xMinPeripheral)
{
PreviousOutOfRangeDataPoint = DataPoint;
previousOutOfRangePointValid = true;
}
}
previousRangeArea = rangeArea;
}
}
}
problems:
1. There's an added "return" in the while loop. Where did that come from?
2. The new function needs to be const, otherwise it won't compile.
cheers,
Matus