2010-12-04 16:14:34 +00:00
/********************************************************
* List . h *
* *
* XFX Generic List definition file *
* Copyright <EFBFBD> XFX Team . All Rights Reserved *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
# ifndef _SYSTEM_COLLECTIONS_GENERIC_LIST_
# define _SYSTEM_COLLECTIONS_GENERIC_LIST_
2011-03-07 19:14:57 +00:00
# include <System/Array.h>
2012-03-29 22:02:43 +00:00
# include <System/FrameworkResources.h>
2011-11-07 01:29:50 +00:00
# include <System/Object.h>
2012-03-29 22:02:43 +00:00
# include <System/String.h>
2010-12-04 16:14:34 +00:00
# include "Interfaces.h"
2011-05-02 17:33:24 +00:00
# include <stdlib.h>
# include <string.h>
2012-03-29 22:02:43 +00:00
# include <sassert.h>
2011-11-07 01:29:50 +00:00
2010-12-04 16:14:34 +00:00
namespace System
{
namespace Collections
{
namespace Generic
{
2011-11-07 01:29:50 +00:00
// Represents a strongly typed list of objects that can be accessed by index. Provides methods to search, sort, and manipulate lists.
2010-12-04 16:14:34 +00:00
template < class T >
2011-11-07 01:29:50 +00:00
class List : public IList < T > , virtual Object
2010-12-04 16:14:34 +00:00
{
private :
static const int _defaultCapacity = 4 ;
2011-03-07 19:14:57 +00:00
T * _items ;
2010-12-04 16:14:34 +00:00
int _size ;
2011-05-02 17:33:24 +00:00
int _actualSize ;
2010-12-04 16:14:34 +00:00
int _version ;
2011-11-07 01:29:50 +00:00
void EnsureCapacity ( int capacity )
{
if ( _actualSize < capacity )
{
int num = ( _actualSize = = 0 ) ? _defaultCapacity : _actualSize * 2 ;
if ( num > 0x7fefffff )
{
num = 0x7fefffff ;
}
if ( num < capacity )
{
num = capacity ;
}
2012-03-29 22:02:43 +00:00
setCapacity ( num ) ;
2011-11-07 01:29:50 +00:00
}
}
2010-12-04 16:14:34 +00:00
public :
2011-11-07 01:29:50 +00:00
// Gets the number of elements actually contained in the List<>.
2012-03-29 22:02:43 +00:00
int Count ( ) const
2011-11-07 01:29:50 +00:00
{
return _size ;
}
// Gets the total number of elements the internal data structure can hold without resizing.
2012-03-29 22:02:43 +00:00
int getCapacity ( ) const
2011-11-07 01:29:50 +00:00
{
return _actualSize ;
}
// Sets the total number of elements the internal data structure can hold without resizing.
2012-03-29 22:02:43 +00:00
void setCapacity ( const int value )
2011-11-07 01:29:50 +00:00
{
if ( value < _size )
return ;
if ( value ! = _actualSize )
{
if ( value > 0 )
{
T * destinationArray = new T [ value ] ;
if ( _size > 0 )
{
Array : : Copy ( _items , 0 , destinationArray , 0 , _size ) ;
}
delete [ ] _items ;
_items = destinationArray ;
}
else
{
delete [ ] _items ;
_items = new T [ 0 ] ;
}
_actualSize = value ;
}
}
2012-03-29 22:02:43 +00:00
bool IsReadOnly ( ) const
2011-11-07 01:29:50 +00:00
{
return false ;
}
// Initializes a new instance of the List<> class that is empty and has the default initial capacity.
List ( )
{
_actualSize = _defaultCapacity ;
_size = 0 ;
_items = new T [ _actualSize ] ;
}
// Initializes a new instance of the List<> class that is empty and has the specified initial capacity.
2012-03-29 22:02:43 +00:00
List ( const int capacity )
2011-11-07 01:29:50 +00:00
{
if ( capacity < 0 )
_actualSize = _defaultCapacity ;
_actualSize = capacity ;
_size = 0 ;
_items = new T [ _actualSize ] ;
}
2011-03-07 19:14:57 +00:00
2012-03-29 22:02:43 +00:00
// Copy constructor
List ( const List < T > & obj )
{
_actualSize = obj . _actualSize ;
_size = obj . _size ;
_items = new T [ obj . _actualSize ] ;
Array : : Copy ( obj . _items , 0 , _items , 0 , obj . _size ) ;
_version = obj . _version ;
}
2011-03-07 19:14:57 +00:00
2011-11-07 01:29:50 +00:00
~ List ( )
{
delete [ ] _items ;
}
// Adds an element to the end of the list
2012-03-29 22:02:43 +00:00
void Add ( const T & item )
2011-11-07 01:29:50 +00:00
{
2012-03-29 22:02:43 +00:00
if ( _size = = _actualSize )
2011-11-07 01:29:50 +00:00
{
EnsureCapacity ( _size + 1 ) ;
}
_items [ _size + + ] = item ;
_version + + ;
}
//Removes all elements from the list
void Clear ( )
{
if ( _size > 0 )
{
2012-03-29 22:02:43 +00:00
delete [ ] _items ;
2011-11-07 01:29:50 +00:00
_items = new T [ _actualSize ] ;
_size = 0 ;
}
_version + + ;
}
// Determines whether an element is in the List<>.
2012-03-29 22:02:43 +00:00
bool Contains ( const T & item ) const
2011-03-07 19:14:57 +00:00
{
for ( int i = 0 ; i < _size ; i + + )
{
2012-03-29 22:02:43 +00:00
if ( _items [ i ] = = item )
2011-03-07 19:14:57 +00:00
{
return true ;
2012-03-29 22:02:43 +00:00
}
2011-03-07 19:14:57 +00:00
}
return false ;
}
2011-11-07 01:29:50 +00:00
// Copies the entire List<> to a compatible one-dimensional array, starting at the specified index of the target array.
2012-03-29 22:02:43 +00:00
void CopyTo ( T array [ ] , const int arrayIndex ) const
2011-03-07 19:14:57 +00:00
{
Array : : Copy ( _items , 0 , array , arrayIndex , _size ) ;
}
2011-11-07 01:29:50 +00:00
// Searches for the specified object and returns the zero-based index of the first occurrence within the entire List<>.
2012-03-29 22:02:43 +00:00
int IndexOf ( const T & item ) const
2011-03-07 19:14:57 +00:00
{
return Array : : IndexOf ( _items , item , 0 , _size ) ;
}
2011-11-07 01:29:50 +00:00
// Inserts an element into the List<> at the specified index.
2012-03-29 22:02:43 +00:00
void Insert ( const int index , const T & item )
2011-03-07 19:14:57 +00:00
{
2012-03-29 22:02:43 +00:00
sassert ( index < _size , " Index must be within the bounds of the List. " ) ;
if ( _size = = _actualSize )
2011-03-07 19:14:57 +00:00
{
EnsureCapacity ( _size + 1 ) ;
}
if ( index < _size )
{
Array : : Copy ( _items , index , _items , index + 1 , _size - index ) ;
}
_items [ index ] = item ;
_size + + ;
_version + + ;
}
2011-11-07 01:29:50 +00:00
// Removes the first occurrence of a specific object from the List<>.
2012-03-29 22:02:43 +00:00
bool Remove ( const T & item )
2011-03-07 19:14:57 +00:00
{
int index = IndexOf ( item ) ;
if ( index > = 0 )
{
RemoveAt ( index ) ;
return true ;
}
return false ;
}
2011-11-07 01:29:50 +00:00
// Removes the element at the specified index of the List<>.
2012-03-29 22:02:43 +00:00
void RemoveAt ( const int index )
2011-11-07 01:29:50 +00:00
{
Array : : Copy ( _items , index + 1 , _items , index , _size - index ) ;
_size - - ;
_version + + ;
}
// Removes a range of elements from the List<>.
2012-03-29 22:02:43 +00:00
void RemoveRange ( const int index , const int count )
2011-03-07 19:14:57 +00:00
{
2012-03-29 22:02:43 +00:00
sassert ( index > = 0 , String : : Format ( " index; %s " , FrameworkResources : : ArgumentOutOfRange_NeedNonNegNum ) ) ;
sassert ( count > = 0 , String : : Format ( " count; %s " , FrameworkResources : : ArgumentOutOfRange_NeedNonNegNum ) ) ;
sassert ( ! ( ( _size - index ) < count ) , " Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection. " ) ;
2011-03-07 19:14:57 +00:00
if ( count > 0 )
{
_size - = count ;
if ( index < _size )
{
Array : : Copy ( _items , index + count , _items , index , _size - index ) ;
}
Array : : Clear ( _items , _size , count ) ;
_version + + ;
}
}
void Reverse ( )
{
2011-11-07 01:29:50 +00:00
Reverse ( 0 , _size ) ;
2011-03-07 19:14:57 +00:00
}
2012-03-29 22:02:43 +00:00
void Reverse ( const int index , const int count )
2011-03-07 19:14:57 +00:00
{
2012-03-29 22:02:43 +00:00
sassert ( index > = 0 , String : : Format ( " index; %s " , FrameworkResources : : ArgumentOutOfRange_NeedNonNegNum ) ) ;
sassert ( count > = 0 , String : : Format ( " count; %s " , FrameworkResources : : ArgumentOutOfRange_NeedNonNegNum ) ) ;
sassert ( ! ( ( _size - index ) < count ) , " Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection. " ) ;
2011-03-07 19:14:57 +00:00
Array : : Reverse ( _items , index , count ) ;
_version + + ;
}
2012-03-29 22:02:43 +00:00
T * ToArray ( ) const
2011-03-07 19:14:57 +00:00
{
2011-11-07 01:29:50 +00:00
T * destinationArray = new T [ _size ] ;
Array : : Copy ( _items , 0 , destinationArray , 0 , _size ) ;
return destinationArray ;
}
2012-03-29 22:02:43 +00:00
const char * ToString ( ) const
2011-11-07 01:29:50 +00:00
{
return " " ;
2011-03-07 19:14:57 +00:00
}
void TrimExcess ( )
{
2012-03-29 22:02:43 +00:00
int num = ( int ) ( _actualSize * 0.9 ) ;
2011-03-07 19:14:57 +00:00
if ( _size < num )
{
2011-05-02 17:33:24 +00:00
setCapacity ( _size ) ;
2011-03-07 19:14:57 +00:00
}
}
2012-03-29 22:02:43 +00:00
T & operator [ ] ( int index )
2011-11-07 01:29:50 +00:00
{
return _items [ index ] ;
}
2012-03-29 22:02:43 +00:00
const List < T > & operator = ( const List < T > other )
2011-11-07 01:29:50 +00:00
{
delete [ ] _items ;
_actualSize = other . _actualSize ;
_size = other . _size ;
_items = new T [ other . _actualSize ] ;
Array : : Copy ( other . _items , 0 , _items , 0 , other . _size ) ;
_version = other . _version ;
2012-03-29 22:02:43 +00:00
return * this ;
2011-11-07 01:29:50 +00:00
}
2011-03-07 19:14:57 +00:00
} ;
2011-05-02 17:33:24 +00:00
//////////////////////////////////////////////////////////////////////
2012-03-29 22:02:43 +00:00
/*template <class T>
class List < T * > : public IList < T * > , virtual Object
2011-03-07 19:14:57 +00:00
{
2011-11-07 01:29:50 +00:00
private :
static const int _defaultCapacity = 4 ;
2012-03-29 22:02:43 +00:00
static T * * _emptyArray ;
T * * _items ;
2011-11-07 01:29:50 +00:00
int _size ;
int _actualSize ;
int _version ;
void EnsureCapacity ( int capacity )
2011-03-07 19:14:57 +00:00
{
2011-11-07 01:29:50 +00:00
if ( _actualSize < capacity )
{
int num = ( _actualSize = = 0 ) ? _defaultCapacity : _actualSize * 2 ;
if ( num > 0x7fefffff )
{
num = 0x7fefffff ;
}
if ( num < capacity )
{
num = capacity ;
}
_actualSize = num ;
}
}
2011-03-07 19:14:57 +00:00
2011-11-07 01:29:50 +00:00
public :
// Gets the number of elements actually contained in the List<>.
int Count ( )
{
return _size ;
}
2011-03-07 19:14:57 +00:00
2011-11-07 01:29:50 +00:00
bool IsReadOnly ( )
{
return false ;
2011-03-07 19:14:57 +00:00
}
2011-11-07 01:29:50 +00:00
List ( )
{
_actualSize = _defaultCapacity ;
2011-03-07 19:14:57 +00:00
2011-11-07 01:29:50 +00:00
_size = 0 ;
2011-03-07 19:14:57 +00:00
2011-11-07 01:29:50 +00:00
_items = new T [ _actualSize ] ;
}
2011-03-07 19:14:57 +00:00
2011-11-07 01:29:50 +00:00
List ( int capacity )
{
if ( capacity < 0 )
_actualSize = _defaultCapacity ;
2011-03-07 19:14:57 +00:00
2011-11-07 01:29:50 +00:00
_actualSize = capacity ;
2011-03-07 19:14:57 +00:00
2011-11-07 01:29:50 +00:00
_size = 0 ;
2011-03-07 19:14:57 +00:00
2011-11-07 01:29:50 +00:00
_items = new T [ _actualSize ] ;
}
2011-03-07 19:14:57 +00:00
2012-03-29 22:02:43 +00:00
List ( const List < T * > & obj )
{
_size = obj . _size ;
_actualSize = obj . _actualSize ;
_items = new T [ _actualSize ] ;
Array : : Copy ( obj . _items , 0 , _items , 0 , _size ) ;
}
2011-11-07 01:29:50 +00:00
~ List ( )
2011-03-07 19:14:57 +00:00
{
2011-11-07 01:29:50 +00:00
delete [ ] _items ;
2011-03-07 19:14:57 +00:00
}
2012-03-29 22:02:43 +00:00
void Add ( T * item )
2011-11-07 01:29:50 +00:00
{
if ( _size = = Array : : Length ( _items ) )
{
EnsureCapacity ( _size + 1 ) ;
}
2012-03-29 22:02:43 +00:00
_items [ _size + + ] = item ;
2011-11-07 01:29:50 +00:00
_version + + ;
}
2011-03-07 19:14:57 +00:00
2012-03-29 22:02:43 +00:00
bool Contains ( T * item )
2011-11-07 01:29:50 +00:00
{
for ( int i = 0 ; i < _size ; i + + )
{
2012-03-29 22:02:43 +00:00
if ( _items [ i ] = = * item )
2011-11-07 01:29:50 +00:00
{
return true ;
2012-03-29 22:02:43 +00:00
}
2011-11-07 01:29:50 +00:00
}
return false ;
}
2011-03-07 19:14:57 +00:00
2011-11-07 01:29:50 +00:00
//Removes all elements from the list
void Clear ( )
{
if ( _size > 0 )
{
delete _items ;
_items = new T [ _actualSize ] ;
_size = 0 ;
}
_version + + ;
}
2011-03-07 19:14:57 +00:00
2011-11-07 01:29:50 +00:00
// Copies the entire List<> to a compatible one-dimensional array, starting at the beginning of the target array.
2012-03-29 22:02:43 +00:00
void CopyTo ( T * array [ ] )
2011-11-07 01:29:50 +00:00
{
2012-03-29 22:02:43 +00:00
Array : : Copy ( _items , 0 , array , 0 , _size ) ;
2011-11-07 01:29:50 +00:00
}
2011-03-07 19:14:57 +00:00
2011-11-07 01:29:50 +00:00
// Copies the entire List<> to a compatible one-dimensional array, starting at the specified index of the target array.
2012-03-29 22:02:43 +00:00
void CopyTo ( T * array [ ] , int arrayIndex )
2011-11-07 01:29:50 +00:00
{
2012-03-29 22:02:43 +00:00
Array : : Copy ( _items , 0 , array , arrayIndex , _size ) ;
2011-11-07 01:29:50 +00:00
}
2011-03-07 19:14:57 +00:00
2011-11-07 01:29:50 +00:00
// Searches for the specified object and returns the zero-based index of the first occurrence within the entire List<>.
2012-03-29 22:02:43 +00:00
int IndexOf ( T * item )
2011-11-07 01:29:50 +00:00
{
return Array : : IndexOf ( _items , * item , 0 , _size ) ;
}
// Inserts an element into the List<> at the specified index.
2012-03-29 22:02:43 +00:00
void Insert ( int index , T * item )
2011-03-07 19:14:57 +00:00
{
2011-11-07 01:29:50 +00:00
if ( index > _size )
{
2012-03-29 22:02:43 +00:00
# if DEBUG
printf ( " ARGUMENT_OUT_OF_RANGE in function %s, at line %i in file %s, argument \" %s \" : %s \n " , __FUNCTION__ , __LINE__ , __FILE__ , " index " , " Index must be within the bounds of the List. " ) ;
# endif
2011-11-07 01:29:50 +00:00
return ;
}
if ( _size = = Array : : Length ( _items ) )
2011-03-07 19:14:57 +00:00
{
2011-11-07 01:29:50 +00:00
EnsureCapacity ( _size + 1 ) ;
2011-03-07 19:14:57 +00:00
}
2011-11-07 01:29:50 +00:00
if ( index < _size )
{
Array : : Copy ( _items , index , _items , index + 1 , _size - index ) ;
}
2012-03-29 22:02:43 +00:00
_items [ index ] = item ;
2011-11-07 01:29:50 +00:00
_size + + ;
_version + + ;
2011-03-07 19:14:57 +00:00
}
2011-11-07 01:29:50 +00:00
// Removes the first occurrence of a specific object from the List<>.
2012-03-29 22:02:43 +00:00
bool Remove ( T * item )
2011-11-07 01:29:50 +00:00
{
int index = IndexOf ( item ) ;
if ( index > = 0 )
{
RemoveAt ( index ) ;
return true ;
}
return false ;
}
// Removes the element at the specified index of the List<>.
void RemoveAt ( int index )
{
Array : : Copy ( _items , index + 1 , _items , index , _size - index ) ;
2011-05-02 17:33:24 +00:00
_size - - ;
2011-11-07 01:29:50 +00:00
_version + + ;
2011-03-07 19:14:57 +00:00
}
2012-03-29 22:02:43 +00:00
T * * ToArray ( )
2011-11-07 01:29:50 +00:00
{
2012-03-29 22:02:43 +00:00
T * * destinationArray = new T [ _size ] ;
2011-11-07 01:29:50 +00:00
Array : : Copy ( _items , 0 , destinationArray , 0 , _size ) ;
return destinationArray ;
}
2011-03-07 19:14:57 +00:00
2011-11-07 01:29:50 +00:00
char * ToString ( )
{
return " " ;
}
2011-03-07 19:14:57 +00:00
2011-11-07 01:29:50 +00:00
T * operator [ ] ( int index )
{
2012-03-29 22:02:43 +00:00
return _items [ index ] ;
2011-11-07 01:29:50 +00:00
}
2012-03-29 22:02:43 +00:00
} ; */
2010-12-04 16:14:34 +00:00
}
}
}
# endif //_SYSTEM_COLLECTIONS_GENERIC_LIST_