libtcod
Loading...
Searching...
No Matches
TCODList< T > Class Template Reference

#include <list.hpp>

Public Member Functions

 TCODList ()=default
 You can create an empty list with the default constructor.
 TCODList (const TCOD_list_t l)
 You can create a list by duplicating an existing list.
 TCODList (const TCODList< T > &rhs)
TCODList< T > & operator= (const TCODList< T > &rhs)
 TCODList (TCODList &&rhs) noexcept
TCODListoperator= (TCODList &&rhs) noexcept
virtual ~TCODList ()
 You can delete a list, freeing any allocated resources.
 TCODList (int nbElements)
 You can also create an empty list and pre-allocate memory for elements.
void set (const T elt, int idx)
 You can assign a value with set.
get (int idx) const
 You can retrieve a value with get.
bool isEmpty () const
int size () const
bool contains (const T elt) const
T * insertBefore (const T elt, int before)
void remove (const T elt)
 The _fast versions replace the element to remove with the last element of the list.
void removeFast (const T elt)
void addAll (const TCODList< T > &l2)
 You can concatenate two lists.
void clear ()
void clearAndDelete ()
 For lists containing pointers, you can clear the list and delete (or free for C) the elements :
void reverse ()
 This function reverses the order of the elements in the list.
void push (const T elt)
 You can push an element on the stack (append it to the end of the list) :
pop ()
 You can pop an element from the stack (remove the last element of the list).
peek () const
 You can read the last element of the stack without removing it :
T * begin () const
 You can iterate through the elements of the list using an iterator.
T * end () const
T * remove (T *elt)
 You can remove an element from the list while iterating.
T * removeFast (T *elt)

Protected Member Functions

void allocate ()

Friends

void swap (TCODList &lhs, TCODList &rhs) noexcept

Detailed Description

template<class T>
class TCODList< T >

This is a fast, lightweight and generic container, that provides array, list and stack paradigms. Note that this module has no Python wrapper. Use Python built-in containers instead.

Template Parameters
T
Deprecated
All instances of TCODList should be replaced with std::vector where possible.

Constructor & Destructor Documentation

◆ TCODList() [1/3]

template<class T>
TCODList< T >::TCODList ( )
default

You can create an empty list with the default constructor.

The C version returns a handler on the list.

TCODList<int> intList; TCODList<float> *floatList = new TCODList<float>();

TCOD_list_t intList = TCOD_list_new(); TCOD_list_t floatList = TCOD_list_new();

◆ TCODList() [2/3]

template<class T>
TCODList< T >::TCODList ( const TCOD_list_t l)
inline

You can create a list by duplicating an existing list.

Parameters
lExisting list to duplicate.

TCODList<int> intList; intList.push(3); intList.push(5); TCODList<int> intList2(intList); // intList2 contains two elements : 3 and 5

TCOD_list_t intList = TCOD_list_new(); TCOD_list_push(intList,(const void *)3); TCOD_list_push(intList,(const void *)5); TCOD_list_t intList2 = TCOD_list_duplicate(intList); // intList2 contains two elements : 3 and 5

◆ ~TCODList()

template<class T>
virtual TCODList< T >::~TCODList ( )
inlinevirtual

You can delete a list, freeing any allocated resources.

Note that deleting the list does not delete it's elements. You have to use clearAndDelete before deleting the list if you want to destroy the elements too.

Parameters
lIn the C version, the list handler, returned by a constructor.

TCODList<int> *intList = new TCODList<int>(); // allocate a new empty list intList->push(5); // the list contains 1 element at position 0, value = 5 delete intList; // destroy the list

TCOD_list_t intList = TCOD_list_new(); TCOD_list_push(intList,(const void *)5); TCOD_list_delete(intList);

◆ TCODList() [3/3]

template<class T>
TCODList< T >::TCODList ( int nbElements)
inline

You can also create an empty list and pre-allocate memory for elements.

Use this if you know the list size and want the memory to fit it perfectly.

Parameters
nbElementsAllocate memory for nbElements.

Member Function Documentation

◆ addAll()

template<class T>
void TCODList< T >::addAll ( const TCODList< T > & l2)
inline

