#ifndef __RWEP_INMAP_H__
#define __RWEP_INMAP_H__

/***************************************************************************
 *
 * ep_inmap.h - RW_PMapAssoc<StdColl,RWColl,Key,T>
 *   
 *     : pointer-based Key/Data interface components for STL Associative
 *       Containers 
 *
 * $Id: ep_inmap.h,v 7.18 1996/05/08 00:18:08 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_inmap.h,v $
 * Revision 7.18  1996/05/08 00:18:08  kyle
 * Added export pragma for Mac code fragments
 *
 * Revision 7.17  1996/02/18 01:40:37  griswolf
 * Replace tabs with spaces, per Rogue Wave standard.
 *
 * Revision 7.16  1996/02/16 19:18:50  jims
 * Small to changes to some formal arguments
 *
 * Revision 7.15  1996/02/02 01:36:31  jims
 * Move definition of hashCollEqual to ep_inmap.cc
 *
 * Revision 7.14  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.13  1996/01/23 07:45:07  jims
 * Change name of hashCollEqual to hashMapCollEqual
 *
 * Revision 7.12  1995/12/14 18:01:21  jims
 * saveOn, restoreFrom now global instead of member functions
 *
 * Revision 7.11  1995/12/12 00:03:06  jims
 * Add RWTExport
 *
 * Revision 7.10  1995/11/17 08:11:59  jims
 * ;modify remove() to return pointer instead of boolean for
 * Tools 6 compatibility
 *
 * Revision 7.9  1995/09/26 17:14:13  griswolf
 * Add insert and apply as synonyms ...KeyAndValue methods.
 *
 * Revision 7.8  1995/09/22  16:31:46  griswolf
 * Fix typo, fix bug in removeAll(function).
 *
 * Revision 7.7  1995/09/09  01:14:14  jims
 * New alg for removeAll(bool (*fn)(...),void* x)
 *
 * Revision 7.6  1995/09/05  21:41:38  jims
 * Change tlyrs.h to tlyrs.str
 *
 * Revision 7.5  1995/08/11  22:41:01  jims
 * Add #include directive for compile-time instantiation
 *
 * Revision 7.4  1995/07/27  22:02:33  jims
 * Add header with file name, short description, $Id and $Log
 * 
 ****************************************************************************/

#include "rw/ep_mcntn.h"

// handy macros, #undef'd below
#define KEY(i)    ((*i).first)
#define DATA(i)   ((*i).second)

#ifdef RW_PRAGMA_EXPORT
#pragma export on
#endif

template <class StdColl, class RWColl, class Key, class T>
class RWTExport RW_PMapAssoc
  : public RW_PMapCntnr<StdColl, RWColl, Key, T> {

private:
  typedef RW_PMapCntnr<StdColl,RWColl,Key,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 Key*              value_type_key;
  typedef const Key*        const_value_key;
  typedef Key*&             reference_key;
  typedef const Key*const&  const_reference_key;

  typedef T*                value_type_data;
  typedef const T*          const_value_data;
  typedef T*&               reference_data;
  typedef const T*const&    const_reference_data;

  void  apply(void (*fn)(const Key*, T*&, void*),void* x)
  {
    for(iterator i = begin(); i != end(); ++i)
      (*fn)(KEY(i), DATA(i), x);
  }
  
  void  apply(void (*fn)(const Key*, const T*, void*),void*x) const
  {
    for(const_iterator i = begin(); i != end(); ++i)
      (*fn)(KEY(i), DATA(i), x);
  }

  // deprecated:
  void  applyToKeyAndValue(void (*fn)(const Key*, T*&, void*),void* x)
  { apply(fn,x); }

  // deprecated:
  void  applyToKeyAndValue(void (*fn)(const Key*,
                                      const T*, void*),void*x) const
  { apply(fn,x); }

  void       clear() { std().erase(begin(),end()); }

  void       clearAndDestroy();

  bool       contains(const Key* key) const
    { const_iterator i = std().find((Key*)key); return i != end(); }

  bool       contains(bool(*fn)(value_type,void*),void* x) const
    { return base_type::contains(fn,x); }

  const Key*         find(const Key* key) const
  {
    const_iterator i = std().find((Key*)key);
    return ( i == end()) ? 0 : (Key*) KEY(i);
  }

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

  const T*     findValue(const Key* key) const
  {
    const_iterator i = std().find((Key*)key);
    return ( i == end()) ? 0 : (const T*) DATA(i);
  }

  T*     findValue(const Key* key)
  {
    const_iterator i = std().find((Key*)key);
    return ( i == end()) ? 0 : (T*) DATA(i);
  }

  const Key* findKeyAndValue(const Key* key, const T*& vret) const
  {
    const_iterator i = std().find((Key*)key);
    if(i != end()) {
      vret = (const T*&) DATA(i);
      return (const Key*) KEY(i);
    }
    return 0;
  }

  const Key* findKeyAndValue(const Key* key, T*& vret)
  {
    const_iterator i = std().find((Key*)key);
    if(!(i == end())) {
      vret = (T*&) DATA(i);
      return (const Key*) KEY(i);
    }
    return 0;
  }

  size_type  occurrencesOf(const Key* k) const
    { return std().count((Key*)k); }

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

  Key*       remove(const Key* key)
  {
    iterator i = std().find((Key*)key);
    if(i != end()) {
      Key* ret = (Key*)(*i).first;
      std().erase(i);
      return ret;
    }
    return rwnil;
  }

  Key*        remove(bool(*fn)(value_type,void*),void* x)
    {
      iterator hit = begin();
      for(/**/; hit != end(); ++hit) {
        if ((*fn)(*(value_type*)(&*hit),x)) {
          Key* ret = (Key*)(*hit).first;
          std().erase(hit);
          return ret;
        }
      }
      return rwnil;
    }
      
  size_type removeAll(const Key* key) { return std().erase((Key*)key); }

  size_type removeAll(bool (*fn)(value_type, void*),void* x)
    {
      size_type    ret = entries();
      
      while (entries() && (*fn)(*(value_type*)&*begin(),x))
        std().erase(begin());

      iterator iter(begin());
      iterator iter2;

      while (iter != end()) {
        iter2 = iter;
        ++iter2;
        if (iter2 != end() && (*fn)(*(value_type*)&*iter2,x))
          std().erase(iter2);
        else
          ++iter;
      }

      return ret - entries();
    }

protected:
  static value_type nilValue_;  // needed for base class

};
#undef KEY
#undef DATA

/*
 * Special function for testing equality of two hash collections.
 * The standard algorithm 'equal' is not suitable due to the
 * inherent un-orderedness of hash collections.
 */
template <class RWColl>
bool hashMapCollEqual(const RWColl& h1, const RWColl& h2);


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

#ifdef RW_PRAGMA_EXPORT
#pragma export off
#endif

#endif /* __RWEP_INMAP_H__ */
