summaryrefslogtreecommitdiff
path: root/src/lib/list.c
blob: 62b2eb27e8b8f7e5dae2c7c8a9d74b924f898eeb (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
 * Ouroboros - Copyright (C) 2016 - 2024
 *
 * Simple doubly linked list implementation.
 *
 *    Dimitri Staessens <dimitri@ouroboros.rocks>
 *    Sander Vrijders   <sander@ouroboros.rocks>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * version 2.1 as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., http://www.fsf.org/about/contact/.
 */

#include <ouroboros/list.h>

#include <stddef.h>

void list_head_init(struct list_head * h)
{
        h->nxt = h;
        h->prv = h;
}

static void add_list(struct list_head * n,
                     struct list_head * prv,
                     struct list_head * nxt)
{
        nxt->prv = n;
        n->nxt = nxt;
        n->prv = prv;
        prv->nxt = n;
}

static void del_list(struct list_head * prv,
                     struct list_head * nxt)
{
        nxt->prv = prv;
        prv->nxt = nxt;
}

void list_add(struct list_head * n,
              struct list_head * h)
{
        add_list(n, h, h->nxt);
}

void list_add_tail(struct list_head * n,
                   struct list_head * h)
{
        add_list(n, h->prv, h);
}

void list_del(struct list_head * e)
{
        del_list(e->prv, e->nxt);
        e->nxt = e->prv = e;
}

bool list_is_empty(const struct list_head * h)
{
        return h->nxt == h;
}

void list_move(struct list_head * n,
               struct list_head * h)
{
        del_list(n->prv, n->nxt);
        add_list(n, h, h->nxt);
}