/***************************************************************************
 *
 * ev_srtsq.cc - template definitions for RW_VSrtSeq<StdColl,RWColl,Compare>
 * 
 * $Id: ev_srtsq.cc,v 7.15 1996/03/18 18:28:09 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_srtsq.cc,v $
 * Revision 7.15  1996/03/18 18:28:09  griswolf
 * Scopus 2825: Fix warning in method index(key).
 *
 * Revision 7.14  1996/03/14 00:17:40  griswolf
 * Scopus #2555: Move index(key) here for efficiency.
 *
 * Revision 7.13  1996/02/22 20:57:01  griswolf
 * patch insertSorted declaration of comp to avoid warning.
 *
 * Revision 7.12  1996/02/18 04:37:38  kyle
 * Millenium adds support for compilers wanting extensions for StdLib
 *
 * Revision 7.11  1996/02/18 01:41:39  griswolf
 * Replace tabs with spaces, per Rogue Wave standard.
 *
 * Revision 7.10  1996/01/30 23:04:59  jims
 * Move functions with "while" statements out of line
 *
 * Revision 7.9  1996/01/30 08:08:17  jims
 * Remove use of obsolete std_iterator type
 *
 * Revision 7.8  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.7  1995/09/23 00:39:55  griswolf
 * Fix bug in method remove(key).
 *
 * Revision 7.6  1995/09/05  21:59:45  jims
 * Change tlyrs.h to tlyrs.str
 *
 * Revision 7.5  1995/08/02  00:04:36  jims
 * Qualify include file names with "rw/"
 *
 * Revision 7.4  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, class Compare> 
#define VSSS     RW_VSrtSeq<StdColl,RWColl,Compare>

TEMPLATE
bool VSSS::contains(const_reference key) const
{
  const_iterator hit = lower_bound(begin(),end(),key,Compare());
  if(hit == end())
    return false;
  return (*hit == key);
}

TEMPLATE
bool VSSS::find(const_reference key, reference val) const
{
  const_iterator hit = lower_bound(begin(),end(),key,Compare());
  if(hit != end() && *hit == key) {
    val = *hit;
    return true;
  }
  return false;
}

// Not efficient on classes without random access iterators. Override.
TEMPLATE 
VSSS::size_type 
VSSS::index(const_reference key) const
{
  const_iterator it = lower_bound(begin(),end(),key,Compare());
  if (it == end()) return npos;
  size_type ret = 0;
  distance(begin(),it,ret);
  return ret;
}

TEMPLATE
VSSS::size_type
VSSS::insert(const StdColl& coll)
{
  size_type ret = coll.size();
  if (ret) {
    iterator it = begin();
    iterator first = coll.begin();
    iterator last = coll.end();
    while(first != last)
      it = insertSorted(it,*first++);
  }
  return ret;
}


TEMPLATE
VSSS::size_type
VSSS::occurrencesOf(const_reference key) const
{
  pair<const_iterator,const_iterator> range =
                        equal_range(begin(),end(),key, Compare());
  size_type ret(0);
  distance(range.first,range.second,ret);
  return ret;
}

TEMPLATE
VSSS::size_type
VSSS::removeAll(const_reference key)
{
  StdColl::iterator start = ::find(begin(),end(),key);
  if(start == end())
    return 0;
  
  size_type counter(1);
  StdColl::iterator bound = start;
  ++bound;
  while(bound != end() && *bound == key) {
    ++bound;
    ++counter;
  }
  std().erase(start,bound);
  return counter;
}

TEMPLATE
bool VSSS::remove(const_reference key)
{
  StdColl::iterator hit = lower_bound(begin(),end(),key,Compare());
  if(hit != end() && *hit == key){
    std().erase(hit);
    return true;
  }
  return false;
}    

/** should be protected by #if RWDEBUG but xlC bug prevents it. Sigh **/
TEMPLATE
bool VSSS::isSorted() const
{
  if(entries() < 2) return true; // empty and single are sorted
  const_iterator trail;
  for(const_iterator it = begin(); it != end();/**/)
  {
    trail = it++;
    if(it == end()) break;
    if(Compare().operator()(*it, *trail))
      return false;
  }
  return true;
}

// protected methods

TEMPLATE
VSSS::iterator
VSSS::insertSorted(iterator hint, const_reference a) 
{
  StdColl::iterator hit;
  Compare comp = Compare();  // avoid warning "comp used before set"
  if(hint == end())
    hit = upper_bound(begin(),end(),a,comp);
  else {
    // test for "held is 'less than' a or a is not 'less than' held"
    // which amounts to "less than or equal"
    if(comp(*hint,a) || ! comp(a,*hint))
//      hit = upper_bound(hint,end(),a,comp);
      hit = upper_bound(begin(),end(),a,comp);
    else // a must be "greater than" the contained value
//      hit = upper_bound(begin(),hint,a,comp);
      hit = upper_bound(begin(),end(),a,comp);
  }
  return std().insert(hit,a);
}

#undef TEMPLATE
#undef VSS
