#ifndef __RWEP_INSET_H__
#define __RWEP_INSET_H__

/***************************************************************************
 *
 * ep_inset.h - RW_PAssoc<StdColl,RWColl,DataTypes>
 *   
 *     : pointer-based interface components for STL Associative Containers
 * 
 * $Id: ep_inset.h,v 7.15 1996/05/08 00:18:21 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_inset.h,v $
 * Revision 7.15  1996/05/08 00:18:21  kyle
 * Added export pragma for Mac code fragments
 *
 * Revision 7.14  1996/03/22 22:03:42  griswolf
 * Scopus #2898: Make things "bool correct".
 *
 * Revision 7.13  1996/03/16 13:23:22  jims
 * Modify remove mfs to return value_type instead of const_value
 *
 * Revision 7.12  1996/02/18 01:40:42  griswolf
 * Replace tabs with spaces, per Rogue Wave standard.
 *
 * Revision 7.11  1996/02/16 19:18:50  jims
 * Small to changes to some formal arguments
 *
 * Revision 7.10  1996/02/01 21:50:02  jims
 * Separate out hash-only set operations into separate header
 *
 * Revision 7.9  1996/01/29 20:06:28  pearson
 * Remove redundant typedef for size_type
 *
 * Revision 7.8  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.7  1996/01/04 00:50:49  pearson
 * Change isSubset to isSubsetOf and add multiset methods
 *
 * Revision 7.6  1995/12/15 00:28:58  pearson
 * Remove isUnequal and rename isEqual to isEquivalent
 * Move isEquivalent, isSubset and isProperSubset to the base class
 * Add access adjustment due to private inheritance
 *
 * Revision 7.5  1995/12/12 00:03:06  jims
 * Add RWTExport
 *
 * Revision 7.4  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.3  1995/10/11 22:36:45  griswolf
 * Scopus #2160: pass more constructor args through to implementation.
 *
 * Revision 7.2  1995/09/21  00:22:35  griswolf
 * Add new hash-based set operations.
 *
 * Revision 7.1  1995/09/13  10:35:15  jims
 * Move revision to 7.1
 *
 * Revision 7.8  1995/09/05  21:41:51  jims
 * Change tlyrs.h to tlyrs.str
 *
 * Revision 7.7  1995/08/31  12:03:51  jims
 * next rev
 *
 * Revision 7.6  1995/08/02  00:04:36  jims
 * Qualify include file names with "rw/"
 *
 * Revision 7.5  1995/07/27  22:02:33  jims
 * Add header with file name, short description, $Id and $Log
 * 
 ****************************************************************************/

#include "rw/ep_scntn.h"

#ifdef RW_PRAGMA_EXPORT
#pragma export on
#endif

template <class StdColl, class RWColl, class T>
class RWTExport RW_PAssoc : public RW_PCntnr<StdColl,RWColl,T>
{
private:
  typedef RW_PCntnr<StdColl,RWColl,T>   base_type;

public:
  typedef StdColl::value_type        value_type;
  typedef StdColl::size_type         size_type;
  typedef StdColl::iterator          iterator;
  typedef StdColl::const_iterator    const_iterator;
  typedef StdColl::reference         reference;
  typedef StdColl::const_reference   const_reference;
  typedef const T*                   const_value;


// Additional interface components
  void  clear() { std().erase(begin(),end()); }

// clearAndDestroy should be overridden in any leaf class that knows
// how to save its iterators while deleting. This version uses 2
// passes: One to delete, then clear()
  void  clearAndDestroy();

  StdColl::size_type    removeAll(const_value key);

  StdColl::size_type    removeAll(bool (*fn)(const_value, void*),void*);

  value_type            remove(const_value key)
    {
      const_value ret = 0;
      iterator hit = std().find((T*)key);
      if(hit != end()) {
        ret = *iterator(hit);
        std().erase(hit);
      }
      return (value_type)ret;
    }

  value_type  remove(bool (*fn)(const_value,void*),void*);

// overridden interface components
  bool  contains(const_value key) const
  {
    iterator iter = std().find((T*)key);
    return iter != end();
  }

  const_value   find(const_value) const;

  size_type     occurrencesOf(const_value key)
    { return std().count((value_type)key); }

// Avoid hiding overloads
  bool  contains(bool(*fn)(const_value, void*),void* x) const
    { return base_type::contains(fn,x); }

  size_type     occurrencesOf(bool(*fn)(const_value,void*),void* x) const
    { return base_type::occurrencesOf(fn,x); }

  const_value   find(bool(*fn)(const_value, void*),void* x) const
    { return base_type::find(fn,x); }

  void     Union(const RWColl& rwcoll)
    { Union((StdColl&)rwcoll.std()); }

  void     intersection(const RWColl& rwcoll)
    { intersection((StdColl&)rwcoll.std()); }

  void     difference(const RWColl& rwcoll)
    { difference((StdColl&)rwcoll.std()); }

  void     symmetricDifference(const RWColl& rwcoll)
    { symmetricDifference((StdColl&)rwcoll.std()); }

  bool isSubsetOf(const RWColl& rhs) const
  {
     if(entries() > rhs.entries())
        return FALSE;

     const_iterator itr = begin();
     while(itr != end() && rhs.contains(*itr) )
          ++itr;

     return end() == itr;
  }

  bool isSubsetMultiOf(const RWColl& rhs) const
  {
     if(entries() > rhs.entries())
        return FALSE;

     const_iterator itr = begin();
     while(itr != end() && std().count(*itr) <=
                       rhs.std().count(*itr) )
         ++itr;

     return end() == itr;
  }

  bool isProperSubsetOf(const RWColl& rhs) const
  { return isSubsetOf(rhs) && (entries() < rhs.entries()); }

  bool isProperSubsetMultiOf(const RWColl& rhs) const
  { return isSubsetMultiOf(rhs) && (entries() < rhs.entries()); }

  bool isEquivalent(const RWColl& rhs) const
  { return isSubsetOf(rhs) && (entries() == rhs.entries()); }

  bool isEquivalentMulti(const RWColl& rhs) const
  { return isSubsetMultiOf(rhs) && (entries() == rhs.entries()); }

protected:
  void     Union(const StdColl&);
  void     intersection(const StdColl&);
  void     difference(const StdColl&);
  void     symmetricDifference(const StdColl&);

protected:
  RW_PAssoc() { } // can only be used as base class 
};

#ifdef RW_COMPILE_INSTANTIATE
#include "rw/ep_inset.cc"
#endif

#ifdef RW_PRAGMA_EXPORT
#pragma export off
#endif

#endif /* __RWEP_INSET_H__ */
