#include "stdafx.h" // uses MFC
#include "MultiFor.h"
// Add a for loop
// the loop is used as if it is implemented
// for (i=start; i<end; i++)
void CMultiFor::Add(int start, int end)
{
// this class assumes that counters increment
ASSERT( end >= start );
m_start.Add( start );
m_end.Add( end );
}
// Initialise the constructs
void CMultiFor::Initialize()
{
m_current.SetSize( Count() );
for (int i=0; i<Count(); i++) {
m_current[i] = m_start[i];
}
}
int CMultiFor::Index( int i )
{
// check for invalid conditions
if (i > m_current.GetUpperBound()) {
ASSERT(FALSE);
return -1;
}
// have we called Initialize?
ASSERT( m_current.GetSize() == Count() );
return m_current[i];
}
BOOL CMultiFor::CarryOn()
{
// just in case we are called with no entries
if (Count()==0)
return FALSE;
// have we called Initialize?
ASSERT( m_current.GetSize() == Count() );
return ( m_current[0] < m_end[0] );
}
void CMultiFor::Increment()
{
// have we called Initialize?
ASSERT( m_current.GetSize() == Count() );
// loop over the array of loop indices
// starting from the last one added
int position = m_current.GetUpperBound();
for (;;) {
m_current[position]++;
if (m_current[position] < m_end[position])
break; // no need to go to the next loop index
if (position==0)
break; // terminating condition reached
// reset this index, and start on the previous one
m_current[position] = m_start[position];
position--;
}
}
//End of File