File: ll.c

package info (click to toggle)
libcddb 1.3.2-6
  • links: PTS
  • area: main
  • in suites: bullseye, buster
  • size: 2,276 kB
  • sloc: sh: 10,946; ansic: 4,721; makefile: 44
file content (202 lines) | stat: -rw-r--r-- 4,042 bytes parent folder | download | duplicates (6)
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
/*
    $Id: ll.c,v 1.1 2005/05/29 08:24:05 airborne Exp $

    Copyright (C) 2005 Kris Verbeeck <airborne@advalvas.be>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    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
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public
    License along with this library; if not, write to the
    Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    Boston, MA  02111-1307, USA.
*/


#include <stdlib.h>

#include "cddb/ll.h"


/* --- type and structure definitions */


/**
 * Linked list element.
 */
struct elem_s {
    struct elem_s *prev;        /**< previous element */
    struct elem_s *next;        /**< next element  */
    void *data;                 /**< the actual data of the element */
};

struct list_s {
    int cnt;                    /**< number of elements in the list */
    elem_destroy_cb *free_data; /**< callback used to free element data */
    elem_t *first;              /**< the first element in the list  */
    elem_t *last;               /**< the last element in the list  */
    elem_t *it;                 /**< iterator element */
};


/* --- private prototypes */


static elem_t *elem_construct(void *data);

/**
 * @return The next element.
 */
static elem_t *elem_destroy(elem_t *elem, elem_destroy_cb *cb);


/* --- private functions */


static elem_t *elem_construct(void *data)
{
    elem_t *elem;

    elem = (elem_t*)calloc(1, sizeof(elem_t));
    elem->data = data;
    return elem;
}

static elem_t *elem_destroy(elem_t *elem, elem_destroy_cb *cb)
{
    elem_t *next = NULL;

    if (elem) {
        next = elem->next;
        if (cb) {
            cb(elem->data);
        }
        free(elem);
    }
    return next;
}


/* --- construction / destruction */


list_t *list_new(elem_destroy_cb *cb)
{
    list_t *list;

    list = (list_t*)calloc(1, sizeof(list_t));
    list->free_data = cb;
    return list;
}

void list_destroy(list_t *list)
{
    list_flush(list);
    if (list) {
        free(list);
    }
}

void list_flush(list_t *list)
{
    elem_t *elem;

    if (list) {
        elem = list->first;
        while (elem) {
            elem = elem_destroy(elem, list->free_data);
        }
        list->first = list->last = NULL;
        list->cnt = 0;
    }
}


/* --- list elements --- */


void *element_data(elem_t *elem)
{
    if (elem) {
        return elem->data;
    }
    return NULL;
}


/* --- getters & setters --- */


elem_t *list_append(list_t *list, void *data)
{
    elem_t *elem = NULL;

    if (list) {
        elem = elem_construct(data);
        if (elem) {
            if (list->cnt == 0) {
                list->first = list->last = elem;
            } else {
                list->last->next = elem;
                elem->prev = list->last;
                list->last = elem;
            }
            list->cnt++;
        }
    }
    return elem;
}

int list_size(list_t *list)
{
    if (list) {
        return list->cnt;
    }
    return 0;
}

elem_t *list_get(list_t *list, int idx)
{
    elem_t *elem = NULL;

    if (list) {
        if ((idx >= 0) && (idx < list->cnt)) {
            elem = list->first;
            while (idx) {
                elem = elem->next;
                idx--;
            }
        }
    }
    return elem;
}

elem_t *list_first(list_t *list)
{
    if (list) {
        list->it = list->first;
        return list->it;
    }
    return NULL;
}

elem_t *list_next(list_t *list)
{
    if (list) {
        if (list->it) {
            list->it = list->it->next;
        }
        return list->it;
    }
    return NULL;
}


/* --- iteration */