libtcoddocumentation

6.2. Basic array operations

Setting an element

You can assign a value with set. If needed, the array will allocate new elements up to idx.

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

void TCOD_list_set(TCOD_list_t l,const void *elt, int idx)

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

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);

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


Getting an element value

You can retrieve a value with get.

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

void * TCOD_list_get(TCOD_list_t l,int idx)

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

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

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


Checking if a list is empty

template <class T> bool TCODList::isEmpty() const

bool TCOD_list_is_empty(TCOD_list_t l)

ParameterDescription
lIn the C version, the handler, returned by a constructor.
Example:

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

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);


Getting the list size

template <class T> int TCODList::size() const

int TCOD_list_size(TCOD_list_t l)

ParameterDescription
lIn the C version, the handler, returned by a constructor.
Example:

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

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);


Checking if an element is in the list

template <class T> bool TCODList::contains(const T elt) const

bool TCOD_list_contains(TCOD_list_t l,const void * elt)

ParameterDescription
eltThe element.
lIn the C version, the handler, returned by a constructor.
Example:

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

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);