#ifndef __RWTPSLIST_H__
#define __RWTPSLIST_H__

/***************************************************************************
 *
 * tpslist.h - RWTPtrSlist<T>
 *   
 *     : pointer-based singly-linked list wrapper for STL-compliant rw_slist
 * 
 * $Id: tpslist.h,v 7.20 1996/05/07 19:57:40 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: tpslist.h,v $
 * Revision 7.20  1996/05/07 19:57:40  kyle
 * Added export pragma for Mac code fragments
 *
 * Revision 7.19  1996/03/16 13:14:56  jims
 * Add non-const std() mf
 *
 * Revision 7.18  1996/03/16 13:12:45  jims
 * Fix arg names in op=
 *
 * Revision 7.17  1996/03/14 21:49:07  kyle
 * Millenium changes <rw/compiler.h> to "rw/compiler.h".
 *
 * Revision 7.16  1996/02/18 01:48:40  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 02:42:30  jims
 * Standard library extensions now in directory rw/stdex
 *
 * Revision 7.13  1996/01/25 01:51:32  kevinj
 * Improved ETP macros.
 *
 * Revision 7.12  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.11  1996/01/23 07:44:33  jims
 * Change ctor arg from T** to T*const*
 *
 * Revision 7.10  1996/01/13 23:26:30  kevinj
 * Simple 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.7  1995/10/20 06:46:20  jims
 * Port to msvc 4.0: don't use access adjustment for typedefs
 *
 * Revision 7.6  1995/09/25  11:06:20  jims
 * Use clearAndDestroy from base class
 * Add NO_CONST_INST version of T**,T** ctor
 *
 * Revision 7.5  1995/09/15  21:14:32  griswolf
 * Remove un-needed destructor, leaving it commented as documentation.
 *
 * Revision 7.4  1995/09/12  19:41:49  griswolf
 * Change to #include renamed rw/rwstl/ header file.
 *
 * Revision 7.3  1995/09/07  23:39:19  griswolf
 * Scopus #2053: remove a hack that mimics template member functions.
 *
 * Revision 7.2  1995/09/05  21:52:14  jims
 * Change tlyrs.h to tlyrs.str
 *
 * Revision 7.1  1995/08/24  18:39:34  jims
 * Move to revision 7.1
 *
 ****************************************************************************/

#include "rw/compiler.h"

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

// STL-based implementation:

#include <rw/stdex/slist.h>
#include "rw/ep_seq.h"

template <class T> class RWTExport RWTPtrSlistIterator;

#ifdef RW_PRAGMA_EXPORT
#pragma export on
#endif

template <class T>
class RWTExport RWTPtrSlist
  : private RW_PSeq<rw_slist<T*>, RWTPtrSlist<T>, T>
{
public:
// typedefs
  typedef rw_slist<T*> container_type;
  typedef RWTPtrSlist<T> this_type;
  typedef RW_PSeq<rw_slist<T*>,RWTPtrSlist<T>, T> base_type;
  typedef container_type::size_type size_type;
  typedef container_type::difference_type difference_type;

  typedef base_type::iterator iterator;
  typedef base_type::const_iterator const_iterator;
  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;

// STL-level access
  const container_type&  std() const { return impl_; }
  container_type&  std() { return impl_; }

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

// additional interface components
  T*&       last()       { return impl_.back(); }
  T*const&  last() const { return impl_.back(); }
  T*        removeLast()
    { T* ret = last(); removeAt(entries()-1); return ret; }

  void  sort();

// assignment operators
  RWTPtrSlist<T>&   operator=(const RWTPtrSlist<T>& rwlst)
    { impl_ = rwlst.impl_; return *this; }
  RWTPtrSlist<T>&   operator=(const rw_slist<T*>& stdlst)
    { impl_ = stdlst; return *this; }

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

// ~RWTPtrSlist(); // 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 RWTPtrSlistIterator<T>;
};

RWDECLARE_PERSISTABLE_TEMPLATE(RWTPtrSlist)

/****************************************************************
 *                                                              *
 *      Declarations for RWTPtrSlistIterator<T>         *
 *                                                              *
 ****************************************************************/

template <class T> class RWTExport RWTPtrSlistIterator
{
private:
  RWTPtrSlist<T>*           list_;
  RWTPtrSlist<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:
  RWTPtrSlistIterator(RWTPtrSlist<T>& s)
    : list_(&s), head_(true), tail_(false) { }

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

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

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

  void          insertAfterPoint(T* a);

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

  T*            remove();

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

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

private:

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


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

#ifdef RW_PRAGMA_EXPORT
#pragma export off
#endif

#endif /* RW_NO_STL */

#endif /* __RWTPDLIST_H__ */
