File: ptrlist.c

package info (click to toggle)
notion 4.0.2%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 4,676 kB
  • sloc: ansic: 47,508; sh: 2,096; makefile: 603; perl: 270
file content (208 lines) | stat: -rw-r--r-- 3,352 bytes parent folder | download | duplicates (4)
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
/*
 * libtu/ptrlist.c
 *
 * Copyright (c) Tuomo Valkonen 1999-2005.
 *
 * You may distribute and modify this library under the terms of either
 * the Clarified Artistic License or the GNU LGPL, version 2.1 or later.
 */

#include "obj.h"
#include "ptrlist.h"
#include "types.h"
#include "dlist.h"
#include "misc.h"


static void free_node(PtrList **ptrlist, PtrList *node)
{
    UNLINK_ITEM(*ptrlist, node, next, prev);
    free(node);
}


static PtrList *mknode(void *ptr)
{
    PtrList *node;

    if(ptr==NULL)
        return NULL;

    node=ALLOC(PtrList);

    if(node==NULL)
        return FALSE;

    node->ptr=ptr;

    return node;
}


static PtrList *ptrlist_find_node(PtrList *ptrlist, void *ptr)
{
    PtrList *node=ptrlist;

    while(node!=NULL){
        if(node->ptr==ptr)
            break;
        node=node->next;
    }

    return node;
}


bool ptrlist_contains(PtrList *ptrlist, void *ptr)
{
    return (ptrlist_find_node(ptrlist, ptr)!=NULL);
}


bool ptrlist_insert_last(PtrList **ptrlist, void *ptr)
{
    PtrList *node=mknode(ptr);

    if(node==NULL)
        return FALSE;

    LINK_ITEM_LAST(*ptrlist, node, next, prev);

    return TRUE;
}


bool ptrlist_insert_first(PtrList **ptrlist, void *ptr)
{
    PtrList *node=mknode(ptr);

    if(node==NULL)
        return FALSE;

    LINK_ITEM_FIRST(*ptrlist, node, next, prev);

    return TRUE;
}


bool ptrlist_reinsert_last(PtrList **ptrlist, void *ptr)
{
    PtrList *node=ptrlist_find_node(*ptrlist, ptr);

    if(node==NULL)
        return ptrlist_insert_last(ptrlist, ptr);

    UNLINK_ITEM(*ptrlist, node, next, prev);
    LINK_ITEM_LAST(*ptrlist, node, next, prev);

    return TRUE;
}


bool ptrlist_reinsert_first(PtrList **ptrlist, void *ptr)
{
    PtrList *node=ptrlist_find_node(*ptrlist, ptr);

    if(node==NULL)
        return ptrlist_insert_first(ptrlist, ptr);

    UNLINK_ITEM(*ptrlist, node, next, prev);
    LINK_ITEM_FIRST(*ptrlist, node, next, prev);

    return TRUE;
}


bool ptrlist_remove(PtrList **ptrlist, void *ptr)
{
    PtrList *node=ptrlist_find_node(*ptrlist, ptr);

    if(node!=NULL)
        free_node(ptrlist, node);

    return (node!=NULL);
}


void ptrlist_clear(PtrList **ptrlist)
{
    while(*ptrlist!=NULL)
        free_node(ptrlist, *ptrlist);
}


PtrListIterTmp ptrlist_iter_tmp=NULL;


void ptrlist_iter_init(PtrListIterTmp *state, PtrList *ptrlist)
{
    *state=ptrlist;
}


void *ptrlist_iter(PtrListIterTmp *state)
{
    void *ptr=NULL;

    if(*state!=NULL){
        ptr=(*state)->ptr;
        (*state)=(*state)->next;
    }

    return ptr;
}


void ptrlist_iter_rev_init(PtrListIterTmp *state, PtrList *ptrlist)
{
    *state=(ptrlist==NULL ? NULL : ptrlist->prev);
}


void *ptrlist_iter_rev(PtrListIterTmp *state)
{
    void *ptr=NULL;

    if(*state!=NULL){
        ptr=(*state)->ptr;
        *state=(*state)->prev;
        if((*state)->next==NULL)
            *state=NULL;
    }

    return ptr;
}


void *ptrlist_take_first(PtrList **ptrlist)
{
    PtrList *node=*ptrlist;
    void *ptr;

    if(node==NULL)
        return NULL;

    ptr=node->ptr;

    free_node(ptrlist, node);

    return ptr;
}


void *ptrlist_take_last(PtrList **ptrlist)
{
    PtrList *node=*ptrlist;
    void *ptr;

    if(node==NULL)
        return NULL;

    node=node->prev;

    ptr=node->ptr;

    free_node(ptrlist, node);

    return ptr;
}