blob: bad147fde05b364a5f0046b5302e197526f7c3b5 [file] [log] [blame] [edit]
//===========================================================================//
// Purpose : Template version for a TCT_Stack stack (FILO) class.
//
// Inline methods include:
// - GetLength
// - Clear
// - IsValid
//
// Public methods include:
// - TCT_Stack_c, ~TCT_Stack_c
// - operator=
// - operator==, operator!=
// - operator[]
// - Print
// - Push, Pop, Peek
//
//===========================================================================//
//---------------------------------------------------------------------------//
// Copyright (C) 2012-2013 Jeff Rudolph, Texas Instruments (jrudolph@ti.com) //
// //
// Permission is hereby granted, free of charge, to any person obtaining a //
// copy of this software and associated documentation files (the "Software"),//
// to deal in the Software without restriction, including without limitation //
// the rights to use, copy, modify, merge, publish, distribute, sublicense, //
// and/or sell copies of the Software, and to permit persons to whom the //
// Software is furnished to do so, subject to the following conditions: //
// //
// The above copyright notice and this permission notice shall be included //
// in all copies or substantial portions of the Software. //
// //
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS //
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF //
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN //
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, //
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR //
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE //
// USE OR OTHER DEALINGS IN THE SOFTWARE. //
//---------------------------------------------------------------------------//
#ifndef TCT_STACK_H
#define TCT_STACK_H
#include <cstdio>
#include <deque>
using namespace std;
//===========================================================================//
// Class declaration
//---------------------------------------------------------------------------//
// Author : Jeff Rudolph
//---------------------------------------------------------------------------//
// Version history
// 05/30/12 jeffr : Original
//===========================================================================//
template< class T > class TCT_Stack_c
{
public:
TCT_Stack_c( void );
TCT_Stack_c( const TCT_Stack_c< T >& stack );
~TCT_Stack_c( void );
TCT_Stack_c& operator=( const TCT_Stack_c& stack );
bool operator==( const TCT_Stack_c< T >& stack ) const;
bool operator!=( const TCT_Stack_c< T >& stack ) const;
T* operator[]( size_t index );
T* operator[]( size_t index ) const;
void Print( FILE* pfile = stdout, size_t spaceLen = 0 ) const;
size_t GetLength( void ) const;
void Push( const T& data );
bool Pop( T* pdata );
T* Peek( void );
T* Peek( size_t count );
void Clear( void );
bool IsValid( void ) const;
private:
std::deque< T > stack_;
};
//===========================================================================//
// Class inline definition(s)
//---------------------------------------------------------------------------//
// Author : Jeff Rudolph
//---------------------------------------------------------------------------//
// Version history
// 05/30/12 jeffr : Original
//===========================================================================//
template< class T > inline size_t TCT_Stack_c< T >::GetLength(
void ) const
{
return( this->stack_.size( ));
}
//===========================================================================//
template< class T > inline void TCT_Stack_c< T >::Clear(
void )
{
this->stack_.clear( );
}
//===========================================================================//
template< class T > inline bool TCT_Stack_c< T >::IsValid(
void ) const
{
return( this->GetLength( ) > 0 ? true : false );
}
//===========================================================================//
// Method : TCT_Stack_c
// Author : Jeff Rudolph
//---------------------------------------------------------------------------//
// Version history
// 05/30/12 jeffr : Original
//===========================================================================//
template< class T > TCT_Stack_c< T >::TCT_Stack_c(
void )
{
}
//===========================================================================//
template< class T > TCT_Stack_c< T >::TCT_Stack_c(
const TCT_Stack_c< T >& stack )
:
stack_( stack.stack_ )
{
}
//===========================================================================//
// Method : ~TCT_Stack_c
// Author : Jeff Rudolph
//---------------------------------------------------------------------------//
// Version history
// 05/30/12 jeffr : Original
//===========================================================================//
template< class T > TCT_Stack_c< T >::~TCT_Stack_c(
void )
{
this->stack_.clear( );
}
//===========================================================================//
// Method : operator=
// Author : Jeff Rudolph
//---------------------------------------------------------------------------//
// Version history
// 05/30/12 jeffr : Original
//===========================================================================//
template< class T > inline TCT_Stack_c< T >& TCT_Stack_c< T >::operator=(
const TCT_Stack_c& stack )
{
this->stack_.operator=( stack.stack_ );
return( *this );
}
//===========================================================================//
// Method : operator==
// Author : Jeff Rudolph
//---------------------------------------------------------------------------//
// Version history
// 05/30/12 jeffr : Original
//===========================================================================//
template< class T > bool TCT_Stack_c< T >::operator==(
const TCT_Stack_c< T >& stack ) const
{
bool isEqual = false;
if(( this->GetLength( ) > 0 ) &&
( stack.GetLength( ) > 0 ) &&
( this->GetLength( ) == stack.GetLength( )))
{
for( size_t i = 0; i < this->GetLength( ); ++i )
{
const T& thisData = *this->operator[]( i );
const T& stackData = *stack.operator[]( i );
if( thisData == stackData )
{
isEqual = true;
continue;
}
else
{
isEqual = false;
break;
}
}
}
return( isEqual );
}
//===========================================================================//
// Method : operator!=
// Author : Jeff Rudolph
//---------------------------------------------------------------------------//
// Version history
// 05/30/12 jeffr : Original
//===========================================================================//
template< class T > bool TCT_Stack_c< T >::operator!=(
const TCT_Stack_c< T >& stack ) const
{
return( !this->operator==( stack ) ? true : false );
}
//===========================================================================//
// Method : operator[]
// Author : Jeff Rudolph
//---------------------------------------------------------------------------//
// Version history
// 05/30/12 jeffr : Original
//===========================================================================//
template< class T > T* TCT_Stack_c< T >::operator[](
size_t index )
{
T* pdata = 0;
if( index < this->GetLength( ))
{
pdata = &this->stack_.operator[]( index );
}
return( pdata );
}
//===========================================================================//
template< class T > T* TCT_Stack_c< T >::operator[](
size_t index ) const
{
T* pdata = 0;
if( index < this->GetLength( ))
{
pdata = const_cast< TCT_Stack_c< T >* >( this )->operator[]( index );
}
return( pdata );
}
//===========================================================================//
// Method : Print
// Author : Jeff Rudolph
//---------------------------------------------------------------------------//
// Version history
// 05/30/12 jeffr : Original
//===========================================================================//
template< class T > void TCT_Stack_c< T >::Print(
FILE* pfile,
size_t spaceLen ) const
{
for( size_t i = 0; i < this->GetLength( ); ++i )
{
const T& data = *this->operator[]( i );
if( data.IsValid( ))
{
data.Print( pfile, spaceLen );
}
}
}
//===========================================================================//
// Method : Push
// Author : Jeff Rudolph
//---------------------------------------------------------------------------//
// Version history
// 05/30/12 jeffr : Original
//===========================================================================//
template< class T > void TCT_Stack_c< T >::Push(
const T& data )
{
this->stack_.push_back( data );
}
//===========================================================================//
// Method : Pop
// Author : Jeff Rudolph
//---------------------------------------------------------------------------//
// Version history
// 05/30/12 jeffr : Original
//===========================================================================//
template< class T > bool TCT_Stack_c< T >::Pop(
T* pdata )
{
size_t len = this->GetLength( );
if( pdata && len )
{
*pdata = this->stack_.back( );
this->stack_.pop_back( );
}
return( len ? true : false );
}
//===========================================================================//
// Method : Peek
// Author : Jeff Rudolph
//---------------------------------------------------------------------------//
// Version history
// 05/30/12 jeffr : Original
//===========================================================================//
template< class T > T* TCT_Stack_c< T >::Peek(
void )
{
T* pdata = 0;
size_t len = this->GetLength( );
if( len )
{
*pdata = this->stack_.back( );
}
return( pdata );
}
//===========================================================================//
template< class T > T* TCT_Stack_c< T >::Peek(
size_t count )
{
T* pdata = 0;
size_t len = this->GetLength( );
if( len && count < len )
{
pdata = this->operator[]( len - count - 1 );
}
return( pdata );
}
#endif