/***************************************************************************
 *
 * tpslist.cc - template definitions for RWTPtrSlist<T>
 * 
 * $Id: tpslist.cc,v 7.10 1996/02/21 23:10:51 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: tpslist.cc,v $
 * Revision 7.10  1996/02/21 23:10:51  griswolf
 * Fix an N^2 order saveGuts technique to make it linear.
 *
 * Revision 7.9  1996/02/18 01:48:38  griswolf
 * Replace tabs with spaces, per Rogue Wave standard.
 *
 * Revision 7.8  1996/02/12 21:03:06  kevinj
 * Switched stream and class args in rw*Guts.
 *
 * Revision 7.7  1996/01/25 01:51:32  kevinj
 * Improved ETP macros.
 *
 * Revision 7.6  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.5  1996/01/13 23:26:30  kevinj
 * Simple External Template Persistence (ETP).
 *
 * Revision 7.4  1995/12/20 16:43:24  pearson
 * Fix typo
 *
 * Revision 7.3  1995/11/13  12:47:12  jims
 * New stl-based collections now one with Tools-6 templates
 *
 * Revision 7.3  1995/09/25 11:11:06  jims
 * comment out clearAndDestroy in favor of base class version
 *
 * Revision 7.2  1995/09/05  21:58:59  jims
 * Change tlyrs.h to tlyrs.str
 *
 * Revision 7.1  1995/08/29  11:14:19  jims
 * Move to revision 7.1
 *
 * Revision 1.1  1995/08/29  11:14:07  jims
 * Initial revision
 * 
 ****************************************************************************/

/*
 * Having trouble with these overridden clearAndDestroy and
 * removeAll functions.
 * For now we'll use the version inherited from the base class.
 */

#if 0
template <class T> void
RWTPtrSlist<T>::clearAndDestroy() {
  for(iterator i = begin(); i != end(); /**/) {
    iterator nx = i;
    ++nx;
    delete *i;
    impl_.erase(i);
    i = nx;
  }
}
#endif

#if 0
template <class T> rw_slist<T*>::size_type
RWTPtrSlist<T>::removeAll(bool (*fn)(RWTPtrSlist<T>::const_value key,void*),void* x)
{
  rw_slist<T*>::size_type ret = 0;
  for(iterator it = begin(); it != end();/**/) {
    if((*fn)(*it,x)) {
      impl_.erase(it);
      ++ret;
    }
    ++it;
  }
  return ret;
}

template <class T> rw_slist<T*>::size_type
RWTPtrSlist<T>::removeAll(const_value key)
{
  rw_slist<T*>::size_type ret = 0;
  for(iterator it = begin(); it != end();/**/) {
    if(**it == *key) {
      impl_.erase(it);
      ++ret;
    }
    ++it;
  }
  return ret;
}
#endif

/*
 * This is a "for now" sorting algorithm.  Ultimately the underlying
 * implementation will have a template sort member function that
 * takes a comparitor, needed for dereferencing the elements
 * in the rw_slist<T*> impl_.
 *
 * So, for now, here is an insertion sort just like the one in "Algorithms"
 * by Robert Sedgwick, 2nd Edition, p.100:
 */

template <class T>
void RWTPtrSlist<T>::sort()
{
  int i, j;
  T* v;
  for (i=1; i<entries(); i++) {
    v = at(i);
    j = i;
    while (j > 0 && *at(j-1) > *v) {
      at(j) = at(j-1);
      --j;
    }
    at(j) = v;
  }
}
        

//===========================================

template <class T> RWBoolean
RWTPtrSlistIterator<T>::operator++()
{
  RWASSERT(!(head_ == true  &&  tail_ == true)); 

  if (tail_)
    /* do nothing */ ;

  else if (head_) {
    iter_ = list_->begin();
    head_ = false;
  }
  else
    ++iter_;

  if (!tail_ && iter_ == list_->end())
    tail_ = true;

  RWASSERT(!(head_ == true  &&  tail_ == true)); 

  return !tail_;
}


template <class T> RWBoolean
RWTPtrSlistIterator<T>::operator+=(size_t n)
{
  while (n--) ++(*this); return !tail_;
}     

template <class T> T*
RWTPtrSlistIterator<T>::findNext(const T* p)
{
  T* a;
  while ( (a = (*this)()) != rwnil)
  {
    if (*a == *p)
      return a;
  }
  return rwnil;
}

template <class T> T*
RWTPtrSlistIterator<T>::findNext(RWBoolean (*testFun)(T*, void*), 
                                 void* d)
{
  RWPRECONDITION(testFun!=rwnil);

  T* a;
  while ( (a = (*this)()) != rwnil)
  {
    if (testFun(a, d))
      return a;
  }
  return rwnil;
}

template <class T> void
RWTPtrSlistIterator<T>::insertAfterPoint(T* a)
{
  RWPRECONDITION2(!tail_, "RWTPtrSlistIterator::insertAfterPoint(T* a): Attempt to insertAfterPoint while iterator is past the end");

  RWTPtrSlist<T>::iterator i;

  if (head_)
    list_->prepend(a);
  else {
    i = iter_;
    ++i;
    list_->impl_.insert(i,a);
  }
}

template <class T> T*
RWTPtrSlistIterator<T>::remove()
{
  RWTPtrSlist<T>::iterator i, j;
  T* ret;

  if (head_ || tail_)
    return rwnil;               // iterator is not active

  if (iter_ == list_->begin()) {
    ret = list_->removeFirst();
    head_ = true;
  }
  else {
    // As the manual warns, for slists this is "relatively inefficient:"
    i = list_->begin();  
    do {
      j = i;
      ++i;
    } while (i != iter_);

    ret = *iter_;
    list_->impl_.erase(iter_);
    iter_ = j;
  }
  return ret;
}

template <class T> T*
RWTPtrSlistIterator<T>::removeNext(const T* p)
{
  while (++(*this)) {
    if (*(*iter_)  == *p) {
      return remove();
    }
  }
  return rwnil;
}

template <class T> T*
RWTPtrSlistIterator<T>::removeNext(RWBoolean (*testFun)(T*, void*),
                                   void* d)
{
  RWPRECONDITION(testFun!=rwnil);

  while (++(*this)) {
    if (testFun(*iter_,d)) {
      return remove();
    }
  }
  return rwnil;
}

#include "rw/epersist.h"

RWDEFINE_PERSISTABLE_TEMPLATE(RWTPtrSlist)

template <class T, class S>
void rwSaveGuts(S& str, const RWTPtrSlist<T>& c)
{
  str << c.entries();
  RWTPtrSlist<T>::const_iterator it = c.begin();
  while(it != c.end()) {
    str << *(*it);
    ++it;
  }
}

template <class T, class S>
void rwRestoreGuts(S& str, RWTPtrSlist<T>& c)
{
  typedef RWTPtrSlist<T>::size_type size_type;
  size_type count;
  str >> count;
  c.clear();
  for (size_type i=0; i < count; ++i) {
    T* p;
    str >> p;
    c.insert(p);
  }
}
