#ifndef __RWTPSRTVEC_H__
#define __RWTPSRTVEC_H__

/***************************************************************************
 *
 * tpsrtvec.h - RWTPtrSortedVector<T,C>
 *   
 *     : pointer-based sorted vector wrapper for STL vector
 * 
 * $Id: tpsrtvec.h,v 7.27 1996/05/07 19:58:16 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: tpsrtvec.h,v $
 * Revision 7.27  1996/05/07 19:58:16  kyle
 * Added export pragma for Mac code fragments
 *
 * Revision 7.26  1996/04/29 19:12:43  groening
 * needed to include compiler.h prior to making any references to
 * RW_NO_STL.
 *
 * Revision 7.25  96/03/21  23:51:38  23:51:38  pearson (Thomas Pearson)
 * #2904 - Put #include "rw/ep_srtsq.h" before #include<vector>
 * 
 * Revision 7.24  1996/03/16 13:18:27  jims
 * Modify resize mf to take size_type instead of size_t
 *
 * Revision 7.23  1996/03/16 13:16:16  jims
 * Change T** args in ctor to T*const*
 *
 * Revision 7.22  1996/03/14 16:52:39  griswolf
 * Avoid possible "redefinition of RWDefCargs" warnings.
 *
 * Revision 7.21  1996/02/19 12:34:12  jims
 * Port to Stdlib 1.2
 *
 * Revision 7.20  1996/02/18 03:07:20  kyle
 * Fixed base_type
 *
 * Revision 7.19  1996/02/18 02:29:26  kyle
 * Millenium adds support for compilers with bad access adjust and for
 * StdLib needing file name extensions.
 *
 * Revision 7.18  1996/02/18 01:48:45  griswolf
 * Replace tabs with spaces, per Rogue Wave standard.
 *
 * Revision 7.17  1996/02/13 12:14:17  jims
 * Small mods to the interface
 *
 * Revision 7.16  1996/02/07 06:04:13  jims
 * Remove devnotes (#2555)
 *
 * Revision 7.15  1996/01/30 22:42:12  jims
 * Modify return type of data() mf
 *
 * Revision 7.14  1996/01/26 04:47:13  jims
 * Remove redundant declarations of apply, begin, and end
 *
 * 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/15 17:56:06  kevinj
 * Fixed bug in ETP.
 *
 * Revision 7.10  1996/01/14 00:10:18  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/17 08:15:43  jims
 * Merge otis-et and otis-stl templates
 *
 * Revision 7.17  1995/10/20 06:46:20  jims
 * Port to msvc 4.0: don't use access adjustment for typedefs
 *
 * Revision 7.16  1995/09/25  11:09:35  jims
 * Add NO_CONST_INST version of T**,T** ctor
 *
 * Revision 7.15  1995/09/18  17:42:07  jims
 * Remove prepend mf
 *
 * Revision 7.14  1995/09/15  21:14:32  griswolf
 * Remove un-needed destructor, leaving it commented as documentation.
 *
 * Revision 7.13  1995/09/05  21:52:59  jims
 * Change tlyrs.h to tlyrs.str
 *
 * Revision 7.12  1995/08/18  17:55:34  kevinj
 * Added RW_DEFINE_PERSISTABLE_COLLECTION.
 *
 * Revision 7.11  1995/08/15  17:35:40  kevinj
 * Added restoreFrom and saveOn.
 *
 * Revision 7.10  1995/08/11  22:17:22  jims
 * Port to SL1007 / bcc45
 *
 * Revision 7.9  1995/08/02  00:04:36  jims
 * Qualify include file names with "rw/"
 *
 * 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
// Tools 6.1 implementation:
#  include "rw/xpsrtvec.h"
#  undef RWDefCArgs
#  define RWDefCArgs(T)

#else
#  ifndef RWDefCArgs
#    define RWDefCArgs(T) ,less<T>
#  endif

// STL-based implementation:

#include "rw/ep_srtsq.h"   // #2904 - before #include<vector>

#ifndef RW_HEADER_WITH_EXTENSION
# include <vector>
#else
# include <vector.hpp>
#endif

#ifdef RW_PRAGMA_EXPORT
#pragma export on
#endif

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

  typedef container_type::value_type                value_type;
  typedef container_type::reference                 reference;
  typedef container_type::const_reference           const_reference;
  typedef container_type::const_iterator            const_iterator;
  typedef container_type::const_iterator            iterator;
  typedef container_type::size_type                 size_type;
  typedef container_type::difference_type           difference_type;
  typedef const T*                                  const_value;

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

#ifndef RW_BROKEN_ACCESS_ADJUST
  typedef base1_type base2_type;
  typedef base1_type base3_type;
  typedef base1_type base4_type;
#else
    // for inherited interface components cannot always follow base_type
  typedef RW_PSeq<container_type,this_type,T> base2_type;
  typedef RW_PCntnr<container_type,this_type,T> base3_type;
  typedef RW_PBase<container_type,this_type> base4_type;
#endif
  
  base2_type::clear;    
  base2_type::clearAndDestroy;                      
  base1_type::contains;
  base4_type::entries;
  base1_type::find;
  base4_type::first;
  base1_type::index;
  base1_type::insert;
  base4_type::isEmpty;
  base1_type::isSorted;
  base4_type::npos;
  base1_type::occurrencesOf;
  base3_type::operator<;
  base3_type::operator==;
  base1_type::remove;
  base1_type::removeAll;
  base2_type::removeAt;
  base2_type::removeFirst;
  /*
   * Be careful with these, misuse can lead to the collection
   * becoming out of sorts, so to speak.  (We need to allow the
   * index ops for source compatibility with Tools 6.)
   */
  base4_type::operator[];
  base4_type::operator();
  base3_type::apply;
  base4_type::at;
  base4_type::begin;
  base4_type::end;

// additional interface components
  T*const*      data() const { return &(*begin()); }

  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) {
        size_type n = entries() + ret;
        container_type v(n);
        ::merge(impl_.begin(), impl_.end(),
                coll.impl_.begin(), coll.impl_.end(), v.begin(),
                container_comp());
        impl_ = v;
      }
      return ret;
    }

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

  size_type         length() const       { return std().size(); }
  void              resize(size_type n)  { impl_.reserve(n); }

// constructors
  RWTPtrSortedVector() { }
  RWTPtrSortedVector(const this_type& t) : impl_(t.impl_) { }
  RWTPtrSortedVector(const container_type& t) {
    resize(t.size()); // purely for efficiency
    insert(t);
  }
  RWTPtrSortedVector(size_type n, T* val) : impl_(n,val) { }

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

  // For Tools-6 compatibility (not particularly efficient):
  RWTPtrSortedVector(size_type n) { resize(n); }

// ~RWTPtrSortedVector(); // handled by auto-generated destructor

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

RWDECLARE_PERSISTABLE_TEMPLATE_2(RWTPtrSortedVector)

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

#ifdef RW_PRAGMA_EXPORT
#pragma export off
#endif

#endif /* RW_NO_STL */

#endif /* __RWTPSRTVEC_H__ */
