#ifndef __RWTPDLIST_H__
#define __RWTPDLIST_H__

/***************************************************************************
 *
 * tpdlist.h - RWTPtrDlist<T>
 *   
 *     : pointer-based doubly-linked list wrapper for STL list
 * 
 * $Id: tpdlist.h,v 7.23 1996/05/22 20:11:08 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: tpdlist.h,v $
 * Revision 7.23  1996/05/22 20:11:08  griswolf
 * Change base_type to base1_type. (Finishes prior changes.)
 *
 * Revision 7.22  1996/05/07 20:47:29  kyle
 * Added export pragma for Mac code fragments
 * Added support for RW_BROKEN_ACCESS_ADJUST
 *
 * Revision 7.21  1996/03/14 22:49:36  kyle
 * Changed guards for standard library so it supported RW_HEADER_WITH_EXTENSION
 *
 * Revision 7.20  1996/02/19 12:34:12  jims
 * Port to Stdlib 1.2
 *
 * Revision 7.19  1996/02/18 01:47:28  griswolf
 * Replace tabs with spaces, per Rogue Wave standard.
 *
 * Revision 7.18  1996/02/13 12:14:17  jims
 * Small mods to the interface
 *
 * Revision 7.17  1996/01/29 23:55:39  pearson
 * Reverse sense of RW_NO_NEW_HEADERS
 *
 * Revision 7.16  1996/01/29 18:30:53  kyle
 * Millenium updated to support pre-C++ Standard headers using
 * RWSTD_NO_NEW_HEADER config define
 *
 * Revision 7.15  1996/01/26 04:44:34  jims
 * Tweak formal argument types for sgi
 *
 * Revision 7.14  1996/01/25 01:51:32  kevinj
 * Improved ETP macros.
 *
 * Revision 7.13  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.12  1996/01/23 07:39:25  jims
 * Change ctor args from T** to T*const*
 * Remove duplicate inclusion of rw/tpdlist.cc
 * Use template arg when class used in code body
 *
 * Revision 7.11  1996/01/13 23:02:26  kevinj
 * Added RW_COMPILE_INSTANTIATE.
 *
 * Revision 7.10  1996/01/13 02:34:45  kevinj
 * Simplified External Template Persistence (ETP).
 *
 * Revision 7.9  1995/12/20 23:19:20  kevinj
 * External template persistence using RW*Persistor.
 *
 * 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/13 12:47:12  jims
 * New stl-based collections now one with Tools-6 templates
 *
 * Revision 7.14  1995/10/20 06:46:20  jims
 * Port to msvc 4.0: don't use access adjustment for typedefs
 *
 * Revision 7.13  1995/09/25  11:09:35  jims
 * Add NO_CONST_INST version of T**,T** ctor
 *
 * Revision 7.12  1995/09/15  21:14:32  griswolf
 * Remove un-needed destructor, leaving it commented as documentation.
 *
 * Revision 7.11  1995/09/05  21:42:36  jims
 * Change tlyrs.h to tlyrs.str
 *
 * Revision 7.10  1995/08/18  16:52:25  kevinj
 * Added RW_DEFINE_PERSISTABLE_COLLECTION.
 *
 * Revision 7.9  1995/08/14  23:52:33  kevinj
 * Added restoreFrom, saveOn.
 *
 * Revision 7.8  1995/08/02  00:04:36  jims
 * Qualify include file names with "rw/"
 *
 * Revision 7.7  1995/07/27  22:02:33  jims
 * Add header with file name, short description, $Id and $Log
 * 
 ****************************************************************************/

#include "rw/compiler.h"

#ifdef RW_NO_STL
#  include "rw/xpdlist.h"
#else

// STL-based implementation:

#ifndef RW_HEADER_WITH_EXTENSION
# include <list>
#else
# include <list.hpp>
#endif  /* RW_HEADER_WITH_EXTENSION */

#include "rw/ep_seq.h"

template <class T> class RWTExport RWTPtrDlistIterator;

#ifdef RW_PRAGMA_EXPORT
#pragma export on
#endif

template <class T>
class RWTExport RWTPtrDlist : private RW_PSeq<list<T*,allocator>, RWTPtrDlist<T>, T>
{
public:
// typedefs
  typedef list<T*,allocator> container_type;
  typedef RWTPtrDlist<T> this_type;
  typedef RW_PSeq<container_type, RWTPtrDlist<T>, T> base1_type;
  typedef container_type::size_type size_type;
  typedef container_type::difference_type difference_type;

