Boost logo

Boost :

From: Serge Pashkov (psw_at_[hidden])
Date: 2001-06-18 00:51:24


Hello,

In boost/pending/container_traits.hpp there is the code
//-----
  template <class AssociativeContainer, class Predicate>
  void erase_if_dispatch(AssociativeContainer& c, Predicate p,
                         associative_container_tag, unstable_tag)
  {
    // This method is really slow, so hopefully we won't have any
    // associative containers with unstable iterators!
    // Is there a better way to do this?
    typename AssociativeContainer::iterator i;
    typename AssociativeContainer::size_type n = c.size();
    while (n--)
      for (i = c.begin(); i != c.end(); ++i)
        if (p(*i)) {
          c.erase(i);
          break;
        }
  }
//---------

But when erasing change the container size we can make this fuction
faster in the important case when no more item for erasing is found:

  template <class AssociativeContainer, class Predicate>
  void erase_if_dispatch(AssociativeContainer& c, Predicate p,
                         associative_container_tag, unstable_tag)
  {
    // This method is really slow, so hopefully we won't have any
    // associative containers with unstable iterators!
    // Is there a better way to do this?
    typename AssociativeContainer::iterator i;
    typename AssociativeContainer::size_type n = c.size();
    bool looping = true;
    while (looping && (n-- > 0))
    {
      looping = false;
      for (i = c.begin(); i != c.end(); ++i)
        if (p(*i)) {
          looping = true;
          c.erase(i);
          break;
        }
    }
  }

-- 
Best regards,
 Serge                          mailto:psw_at_[hidden]

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk