/***************************************************************************
 *
 * ev_hash.cc - 
 * 
 * $Id: ev_hash.cc,v 7.5 1996/02/18 01:41:18 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_hash.cc,v $
 * Revision 7.5  1996/02/18 01:41:18  griswolf
 * Replace tabs with spaces, per Rogue Wave standard.
 *
 * Revision 7.4  1996/02/01 21:59:12  jims
 * Separate out hash-only set operations into separate header
 *
 * Revision 7.3  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.2  1995/10/20 07:04:15  jims
 * port to msvc 4.0
 *
 * Revision 7.1  1995/09/25  10:42:47  jims
 * Move to revision 7.1
 * 
 ****************************************************************************/

#define TEMPLATE template <class StdColl, class RWColl>
#define VHA      RW_VHAssoc<StdColl, RWColl>


TEMPLATE
void VHA::UnionHash(const StdColl& stdcoll)
{
// depends on all eq items being adjacent in each collection
  StdColl::key_equal_type eq = std().key_equal();
  const_iterator probe = stdcoll.begin();
  while(probe != stdcoll.end()) {
    iterator found = std().find(*probe);
    if(found != end())          // cope with (multiple) match(es)
    {
      const StdColl::value_type& match = *found;
      while(   found != end() && probe != stdcoll.end()
            && eq(match,*found) && eq(*found,*probe))
        // if we have a match, then march in sequence 1 or more times
      {
        ++found;
        ++probe;
      }
      // if no match, or no _more_ match; insert (remainder) into self
      while(probe != stdcoll.end() && eq(match,*probe))
      {
        std().insert(*probe++);
      }
    }
    else                        // self has no match to *probe
    {
      const StdColl::value_type& match = *probe;
      while(probe != stdcoll.end() && eq(match,*probe)) {
        std().insert(*probe++);
      }
    }
  }
}


TEMPLATE
void VHA::intersectionHash(const StdColl& stdcoll)
{
  // depends on all eq items being adjacent in each collection
  StdColl::key_equal_type eq = std().key_equal();
  const_iterator look = begin();
  while(look != end()) {
    const_iterator probe = stdcoll.find(*look);
    const StdColl::value_type& match = *look;
    while(   probe != stdcoll.end() && look != end() 
          && eq(match,*look) && eq(*look,*probe)) 
      // if we have a match, then march in sequence 1 or more times
    {
      ++probe;
      ++look;
    }
    // no match, or no _more_ match: remove (trailing) part in self
    while(look != end() && eq(match,*look))
      look = std().erase(look); // depends on "next iter" semantics
  }
}


TEMPLATE
void VHA::differenceHash(const StdColl& stdcoll)
{
  // depends on all eq items being adjacent in each collection
  StdColl::key_equal_type eq = std().key_equal();
  const_iterator look = begin();
  while(look != end()) {
    const_iterator probe = stdcoll.find(*look);
    const StdColl::value_type& match = *look;
    while(   probe != stdcoll.end() && look != end()
          && eq(match,*look) && eq(*look,*probe))
    { // if there is a match, then erase (them) in sequence
      look = std().erase(look); // depends on "next iter" semantics
      ++probe;
    }
    // no match, or no more match: skip (trailing) part in self
    while(look != end() && eq(match,*look))
      ++look;
  }
}  


TEMPLATE
void VHA::symmetricDifferenceHash(const StdColl& stdcoll)
{
  // depends on all eq items being adjacent in each collection
  StdColl::key_equal_type eq = std().key_equal();
  const_iterator probe = stdcoll.begin();
  while(probe != stdcoll.end()) {
    iterator found = std().find(*probe);
    if(found != end())          // cope with (multiple) match(es)
    {
      const StdColl::value_type& match = *found;
      while(   found != end() && probe != stdcoll.end()
            && eq(match,*found) && eq(*found,*probe))
        // if we have a match, then remove in sequence 1 or more times
      {
        found = std().erase(found);
        ++probe;
      }
      // if no match, or no _more_ match; insert (remainder) into self
      while(probe != stdcoll.end() && eq(match,*probe))
      {
        std().insert(*probe++); 
      }
    }
    else                        // self has no match to *probe 
    {
      const StdColl::value_type& match = *probe;
      while(probe != stdcoll.end() && eq(match,*probe)) {
        std().insert(*probe++);
      }
    }
  }
}

#undef TEMPLATE
#undef VHA
