#ifndef __RWTPSRTDLI_H__
#define __RWTPSRTDLI_H__

/***************************************************************************
 *
 * tpsrtdli.h - RWTPtrSortedDlist<T,Compare>
 *   
 *     : pointer-based sorted doubly-linked list wrapper for STL list
 * 
 * $Id: tpsrtdli.h,v 7.20 1996/05/07 19:57:56 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: tpsrtdli.h,v $
 * Revision 7.20  1996/05/07 19:57:56  kyle
 * Added export pragma for Mac code fragments
 *
 * Revision 7.19  1996/03/16 13:16:58  jims
 * Change T** args in ctor to T*const*
 *
 * Revision 7.18  1996/03/13 23:26:48  griswolf
 * Override index(key) method to make a single traversal.
 *
 * Revision 7.17  1996/02/19 12:34:12  jims
 * Port to Stdlib 1.2
 *
 * Revision 7.16  1996/02/18 01:48:43  griswolf
 * Replace tabs with spaces, per Rogue Wave standard.
 *
 * Revision 7.15  1996/02/13 12:14:17  jims
 * Small mods to the interface
 *
 * Revision 7.14  1996/02/07 06:04:13  jims
 * Remove devnotes (#2555)
 *
 * Revision 7.13  1996/01/26 15:58:58  pearson
 * Per Jim S., remove redundant declarations of base_type::begin base_type::end
 * and base_type::apply
 *
 * Revision 7.12  1996/01/25  01:51:32  kevinj
 * Improved ETP macros.
 *
 * Revision 7.11  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.10  1996/01/23 07:33:55  jims
 * Remove insertAfterPoint from external iterator
 * Add template args when class used within code body
 *
 * Revision 7.9  1996/01/13 23:52:40  kevinj
 * Simple External Template Persistence (ETP).
 *
 * Revision 7.8  1995/12/20 23:19:20  kevinj
 * External template persistence using RW*Persistor.
 *
 * Revision 7.7  1995/12/14 18:01:21  jims
 * saveOn, restoreFrom now global instead of member functions
 *
 * Revision 7.6  1995/12/13 00:22:58  jims
 * Remove member functions saveOn and restoreFrom (now global)
 *
 * 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/12/02 02:24:37  jims
 * Use base_type::iterator for RWTPtrSortedDlistIterator
 *
 * Revision 7.2  1995/12/01 18:08:01  jims
 * Rename to 6.1 style name, add 6.1 style external iterator
 *
 * Revision 7.1  1995/11/29 18:27:27  jims
 * Move to revision 7.1
 *
 * Revision 7.16  1995/10/20  06:46:20  jims
 * Port to msvc 4.0: don't use access adjustment for typedefs
 *
 * Revision 7.15  1995/09/25  11:09:35  jims
 * Add NO_CONST_INST version of T**,T** ctor
 *
 * Revision 7.14  1995/09/18  17:42:07  jims
 * Remove prepend mf
 *
 * Revision 7.13  1995/09/15  21:28:02  griswolf
 * Remove destructor, leaving comment as documentation.
 *
 * Revision 7.12  1995/09/05  21:52:55  jims
 * Change tlyrs.h to tlyrs.str
 *
 * Revision 7.11  1995/08/18  17:32:51  kevinj
 * Added RW_DEFINE_PERSISTABLE_COLLECTION.
 *
 * Revision 7.10  1995/08/15  16:01:53  kevinj
 * Added restoreFrom and saveOn.
 *
 * Revision 7.9  1995/08/11  22:16:13  jims
 * Port to SL1007 / bcc45
 *
 * Revision 7.8  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

// No Tools 6.1 equivalent class, STL-based implementation only

#error Cannot include header if RW_NO_STL macro is defined for your compiler

#endif

#include <list>
#include "rw/ep_srtsq.h"

template <class T, class C> class RWTExport RWTPtrSortedDlistIterator;

#ifdef RW_PRAGMA_EXPORT
#pragma export on
#endif

template <class T, class C>
class RWTExport RWTPtrSortedDlist
  : private RW_PSrtSeq<list<T*,allocator>,  RWTPtrSortedDlist<T,C>,
                          rw_deref_compare<C,T>, T>
{
public:
  typedef rw_deref_compare<C,T>      container_comp;
  typedef list<T*,allocator>         container_type;
  typedef RWTPtrSortedDlist<T,C>     this_type;

  typedef RW_PSrtSeq<container_type,this_type,container_comp, T> base_type;

  typedef container_type::size_type       size_type;
  typedef container_type::difference_type difference_type;

  typedef base_type::value_type      value_type;
  typedef base_type::const_value     const_value;
  typedef base_type::reference       reference;
  typedef base_type::const_reference const_reference;
  typedef base_type::const_iterator  const_iterator;
  typedef base_type::iterator        iterator;
  
// STL-level access
  const container_type&  std() const { return impl_; }
  container_type&        std()       { return impl_; }

// inherited interface components
  base_type::clear;
  base_type::clearAndDestroy;
  base_type::contains;
  base_type::entries;
  base_type::find;
  base_type::first;
  size_type index(const_value) const;
  size_type index(bool (*fn)(const_value, void*), void* x) const
  { return base_type::index(fn,x); }
  base_type::insert;
  base_type::isEmpty;
  base_type::isSorted;
  base_type::npos;
  base_type::occurrencesOf;
  base_type::operator<;
  base_type::operator==;
  base_type::remove;
  base_type::removeAll;
  base_type::removeAt;
  base_type::removeFirst;
  /*
   * Be careful with these, misuse can lead to the collection
   * becoming out of sorts, so to speak.  
   */
  base_type::operator[];
  base_type::operator();
  base_type::apply;
  base_type::at;
  base_type::begin;
  base_type::end;

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


  size_type         merge(const this_type& coll)
    {
      size_type ret = coll.entries();

      if (ret) {
        container_type::iterator first1 = impl_.begin();
        container_type::iterator last1  = impl_.end();
        const_iterator    first2 = coll.begin();
        const_iterator    last2 = coll.end();

        while (first1 != last1 && first2 != last2) {
          while (first2 != last2 &&
                 container_comp()(*first2,*first1)) {
            impl_.insert(first1,*first2);
            ++first2;
          }
          ++first1;
        }

        while (first2 != last2) {
          impl_.insert(impl_.end(), *first2);
          ++first2;
        }
      }
      return ret;
    }
  T*    removeLast() { T* ret = *(--base_type::end());
                       impl_.pop_back(); return ret; }

