~dexter/parrot-pkg/maverick

« back to all changes in this revision

Viewing changes to src/list.c

  • Committer: Piotr Roszatycki
  • Date: 2011-01-11 14:34:28 UTC
  • mfrom: (1.1.13 upstream)
  • Revision ID: piotr.roszatycki@gmail.com-20110111143428-s7pa7qz38m61o4tw
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright (C) 2010, Parrot Foundation.
 
3
 
 
4
=head1 NAME
 
5
 
 
6
src/list.c - Implementation of double linked lists.
 
7
 
 
8
=head1 DESCRIPTION
 
9
 
 
10
This code implements double linked list of GCable objects.
 
11
 
 
12
=cut
 
13
 
 
14
*/
 
15
 
 
16
#include "parrot/parrot.h"
 
17
#include "parrot/list.h"
 
18
 
 
19
/* HEADERIZER HFILE: include/parrot/list.h */
 
20
 
 
21
/* HEADERIZER BEGIN: static */
 
22
/* Don't modify between HEADERIZER BEGIN / HEADERIZER END.  Your changes will be lost. */
 
23
 
 
24
/* Don't modify between HEADERIZER BEGIN / HEADERIZER END.  Your changes will be lost. */
 
25
/* HEADERIZER END: static */
 
26
 
 
27
/*
 
28
 
 
29
=over 4
 
30
 
 
31
=item C<struct Linked_List* Parrot_list_new(PARROT_INTERP)>
 
32
 
 
33
Allocate a doubly link list
 
34
 
 
35
=cut
 
36
 
 
37
*/
 
38
 
 
39
PARROT_EXPORT
 
40
PARROT_CANNOT_RETURN_NULL
 
41
struct Linked_List*
 
42
Parrot_list_new(SHIM_INTERP)
 
43
{
 
44
    ASSERT_ARGS(Parrot_list_new)
 
45
 
 
46
    Linked_List * const res = (Linked_List*)mem_sys_allocate_zeroed(sizeof (Linked_List));
 
47
    return res;
 
48
}
 
49
 
 
50
/*
 
51
 
 
52
=item C<void Parrot_list_destroy(PARROT_INTERP, Linked_List* list)>
 
53
 
 
54
Destroy the specified list (free up memory associated with the list)
 
55
 
 
56
=cut
 
57
 
 
58
*/
 
59
 
 
60
PARROT_EXPORT
 
61
void
 
62
Parrot_list_destroy(SHIM_INTERP, ARGMOD(Linked_List* list))
 
63
{
 
64
    ASSERT_ARGS(Parrot_list_destroy)
 
65
 
 
66
    mem_sys_free(list);
 
67
}
 
68
 
 
69
/*
 
70
 
 
71
=item C<void Parrot_list_append(PARROT_INTERP, Linked_List *list,
 
72
List_Item_Header *item)>
 
73
 
 
74
Append an item to the list
 
75
 
 
76
=cut
 
77
 
 
78
*/
 
79
 
 
80
PARROT_EXPORT
 
81
void
 
82
Parrot_list_append(SHIM_INTERP, ARGMOD(Linked_List *list), ARGMOD(List_Item_Header *item))
 
83
{
 
84
    ASSERT_ARGS(Parrot_list_append)
 
85
 
 
86
    item->prev = item->next = NULL;
 
87
 
 
88
    if (list->last) {
 
89
        item->prev = list->last;
 
90
        list->last->next = item;
 
91
    }
 
92
 
 
93
    list->last = item;
 
94
 
 
95
    if (!list->first)
 
96
        list->first = item;
 
97
 
 
98
    list->count++;
 
99
#ifndef NDEBUG
 
100
    item->owner = list;
 
101
#endif
 
102
}
 
103
 
 
104
/*
 
105
 
 
106
=item C<List_Item_Header* Parrot_list_remove(PARROT_INTERP, Linked_List *list,
 
107
List_Item_Header *item)>
 
108
 
 
109
Remove an item from the list, returning the (pointer to) item
 
110
 
 
111
=cut
 
112
 
 
113
*/
 
114
 
 
115
PARROT_EXPORT
 
116
PARROT_CAN_RETURN_NULL
 
117
List_Item_Header*
 