You can concatenate two lists.

Every element of l2 will be added to current list (or l in the C version) :

Parameters
lThe list inside which elements will be added.
l2the list handler containing elements to insert.

TCODList<int> intList; intList.set(1,3); // intList contains 2 elements : 0, 3 TCODList<int> intList2; // intList2 is empty intList2.set(0,1); // intList2 contains 1 element : 1 intList2.addAll(intList); // intList2 contains 3 elements : 1, 0, 3

TCOD_list_t intList = TCOD_list_new(); TCOD_list_set(intList,1,(const void *)3); TCOD_list_t intList2 = TCOD_list_new(); TCOD_list_set(intList2,0,(const void *)1); TCOD_list_add_all(intList2,intList);

◆ begin()

template<class T>
T * TCODList< T >::begin ( ) const
inline

You can iterate through the elements of the list using an iterator.

begin() returns the address of the first element of the list. You go to the next element using the increment operator ++. When the iterators value is equal to end(), you've gone through all the elements. Warning ! You cannot insert elements in the list while iterating through it. Inserting elements can result in reallocation of the list and your iterator will not longer be valid.

template <class T> T * TCODList::begin() const template <class T> T * TCODList::end() const

void ** TCOD_list_begin(TCOD_list_t l) void ** TCOD_list_end(TCOD_list_t l)

Parameters
lIn the C version, the list handler, returned by a constructor.

TCODList<int> intList; // the list is empty (contains 0 elements) intList.push(5); // the list contains 1 element at position 0, value = 5 intList.push(2); // the list contains 2 elements : 5,2 for ( int * iterator = intList.begin(); iterator != intList.end(); iterator ++ ) { int currentValue=*iterator; printf("value : %d\n", currentValue ); }

TCOD_list_t intList = TCOD_list_new(); TCOD_list_push(intList,(const void *)5); TCOD_list_push(intList,(const void *)2); for ( int * iterator = (int *)TCOD_list_begin(intList); iterator != (int *)TCOD_list_end(intList); iterator ++ ) { int currentValue=*iterator; printf("value : %d\n", currentValue ); }

◆ clear()

template<class T>
void TCODList< T >::clear ( )
inline
Parameters
lIn the C version, the list handler, returned by a constructor.

TCODList<int> intList; intList.set(0,3); // intList contains 1 element intList.clear(); // intList is empty

TCOD_list_t intList = TCOD_list_new(); TCOD_list_set(intList,0,(const void *)5); TCOD_list_clear(intList);

◆ clearAndDelete()

template<class T>
void TCODList< T >::clearAndDelete ( )
inline

For lists containing pointers, you can clear the list and delete (or free for C) the elements :

Parameters
lIn the C version, the list handler, returned by a constructor.

TCODList<MyClass *> intList; MyClass * cl=new MyClass(); // new instance of MyClass allocated here intList.set(0,cl); intList.clear(); // the list is empty. cl is always valid intList.set(0,cl); intList.clearAndDelete(); // the list is empty. delete cl has been called. The address cl is no longer valid.

TCOD_list_t intList = TCOD_list_new(); void *data=calloc(1,10); // some memory allocation here TCOD_list_set(intList,0,(const void *)data); TCOD_list_clear(intList); // the list is empty, but data is always valid TCOD_list_set(intList,0,(const void *)data); TCOD_list_clear_and_delete(intList); // the list is empty, free(data) has been called. The address data is no longer valid

◆ contains()

template<class T>
bool TCODList< T >::contains ( const T elt) const
inline
Parameters
eltThe element.
lIn the C version, the handler, returned by a constructor.

TCODList<int> intList; intList.set(3,0); bool has3 = intList.contains(3); // has3 == true bool has4 = intList.contains(4); // has4 == false

TCOD_list_t intList = TCOD_list_new(); TCOD_list_set(intList,(const void *)3,0); bool has3 = TCOD_list_contains(intList,(const void *)3); // has3 == true bool has4 = TCOD_list_contains(intList,(const void *)4); // has4 == false

◆ get()

template<class T>
T TCODList< T >::get ( int idx) const
inline

You can retrieve a value with get.

