summaryrefslogtreecommitdiffstats
path: root/firmware/common/linked_list.c
blob: b8f2dd181ccf93abdcbb49071974c65058350093 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2014 by Michael Sevakis
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
#include <stddef.h>
#include "linked_list.h"


/** (L)inked (L)ist **/

/**
 * Helper to find the node previous to 'find'
 *
 * returns: NULL if 'find' is the first node
 *          last node if the 'find' isn't found in the list
 */
static struct ll_node * ll_search_prev(struct ll_head *list,
                                       struct ll_node *find)
{
    struct ll_node *prev = NULL;
    struct ll_node *node = list->head;

    while (node != NULL && node != find)
    {
        prev = node;
        node = node->next;
    }

    return prev;
}

/**
 * Initializes the singly-linked list
 */
void ll_init(struct ll_head *list)
{
    list->head = NULL;
    list->tail = NULL;
}

/**
 * Adds a node to s singly-linked list using "insert next"
 */
void ll_insert_next(struct ll_head *list, struct ll_node *node,
                    struct ll_node *newnode)
{
    struct ll_node **nodep = node != NULL ? &node->next : &list->head;
    struct ll_node *next = *nodep;

    newnode->next = next;
    *nodep = newnode;

    if (next == NULL)
        list->tail = newnode;
}

/**
 * Adds a node to a singly-linked list using "insert first"
 */
void ll_insert_first(struct ll_head *list, struct ll_node *node)
{
    struct ll_node *head = list->head;

    node->next = head;
    list->head = node;

    if (head == NULL)
        list->tail = node;
}

/**
 * Adds a node to a singly-linked list using "insert last"
 */
void ll_insert_last(struct ll_head *list, struct ll_node *node)
{
    struct ll_node *tail = list->tail;

    node->next = NULL;
    list->tail = node;

    if (tail == NULL)
        list->head = node;
    else
        tail->next = node;
}

/**
 * Removes the node after "node"; if there is none, nothing is changed
 */
void ll_remove_next(struct ll_head *list, struct ll_node *node)
{
    struct ll_node **nodep = node != NULL ? &node->next : &list->head;
    struct ll_node *next = *nodep;

    if (next != NULL)
    {
        next = next->next;
        *nodep = next;

        if (next == NULL)
           list->tail = node;
    }
}

/**
 * Removes the first node in the list
 */
void ll_remove_first(struct ll_head *list)
{
    struct ll_node *node = list->head->next;

    list->head = node;

    if (node == NULL)
        list->tail = NULL;
}

/**
 * Removes the node from the list
 */
void ll_remove(struct ll_head *list, struct ll_node *node)
{
    ll_remove_next(list, ll_search_prev(list, node));
}


/** (L)inked (L)ist (D)ouble **/

/**
 * Initializes the doubly-linked list
 */
void lld_init(struct lld_head *list)
{
    list->head = NULL;
    list->tail = NULL;

    /* tail could be stored in first item's prev pointer but this simplifies
       the routines and maintains the non-circularity */
}

/**
 * Adds a node to a doubly-linked list using "insert first"
 */
void lld_insert_first(struct lld_head *list, struct lld_node *node)
{
    struct lld_node *head = list->head;

    list->head = node;

    if (head == NULL)
        list->tail = node;
    else
        head->prev = node;

    node->next = head;
    node->prev = NULL;
}

/**
 * Adds a node to a doubly-linked list using "insert last"
 */
void lld_insert_last(struct lld_head *list, struct lld_node *node)
{
    struct lld_node *tail = list->tail;

    list->tail = node;

    if (tail == NULL)
        list->head = node;
    else
        tail->next = node;

    node->next = NULL;
    node->prev = tail;
}

/**
 * Removes a node from a doubly-linked list
 */
void lld_remove(struct lld_head *list, struct lld_node *node)
{
    struct lld_node *next = node->next;
    struct lld_node *prev = node->prev;

    if (prev == NULL)
        list->head = next;
    else
        prev->next = next;

    if (next == NULL)
        list->tail = prev;
    else
        next->prev = prev;
}


/** (L)inked (L)ist (D)ouble (C)ircular **/

/**
 * Helper that inserts a node into a doubly-link circular list; does not
 * affect list->head, just returns its state
 */
static inline struct lldc_node * lldc_insert(struct lldc_head *list,
                                             struct lldc_node *node)
{
    struct lldc_node *head = list->head;

    if (head == NULL)
    {
        node->prev = node;
        node->next = node;
    }
    else
    {
        node->prev = head->prev;
        node->next = head;
        head->prev->next = node;
        head->prev = node;
    }

    return head;
}

/**
 * Initializes the doubly-linked circular list
 */
void lldc_init(struct lldc_head *list)
{
    list->head = NULL;
}

/**
 * Adds a node to a doubly-linked circular list using "insert first"
 */
void lldc_insert_first(struct lldc_head *list, struct lldc_node *node)
{
    lldc_insert(list, node);
    list->head = node;
}

/**
 * Adds a node to a doubly-linked circular list using "insert last"
 */
void lldc_insert_last(struct lldc_head *list, struct lldc_node *node)
{
    if (lldc_insert(list, node) == NULL)
        list->head = node;
}

/**
 * Removes a node from a doubly-linked circular list
 */
void lldc_remove(struct lldc_head *list, struct lldc_node *node)
{
    struct lldc_node *next = node->next;

    if (node == list->head)
    {
        if (node == next)
        {
            list->head = NULL;
            return;
        }

        list->head = next;
    }

    struct lldc_node *prev = node->prev;
    next->prev = prev;
    prev->next = next;
}