  typedef base1_type::iterator iterator;
  typedef base1_type::const_iterator const_iterator;
  typedef base1_type::value_type value_type;
  typedef base1_type::const_value const_value;
  typedef base1_type::reference reference;
  typedef base1_type::const_reference const_reference;

#  ifndef RW_BROKEN_ACCESS_ADJUST   // djk
     typedef base1_type base2_type;
     typedef base1_type base3_type;
#  else
     typedef RW_PCntnr<container_type,this_type,T> base2_type;
     typedef RW_PBase<container_type,this_type> base3_type;
#  endif  /* RW_BROKEN_ACCESS_ADJUST */
  
// STL-level access
  const container_type&  std() const { return impl_; }
  container_type&  std() { return impl_; }

// inherited interface components
  base3_type::at;
  base3_type::begin;
  base3_type::end;
  base3_type::entries;
  base3_type::first;
  base3_type::isEmpty;
  base3_type::npos;
  base3_type::operator();
  base3_type::operator[];
  base2_type::apply;
  base2_type::contains;
  base2_type::find;
  base2_type::index;
  base2_type::occurrencesOf;
  base2_type::operator<;
  base2_type::operator==;
  base1_type::append;
  base1_type::clear;
  base1_type::get;
  base1_type::insert;
  base1_type::insertAt;
  base1_type::minElement;
  base1_type::maxElement;
  base1_type::prepend;
  base1_type::remove;
  base1_type::removeAt;
  base1_type::removeFirst;
  base1_type::replaceAll;

  container_type::size_type  removeAll(const T* key);
  container_type::size_type  removeAll(bool (*fn)(const T*, void*),void* x);
  container_type::size_type  removeAll(bool (*fn)(T*, void*),void* x);
  void                       clearAndDestroy();

// additional interface components
  T*&       last()       { return impl_.back(); }
  T*const&  last() const { return impl_.back(); }

  T*    removeLast()
    { T* ret = *(--end());
      impl_.pop_back();
      return ret; }

  void  sort();

// assignment operators
  RWTPtrDlist<T>&   operator=(const RWTPtrDlist<T>& rwset)
    { impl_ = rwset.impl_; return *this; }
  RWTPtrDlist<T>&   operator=(const container_type& stdset)
    { impl_ = stdset; return *this; }

// constructors
  RWTPtrDlist()                                : impl_() {}
  RWTPtrDlist(const container_type& lst)       : impl_(lst) {}
  RWTPtrDlist(const this_type& lst)            : impl_(lst.impl_) {}
  RWTPtrDlist(size_type n, T* p = 0)           : impl_(n, p) {}
  RWTPtrDlist(T*const* first, T*const* last)
    : impl_(first, last) { }

//  ~RWTPtrDlist(); // handled by compiler-generated destructor

protected:
  void sortPairs();
  void pointerMerge(iterator, iterator, iterator);

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

RWDECLARE_PERSISTABLE_TEMPLATE(RWTPtrDlist)

/****************************************************************
 *                                                              *
 *      Declarations for RWTPtrDlistIterator<T>                 *
 *                                                              *
 ****************************************************************/

template <class T> class RWTExport RWTPtrDlistIterator
{
private:
  RWTPtrDlist<T>*           list_;
  RWTPtrDlist<T>::iterator  iter_;
  bool                      head_;  // simulate head sentinel
  bool                      tail_;  // simulate tail sentinel
/*
 * Invariants:
 *   head_ and tail_ can never both be true
 *   if head_ and tail_ are both false then iter_ is valid
 *     (unless invalidated from outside this class)
 */
public:
  RWTPtrDlistIterator(RWTPtrDlist<T>& s)
    : list_(&s), head_(true), tail_(false) { }

  // Operators:
  RWBoolean     operator++();
  RWBoolean     operator--();
  RWBoolean     operator+=(size_t n);
  RWBoolean     operator-=(size_t n);
  T*            operator()()   { return ++(*this) ? key() : rwnil; }         

  // Methods:
  RWTPtrDlist<T>* container() const  { return list_; }

  T*            findNext(const T* p);
  T*            findNext(RWBoolean (*testFun)(T*, void*), void* d);

  void          insertAfterPoint(T* p);

  T*            key() const { return *((RWTPtrDlistIterator<T>*)this)->iter_; }

  T*            remove();

  T*            removeNext(const T* p);
  T*            removeNext(RWBoolean (*testFun)(T*, void*), void* d);

  void          reset()       { head_ = true; tail_ = false; } 
  void          reset(RWTPtrDlist<T>& s)
    { head_ = true; tail_ = false; list_ = &s; } 

private:

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

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

#ifdef RW_PRAGMA_EXPORT
#pragma export off
#endif

#endif /* RW_NO_STL */

#endif /* __RWTPDLIST_H__ */