Parameters
idxIndex of the element. 0 <= idx < size of the array
lIn the C version, the handler, returned by a constructor.

TCODList<int> intList; intList.set(5,0); int val = intList.get(0); // val == 5

TCOD_list_t intList = TCOD_list_new(); TCOD_list_set(intList,(const void *)5,0); int val = (int)TCOD_list_get(intList,0); // val == 5

◆ insertBefore()

template<class T>
T * TCODList< T >::insertBefore ( const T elt,
int before )
inline
Parameters
eltElement to insert in the list.
idxIndex of the element after the insertion. 0 <= idx < list size
lIn the C version, the list handler, returned by a constructor.

TCODList<int> intList; // the list is empty (contains 0 elements) intList.set(0,5); // the list contains 1 element at position 0, value = 5 intList.insertBefore(2,0); // the list contains 2 elements : 2,5

TCOD_list_t intList = TCOD_list_new(); TCOD_list_set(intList,0,(const void *)5); TCOD_list_insert_before(intList,(const void *)2,0);

◆ isEmpty()

template<class T>
bool TCODList< T >::isEmpty ( ) const
inline
Parameters
lIn the C version, the handler, returned by a constructor.

TCODList<int> intList; bool empty=intList.isEmpty(); // empty == true intList.set(3,0); empty=intList.isEmpty(); // empty == false

TCOD_list_t intList = TCOD_list_new(); bool empty=TCOD_list_is_empty(intList); // empty == true TCOD_list_set(intList,(const void *)5,0); empty=TCOD_list_is_empty(intList); // empty == false

◆ peek()

template<class T>
T TCODList< T >::peek ( ) const
inline

You can read the last element of the stack without removing it :

Parameters
lIn the C version, the list handler, returned by a constructor.

TCODList<int> intList; intList.push(3); // intList contains 1 elements : 3 int val = intList.peek(); // val == 3, inList contains 1 elements : 3 intList.push(2); // intList contains 2 elements : 3, 2 val = intList.peek(); // val == 2, inList contains 2 elements : 3, 2

TCOD_list_t intList = TCOD_list_new(); TCOD_list_push(intList,(const void *)3); int val = (int)TCOD_list_peek(intList); TCOD_list_push(intList,(const void *)2); val = (int)TCOD_list_peek(intList);

◆ pop()

template<class T>
T TCODList< T >::pop ( )
inline

You can pop an element from the stack (remove the last element of the list).

Parameters
lIn the C version, the list handler, returned by a constructor.

TCODList<int> intList; // the list is empty (contains 0 elements) intList.push(5); // the list contains 1 element at position 0, value = 5 intList.push(2); // the list contains 2 elements : 5,2 int val = intList.pop(); // val == 2, the list contains 1 element : 5 val = intList.pop(); // val == 5, the list is empty

TCOD_list_t intList = TCOD_list_new(); TCOD_list_push(intList,(const void *)5); TCOD_list_push(intList,(const void *)2); int val = (int)TCOD_list_pop(intList); val = (int)TCOD_list_pop(intList);

◆ push()

template<class T>
void TCODList< T >::push ( const T elt)
inline

You can push an element on the stack (append it to the end of the list) :

Parameters
eltElement to append to the list.
lIn the C version, the list handler, returned by a constructor.

TCODList<int> intList; // the list is empty (contains 0 elements) intList.push(5); // the list contains 1 element at position 0, value = 5 intList.push(2); // the list contains 2 elements : 5,2

TCOD_list_t intList = TCOD_list_new(); TCOD_list_push(intList,(const void *)5); TCOD_list_push(intList,(const void *)2);

◆ remove() [1/2]

template<class T>
void TCODList< T >::remove ( const T elt)
inline

The _fast versions replace the element to remove with the last element of the list.

They're faster, but do not preserve the list order.

template <class T> void TCODList::remove(const T elt) template <class T> void TCODList::removeFast(const T elt)

void TCOD_list_remove(TCOD_list_t l, const void * elt) void TCOD_list_remove_fast(TCOD_list_t l, const void * elt)

Parameters
eltThe element to remove
lIn the C version, the list handler, returned by a constructor.