// constructors
  RWTPtrSortedDlist() { }
  RWTPtrSortedDlist(const this_type& t) : impl_(t.impl_) { }
  RWTPtrSortedDlist(const container_type& t) {
    insert(t);
  }
  RWTPtrSortedDlist(size_type n, T* val) : impl_(n,val) { }

  RWTPtrSortedDlist(T*const* first, T*const* last);

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

private:
  container_type impl_;
  friend class RW_PBase<container_type, this_type>;
  friend class RWTExport RWTPtrSortedDlistIterator<T,C>;
};

RWDECLARE_PERSISTABLE_TEMPLATE_2(RWTPtrSortedDlist)

/****************************************************************
 *                                                              *
 *      Declarations for RWTPtrSortedDlistIterator<T>           *
 *                                                              *
 ****************************************************************/

template <class T, class C> class RWTExport RWTPtrSortedDlistIterator
{
public:
  RWTPtrSortedDlistIterator(RWTPtrSortedDlist<T,C>& 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:
  RWTPtrSortedDlist<T,C>* container() const  { return list_; }

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

  T*    key() const
    { return *((RWTPtrSortedDlistIterator<T,C>*)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(RWTPtrSortedDlist<T,C>& s)
    { head_ = true; tail_ = false; list_ = &s; }

private:
  RWTPtrSortedDlist<T,C>*           list_;
  RWTPtrSortedDlist<T,C>::base_type::iterator  iter_;
  bool                              head_;  // simulate head sentinel
  bool                              tail_;  // simulate tail sentinel

  // 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/tpsrtdli.cc"
#endif

#ifdef RW_PRAGMA_EXPORT
#pragma export off
#endif

#endif /* __RWTPSRTDLI_H__ */
