From 6d73a796cac1c90e3c401c432f11c940e92b467c Mon Sep 17 00:00:00 2001 From: Walter Julius Hennecke Date: Fri, 12 Apr 2013 23:52:21 +0200 Subject: [PATCH] Added list_at function --- code/game/list.c | 19 +++++++++++++++++++ code/game/list.h | 9 +++++++++ 2 files changed, 28 insertions(+) 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