TCODList<int> intList; // the list is empty (contains 0 elements) intList.set(0,5); // the list contains 1 element at position 0, value = 5 intList.remove(5); // the list is empty

TCOD_list_t intList = TCOD_list_new(); TCOD_list_set(intList,0,(const void *)5); TCOD_list_remove(intList,(const void *)5);

◆ remove() [2/2]

template<class T>
T * TCODList< T >::remove ( T * elt)
inline

You can remove an element from the list while iterating.

The element at the iterator position will be removed. The function returns the new iterator. The _fast versions replace the element to remove with the last element of the list. They're faster, but do not preserve the list order.

template <class T> T *TCODList::remove(T *iterator) template <class T> T *TCODList::removeFast(T *iterator)

void **TCOD_list_remove_iterator(TCOD_list_t l, void **iterator) void **TCOD_list_remove_iterator_fast(TCOD_list_t l, void **iterator)

Parameters
iteratorThe list iterator.
lIn the C version, the list handler, returned by a constructor.

TCODList<int> intList; // the list is empty (contains 0 elements) intList.push(5); // the list contains 1 element at position 0, value = 5 intList.push(2); // the list contains 2 elements : 5,2 intList.push(3); // the list contains 3 elements : 5,2,3 for ( int * iterator = intList.begin(); iterator != intList.end(); iterator ++ ) { int currentValue=*iterator; if ( currentValue == 2 ) { remove this value from the list and keep iterating on next element (value == 3) iterator = intList.remove(iterator); } printf("value : %d\n", currentValue ); // all 3 values will be printed : 5,2,3 } now the list contains only two elements : 5,3

TCOD_list_t intList = TCOD_list_new(); TCOD_list_push(intList,(const void *)5); TCOD_list_push(intList,(const void *)2); TCOD_list_push(intList,(const void *)3); for ( int * iterator = (int *)TCOD_list_begin(intList); iterator != (int *)TCOD_list_end(intList); iterator ++ ) { int currentValue=*iterator; if ( currentValue == 2 ) { iterator = (int *)TCOD_list_remove_iterator(intList,(void **)iterator); } printf("value : %d\n", currentValue ); }

◆ reverse()

template<class T>
void TCODList< T >::reverse ( )
inline

This function reverses the order of the elements in the list.

void TCODList::reverse()

void TCOD_list_reverse(TCOD_list_t l)

Parameters
lIn the C version, the list handler, returned by a constructor.

TCODList<int> intList; // the list is empty (contains 0 elements) intList.push(5); // the list contains 1 element at position 0, value = 5 intList.push(2); // the list contains 2 elements : 5,2 intList.reverse(); // now order is 2,5

TCOD_list_t intList = TCOD_list_new(); TCOD_list_push(intList,(const void *)5); TCOD_list_push(intList,(const void *)2); TCOD_list_reverse();

◆ set()

template<class T>
void TCODList< T >::set ( const T elt,
int idx )
inline

You can assign a value with set.

If needed, the array will allocate new elements up to idx.

Parameters
eltElement to put in the array.
idxIndex of the element. 0 <= idx
lIn the C version, the handler, returned by a constructor.

TCODList<int> intList; // the array is empty (contains 0 elements) intList.set(5,0); // the array contains 1 element at position 0, value = 5 intList.set(7,2); // the array contains 3 elements : 5, 0, 7

TCOD_list_t intList = TCOD_list_new(); TCOD_list_set(intList,(const void *)5,0); TCOD_list_set(intList,(const void *)7,2);

◆ size()

template<class T>
int TCODList< T >::size ( ) const
inline
Parameters
lIn the C version, the handler, returned by a constructor.

TCODList<int> intList; int size=intList.size(); // size == 0 intList.set(3,0); size=intList.size(); // size == 1

TCOD_list_t intList = TCOD_list_new(); int size=TCOD_list_size(intList); // size == 0 TCOD_list_set(intList,(const void *)5,0); size=TCOD_list_size(intList); // size == 1


The documentation for this class was generated from the following file:
  • C:/Users/4b796/Projects/libtcod/src/libtcod/list.hpp