FMI Table
| Kamen Mladenov | FMI Computer Science Year 1 Group 6 | Project 2 | Project Topic 7 |
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
List< T > Class Template Reference

Templated class that stores an array of elements in dynamic memory. More...

#include <List.hpp>

Public Member Functions

 List (const T *elements, unsigned elementsCount)
 Copies all elements in given array.
 
 List (unsigned length)
 Allocates memory for the given length.
 
void add (const T &element)
 Adds an element. More...
 
void insertAt (const T &element, unsigned index)
 Inserts element at given index. More...
 
bool removeAt (unsigned index)
 Removes element at index. More...
 
unsigned findIndex (const T &element) const
 Finds the index of element. More...
 
bool contain (const T &element) const
 
T & operator[] (unsigned index)
 Returns reference to element at index.
 
const T & operator[] (unsigned index) const
 Returns constant reference to element at index.
 
const T * raw_data () const
 Returns raw array of data.
 
void clear ()
 Removes all data.
 
List< T > & operator+= (const List< T > other)
 Appends elements from other list.
 
std::istream & read (std::istream &istr)
 Reads from stream. More...
 
std::ostream & write (std::ostream &ostr) const
 Writes to stream. More...
 
unsigned get_length () const
 Returns the length. More...
 
unsigned get_count () const
 Returns the count. More...
 
List & operator= (const List &other)
 
 List (const List &other)
 
 List (List &&other)
 
List & operator= (List &&other)
 

Protected Member Functions

void resize ()
 
void free ()
 
void copyFrom (const List &other)
 

Protected Attributes

T * elements
 
unsigned length
 
unsigned count
 

Detailed Description

template<typename T>
class List< T >

Templated class that stores an array of elements in dynamic memory.

Warning
findIndex(), contain(), read(), write(), operator<<() and operator>>() require the type to have defined a couple of functions.
See also
findIndex()
contain()
read()
write()
operator<<()
operator>>()

Member Function Documentation

◆ add()

template<typename T >
void List< T >::add ( const T &  element)

Adds an element.

Resizes internal array if there is no space for additional elements.

◆ contain()

template<typename T >
bool List< T >::contain ( const T &  element) const
Returns
Whether the element is contained in the current List
Warning
Function depends on findIndex(), which means the same "compare" function must be defined in the type
See also
findIndex()

◆ findIndex()

template<typename T >
unsigned List< T >::findIndex ( const T &  element) const

Finds the index of element.

Returns
Index of element. If element isn't found, returns the count of element.
Warning
The function depends on the type having a function "compare" defined, which takes two elements and returns a number <0 if elem1 < elem2, >0 if elem1 > elem2, 0 if elem1 == elem2
Note
Searching is done linearly

◆ get_count()

template<typename T >
unsigned List< T >::get_count

Returns the count.

Count is the amount of elements that are stored.

◆ get_length()

template<typename T >
unsigned List< T >::get_length

Returns the length.

Length is the size of the underlying array (allocated memory).

◆ insertAt()

template<typename T >
void List< T >::insertAt ( const T &  element,
unsigned  index 
)

Inserts element at given index.

If index is after the last element, the element is just added. Otherwise all elements after the index are shifted right and element is put in place.

Resizes internal array if there is no space for additional elements.

◆ read()

template<typename T >
std::istream & List< T >::read ( std::istream &  istr)

Reads from stream.

Parameters
istrAn input stream

Directly reads bytes from stream (calls read() function). Any stored values are deleted and replaced with those from the stream.

Warning
The function depends on the type having a function "read" defined, which takes an std::istream& and writes it's data to it. Return type doesn't matter.
Remarks
Doesn't alter the stream in any other way.
Note
Best used with binary ifstream

◆ removeAt()

template<typename T >
bool List< T >::removeAt ( unsigned  index)

Removes element at index.

Returns
Wether element could be removed

If index is after that of the last element, nothing is done and false is returned. Otherwise elements after index are shifted right and count is reduced.

◆ write()

template<typename T >
std::ostream & List< T >::write ( std::ostream &  ostr) const

Writes to stream.

Parameters
ostrAn output stream

Directly writes bytes to stream (calls write() function).

Warning
The function depends on the type having a function "write" defined, which takes an std::ostream& and writes it's data to it. Return type doesn't matter.
Remarks
Doesn't alter the stream in any other way.
Note
Best used with binary ofstream

The documentation for this class was generated from the following file: