2011-05-02 17:33:24 +00:00
# ifndef _SYSTEM_COLLECTIONS_GENERIC_STACK_
# define _SYSTEM_COLLECTIONS_GENERIC_STACK_
2012-03-29 22:02:43 +00:00
# include <System/Array.h>
2011-05-02 17:33:24 +00:00
# include <System/Object.h>
2012-09-28 20:36:02 +00:00
# include <System/String.h>
2012-03-29 22:02:43 +00:00
# include <System/Collections/Generic/EqualityComparer.h>
2011-05-02 17:33:24 +00:00
2012-03-29 22:02:43 +00:00
# include <sassert.h>
2011-11-07 01:29:50 +00:00
2011-05-02 17:33:24 +00:00
namespace System
{
namespace Collections
{
namespace Generic
{
2012-09-28 20:36:02 +00:00
// Represents a variable size last-in-first-out (LIFO) collection of instances of the same arbitrary type.
2011-05-02 17:33:24 +00:00
template < class T >
2012-09-28 20:36:02 +00:00
class Stack : public ICollection < T > , public Object
2011-05-02 17:33:24 +00:00
{
private :
2012-03-29 22:02:43 +00:00
T * _array ;
static const int _defaultCapacity = 4 ;
int _actualSize ;
2011-05-02 17:33:24 +00:00
int _size ;
int _version ;
2012-03-29 22:02:43 +00:00
void EnsureCapacity ( int capacity )
{
2012-09-28 20:36:02 +00:00
if ( _actualSize < capacity )
{
int num = ( _actualSize = = 0 ) ? _defaultCapacity : _actualSize * 2 ;
if ( num > 0x7fefffff )
{
num = 0x7fefffff ;
}
if ( num < capacity )
{
num = capacity ;
}
// TODO: implement
//setCapacity(num);
}
2012-03-29 22:02:43 +00:00
}
2011-05-02 17:33:24 +00:00
public :
int Count ( )
{
return _size ;
}
bool IsSynchronized ( )
{
return false ;
}
Stack ( )
{
2012-09-28 20:36:02 +00:00
_array = new T [ _defaultCapacity ] ;
2012-03-29 22:02:43 +00:00
_actualSize = _defaultCapacity ;
2011-05-02 17:33:24 +00:00
_size = 0 ;
_version = 0 ;
}
Stack ( ICollection < T > * col )
{
2012-03-29 22:02:43 +00:00
sassert ( col ! = null , " " ) ;
2011-05-02 17:33:24 +00:00
2012-03-29 22:02:43 +00:00
_array = new T [ col - > Count ( ) ] ;
_actualSize = col - > Count ( ) ;
2011-05-02 17:33:24 +00:00
2012-09-28 20:36:02 +00:00
col - > CopyTo ( _array , 0 ) ;
2011-05-02 17:33:24 +00:00
}
Stack ( int initialCapacity )
{
2012-03-29 22:02:43 +00:00
sassert ( initialCapacity > = 0 , " " ) ;
2011-05-02 17:33:24 +00:00
if ( initialCapacity < _defaultCapacity )
initialCapacity = _defaultCapacity ;
_bottom = new T [ initialCapacity ] ;
_size = 0 ;
_version = 0 ;
}
virtual ~ Stack ( )
{
2012-03-29 22:02:43 +00:00
delete [ ] _array ;
2011-05-02 17:33:24 +00:00
}
2012-09-28 20:36:02 +00:00
// Removes all objects from the System::Collections::Generic::Stack<T>.
2011-05-02 17:33:24 +00:00
virtual void Clear ( )
{
2012-03-29 22:02:43 +00:00
Array : : Clear ( _array , 0 , _size ) ;
2011-05-02 17:33:24 +00:00
_size = 0 ;
_version + + ;
}
2012-09-28 20:36:02 +00:00
// Determines whether an element is in the System::Collections::Generic::Stack<T>.
// item
// The object to locate in the System::Collections::Generic::Stack<T>. The value can be null for reference types.
// Returns
// true if item is found in the System::Collections::Generic::Stack<T>; otherwise, false.
virtual bool Contains ( const T & item )
2012-03-29 22:02:43 +00:00
{
int index = _size ;
EqualityComparer < T > comparer = EqualityComparer < T > : : Default ( ) ;
while ( index - - > 0 )
{
if ( comparer . Equals ( _array [ index ] , item ) )
return true ;
}
return false ;
}
2012-09-28 20:36:02 +00:00
virtual void CopyTo ( T array [ ] , const int index )
2012-03-29 22:02:43 +00:00
{
2012-09-28 20:36:02 +00:00
sassert ( array ! = null , String : : Format ( " array; %s " , FrameworkResources : : ArgumentNull_Generic ) ) ;
2012-03-29 22:02:43 +00:00
Array : : Copy ( _array , 0 , array , index , _size ) ;
Array : : Reverse ( _array , index , _size ) ;
}
2011-05-02 17:33:24 +00:00
2012-09-28 20:36:02 +00:00
// Returns the object at the top of the System::Collections::Generic::Stack<T> without removing it.
// Returns
// The object at the top of the System::Collections::Generic::Stack<T>.
2012-03-29 22:02:43 +00:00
virtual T & Peek ( ) ;
2011-05-02 17:33:24 +00:00
{
2012-03-29 22:02:43 +00:00
sassert ( size ! = 0 , " " ) ;
return _array [ _size - 1 ] ;
2011-05-02 17:33:24 +00:00
}
2012-09-28 20:36:02 +00:00
// Removes and returns the object at the top of the System::Collections::Generic::Stack<T>.
// Returns
// The object removed from the top of the System::Collections::Generic::Stack<T>.
2012-03-29 22:02:43 +00:00
virtual T & Pop ( )
2011-05-02 17:33:24 +00:00
{
2012-03-29 22:02:43 +00:00
sassert ( size ! = 0 , " " ) ;
2011-05-02 17:33:24 +00:00
_version + + ;
2012-03-29 22:02:43 +00:00
T local = _array [ - - _size ] ;
_array [ _size ] = NULL ;
return local ;
2011-05-02 17:33:24 +00:00
}
2012-09-28 20:36:02 +00:00
// Inserts an object at the top of the System::Collections::Generic::Stack<T>.
// item
// The object to push onto the System::Collections::Generic::Stack<T>. The value can be null for reference types.
2012-03-29 22:02:43 +00:00
virtual void Push ( T & obj )
2011-05-02 17:33:24 +00:00
{
2012-09-28 20:36:02 +00:00
_array [ _size ] = obj ;
_size + + ;
2011-05-02 17:33:24 +00:00
_version + + ;
}
2012-09-28 20:36:02 +00:00
// Copies the System::Collections::Generic::Stack<T> to a new array.
// Returns
// A new array containing copies of the elements of the System::Collections::Generic::Stack<T>.
2011-05-02 17:33:24 +00:00
virtual T * ToArray ( )
{
2012-03-29 22:02:43 +00:00
T [ ] localArray = new T [ _size ] ;
for ( int i = 0 ; i < _size ; i + + )
{
localArray [ i ] = _array [ ( _size - i ) - 1 ] ;
}
return localArray ;
2011-05-02 17:33:24 +00:00
}
2012-09-28 20:36:02 +00:00
// Sets the capacity to the actual number of elements in the System.Collections.Generic.Stack<T1>, if that number is less than 90 percent of current capacity.
virtual void TrimExcess ( )
{
}
2011-05-02 17:33:24 +00:00
} ;
}
}
}
# endif //_SYSTEM_COLLECTIONS_GENERIC_STACK_