2012-11-13 15:14:08 -08:00
|
|
|
/* llist.c - Linked list functions
|
2006-11-01 19:28:46 -08:00
|
|
|
*
|
|
|
|
* Linked list structures have a next pointer as their first element.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "toys.h"
|
|
|
|
|
2014-05-21 04:57:43 -07:00
|
|
|
// Callback function to free data pointer of double_list or arg_list
|
|
|
|
|
|
|
|
void llist_free_arg(void *node)
|
|
|
|
{
|
|
|
|
struct arg_list *d = node;
|
|
|
|
|
|
|
|
free(d->arg);
|
|
|
|
free(d);
|
|
|
|
}
|
|
|
|
|
|
|
|
void llist_free_double(void *node)
|
|
|
|
{
|
|
|
|
struct double_list *d = node;
|
|
|
|
|
|
|
|
free(d->data);
|
|
|
|
free(d);
|
|
|
|
}
|
|
|
|
|
2012-07-15 15:22:04 -07:00
|
|
|
// Call a function (such as free()) on each element of a linked list.
|
2014-05-21 04:57:43 -07:00
|
|
|
void llist_traverse(void *list, void (*using)(void *node))
|
2006-11-01 19:28:46 -08:00
|
|
|
{
|
2012-11-22 19:18:09 -08:00
|
|
|
void *old = list;
|
|
|
|
|
2012-11-13 15:14:08 -08:00
|
|
|
while (list) {
|
|
|
|
void *pop = llist_pop(&list);
|
|
|
|
using(pop);
|
2008-10-23 14:44:30 -07:00
|
|
|
|
2012-11-13 15:14:08 -08:00
|
|
|
// End doubly linked list too.
|
2012-11-22 19:18:09 -08:00
|
|
|
if (old == list) break;
|
2012-11-13 15:14:08 -08:00
|
|
|
}
|
2006-11-01 19:28:46 -08:00
|
|
|
}
|
2006-11-02 21:05:52 -08:00
|
|
|
|
|
|
|
// Return the first item from the list, advancing the list (which must be called
|
|
|
|
// as &list)
|
|
|
|
void *llist_pop(void *list)
|
|
|
|
{
|
2021-12-28 14:17:50 -08:00
|
|
|
void **llist = list, **next;
|
|
|
|
|
|
|
|
if (!list || !*llist) return 0;
|
|
|
|
next = (void **)*llist;
|
2012-11-13 15:14:08 -08:00
|
|
|
*llist = *next;
|
|
|
|
|
2021-12-28 14:17:50 -08:00
|
|
|
return next;
|
2006-11-02 21:05:52 -08:00
|
|
|
}
|
2008-01-20 15:34:53 -08:00
|
|
|
|
2019-07-09 23:53:23 -07:00
|
|
|
// Remove first item from &list and return it
|
2013-09-09 02:26:03 -07:00
|
|
|
void *dlist_pop(void *list)
|
|
|
|
{
|
|
|
|
struct double_list **pdlist = (struct double_list **)list, *dlist = *pdlist;
|
|
|
|
|
2019-06-20 14:59:49 -07:00
|
|
|
if (!dlist) return 0;
|
2013-09-09 03:26:52 -07:00
|
|
|
if (dlist->next == dlist) *pdlist = 0;
|
|
|
|
else {
|
2019-06-20 14:59:49 -07:00
|
|
|
if (dlist->next) dlist->next->prev = dlist->prev;
|
|
|
|
if (dlist->prev) dlist->prev->next = dlist->next;
|
|
|
|
*pdlist = dlist->next;
|
2013-09-09 03:26:52 -07:00
|
|
|
}
|
2013-09-09 02:26:03 -07:00
|
|
|
|
|
|
|
return dlist;
|
|
|
|
}
|
|
|
|
|
2019-07-09 23:53:23 -07:00
|
|
|
// remove last item from &list and return it (stack pop)
|
|
|
|
void *dlist_lpop(void *list)
|
|
|
|
{
|
|
|
|
struct double_list *dl = *(struct double_list **)list;
|
|
|
|
void *v = 0;
|
|
|
|
|
|
|
|
if (dl) {
|
|
|
|
dl = dl->prev;
|
|
|
|
v = dlist_pop(&dl);
|
|
|
|
if (!dl) *(void **)list = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2021-05-15 09:14:03 -07:00
|
|
|
// Append to list in-order (*list unchanged unless empty, ->prev is new node)
|
2012-03-11 22:25:40 -07:00
|
|
|
void dlist_add_nomalloc(struct double_list **list, struct double_list *new)
|
|
|
|
{
|
2012-11-13 15:14:08 -08:00
|
|
|
if (*list) {
|
|
|
|
new->next = *list;
|
|
|
|
new->prev = (*list)->prev;
|
|
|
|
(*list)->prev->next = new;
|
|
|
|
(*list)->prev = new;
|
|
|
|
} else *list = new->next = new->prev = new;
|
2012-03-11 22:25:40 -07:00
|
|
|
}
|
|
|
|
|
2010-01-05 08:43:36 -08:00
|
|
|
// Add an entry to the end of a doubly linked list
|
2008-10-23 14:44:30 -07:00
|
|
|
struct double_list *dlist_add(struct double_list **list, char *data)
|
2008-01-20 15:34:53 -08:00
|
|
|
{
|
2012-11-13 15:14:08 -08:00
|
|
|
struct double_list *new = xmalloc(sizeof(struct double_list));
|
2008-01-20 15:34:53 -08:00
|
|
|
|
2012-11-13 15:14:08 -08:00
|
|
|
new->data = data;
|
|
|
|
dlist_add_nomalloc(list, new);
|
2008-10-23 14:44:30 -07:00
|
|
|
|
2012-11-13 15:14:08 -08:00
|
|
|
return new;
|
2008-01-20 15:34:53 -08:00
|
|
|
}
|
2014-05-29 03:22:02 -07:00
|
|
|
|
|
|
|
// Terminate circular list for traversal in either direction. Returns end *.
|
|
|
|
void *dlist_terminate(void *list)
|
|
|
|
{
|
|
|
|
struct double_list *end = list;
|
|
|
|
|
2019-12-28 14:01:02 -08:00
|
|
|
if (!end || !end->prev) return 0;
|
2014-05-29 03:22:02 -07:00
|
|
|
|
|
|
|
end = end->prev;
|
|
|
|
end->next->prev = 0;
|
|
|
|
end->next = 0;
|
|
|
|
|
|
|
|
return end;
|
|
|
|
}
|
2016-06-19 05:07:44 -07:00
|
|
|
|
|
|
|
// Find num in cache
|
|
|
|
struct num_cache *get_num_cache(struct num_cache *cache, long long num)
|
|
|
|
{
|
|
|
|
while (cache) {
|
|
|
|
if (num==cache->num) return cache;
|
|
|
|
cache = cache->next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Uniquely add num+data to cache. Updates *cache, returns pointer to existing
|
|
|
|
// entry if it was already there.
|
|
|
|
struct num_cache *add_num_cache(struct num_cache **cache, long long num,
|
|
|
|
void *data, int len)
|
|
|
|
{
|
|
|
|
struct num_cache *old = get_num_cache(*cache, num);
|
|
|
|
|
|
|
|
if (old) return old;
|
|
|
|
|
|
|
|
old = xzalloc(sizeof(struct num_cache)+len);
|
|
|
|
old->next = *cache;
|
|
|
|
old->num = num;
|
|
|
|
memcpy(old->data, data, len);
|
|
|
|
*cache = old;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|