Listing 4: Partial listing of RandomMin.cpp

void RandomMin::Compare( int index, double val)
{
   list <ValueGroup>::iterator vg = m_vgList.begin();
   if (vg == m_vgList.end())
   {
      //
      // Empty list. Add the new value.
      m_vgList.push_back( ValueGroup( index, val));
      m_sampleCnt++;
   }
   else if ((val - m_vgList.rbegin()->m_val) > m_tol)
   {
      //
      // New value is worse than current worst value.
      if (m_sampleCnt < m_sampleSize)
      {
         // But we need more samples.
         m_vgList.push_back( ValueGroup( index, val));
         m_sampleCnt++;
      }
   }
   else
   {
      if ((val - vg->m_val) < -m_tol)
      {
         //
         // The new value is better than the best value.
         m_vgList.push_front( ValueGroup(index, val));
         m_sampleCnt++;
      }
      else
      {
         //
         //  Value is somewhere in between best and worst.
         int oldSampleCnt = m_sampleCnt;
         while (vg != m_vgList.end())
         {
            if (fabs(val - vg->m_val) <= m_tol)
            {
               // essentially equal.
               vg->m_index.push_back(index); 
               m_sampleCnt++;
               break;
            }
            else if (val < vg->m_val)
            {
               m_vgList.insert( vg, ValueGroup(index, val));
               m_sampleCnt++;
               break;
            }
            ++vg;
         } 
         assert(m_sampleCnt-oldSampleCnt == 1);
      }
      list <ValueGroup>::reverse_iterator rvg = m_vgList.rbegin();
      int worstValGroupCnt = rvg->m_index.size();
      if ((m_sampleCnt - worstValGroupCnt) == m_sampleSize)
      {
         m_vgList.pop_back();
         m_sampleCnt -= worstValGroupCnt;
      }
      rvg = m_vgList.rbegin();
      worstValGroupCnt = rvg->m_index.size();
      assert((m_sampleCnt - worstValGroupCnt) < m_sampleSize);
   }
}
— End of Listing —