6.3. Basic list operations
Insert an element in the list
template <class T> void TCODList::insertBefore(const T elt,int before)
void TCOD_list_insert_before(TCOD_list_t l,const void *elt,int before)
Parameter | Description |
---|---|
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. |
Example:
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);
TCOD_list_t intList = TCOD_list_new();
TCOD_list_set(intList,0,(const void *)5);
TCOD_list_insert_before(intList,(const void *)2,0);
Removing an element from the list
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)
Parameter | Description |
---|---|
elt | The element to remove |
l | In the C version, the list handler, returned by a constructor. |
Example:
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);
TCOD_list_t intList = TCOD_list_new();
TCOD_list_set(intList,0,(const void *)5);
TCOD_list_remove(intList,(const void *)5);
Concatenating two lists
You can concatenate two lists. Every element of l2 will be added to current list (or l in the C version) :
template <class T> void TCODList::addAll(const TCODList &l2)
void TCOD_list_add_all(TCOD_list_t l, TCOD_list_t l2)
Parameter | Description |
---|---|
l | The list inside which elements will be added. |
l2 | the list handler containing elements to insert. |
Example:
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);
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);
Emptying a list
template <class T> void TCODList::clear()
void TCOD_list_clear(TCOD_list_t l)
Parameter | Description |
---|---|
l | In the C version, the list handler, returned by a constructor. |
Example:
TCODList<int> intList;
intList.set(0,3); // intList contains 1 element
intList.clear();
TCOD_list_t intList = TCOD_list_new();
TCOD_list_set(intList,0,(const void *)5);
TCOD_list_clear(intList);
Emptying a list and destroying its elements
For lists containing pointers, you can clear the list and delete (or free for C) the elements :
template <class T> void TCODList::clearAndDelete()
TCOD_list_t intList = TCOD_list_new();
void *data=calloc(10,1); // 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);
Parameter | Description |
---|---|
l | In the C version, the list handler, returned by a constructor. |
Example:
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();
Reversing a list
This function reverses the order of the elements in the list.
void TCODList::reverse()
void TCOD_list_reverse(TCOD_list_t l)
Parameter | Description |
---|---|
l | In the C version, the list handler, returned by a constructor. |
Example:
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();
TCOD_list_t intList = TCOD_list_new();
TCOD_list_push(intList,(const void *)5);
TCOD_list_push(intList,(const void *)2);
TCOD_list_reverse();