Source: classlib/include/classlib/stacks.h


Annotated List
Files
Globals
Hierarchy
Index
/***************************************************************************
    stacks.h  -  stacks di oggetti
                             -------------------
    begin                : ven dic  7 17:40:01 CET 2001
    copyright            : (C) 2001 by Nicola De Nisco
    email                : nicola@winada.it
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#if !defined( __CLASSLIB_STACKS_H )
#define __CLASSLIB_STACKS_H

#if !defined( __CHECKS_H )
#include 
#endif  // __CHECKS_H

#if !defined( __CLASSLIB_DEFS_H )
#include "classlib/defs.h"
#endif  // __CLASSLIB_DEFS_H

#if !defined( __CLASSLIB_SHDDEL_H )
#include "classlib/shddel.h"
#endif  // __CLASSLIB_SHDDEL_H

#if !defined( __CLASSLIB_VECTIMP_H )
#include "classlib/vectimp.h"
#endif  // __CLASSLIB_VECTIMP_H

#if !defined( __CLASSLIB_LISTIMP_H )
#include "classlib/listimp.h"
#endif  // __CLASSLIB_LISTIMP_H

/**

  template  class TStackAsVectorImp

  Implements the fundamental stack operations, using a vector
  as the underlying implementation.  The type Vect specifies the
  form of the vector, either a TVectorImp or a
  TIVectorImp.  The type T specifies the type of the
  objects to be put on the stack.  When using TVectorImp,
  T should be the same as T0. When using TIVectorImp, T
  should be of type pointer to T0.  See TStackAsVector and
  TIStackAsVector for examples.

*/

template  class TStackAsVectorImp
{

public:

    TStackAsVectorImp( unsigned max = DEFAULT_STACK_SIZE ) :
        Data(max),
        Current(0)
        {
        }

    int IsEmpty() const
        {
        return Current == 0;
        }

    int IsFull() const
        {
        return Current == Data.Limit();
        }

    int GetItemsInContainer() const
        {
        return Current;
        }
protected:

    Vect Data;
    unsigned Current;

};

template  class TMStackAsVectorIterator;

/**

  template  class TMStackAsVector
  template  class TMStackAsVectorIterator

  Implements a managed stack of objects of type T, using a vector as
  the underlying implementation.

*/

template  class TMStackAsVector :
    public TStackAsVectorImp,T>
{

    typedef TStackAsVectorImp,T> Parent;

public:

    friend class TMStackAsVectorIterator;

    typedef void (*IterFunc)(T&, void *);
    typedef int  (*CondFunc)(const T&, void *);

    TMStackAsVector( unsigned max = DEFAULT_STACK_SIZE ) :
        TStackAsVectorImp,T>( max )
        {
        }

    void Push( const T& t )
        {
        PRECONDITION( Current < Data.Limit() );
        Data[Current++] = t;
        }

    T Pop()
        {
        PRECONDITION( Current > 0 );
        return Data[--Current];
        }

    const T& Top() const
        {
        PRECONDITION( Current > 0 );
        return Data[Current-1];
        }

    void ForEach( IterFunc iter, void *args )
        {
        if( !IsEmpty() )
            Data.ForEach( iter, args, 0, Current );
        }

    T *FirstThat( CondFunc cond, void *args ) const
        {
        if( IsEmpty() )
            return 0;
        return Data.FirstThat( cond, args, 0, Current );
        }

    T *LastThat( CondFunc cond, void *args ) const
        {
        if( IsEmpty() )
            return 0;
        return Data.LastThat( cond, args, 0, Current );
        }

    void Flush()
        {
        Current = 0;
        }
};

template  class TMStackAsVectorIterator :
    public TMVectorIteratorImp
{

public:

    TMStackAsVectorIterator( const TMStackAsVector& s ) :
        TMVectorIteratorImp(s.Data,0,s.Current)
        {
        }

};

/**

  template  class TStackAsVector
  template  class TStackAsVectorIterator

  Implements a stack of objects of type T, using a vector as
  the underlying implementation and TStandardAllocator as its memory
  manager.

*/

template  class TStackAsVector :
    public TMStackAsVector
{

public:

    TStackAsVector( unsigned max = DEFAULT_STACK_SIZE ) :
        TMStackAsVector( max )
        {
        }

};

template  class TStackAsVectorIterator :
    public TMStackAsVectorIterator
{

public:

    TStackAsVectorIterator( const TStackAsVector& s ) :
        TMStackAsVectorIterator(s)
        {
        }

};

template  class TMIStackAsVectorIterator;

/**

  template  class TMIStackAsVector
  template  class TMIStackAsVectorIterator

  Implements a managed stack of pointers to objects of type T,
  using a vector as the underlying implementation.

*/

template  class TMIStackAsVector :
    public TStackAsVectorImp, T * >,
    public TShouldDelete
{

    typedef TStackAsVectorImp, T * > Parent;

public:

    friend class TMIStackAsVectorIterator;

    typedef void (*IterFunc)(T&, void *);
    typedef int  (*CondFunc)(const T&, void *);

    TMIStackAsVector( unsigned max = DEFAULT_STACK_SIZE ) :
        TStackAsVectorImp,T *>( max )
        {
        }

    ~TMIStackAsVector()
        {
        Flush();
        }

    void Push( T *t )
        {
        PRECONDITION( Current < Data.Limit() );
        Data[Current++] = t;
        }

    T *Pop()
        {
        PRECONDITION( Current > 0 );
        return Data[--Current];
        }

    T *const& Top() const
        {
        PRECONDITION( Current > 0 );
        return Data[Current-1];
        }

    void Flush( DeleteType dt = DefDelete )
        {
        Data.Flush( DelObj(dt), Current );
        Current = 0;
        }

    void ForEach( IterFunc iter, void *args )
        {
        if( !IsEmpty() )
            Data.ForEach( iter, args, 0, Current );
        }

    T *FirstThat( CondFunc cond, void *args ) const
        {
        if( IsEmpty() )
            return 0;
        return Data.FirstThat( cond, args, 0, Current );
        }

    T *LastThat( CondFunc cond, void *args ) const
        {
        if( IsEmpty() )
            return 0;
        return Data.LastThat( cond, args, 0, Current );
        }
};

template  class TMIStackAsVectorIterator :
    public TMIVectorIteratorImp
{

public:

    TMIStackAsVectorIterator( const TMIStackAsVector& s ) :
        TMIVectorIteratorImp(s.Data,0,s.Current)
        {
        }

};

/**

  template  class TIStackAsVector
  template  class TIStackAsVectorIterator

  Implements a stack of pointers to objects of type T,
  using a vector as the underlying implementation and
  TStandardAllocator as its memory manager.

*/

template  class TIStackAsVector :
    public TMIStackAsVector
{

public:

    TIStackAsVector( unsigned max = DEFAULT_STACK_SIZE ) :
        TMIStackAsVector( max )
        {
        }

};

template  class TIStackAsVectorIterator :
    public TMIStackAsVectorIterator
{

public:

    TIStackAsVectorIterator( const TIStackAsVector& s ) :
        TMIStackAsVectorIterator(s)
        {
        }

};

/**

  template  class TStackAsListImp

  Implements the fundamental stack operations, using a stack
  as the underlying implementation.  The type Stk specifies the
  form of the stack, either a TStackImp or a
  TIStackImp.  The type T specifies the type of the
  objects to be put on the stack.  When using TStackImp,
  T should be the same as T0. When using TIStackImp, T
  should be of type pointer to T0.  See TStackAsList and
  TIStackAsList for examples.

*/

template  class TStackAsListImp
{

public:

    TStackAsListImp()
        {
        }

    int IsEmpty() const
        {
        return Data.IsEmpty();
        }

    int IsFull() const
        {
        return 0;
        }

    int GetItemsInContainer() const
        {
        return Data.GetItemsInContainer();
        }

protected:

    Stk Data;

};

/**

  template  class TMStackAsList
  template  class TMStackAsListIterator

  Implements a managed stack of objects of type T, using a list as
  the underlying implementation.

*/

template  class TMStackAsListIterator;

template  class TMStackAsList :
    public TStackAsListImp,T>
{

    typedef TStackAsListImp,T> Parent;

public:

    friend class TMStackAsListIterator;

    typedef void (*IterFunc)(T&, void *);
    typedef int  (*CondFunc)(const T&, void *);

    void Push( const T& t )
        {
        Data.Add( t );
        }

    T Pop();

    const T& Top() const
        {
        PRECONDITION( !Data.IsEmpty() );
        return Data.PeekHead();
        }

    void ForEach( IterFunc iter, void *args )
        {
        Data.ForEach( iter, args );
        }

    T *FirstThat( CondFunc cond, void *args ) const
        {
        return Data.FirstThat( cond, args );
        }

    T *LastThat( CondFunc cond, void *args ) const
        {
        return Data.LastThat( cond, args );
        }

    void Flush()
        {
        Data.Flush();
        }
};

template  T TMStackAsList::Pop()
{
    T t = Top();
    Data.Detach( t );
    return t;
}

template  class TMStackAsListIterator :
    public TMListIteratorImp
{

public:

    TMStackAsListIterator( const TMStackAsList& s ) :
        TMListIteratorImp(s.Data)
        {
        }

};

/**

  template  class TStackAsList
  template  class TStackAsListIterator

  Implements a stack of objects of type T, using a list as
  the underlying implementatio.

*/

template  class TStackAsList :
    public TMStackAsList
{
};

/**

  template  class TStackAsList
  template  class TStackAsListIterator

  Implements a stack of objects of type T, using a list as
  the underlying implementatio.

*/

template  class TStackAsListIterator :
    public TMStackAsListIterator
{

public:

    TStackAsListIterator( const TStackAsList& s ) :
        TMStackAsListIterator(s)
        {
        }

};

template  class TMIStackAsListIterator;

/**

  template  class TMIStackAsList
  template  class TMIStackAsListIterator

  Implements a managed stack of pointers to objects of type T,
  using a linked list as the underlying implementation.

*/

template  class TMIStackAsList :
    public TStackAsListImp,T *>,
    public TShouldDelete
{

    typedef TStackAsListImp,T *> Parent;

public:

    friend class TMIStackAsListIterator;

    typedef void (*IterFunc)(T&, void *);
    typedef int  (*CondFunc)(const T&, void *);

    ~TMIStackAsList()
        {
        Flush();
        }

    void Push( T *t )
        {
        Data.Add( t );
        }

    T *Pop();

    T *Top() const
        {
        PRECONDITION( !Data.IsEmpty() );
        return Data.PeekHead();
        }

    void Flush( DeleteType dt = DefDelete )
        {
        Data.Flush( DelObj(dt) );
        }

    void ForEach( IterFunc iter, void *args )
        {
        Data.ForEach( iter, args );
        }

    T *FirstThat( CondFunc cond, void *args ) const
        {
        return Data.FirstThat( cond, args );
        }

    T *LastThat( CondFunc cond, void *args ) const
        {
        return Data.LastThat( cond, args );
        }
};

template  T *TMIStackAsList::Pop()
{
    T * t = Top();
    Data.Detach( t );
    return t;
}

template  class TMIStackAsListIterator :
    public TMIListIteratorImp
{

public:

    TMIStackAsListIterator( const TMIStackAsList& s ) :
        TMIListIteratorImp(s.Data)
        {
        }

};

/**

  template  class TIStackAsList
  template  class TIStackAsListIterator

  Implements a stack of pointers to objects of type T,
  using a linked list as the underlying implementation and
  TStandardAllocator as its memory manager.

*/

template  class TIStackAsList :
    public TMIStackAsList
{
};

template  class TIStackAsListIterator :
    public TMIStackAsListIterator
{

public:

    TIStackAsListIterator( const TIStackAsList& s ) :
        TMIStackAsListIterator(s)
        {
        }

};

////////////////////////////////////////////////////////////////////////////

/**

  template  class TStack
  template  class TStackIterator

  Easy names for TStackAsVector and TStackAsVectorIterator.

*/

template  class TStack :
    public TStackAsVector
{

public:

    TStack( unsigned max = DEFAULT_STACK_SIZE ) :
        TStackAsVector( max )
        {
        }

};

template  class TStackIterator :
    public TStackAsVectorIterator
{

public:


    TStackIterator( const TStack& a ) :
        TStackAsVectorIterator(a)
        {
        }

};

////////////////////////////////////////////////////////////////////////////

/**

  template  class TIStack
  template  class TIStackIterator

  Easy names for TIStackAsVector and TIStackAsVectorIterator.

*/

template  class TIStack :
    public TIStackAsVector
{

public:

    TIStack( unsigned max = DEFAULT_STACK_SIZE ) :
        TIStackAsVector( max )
        {
        }

};

template  class TIStackIterator :
    public TIStackAsVectorIterator
{

public:


    TIStackIterator( const TIStack& a ) :
        TIStackAsVectorIterator(a)
        {
        }

};

#endif  // __CLASSLIB_STACKS_H


Generated by: nicola on gulliver.wadahome.it on Sun May 25 13:54:34 2003, using kdoc 2.0a53.