/***************************************************************************
 *
 * ep_seq.cc - template definitions for RW_PSeq<StdColl,RWColl,DataTypes>
 * 
 * $Id: ep_seq.cc,v 7.15 1996/02/18 04:52:09 kyle 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: ep_seq.cc,v $
 * Revision 7.15  1996/02/18 04:52:09  kyle
 * Millenium adds support for compilers wanting StdLib files to have
 * extensions.
 *
 * Revision 7.14  1996/02/18 01:40:55  griswolf
 * Replace tabs with spaces, per Rogue Wave standard.
 *
 * Revision 7.13  1996/02/13 12:14:17  jims
 * Small mods to the interface
 *
 * Revision 7.12  1996/01/26 04:44:34  jims
 * Tweak formal argument types for sgi
 *
 * Revision 7.11  1996/01/24 21:07:41  jims
 * Simplify implementation of otis-stl ptr-based collections
 * by using container<T*> impls instead of container<void*>
 *
 * 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/11/13 12:43:53  jims
 * Change return type of remove from boolean to value_type
 * Add value_type versions of some mfs
 *
 * Revision 7.8  1995/09/05 21:58:28  jims
 * Change tlyrs.h to tlyrs.str
 *
 * Revision 7.7  1995/08/01  22:52:35  jims
 * Qualify include file name with "rw/"
 *
 * Revision 7.6  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 RWColl, class StdColl, class T>
#define PSS      RW_PSeq<RWColl,StdColl,T>

TEMPLATE
void PSS::clearAndDestroy()
{
  for(iterator it = begin(); it != end(); ++it)
    delete *it;
  clear();
}

TEMPLATE
PSS::size_type
PSS::removeAll(bool (*fn)(const T*,void*),void* x )
{
  typedef x_pointer_to_binary_function<const_value, void*, bool>  boolPtrFun;
  size_type ret = entries();
  iterator newEnd = ::remove_if(begin(),end(),
                                x_binder2nd<boolPtrFun>(fn,x));
  
  std().erase(newEnd,end());
  return ret - entries();
}

TEMPLATE
PSS::size_type
PSS::removeAll(bool (*fn)(T*,void*),void* x )
{
  typedef x_pointer_to_binary_function<value_type, void*, bool>  boolPtrFun;
  size_type ret = entries();
  iterator newEnd = ::remove_if(begin(),end(),
                                x_binder2nd<boolPtrFun>(fn,x));
  
  std().erase(newEnd,end());
  return ret - entries();
}

template <class PtrToConst>
struct deref_match : public unary_function<PtrToConst,bool> {
  PtrToConst val;
  deref_match(PtrToConst t) : val(t) {}
  bool operator()(PtrToConst t) {return *t == *val;}
};
  
TEMPLATE
PSS::size_type
PSS::removeAll(const T* key)
{
  size_type ret = entries();
  iterator newEnd = remove_if(begin(),end(),deref_match<const_value>(key));
  std().erase(newEnd,end());
  return ret - entries();
}

TEMPLATE
PSS::value_type PSS::remove(bool (*fn)(PSS::const_value key,void*),void* x )
{
  value_type ret;
  for(iterator hit = begin(); hit != end(); ++hit)  {
    if((*fn)(*hit,x)) {
      ret = *hit;
      std().erase(hit);
      return ret;
    }
  }
  return rwnil;
}

TEMPLATE
PSS::value_type PSS::remove(bool (*fn)(PSS::value_type key,void*),void* x )
{
  value_type ret;
  for(iterator hit = begin(); hit != end(); ++hit)  {
    if((*fn)(*hit,x)) {
      ret = *hit;
      std().erase(hit);
      return ret;
    }
  }
  return rwnil;
}

TEMPLATE
PSS::value_type PSS::remove(PSS::const_value key)
{
  value_type ret;
  for(iterator hit = begin(); hit != end(); ++hit) {
    if(**hit == *key) {
      ret = *hit;
      std().erase(hit);
      return ret;
    }
  }
  return rwnil;
}

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

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

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

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

TEMPLATE
PSS::size_type
PSS::replaceAll(PSS::const_value oldVal, PSS::value_type newVal)
{
  size_type count = 0;
  iterator i = begin();

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

#undef TEMPLATE
#undef PSS
