#ifndef __RWTPHMAP_H__
#define __RWTPHMAP_H__

/***************************************************************************
 *
 * tphdict.h - RWHMapPtr<Key,T,H,EQ>
 *   
 *     : pointer-based key/data dictionary wrapper for STL-like rw_hashmap
 * 
 * $Id: tphdict.h,v 7.21 1996/05/07 21:04:48 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: tphdict.h,v $
 * Revision 7.21  1996/05/07 21:04:48  kyle
 * Added export pragma for Mac code fragments
 *
 * Revision 7.20  1996/02/19 12:34:12  jims
 * Port to Stdlib 1.2
 *
 * Revision 7.19  1996/02/18 03:19:15  kyle
 * Millenium adds support for compilers without access adjustment
 *
 * Revision 7.18  1996/02/18 01:47:34  griswolf
 * Replace tabs with spaces, per Rogue Wave standard.
 *
 * Revision 7.17  1996/02/07 02:42:30  jims
 * Standard library extensions now in directory rw/stdex
 *
 * Revision 7.16  1996/01/25 01:51:32  kevinj
 * Improved ETP macros.
 *
 * Revision 7.15  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.14  1996/01/23 07:45:48  jims
 * hashCollEqual now named hashMapCollEqual
 *
 * Revision 7.13  1996/01/16 18:26:35  kevinj
 * Simple ETP.
 *
 * Revision 7.12  1996/01/15 19:56:58  kevinj
 * Patched for OTIS0017 compile.
 *
 * Revision 7.11  1996/01/02 15:59:48  pearson
 * Undef RWCONST
 *
 * Revision 7.11  1995/12/21 18:16:39  pearson
 * Undef RWCONST
 *
 * Revision 7.10  1995/12/20 23:19:20  kevinj
 * External template persistence using RW*Persistor.
 *
 * Revision 7.9  1995/12/20 19:29:26  pearson
 * On Borland 4.5 the first argument of pair cannot be const.
 *
 * Revision 7.8  1995/12/14  18:01:21  jims
 * saveOn, restoreFrom now global instead of member functions
 *
 * Revision 7.7  1995/12/13 00:22:58  jims
 * Remove member functions saveOn and restoreFrom (now global)
 *
 * Revision 7.6  1995/12/12 00:03:06  jims
 * Add RWTExport
 *
 * Revision 7.5  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.4  1995/11/17 08:15:43  jims
 * Merge otis-et and otis-stl templates
 *
 * Revision 7.6  1995/10/20 06:46:20  jims
 * Port to msvc 4.0: don't use access adjustment for typedefs
 *
 * Revision 7.5  1995/09/26  17:14:13  griswolf
 * Add insert and apply as synonyms ...KeyAndValue methods.
 *
 * Revision 7.4  1995/09/22  16:34:41  griswolf
 * Fix bug in two-pointer constructor.
 *
 * Revision 7.3  1995/09/18  18:43:31  griswolf
 * Add capacity, fillRatio and resize methods.
 *
 * Revision 7.2  1995/09/12  19:41:49  griswolf
 * Change to #include renamed rw/rwstl/ header file.
 *
 * Revision 7.1  1995/09/05  21:47:43  jims
 * Move to revision 7.1
 *
 * Revision 1.1  1995/09/05  21:47:14  jims
 * Initial revision
 * 
 ****************************************************************************/

#include "rw/compiler.h"

#ifdef RW_NO_STL
#  include "rw/xphdict.h"
#  define RWDefHArgs(T)
#else

#ifndef RWDefHArgs
#  define RWDefHArgs(T) ,RWTHasher<T>,equal_to<T>
#endif

// STL-based implementation:


#include "rw/stdex/hashmap.h"
#include "rw/ep_inmap.h"

