-
Notifications
You must be signed in to change notification settings - Fork 138
/
list.c
245 lines (166 loc) · 4.12 KB
/
list.c
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
/*
Copyright (C) 2010, Parrot Foundation.
=head1 NAME
src/list.c - Implementation of double linked lists.
=head1 DESCRIPTION
This code implements double linked list of GCable objects.
=cut
*/
#include "parrot/parrot.h"
#include "parrot/list.h"
/* HEADERIZER HFILE: include/parrot/list.h */
/* HEADERIZER BEGIN: static */
/* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */
/* Don't modify between HEADERIZER BEGIN / HEADERIZER END. Your changes will be lost. */
/* HEADERIZER END: static */
/*
=over 4
=item C<struct Linked_List* Parrot_list_new(PARROT_INTERP)>
Allocate a doubly link list
=cut
*/
PARROT_EXPORT
PARROT_CANNOT_RETURN_NULL
struct Linked_List*
Parrot_list_new(SHIM_INTERP)
{
ASSERT_ARGS(Parrot_list_new)
Linked_List * const res = (Linked_List*)mem_sys_allocate_zeroed(sizeof (Linked_List));
return res;
}
/*
=item C<void Parrot_list_destroy(PARROT_INTERP, Linked_List* list)>
Destroy the specified list (free up memory associated with the list)
=cut
*/
PARROT_EXPORT
void
Parrot_list_destroy(SHIM_INTERP, ARGMOD(Linked_List* list))
{
ASSERT_ARGS(Parrot_list_destroy)
mem_sys_free(list);
}
/*
=item C<void Parrot_list_append(PARROT_INTERP, Linked_List *list,
List_Item_Header *item)>
Append an item to the list
=cut
*/
PARROT_EXPORT
void
Parrot_list_append(SHIM_INTERP, ARGMOD(Linked_List *list), ARGMOD(List_Item_Header *item))
{
ASSERT_ARGS(Parrot_list_append)
item->prev = item->next = NULL;
if (list->last) {
item->prev = list->last;
list->last->next = item;
}
list->last = item;
if (!list->first)
list->first = item;
list->count++;
#ifndef NDEBUG
item->owner = list;
#endif
}
/*
=item C<List_Item_Header* Parrot_list_remove(PARROT_INTERP, Linked_List *list,
List_Item_Header *item)>
Remove an item from the list, returning the (pointer to) item
=cut
*/
PARROT_EXPORT
PARROT_CAN_RETURN_NULL
List_Item_Header*
Parrot_list_remove(SHIM_INTERP, ARGMOD(Linked_List *list), ARGMOD(List_Item_Header *item))
{
ASSERT_ARGS(Parrot_list_remove)
List_Item_Header * const next = item->next;
List_Item_Header * const prev = item->prev;
PARROT_ASSERT(list == item->owner);
/* First item */
if (list->first == item)
list->first = next;
if (list->last == item)
list->last = prev;
if (prev)
prev->next = next;
if (next)
next->prev = prev;
list->count--;
return item;
}
/*
=item C<List_Item_Header* Parrot_list_pop(PARROT_INTERP, Linked_List *list)>
Pop an item off the list - i.e. get the first item in the list and remove it.
=cut
*/
PARROT_EXPORT
PARROT_CAN_RETURN_NULL
List_Item_Header*
Parrot_list_pop(SHIM_INTERP, ARGIN(Linked_List *list))
{
ASSERT_ARGS(Parrot_list_pop)
List_Item_Header * const ret = list->first;
if (ret)
LIST_REMOVE(list, ret);
return ret;
}
/*
=item C<INTVAL Parrot_list_check(PARROT_INTERP, const Linked_List *list)>
Check the validity of the list
=cut
*/
PARROT_EXPORT
PARROT_CONST_FUNCTION
INTVAL
Parrot_list_check(SHIM_INTERP, ARGIN(const Linked_List *list))
{
ASSERT_ARGS(Parrot_list_check)
const List_Item_Header *tmp = list->first;
size_t counter = 0;
while (tmp) {
List_Item_Header *next = tmp->next;
PARROT_ASSERT(tmp->owner == list);
tmp = next;
++counter;
PARROT_ASSERT(counter <= list->count);
}
return 1;
}
/*
=item C<INTVAL Parrot_list_contains(PARROT_INTERP, const Linked_List *list,
const List_Item_Header *item)>
Returns True if the is in the list
=cut
*/
PARROT_EXPORT
PARROT_PURE_FUNCTION
INTVAL
Parrot_list_contains(SHIM_INTERP,
ARGIN(const Linked_List *list), ARGIN(const List_Item_Header *item))
{
ASSERT_ARGS(Parrot_list_contains)
const List_Item_Header *tmp = list->first;
#ifndef NDEBUG
if (item->owner != list)
return 0;
#endif
while (tmp) {
if (tmp == item)
return 1;
tmp = tmp->next;
}
return 0;
}
/*
=back
=cut
*/
/*
* Local variables:
* c-file-style: "parrot"
* End:
* vim: expandtab shiftwidth=4 cinoptions='\:2=2' :
*/