#ifndef __RWTVSRTDLI_H__
#define __RWTVSRTDLI_H__

/***************************************************************************
 *
 * tvsrtdli.h - RWTValSortedDlist<T,Compare>
 *   
 *     : value-based sorted doubly-linked list wrapper for STL list
 *
 * $Id: tvsrtdli.h,v 7.18 1996/05/07 18:31:47 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: tvsrtdli.h,v $
 * Revision 7.18  1996/05/07 18:31:47  kyle
 * Added export pragma for Mac code fragments
 *
 * Revision 7.17  1996/03/20 08:50:57  jims
 * Remove const qualifier from operator() size_type argument
 *
 * Revision 7.16  1996/03/14 00:24:16  griswolf
 * Scopus #2555: Override index(key) to optimize for list.
 *
 * Revision 7.15  1996/02/19 12:34:12  jims
 * Port to Stdlib 1.2
 *
 * Revision 7.14  1996/02/18 01:50:22  griswolf
 * Replace tabs with spaces, per Rogue Wave standard.
 *
 * Revision 7.13  1996/02/07 06:04:13  jims
 * Remove devnotes (#2555)
 *
 * Revision 7.12  1996/01/30 08:40:56  jims
 * Use local iterator type instead of container_type::iterator in ctor
 *
 * Revision 7.11  1996/01/25 01:51:32  kevinj
 * Improved ETP macros.
 *
 * Revision 7.10  1996/01/23 08:00:22  jims
 * Remove declaration for insertAfterPoint in external iterator
 *
 * 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:25:12  jims
 * Use standard interator for RWTValSortedDlistIterator
 *
 * 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 20:19:34  jims
 * Move to revision 7.1
 *
 * Revision 7.12  1995/09/18  17:42:07  jims
 * Remove prepend mf
 *
 * Revision 7.11  1995/09/15  21:28:02  griswolf
 * Remove destructor, leaving comment as documentation.
 *
 * Revision 7.10  1995/09/05  21:56:49  jims
 * Change tlyrs.h to tlyrs.str
 *
 * Revision 7.9  1995/08/18  17:41:58  kevinj
 * Added RW_DEFINE_PERSISTABLE_COLLECTION.
 *
 * Revision 7.8  1995/08/15  16:35:09  kevinj
 * Added restoreFrom and saveOn.  Changed "DList" to "Dlist" for
 * consistency with other class names.
 *
 * Revision 7.7  1995/08/02  00:04:36  jims
 * Qualify include file names with "rw/"
 *
 * Revision 7.6  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
#error Cannot include header if RW_NO_STL macro is defined for your compiler
#endif

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

#ifdef RW_PRAGMA_EXPORT
#pragma export on
#endif

template <class T, class C>
class RWTExport RWTValSortedDlist
         : private RW_VSrtSeq<list<T,allocator>,RWTValSortedDlist<T,C>, C>
{
public:
// typedefs
  typedef list<T,allocator> container_type;
  typedef RWTValSortedDlist<T,C> this_type;
  typedef RW_VSrtSeq<container_type,this_type,C> base_type;
  typedef container_type::const_iterator iterator;
  typedef container_type::const_iterator const_iterator;
  typedef container_type::size_type size_type;
  typedef container_type::value_type value_type;
  typedef container_type::const_reference reference;
  typedef container_type::const_reference const_reference;

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

public:  
// inherited interface components
  base_type::begin;
  base_type::clear;
  base_type::contains;
  base_type::end;
  base_type::entries;
  base_type::find;
  base_type::first;
//  base_type::index;
  size_type index(const_reference) const;
  size_type index(bool (*fn)(const_reference, 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;

// selective inheritance (hides other same-name components)
  void              apply(void (*fn)(const_reference,void*),void *x) const
    { base_type::apply(fn,x); }

  const_reference   operator[](size_type index) const
    { return base_type::operator[](index); }

  const_reference   operator()(size_type index) const
    { return base_type::operator()(index); }

  const_reference   at(size_type index) const { return base_type::at(index); }

// additional interface components
  const_reference   last() const { return std().back(); }
  T                 removeLast()
    { T ret = *(--end()); impl_.pop_back(); return ret; }

  // (Can't use STL list<T>::merge mf until member function templates)
  size_type         merge(const this_type& coll);

// assignment operators
  RWTValSortedDlist<T,C>&   operator=(const RWTValSortedDlist<T,C>& rwcoll)
    { impl_ = rwcoll.impl_; return *this; }
  RWTValSortedDlist<T,C>&   operator=(const container_type& stdcoll)
  { clear(); insert(stdcoll); return *this; }
  
// constructors
  RWTValSortedDlist() { }
  RWTValSortedDlist(const RWTValSortedDlist<T,C>& t) : impl_(t.impl_) { }
  RWTValSortedDlist(const container_type& t) { insert(t); }
  RWTValSortedDlist(size_type n, const T& val = T()) : impl_(n,val) {}
  RWTValSortedDlist(const T* first, const T* last) {
    iterator it = begin();
    while(first != last)
      it = insertSorted(it,*first++);
  }
// ~RWTValSortedDlist(); // handled by auto-generated destructor

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

RWDECLARE_PERSISTABLE_TEMPLATE_2(RWTValSortedDlist)

/****************************************************************
 *                                                              *
 *      Declarations for RWTValSortedDlistIterator<T,C>         *
 *                                                              *
 ****************************************************************/

template <class T, class C> class RWTExport RWTValSortedDlistIterator
{
public:
  RWTValSortedDlistIterator(RWTValSortedDlist<T,C>& s)
    : list_(&s), head_(true), tail_(false) { }

  // Operators:
  RWBoolean     operator++();
  RWBoolean     operator--();

  RWBoolean     operator+=(size_t n);
  RWBoolean     operator-=(size_t n);

  RWBoolean     operator()()         { return ++(*this); }

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

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

  T                     key() const   { return *iter_; }

  RWBoolean             remove();       // Remove item at cursor

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

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

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

  // Disallow postfix increment & decrement.  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/tvsrtdli.cc"
#endif

#ifdef RW_PRAGMA_EXPORT
#pragma export off
#endif

#endif /* __RWTVSRTDLI_H__ */