template <class K, class T, class H, class EQ>
class RWTPtrHashMap 
  : private RW_PMapAssoc<rw_hashmap<K*,T*,rw_deref_hash<H,K>,
                         rw_deref_compare<EQ,K> >, RWTPtrHashMap<K,T,H,EQ>,
                         K, T>
{
public:
// typedefs
  typedef rw_deref_hash<H,K>                        container_hash;
  typedef rw_deref_compare<EQ,K>                    container_eq; 
  typedef rw_hashmap<K*,T*,rw_deref_hash<H,K>,
                     rw_deref_compare<EQ,K> >       container_type;
  typedef RWTPtrHashMap<K,T,H,EQ>                   this_type;
  typedef RW_PMapAssoc<container_type, this_type, K, T> base1_type;
                       
# ifndef RW_BROKEN_ACCESS_ADJUST
    typedef base1_type base2_type;
    typedef base1_type base3_type;
# else  
    typedef RW_PMapCntnr<container_type, this_type, K, T> base2_type;
    typedef RW_PBase<container_type, this_type> base3_type;
# endif /* RW_BROKEN_ACCESS_ADJUST */
                       
  typedef container_type::size_type size_type;
  typedef container_type::difference_type difference_type;

  typedef container_type::iterator iterator;
  typedef container_type::const_iterator const_iterator;
  typedef container_type::value_type value_type;
  typedef container_type::reference reference;
  typedef container_type::const_reference const_reference;

  typedef K* value_type_key;
  typedef T* value_type_data;
  typedef K*& reference_key;
  typedef T*& reference_data;
  typedef const K*const& const_reference_key;
  typedef const T*const& const_reference_data;

// STL-level access
  const container_type&  std() const { return impl_; }
  container_type& std() { return impl_; }
  
// inherited interface components
  base1_type::apply;
  base1_type::applyToKeyAndValue;
  base1_type::begin;
  base1_type::clearAndDestroy;
  base1_type::end;
  base3_type::isEmpty;
  base3_type::entries;
  base1_type::clear;
  base1_type::contains;
  base1_type::find;
  base1_type::findValue;
  base1_type::findKeyAndValue;
  base1_type::occurrencesOf;
  base1_type::remove;
  base1_type::removeAll;

// overridden interface comoponents
  bool operator==(const this_type& rwmap) const
    { return entries() == rwmap.entries() && hashMapCollEqual(*this, rwmap); }

// additional interface components
  bool insert(K* key, T* partner)
    { return (impl_.insert(container_type::value_type
                           (key,partner))).second; }

  bool insertKeyAndValue(K* key, T* partner)
  { return insert(key,partner); }

  T*& operator[](K* x)
    { return (T*&) impl_[x]; }

  size_type
    capacity() const { return impl_.capacity(); }

  float
    fillRatio() const { return impl_.fill_ratio(); }

  void
    resize(size_type newsz) { impl_.resize(newsz); }

// assignment operators
  RWTPtrHashMap<K,T,H,EQ>&   operator=(const this_type& rwmap)
    { impl_ = rwmap.impl_; return *this; }
  RWTPtrHashMap<K,T,H,EQ>&   operator=(const container_type& stdmap)
    { impl_ = stdmap; return *this; }
  
// constructors
  RWTPtrHashMap()  { }
  RWTPtrHashMap(const container_type& s)               : impl_(s) { }
  RWTPtrHashMap(const this_type& rws)                  : impl_(rws.impl_) { }
  RWTPtrHashMap(value_type* first, value_type* last)   : impl_(first, last) { }
   
  // Tools-6 compatible constructor:
  RWTPtrHashMap(const H& h, size_type sz = RWDEFAULT_CAPACITY)
    : impl_(sz,h,EQ()) { }

private:
  container_type impl_;
  friend class RW_PBase<container_type, this_type>;
};

RWDECLARE_PERSISTABLE_TEMPLATE_4(RWTPtrHashMap)

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

#define RWTPtrHashDictionary RWTPtrHashMap
#define RWTPtrHashDictionaryIterator RWTPtrHashMapIterator


/****************************************************************
 *                                                              *
 *      Declarations for RWTPtrHashMapIterator<K,V>     *
 *                                                              *
 ****************************************************************/

#ifdef RW_PRAGMA_EXPORT
#pragma export on
#endif

template <class K, class T, class H, class EQ>
class RWTExport RWTPtrHashMapIterator
{
public:

  RWTPtrHashMapIterator(RWTPtrHashMap<K,T,H,EQ>& m)
          : rwmap_(&m), head_(true), tail_(false) { }

  RWBoolean     operator++()            // Advance and test
  {
    RWASSERT(!(head_ == true  &&  tail_ == true)); 

    if (tail_)
      /* do nothing */ ;

    else if (head_) {
      iter_ = rwmap_->begin();
      head_ = false;
    }
    else
      ++iter_;

    if (!tail_ && iter_ == rwmap_->end())
      tail_ = true;

    RWASSERT(!(head_ == true  &&  tail_ == true)); 

    return !tail_;
  }

  K*    operator()()  { return ++(*this) ? key() : rwnil; }

  RWTPtrHashMap<K,T,H,EQ>* container() const    { return rwmap_; }

  K*    key() const   { return (*iter_).first; }

  void  reset()       { head_ = true; tail_ = false; } 
  void  reset(RWTPtrHashMap<K,T,H,EQ>& m)
                              { head_ = true; tail_ = false; rwmap_ = &m; } 

  T*    value() const { return (*iter_).second; }

private:
  RWTPtrHashMap<K,T,H,EQ>*              rwmap_;
  RWTPtrHashMap<K,T,H,EQ>::iterator     iter_;
  bool                                  head_;
  bool                                  tail_;

  // Disallow postfix increment.  Unless we hide it, some compilers will
  // substitute the prefix increment operator in its place.
  RWBoolean             operator++(int);
};

#ifdef RW_PRAGMA_EXPORT
#pragma export off
#endif

#endif /* RW_NO_STL */

#endif /* __RWTPHMAP_H__ */
