/***************************************************************************
 *
 * ev_seq.cc - template definitions for RW_VSeq<StdColl,RWColl>
 * 
 * $Id: ev_seq.cc,v 7.14 1996/03/14 00:30:48 griswolf Exp $
 *
 * (c) Copyright 1989 - 1996 Rogue Wave Software, Inc.
 * ALL RIGHTS RESERVED
 *
 * The software and information contained herein are proprietary to, and
 * comprise valuable trade secrets of, Rogue Wave Software, Inc., which
 * intends to preserve as trade secrets such software and information.
 * This software is furnished pursuant to a written license agreement and
 * may be used, copied, transmitted, and stored only in accordance with
 * the terms of such license and with the inclusion of the above copyright
 * notice.  This software and information or any other copies thereof may
 * not be provided or otherwise made available to any other person.
 *
 * Notwithstanding any other lease or license that may pertain to, or
 * accompany the delivery of, this computer software and information, the
 * rights of the Government regarding its use, reproduction and disclosure
 * are as set forth in Section 52.227-19 of the FARS Computer
 * Software-Restricted Rights clause.
 * 
 * Use, duplication, or disclosure by the Government is subject to
 * restrictions as set forth in subparagraph (c)(1)(ii) of the Rights in
 * Technical Data and Computer Software clause at DFARS 252.227-7013.
 * Contractor/Manufacturer is Rogue Wave Software, Inc.,
 * P.O. Box 2328, Corvallis, Oregon 97339.
 *
 * This computer software and information is distributed with "restricted
 * rights."  Use, duplication or disclosure is subject to restrictions as
 * set forth in NASA FAR SUP 18-52.227-79 (April 1985) "Commercial
 * Computer Software-Restricted Rights (April 1985)."  If the Clause at
 * 18-52.227-74 "Rights in Data General" is specified in the contract,
 * then the "Alternate III" clause applies.
 *
 ****************************************************************************
 * 
 * $Log: ev_seq.cc,v $
 * Revision 7.14  1996/03/14 00:30:48  griswolf
 * Scopus #2555: remove devnote. (leave note alone as "NOTE")
 *
 * Revision 7.13  1996/02/18 04:43:04  kyle
 * Milennium adds support for compilers wanting extensions for StdLib.
 *
 * Revision 7.12  1996/02/18 01:41:36  griswolf
 * Replace tabs with spaces, per Rogue Wave standard.
 *
 * Revision 7.11  1996/01/30 21:49:03  jims
 * Take functions out of line to avoid Borland warnings
 *
 * Revision 7.10  1995/12/11 22:30:41  jims
 * Remove 'Sca' from interior class names
 * Add 'rw_' to global helper classes
 * Change 'muterator' to 'filterator'
 *
 * Revision 7.9  1995/09/05 21:59:39  jims
 * Change tlyrs.h to tlyrs.str
 *
 * Revision 7.8  1995/08/02  00:04:36  jims
 * Qualify include file names with "rw/"
 *
 * Revision 7.7  1995/07/27  22:02:33  jims
 * Add header with file name, short description, $Id and $Log
 * 
 ****************************************************************************/

#include "rw/epfunc.h"

#ifndef RW_HEADER_WITH_EXTENSION
#  include <algorithm>
#else
#  include <algorithm.hpp>
#endif  /* RW_HEADER_WITH_EXTENSION */

#define TEMPLATE template <class StdColl, class RWColl>
#define VSS      RW_VSeq<StdColl, RWColl>

TEMPLATE
VSS::size_type
VSS::removeAll(bool (*fn)(VSS::const_reference key,void*),void* x )
{
  typedef x_pointer_to_binary_function<const_reference, void*, bool>
          boolPtrFun;

  size_type ret = std().size();
  iterator newEnd = remove_if(begin(),end(),
                                x_binder2nd<boolPtrFun>(fn,x));
  std().erase(newEnd,end());
  return ret - std().size();
}

TEMPLATE
VSS::size_type
VSS::removeAll(VSS::const_reference key)
{
  size_type ret(std().size());

  // NOTE: probably bug when namespaces become supported since
  //       standard remove algorithm won't be in global namespace
  iterator newEnd = ::remove(begin(),end(),key);
  std().erase(newEnd,end());
  return ret - std().size();
}

TEMPLATE
VSS::value_type
VSS::removeAt(VSS::size_type index)
{
  boundsCheck(index);
  iterator i = begin();
  advance(i,index);
  value_type ret = *i;
  std().erase(i);
  return ret;
}

TEMPLATE
VSS::value_type
VSS::removeFirst()
{
#ifdef RWDEBUG
  size_type n_dbg = entries();
#endif      
  RWPRECONDITION(!isEmpty());
  iterator i = begin();
  value_type ret = *i;
  std().erase(i);
  RWPOSTCONDITION(entries() == n_dbg - 1);
  return ret;
}


TEMPLATE
bool VSS::remove(bool (*fn)(VSS::const_reference,void*),void* x )
{
  for(iterator hit = begin(); hit != end(); ++hit)  {
    if((*fn)(*hit,x)) {
      std().erase(hit);
      return true;
    }
  }
  return false;
}

TEMPLATE
bool VSS::remove(VSS::const_reference key)
{
  for(iterator i = begin(); i != end(); ++i) {
    if(*i == key) {
      std().erase(i);
      return true;
    }
  }
  return false;
}


TEMPLATE
VSS::size_type
VSS::replaceAll(bool (*fn)(VSS::const_val_ref, void*),void* x,
                const_val_ref newVal)
{
  size_type count = 0;
  iterator i = begin();

  for(/**/; i != end(); ++i) {
    if((*fn)(*i,x)) {
      *i = newVal;
      ++count;
    }
  }
  return count;
}

TEMPLATE
VSS::size_type
VSS::replaceAll(VSS::const_val_ref oldVal, VSS::const_val_ref newVal)
{
  size_type count = 0;
  iterator i = begin();

  for(/**/; i != end(); ++i) {
    if(*i == oldVal) {
      *i = newVal;
      ++count;
    }
  }
  return count;
}

#undef TEMPLATE
#undef VSS     
