summaryrefslogtreecommitdiff
path: root/src/libaudcore/list.cc
blob: f2b7a5e6136e488ef8b0fe4cd974f9a1ccce8ce7 (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
/*
 * list.cpp
 * Copyright 2014 John Lindgren
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions, and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions, and the following disclaimer in the documentation
 *    provided with the distribution.
 *
 * This software is provided "as is" and without any warranty, express or
 * implied. In no event shall the authors be liable for any damages arising from
 * the use of this software.
 */

#include "list.h"

EXPORT void ListBase::insert_after (ListNode * prev, ListNode * node)
{
    ListNode * next;

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

    node->prev = prev;
    node->next = next;

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

EXPORT void ListBase::remove (ListNode * node)
{
    ListNode * prev = node->prev;
    ListNode * next = node->next;

    node->prev = nullptr;
    node->next = nullptr;

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

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

EXPORT void ListBase::clear (DestroyFunc destroy)
{
    ListNode * node = head;
    while (node)
    {
        ListNode * next = node->next;
        destroy (node);
        node = next;
    }

    head = nullptr;
    tail = nullptr;
}