118
Parrot_list_remove(SHIM_INTERP, ARGMOD(Linked_List *list), ARGMOD(List_Item_Header *item))
 
119
{
 
120
    ASSERT_ARGS(Parrot_list_remove)
 
121
 
 
122
    List_Item_Header * const next = item->next;
 
123
    List_Item_Header * const prev = item->prev;
 
124
 
 
125
    PARROT_ASSERT(list == item->owner);
 
126
 
 
127
    /* First item */
 
128
    if (list->first == item)
 
129
        list->first = next;
 
130
 
 
131
    if (list->last == item)
 
132
        list->last = prev;
 
133
 
 
134
    if (prev)
 
135
        prev->next = next;
 
136
    if (next)
 
137
        next->prev = prev;
 
138
 
 
139
    list->count--;
 
140
    return item;
 
141
}
 
142
 
 
143
/*
 
144
 
 
145
=item C<List_Item_Header* Parrot_list_pop(PARROT_INTERP, Linked_List *list)>
 
146
 
 
147
Pop an item off the list - i.e. get the first item in the list and remove it.
 
148
 
 
149
=cut
 
150
 
 
151
*/
 
152
 
 
153
PARROT_EXPORT
 
154
PARROT_CAN_RETURN_NULL
 
155
List_Item_Header*
 
156
Parrot_list_pop(SHIM_INTERP, ARGIN(Linked_List *list))
 
157
{
 
158
    ASSERT_ARGS(Parrot_list_pop)
 
159
 
 
160
    List_Item_Header * const ret = list->first;
 
161
    if (ret)
 
162
        LIST_REMOVE(list, ret);
 
163
    return ret;
 
164
}
 
165
 
 
166
/*
 
167
 
 
168
=item C<INTVAL Parrot_list_check(PARROT_INTERP, const Linked_List *list)>
 
169
 
 
170
Check the validity of the list
 
171
 
 
172
=cut
 
173
 
 
174
*/
 
175
 
 
176
PARROT_EXPORT
 
177
PARROT_CONST_FUNCTION
 
178
INTVAL
 
179
Parrot_list_check(SHIM_INTERP, ARGIN(const Linked_List *list))
 
180
{
 
181
    ASSERT_ARGS(Parrot_list_check)
 
182
 
 
183
    const List_Item_Header *tmp = list->first;
 
184
    size_t counter = 0;
 
185
 
 
186
    while (tmp) {
 
187
        List_Item_Header *next = tmp->next;
 
188
        PARROT_ASSERT(tmp->owner == list);
 
189
        tmp = next;
 
190
        ++counter;
 
191
        PARROT_ASSERT(counter <= list->count);
 
192
    }
 
193
 
 
194
    return 1;
 
195
}
 
196
 
 
197
/*
 
198
 
 
199
=item C<INTVAL Parrot_list_contains(PARROT_INTERP, const Linked_List *list,
 
200
const List_Item_Header *item)>
 
201
 
 
202
Returns True if the is in the list
 
203
 
 
204
=cut
 
205
 
 
206
*/
 
207
 
 
208
PARROT_EXPORT
 
209
PARROT_PURE_FUNCTION
 
210
INTVAL
 
211
Parrot_list_contains(SHIM_INTERP,
 
212
                     ARGIN(const Linked_List *list), ARGIN(const List_Item_Header *item))
 
213
{
 
214
    ASSERT_ARGS(Parrot_list_contains)
 
215
 
 
216
    const List_Item_Header *tmp = list->first;
 
217
 
 
218
#ifndef NDEBUG
 
219
    if (item->owner != list)
 
220
        return 0;
 
221
#endif
 
222
 
 
223
    while (tmp) {
 
224
        if (tmp == item)
 
225
            return 1;
 
226
        tmp = tmp->next;
 
227
    }
 
228
 
 
229
    return 0;
 
230
}
 
231
 
 
232
/*
 
233
 
 
234
=back
 
235
 
 
236
=cut
 
237
 
 
238
*/
 
239
 
 
240
/*
 
241
 * Local variables:
 
242
 *   c-file-style: "parrot"
 
243
 * End:
 
244
 * vim: expandtab shiftwidth=4 cinoptions='\:2=2' :
 
245
 */