Commit 1b7361a9 authored by Jakub Klinkovský's avatar Jakub Klinkovský

Removed Containers::List because it has no benefits over std::list

parent 3ddc54a6
#include <iostream>
#include <TNL/String.h>
#include <TNL/Containers/List.h>
#include <TNL/File.h>
using namespace TNL;
......
......@@ -24,7 +24,6 @@ void expressions()
b.evaluate( [] __cuda_callable__ ( int i )->RealType { return i - 5.0; } );
c = -5;
int arg;
std::cout << "a = " << a << std::endl;
std::cout << "b = " << b << std::endl;
std::cout << "c = " << c << std::endl;
......
......@@ -12,6 +12,5 @@ add_subdirectory( flow-vl )
ADD_EXECUTABLE( ConfigDescriptionExample ConfigDescriptionExample.cpp )
ADD_EXECUTABLE( ListExample ListExample.cpp )
ADD_EXECUTABLE( LoggerExample LoggerExample.cpp )
ADD_EXECUTABLE( MathExample MathExample.cpp )
#include <iostream>
#include <TNL/Config/ConfigDescription.h>
#include <TNL/Containers/List.h>
#include <TNL/Containers/Array.h>
using namespace TNL;
using namespace std;
int main()
{
Containers::List< int > lst;
lst.isEmpty();
lst.Append(1);
lst.Append(3);
lst.isEmpty();
lst.getSize();
lst.Insert(2,1);
Containers::Array<int> array;
lst.toArray(array);
}
\ No newline at end of file
/***************************************************************************
List.h - description
-------------------
begin : Sat, 10 Apr 2004 15:58:51 +0100
copyright : (C) 2004 by Tomas Oberhuber
email : tomas.oberhuber@fjfi.cvut.cz
***************************************************************************/
/* See Copyright Notice in tnl/Copyright */
#pragma once
#include <iostream>
#include <TNL/Assert.h>
#include <TNL/File.h>
#include <TNL/String.h>
#include <TNL/TypeInfo.h>
namespace TNL {
namespace Containers {
template< class T > class ListDataElement;
/// \brief Template for double linked lists
/*! To acces elements in the list one can use method getSize() and
operator[](). To add elements there are methods Append(),
Prepend() and Insert() to insert an element at given
position. To erase particular element there is method
Erase() taking the element position. To erase all elements
there is method reset(). There are also alternatives DeepErase()
and DeepEraseAll() to free dynamicaly allocated data inside the
data elements.
The list stores pointer to last accesed element so if one goes
seqeuntialy through the list there is no inefficiency. The
accesing algorithm is also able to deside whether to start from
the last accesed position or from the begining resp. from the end
of the list. So with common use one does not need to worry about
efficiency :-)
*/
template< class T > class List
{
public:
typedef T ValueType;
/// \brief Basic constructor.
///
/// Constructs an empty list.
List();
/// \brief Copy constructor.
///
/// Construct a copy of \e list.
/// \param list Name of another list.
List( const List& list );
/// \brief Destructor.
///
/// Destroys the list. References to the values in the list become invalid.
~List();
/// Returns \e true if the list contains no items, otherwise returns \e false.
bool isEmpty() const;
/// Returns number of items in the list.
int getSize() const;
/// Indexing operator.
T& operator[] ( const int& ind );
/// Indexing operator for constant instances.
const T& operator[] ( const int& ind ) const;
const List& operator = ( const List& lst );
bool operator == ( const List& lst ) const;
bool operator != ( const List& lst ) const;
/// \brief Appends new data element.
///
/// Inserts \e data at the end of the list.
bool Append( const T& data );
/// \brief Prepends new data element.
///
/// Inserts \e data at the beginning of the list.
bool Prepend( const T& data );
/// \brief Inserts new data element at given position.
///
/// Inserts \e data at index position \e ind in the list.
bool Insert( const T& data, const int& ind );
/// Appends copy of another list.
///
/// \param lst Name of another list.
bool AppendList( const List< T >& lst );
/// Prepends copy of another list.
///
/// \param lst Name of another list.
bool PrependList( const List< T >& lst );
/// Transforms list to an \e array.
template< typename Array >
void toArray( Array& array );
/***
* \brief Checks if there is an element with value \e v in given array.
*
* \param v Reference to a value.
*/
bool containsValue( const T& v ) const;
/// Erases data element at given position.
///
/// \param ind Index of the data element one chooses to remove.
void Erase( const int& ind );
/// Erases data element with contained data at given position.
///
/// \param ind Index of the data element one chooses to remove.
void DeepErase( const int& ind );
/// Erases all data elements.
void reset();
/// \brief Erases all data elements with contained data.
///
/// Frees dynamicaly allocated data inside the data elements
void DeepEraseAll();
/// Saves the list in binary format.
///
/// \param file Name of file.
bool Save( File& file ) const;
/// Saves the list in binary format using method save of type T.
///
/// \param file Name of file.
bool DeepSave( File& file ) const;
/// Loads the list from file.
///
/// \param file Name of file.
bool Load( File& file );
/// Loads the list from file using method Load of the type T.
///
/// \param file Name of file.
bool DeepLoad( File& file );
protected:
/// Pointer to the first element.
ListDataElement< T >* first;
/// Pointer to the last element.
/*! We use pointer to last element while adding new element to keep order of elements
*/
ListDataElement< T >* last;
/// List size.
int size;
/// Iterator.
mutable ListDataElement< T >* iterator;
/// Iterator index.
mutable int index;
};
template< typename T > std::ostream& operator << ( std::ostream& str, const List< T >& list );
//! Data element for List and mStack
template< class T > class ListDataElement
{
//! Main data
T data;
//! Pointer to the next element
ListDataElement< T >* next;
//! Pointer to the previous element
ListDataElement< T >* previous;
public:
//! Basic constructor
ListDataElement()
: next( 0 ),
previous( 0 ){};
//! Constructor with given data and possibly pointer to next element
ListDataElement( const T& dt,
ListDataElement< T >* prv = 0,
ListDataElement< T >* nxt = 0 )
: data( dt ),
next( nxt ),
previous( prv ){};
//! Destructor
~ListDataElement(){};
//! Return data for non-const instances
T& Data() { return data; };
//! Return data for const instances
const T& Data() const { return data; };
//! Return pointer to the next element for non-const instances
ListDataElement< T >*& Next() { return next; };
//! Return pointer to the next element for const instances
const ListDataElement< T >* Next() const { return next; };
//! Return pointer to the previous element for non-const instances
ListDataElement< T >*& Previous() { return previous; };
//! Return pointer to the previous element for const instances
const ListDataElement< T >* Previous() const { return previous; };
};
} // namespace Containers
} // namespace TNL
#include <TNL/Containers/List_impl.h>
/***************************************************************************
List_impl.h - description
-------------------
begin : Mar, 5 Apr 2016 12:46 PM
copyright : (C) 2016 by Tomas Oberhuber
email : tomas.oberhuber@fjfi.cvut.cz
***************************************************************************/
/* See Copyright Notice in tnl/Copyright */
#pragma once
#include <TNL/Containers/List.h>
#include <TNL/Math.h>
namespace TNL {
namespace Containers {
template< typename T >
List< T >::List()
: first( 0 ), last( 0 ), size( 0 ), iterator( 0 ), index( 0 )
{
}
template< typename T >
List< T >::List( const List& list )
: first( 0 ), last( 0 ), size( 0 ), iterator( 0 ), index( 0 )
{
AppendList( list );
}
template< typename T >
List< T >::~List()
{
reset();
}
template< typename T >
bool List< T >::isEmpty() const
{
return ! size;
}
template< typename T >
int List< T >::getSize() const
{
return size;
}
template< typename T >
T& List< T >::operator[]( const int& ind )
{
TNL_ASSERT( ind < size, );
int iter_dist = TNL::abs( index - ind );
if( ! iterator ||
iter_dist > ind ||
iter_dist > size - ind )
{
if( ind < size - ind )
{
//cout << "Setting curent index to 0." << std::endl;
index = 0;
iterator = first;
}
else
{
//cout << "Setting curent index to size - 1." << std::endl;
index = size - 1;
iterator = last;
}
}
while( index != ind )
{
//cout << " current index = " << index
// << " index = " << ind << std::endl;
if( ind < index )
{
iterator = iterator -> Previous();
index --;
}
else
{
iterator = iterator -> Next();
index ++;
}
TNL_ASSERT( iterator, );
}
return iterator -> Data();
};
template< typename T >
const T& List< T >::operator[]( const int& ind ) const
{
return const_cast< List< T >* >( this ) -> operator[]( ind );
}
template< typename T >
const List< T >& List< T >::operator = ( const List& lst )
{
AppendList( lst );
return( *this );
}
template< typename T >
bool List< T >::operator == ( const List& lst ) const
{
if( this->getSize() != lst.getSize() )
return false;
for( int i = 0; i < this->getSize(); i++ )
if( (*this)[ i ] != lst[ i ] )
return false;
return true;
}
template< typename T >
bool List< T >::operator != ( const List& lst ) const
{
return ! operator==( lst );
}
template< typename T >
bool List< T >::Append( const T& data )
{
if( ! first )
{
TNL_ASSERT( ! last, );
first = last = new ListDataElement< T >( data );
}
else
{
ListDataElement< T >* new_element = new ListDataElement< T >( data, last, 0 );
TNL_ASSERT( last, );
last = last -> Next() = new_element;
}
size ++;
return true;
};
template< typename T >
bool List< T >::Prepend( const T& data )
{
if( ! first )
{
TNL_ASSERT( ! last, );
first = last = new ListDataElement< T >( data );
}
else
{
ListDataElement< T >* new_element = new ListDataElement< T >( data, 0, first );
first = first -> Previous() = new_element;
}
size ++;
index ++;
return true;
};
template< typename T >
bool List< T >::Insert( const T& data, const int& ind )
{
TNL_ASSERT( ind <= size || ! size, );
if( ind == 0 ) return Prepend( data );
if( ind == size ) return Append( data );
operator[]( ind );
ListDataElement< T >* new_el =
new ListDataElement< T >( data,
iterator -> Previous(),
iterator );
iterator -> Previous() -> Next() = new_el;
iterator -> Previous() = new_el;
iterator = new_el;
size ++;
return true;
};
template< typename T >
bool List< T >::AppendList( const List< T >& lst )
{
int i;
for( i = 0; i < lst. getSize(); i ++ )
{
if( ! Append( lst[ i ] ) ) return false;
}
return true;
};
template< typename T >
bool List< T >::PrependList( const List< T >& lst )
{
int i;
for( i = lst. getSize(); i > 0; i -- )
if( ! Prepend( lst[ i - 1 ] ) ) return false;
return true;
};
template< typename T >
template< typename Array >
void List< T >::toArray( Array& array )
{
array.setSize( this->getSize() );
for( int i = 0; i < this->getSize(); i++ )
array[ i ] = ( *this )[ i ];
}
template< typename T >
bool List< T >::containsValue( const T& v ) const
{
for( int i = 0; i < this->getSize(); i++ )
if( ( *this )[ i ] == v )
return true;
return false;
}
template< typename T >
void List< T >::Erase( const int& ind )
{
operator[]( ind );
ListDataElement< T >* tmp_it = iterator;
if( iterator -> Next() )
iterator -> Next() -> Previous() = iterator -> Previous();
if( iterator -> Previous() )
iterator -> Previous() -> Next() = iterator -> Next();
if( iterator -> Next() ) iterator = iterator -> Next();
else
{
iterator = iterator -> Previous();
index --;
}
if( first == tmp_it ) first = iterator;
if( last == tmp_it ) last = iterator;
delete tmp_it;
size --;
};
template< typename T >
void List< T >::DeepErase( const int& ind )
{
operator[]( ind );
delete iterator -> Data();
Erase( ind );
};
template< typename T >
void List< T >::reset()
{
iterator = first;
ListDataElement< T >* tmp_it;
while( iterator )
{
TNL_ASSERT( iterator, );
tmp_it = iterator;
iterator = iterator -> Next();
delete tmp_it;
}
first = last = 0;
size = 0;
};
template< typename T >
void List< T >::DeepEraseAll()
{
iterator = first;
ListDataElement< T >* tmp_it;
int i( 0 );
while( iterator )
{
tmp_it = iterator;
iterator = iterator -> Next();
delete tmp_it -> Data();
delete tmp_it;
i++;
}
first = last = 0;
size = 0;
};
template< typename T >
bool List< T >::Save( File& file ) const
{
file.save( &size );
for( int i = 0; i < size; i ++ )
if( ! file. save( &operator[]( i ), 1 ) )
return false;
return true;
}
template< typename T >
bool List< T >::DeepSave( File& file ) const
{
file.save( &size );
for( int i = 0; i < size; i ++ )
if( ! operator[]( i ). save( file ) ) return false;
return true;
}
template< typename T >
bool List< T >::Load( File& file )
{
reset();
int _size;
file.load( &_size, 1 );
if( _size < 0 )
{
std::cerr << "The curve size is negative." << std::endl;
return false;
}
T t;
for( int i = 0; i < _size; i ++ )
{
if( ! file.load( &t, 1 ) )
return false;
Append( t );
}
return true;
};
template< typename T >
bool List< T >::DeepLoad( File& file )
{
reset();
int _size;
file.load( &_size );
if( _size < 0 )
{
std::cerr << "The list size is negative." << std::endl;
return false;
}
for( int i = 0; i < _size; i ++ )
{
T t;
if( ! t. load( file ) ) return false;
Append( t );
}
return true;
};
template< typename T >
std::ostream& operator << ( std::ostream& str, const List< T >& list )
{
int i, size( list. getSize() );
for( i = 0; i < size; i ++ )
str << "Item " << i << ":" << list[ i ] << std::endl;
return str;
};
} // namespace Containers
} // namespace TNL
......@@ -14,8 +14,9 @@
#pragma once
#include <list>
#include <TNL/Containers/Array.h>
#include <TNL/Containers/List.h>
#include <TNL/String.h>
#include <TNL/TypeInfo.h>
#include <TNL/Images//Image.h>
......@@ -102,7 +103,7 @@ class DicomSeries : public Image< int >
bool loadImage( const String& filePath, int number );
Containers::List< String > fileList;
std::list< String > fileList;
Containers::Array<DicomHeader *,Devices::Host,int> dicomSeriesHeaders;
......
......@@ -155,22 +155,22 @@ inline bool DicomSeries::retrieveFileList( const String& filePath)
String fileNamePrefix(fileName.getString(), 0, fileName.getLength() - separatorPosition);
struct dirent **dirp;
Containers::List<String > files;
std::list< String > files;
//scan and sort directory
int ndirs = scandir(directoryPath.getString(), &dirp, filter, alphasort);
for(int i = 0 ; i < ndirs; ++i)
{
files.Append( String((char *)dirp[i]->d_name));
files.push_back( String((char *)dirp[i]->d_name) );
delete dirp[i];
}
for (int i = 0; i < files.getSize(); i++)
for (auto& file : files)
{
//check if file prefix contained
if (strstr(files[ i ].getString(), fileNamePrefix.getString()))
if (strstr(file.getString(), fileNamePrefix.getString()))
{
fileList.Append( directoryPath + files[ i ] );
fileList.push_back( directoryPath + file );
}
}
}
......@@ -182,7 +182,7 @@ inline bool DicomSeries::loadImage( const String& filePath, int number)
#ifdef HAVE_DCMTK_H
//load header
DicomHeader *header = new DicomHeader();
dicomSeriesHeaders.setSize( fileList.getSize() );
dicomSeriesHeaders.setSize( fileList.size() );
dicomSeriesHeaders.setElement( number, header );
if( !header->loadFromFile( filePath ) )
return false;
......@@ -283,7 +283,7 @@ inline bool DicomSeries::loadImage( const String& filePath, int number)
imagesInfo.frameSize = size;
if (pixelData)
delete pixelData;
pixelData = new Uint16[imagesInfo.frameUintsCount * fileList.getSize()];
pixelData = new Uint16[imagesInfo.frameUintsCount * fileList.size()];
}
else
{//check image size for compatibility
......@@ -328,13 +328,14 @@ inline bool DicomSeries::loadDicomSeries( const String& filePath )
}
//load images
int imagesCountToLoad = fileList.getSize();
for( int i=0; i < imagesCountToLoad; i++ )
int counter = 0;
for( auto& file : fileList )
{
if( !loadImage( fileList[ i ].getString(),i ) )
if( !loadImage( file.getString(), counter ) )
{
std::cerr << fileList[ i ] << " skipped";
std::cerr << file << " skipped";
}
counter++;
}
return true;
}
......
......@@ -37,7 +37,7 @@ bool processDicomFiles( const Config::ParameterContainer& parameters )
bool processDicomSeries( const Config::ParameterContainer& parameters )
{
const Containers::List< String >& dicomSeriesNames = parameters.getParameter< Containers::List< String > >( "dicom-series" );
const std::vector< String >& dicomSeriesNames = parameters.getParameter< std::vector< String > >( "dicom-series" );
String meshFile = parameters.getParameter< String >( "mesh-file" );
bool verbose = parameters.getParameter< bool >( "verbose" );
......@@ -45,7 +45,7 @@ bool processDicomSeries( const Config::ParameterContainer& parameters )
GridType grid;
Containers::Vector< double, Devices::Host, int > vector;
Images::RegionOfInterest< int > roi;
for( int i = 0; i < dicomSeriesNames.getSize(); i++ )
for( std::size_t i = 0; i < dicomSeriesNames.size(); i++ )
{
const String& seriesName = dicomSeriesNames[ i ];
std::cout << "Reading a file " << seriesName << std::endl;
......