summaryrefslogtreecommitdiff
path: root/include/list.h
blob: 0364f7c2fd7f1845346b4f276203216eac35a03d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
 * Tiny doubly-linked list implementation.
 * Copyright (c) 2023-2024, Quinn Stephens and the OSMORA team.
 * Provided under the BSD 3-Clause license.
 */

#ifndef _LIST_H
#define _LIST_H

#include <stddef.h>

struct list_entry {
    struct list_entry *prev;
    struct list_entry *next;
};

struct list {
    struct list_entry *tail;
    struct list_entry *head;
    size_t length;
};

static inline void
list_remove(struct list_entry *entry)
{
    entry->prev->next = entry->next;
    entry->next->prev = entry->prev;
}

static inline void
list_append(struct list *list, struct list_entry *entry)
{
    entry->prev = list->tail;
    entry->next = (struct list_entry*)list;
    entry->prev->next = entry;
    list->tail = entry;
    list->length++;
}

static inline void
list_prepend(struct list *list, struct list_entry *entry)
{
    entry->next = list->head;
    entry->prev = (struct list_entry*)list;
    entry->next->prev = entry;
    list->head = entry;
    list->length++;
}

static inline void
list_init(struct list *list)
{
    list->tail = (struct list_entry*)list;
    list->head = (struct list_entry*)list;
    list->length = 0;
}

#endif /* !_LIST_H */