|
libtcod
|
#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 | |
| TCODList & | operator= (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. | |
| T | 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) : | |
| T | pop () |
| You can pop an element from the stack (remove the last element of the list). | |
| T | 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 |
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.
| T |
|
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();
|
inline |
You can create a list by duplicating an existing list.
| l | Existing 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
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.
| l | In 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);
|
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.
| nbElements | Allocate memory for nbElements. |
You can concatenate two lists.
Every element of l2 will be added to current list (or l in the C version) :
| l | The list inside which elements will be added. |
| l2 | the 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);
|
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)
| l | In 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 ); }
|
inline |
| l | In 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);
|
inline |
For lists containing pointers, you can clear the list and delete (or free for C) the elements :
| l | In 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
|
inline |
| elt | The element. |
| l | In 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
|
inline |
You can retrieve a value with get.
| idx | Index of the element. 0 <= idx < size of the array |
| l | In 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
|
inline |
| elt | Element to insert in the list. |
| idx | Index of the element after the insertion. 0 <= idx < list size |
| l | In 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);
|
inline |
| l | In 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
|
inline |
You can read the last element of the stack without removing it :
| l | In 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);
|
inline |
You can pop an element from the stack (remove the last element of the list).
| l | In 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);
|
inline |
You can push an element on the stack (append it to the end of the list) :
| elt | Element to append to the list. |
| l | In 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);
|
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)
| elt | The element to remove |
| l | In 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);
|
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)
| iterator | The list iterator. |
| l | In 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 ); }
|
inline |
This function reverses the order of the elements in the list.
void TCODList::reverse()
void TCOD_list_reverse(TCOD_list_t l)
| l | In 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();
|
inline |
You can assign a value with set.
If needed, the array will allocate new elements up to idx.
| elt | Element to put in the array. |
| idx | Index of the element. 0 <= idx |
| l | In 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);
|
inline |
| l | In 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