diff --git a/code/game/list.c b/code/game/list.c index db4d1ad..77dfffc 100644 --- a/code/game/list.c +++ b/code/game/list.c @@ -337,3 +337,22 @@ void list_init(struct list * l, void (*destructor)(void*)) { l->destructor = destructor; } +container_p list_at(list_p list, int idx) { + list_iter_p iter; + container_p c = NULL; + int i; + + if(idx < 0 || idx >= list->length || list == NULL) { + return NULL; + } + + iter = list_iterator(list, LIST_FRONT); + for(c = list_next(iter), i = 0; c != NULL; c = list_next(iter), i++) { + if(i == idx) { + break; + } + } + + return c; +} + diff --git a/code/game/list.h b/code/game/list.h index 0085b77..062764a 100644 --- a/code/game/list.h +++ b/code/game/list.h @@ -324,4 +324,13 @@ void list_clear(list_p list); */ void list_init(struct list * l, void (*destructor)(void*)); +/** + * Get the element at the given index. + * + * \param list a list + * \param idx index + * \return element at given index or NULL if index is out of bounds + */ +container_p list_at(list_p list, int idx); + #endif \ No newline at end of file