From 2cddc37a67d36d13024bb882a3c73c55878b6a3d Mon Sep 17 00:00:00 2001 From: "K. Lange" Date: Sat, 28 Mar 2020 22:19:52 +0900 Subject: [PATCH] list: add convenience function to get item by index --- base/usr/include/toaru/list.h | 1 + lib/list.c | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/base/usr/include/toaru/list.h b/base/usr/include/toaru/list.h index 34d68fad..95165bd7 100644 --- a/base/usr/include/toaru/list.h +++ b/base/usr/include/toaru/list.h @@ -42,6 +42,7 @@ extern node_t * list_pop(list_t * list); extern node_t * list_dequeue(list_t * list); extern list_t * list_copy(list_t * original); extern void list_merge(list_t * target, list_t * source); +extern void * list_index(list_t * list, int index); extern void list_append_after(list_t * list, node_t * before, node_t * node); extern node_t * list_insert_after(list_t * list, node_t * before, void * item); diff --git a/lib/list.c b/lib/list.c index b76da626..f27de711 100644 --- a/lib/list.c +++ b/lib/list.c @@ -166,6 +166,15 @@ int list_index_of(list_t * list, void * value) { return -1; /* not find */ } +void * list_index(list_t * list, int index) { + int i = 0; + foreach(item, list) { + if (i == index) return item->value; + i++; + } + return NULL; +} + void list_remove(list_t * list, size_t index) { /* remove index from the list */ if (index > list->length) return;