| | | | | next -> |

Ordered List Implementations

  template <typename T, class P>
  typename TMOList<T,P>::Iterator TMOList<T,P>::Insert (const T& t)
  // multimodal
  {
    typename TList<T>::Iterator iter = list_.Begin();
    while (iter != list_.End() && !pred_(t,*iter)) // (*i <= t))
      ++iter; // stop at upper bound
    if (!list_.Insert(iter,t))
      return End();
    return Iterator(iter);
  }

  /* derived class overload */
  template <typename T, class P>
  typename TUOList<T,P>::Iterator TUOList<T,P>::Insert (const T& t)
  // unimodal
  {
    typename TList<T>::Iterator iter = TMOList<T,P>::list_.Begin();
    while (iter != TMOList<T,P>::list_.End() && pred_(*iter,t))
      ++iter; // stop at lower bound
    if (iter.Valid() && *iter == t)
      *iter = t;
    else if (!TMOList<T,P>::list_.Insert(iter,t))
      return TMOList<T,P>::End();
    return Iterator(iter);
  }

  template <typename T, class P>
  size_t TMOList<T,P>::Remove (const T& t)
  // remove all copies of t
  {
    typename TList<T>::Iterator iter = list_.Begin();
    while (iter != list_.End() && pred_(*iter,t))
      ++iter; // stop at lower bound
    size_t count = 0;
    while (iter.Valid() && *iter == t) // remove contiguous elements
    {
      list_.Remove(iter);
      ++count;
    }
    return count;
  }

  template <typename T, class P>
  typename TMOList<T,P>::Iterator TMOList<T,P>::LowerBound (const T& t) const
  // return lower bound
  // Defn: lower bound points to the first item on the list >= t
  {
    Iterator i = Begin();
    while (i != End() && pred_(*i,t)) // (*i < t))
      ++i;
    return i;
  }

  template <typename T, class P>
  typename TMOList<T,P>::Iterator TMOList<T,P>::UpperBound (const T& t) const
  // return upper bound
  // Defn: upper bound points to the first item on the list > t
  {
    Iterator i = Begin();
    while (i != End() && !pred_(t,*i)) // (*i <= t))
      ++i;
    return i;
  }

  template <typename T, class P>
  typename TMOList<T,P>::Iterator TMOList<T,P>::Includes (const T& t) const
  // return LowerBound(t) if found, End() otherwise
  {
    Iterator i = LowerBound(t);
    if (i.Valid() && (*i == t))
      return i;
    return End();
  }

  // structural tests
  template <typename T, class P>
  void TMOList<T,P>::CheckMOList(bool verboseFlag) const
  {
    bool ok = 1;
    Iterator i = TMOList<T,P>::Begin();
    Iterator j;
    size_t number = 0;
    for (j = i++; i != TMOList<T,P>::End(); j = i++, ++number)
    {
      if (verboseFlag || pred_(*i,*j))
      {
	std::cout << "element[" << number << "]= " << *j << " , "
		  << "element[" << number+1 << "]= " << *i;
	if (pred_(*i,*j)) 
	{
	  std::cout << " <- structural error";
	  ok = 0;
	}
	std::cout << '\n';
      }
    }
    if (ok)
      std::cout << " ** Passed MOList structure test\n";
    else
      std::cout << " ** Failed MOList structure test\n";
  }

  template <typename T, class P>
  void TMOList<T,P>::CheckUOList(bool verboseFlag) const
  {
    bool ok = 1;
    Iterator i = TMOList<T,P>::Begin();
    Iterator j;
    size_t number = 0;
    for (j = i++; i != TMOList<T,P>::End(); j = i++, ++number)
    {
      if (verboseFlag || !pred_(*j,*i))
      {
	std::cout << "element[" << number << "]= " << *j << " , "
		  << "element[" << number+1 << "]= " << *i;
	if (!pred_(*j,*i))
	{
	  std::cout << " <- structural error";
	  ok = 0;
	}
	std::cout << '\n';
      }
    }
    if (ok)
      std::cout << " ** Passed UOList structure test\n";
    else
      std::cout << " ** Failed UOList structure test\n";
  }

| next -> | Top of Page | 3. Associative Containers - 8 of 8