OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [insight/] [tix/] [generic/] [tixList.c] - Blame information for rev 1781

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 578 markom
/*
2
 * tixList.c --
3
 *
4
 *      Implements easy-to-use link lists.
5
 *
6
 * Copyright (c) 1996, Expert Interface Technologies
7
 *
8
 * See the file "license.terms" for information on usage and redistribution
9
 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
10
 *
11
 */
12
#include <tixPort.h>
13
#include <tixInt.h>
14
 
15
#define NEXT(info,ptr) (char*)(*(char**)((ptr+(info->nextOffset))))
16
#define PREV(info,ptr) (char*)(*(char**)((ptr+(info->prevOffset))))
17
 
18
static void             SetNext _ANSI_ARGS_((Tix_ListInfo * info,
19
                            char * ptr, char * next));
20
 
21
static void SetNext(info, ptr, next)
22
    Tix_ListInfo * info;
23
    char * ptr;
24
    char * next;
25
{
26
    char ** next_ptr = (char**)((ptr+(info->nextOffset)));
27
    * next_ptr = next;
28
}
29
 
30
void Tix_LinkListInit(lPtr)
31
    Tix_LinkList * lPtr;
32
{
33
    lPtr->numItems = 0;
34
    lPtr->head   = (char*)NULL;
35
    lPtr->tail   = (char*)NULL;
36
}
37
 
38
 
39
void
40
Tix_LinkListAppend(infoPtr, lPtr, itemPtr, flags)
41
    Tix_ListInfo * infoPtr;
42
    Tix_LinkList * lPtr;
43
    char * itemPtr;
44
    int flags;
45
{
46
    char * ptr;
47
 
48
    if (flags | TIX_UNIQUE) {
49
        /* Check for uniqueness */
50
        for (ptr=lPtr->head;
51
             ptr!=NULL;
52
             ptr=NEXT(infoPtr,ptr)) {
53
            if (ptr == itemPtr) {
54
                return;
55
            }
56
        }
57
    }
58
    if (lPtr->head == NULL) {
59
        lPtr->head = lPtr->tail = itemPtr;
60
    } else {
61
        SetNext(infoPtr, lPtr->tail, itemPtr);
62
        lPtr->tail = itemPtr;
63
    }
64
 
65
    SetNext(infoPtr, itemPtr, NULL);
66
    ++ lPtr->numItems;
67
}
68
 
69
void Tix_LinkListIteratorInit(liPtr)
70
    Tix_ListIterator * liPtr;
71
{
72
    liPtr->started = 0;
73
}
74
 
75
void Tix_LinkListStart(infoPtr, lPtr, liPtr)
76
    Tix_ListInfo * infoPtr;
77
    Tix_LinkList * lPtr;
78
    Tix_ListIterator * liPtr;
79
{
80
    if (lPtr->head == NULL) {
81
        liPtr->last = NULL;
82
        liPtr->curr = NULL;
83
    } else {
84
        liPtr->last = liPtr->curr = lPtr->head;
85
    }
86
    liPtr->deleted = 0;
87
    liPtr->started = 1;
88
}
89
 
90
void Tix_LinkListNext(infoPtr, lPtr, liPtr)
91
    Tix_ListInfo * infoPtr;
92
    Tix_LinkList * lPtr;
93
    Tix_ListIterator * liPtr;
94
{
95
    if (liPtr->curr == NULL) {
96
        return;
97
    }
98
    if (liPtr->deleted == 1) {
99
        /* the curr pointer has already been adjusted */
100
        liPtr->deleted = 0;
101
        return;
102
    }
103
 
104
    liPtr->last = liPtr->curr;
105
    liPtr->curr = NEXT(infoPtr, liPtr->curr);
106
}
107
 
108
/*
109
 *----------------------------------------------------------------------
110
 * Tix_LinkListDelete --
111
 *
112
 *      Deletes an element from the linklist. The proper step of deleting
113
 *      an element is:
114
 *
115
 *      for (Tix_SimpleListStart(&list, &li); !Tix_SimpleListDone(&li);
116
 *               Tix_SimpleListNext (&list, &li)) {
117
 *          MyData * p = (MyData*)li.curr;
118
 *          if (someCondition) {
119
 *              Tix_SimpleListDelete(&cPtr->subWDefs, &li);
120
 *              ckfree((char*)p);
121
 *          }
122
 *      }
123
 *
124
 *      i.e., The pointer can be freed only after Tix_SimpleListDelete().
125
 *
126
 * Results:
127
 *      None.
128
 *
129
 * Side effects:
130
 *      The pointers in the list are adjusted  and the liPtr is advanced
131
 *      to the next element.
132
 *----------------------------------------------------------------------
133
 */
134
void
135
Tix_LinkListDelete(infoPtr, lPtr, liPtr)
136
    Tix_ListInfo * infoPtr;
137
    Tix_LinkList * lPtr;
138
    Tix_ListIterator * liPtr;
139
{
140
    if (liPtr->curr == NULL) {
141
        /* %% probably is a mistake */
142
        return;
143
    }
144
    if (liPtr->deleted == 1) {
145
        /* %% probably is a mistake */
146
        return;
147
    }
148
    if (lPtr->head == lPtr->tail) {
149
        lPtr->head  = lPtr->tail = NULL;
150
        liPtr->curr = NULL;
151
    }
152
    else if (lPtr->head == liPtr->curr) {
153
        lPtr->head  = NEXT(infoPtr, liPtr->curr);
154
        liPtr->curr = lPtr->head;
155
        liPtr->last = lPtr->head;
156
    }
157
    else if (lPtr->tail == liPtr->curr) {
158
        lPtr->tail = liPtr->last;
159
        SetNext(infoPtr, lPtr->tail, NULL);
160
        liPtr->curr = NULL;
161
    }
162
    else {
163
        SetNext(infoPtr, liPtr->last, NEXT(infoPtr, liPtr->curr));
164
        liPtr->curr = NEXT(infoPtr, liPtr->last);
165
    }
166
    -- lPtr->numItems;
167
 
168
    liPtr->deleted = 1;
169
}
170
 
171
/*----------------------------------------------------------------------
172
 *  Tix_LinkListInsert --
173
 *
174
 *      Insert the item at the position indicated by liPtr
175
 *----------------------------------------------------------------------
176
 */
177
void Tix_LinkListInsert(infoPtr, lPtr, itemPtr, liPtr)
178
    Tix_ListInfo * infoPtr;
179
    Tix_LinkList * lPtr;
180
    char * itemPtr;
181
    Tix_ListIterator * liPtr;
182
{
183
    if (lPtr->numItems == 0) {
184
        /* Just do an append
185
         */
186
        Tix_LinkListAppend(infoPtr, lPtr, itemPtr, 0);
187
 
188
        /* Fix the iterator (%% I am not sure if this is necessary)
189
         */
190
        liPtr->curr = liPtr->last = lPtr->head;
191
        return;
192
    }
193
 
194
    if (liPtr->curr == NULL) {
195
        /* %% probably is a mistake */
196
        return;
197
    }
198
    if (lPtr->head == lPtr->tail) {
199
        lPtr->head = itemPtr;
200
        SetNext(infoPtr, lPtr->head, lPtr->tail);
201
        liPtr->last = itemPtr;
202
        liPtr->curr = itemPtr;
203
    }
204
    else if (liPtr->curr == lPtr->head) {
205
        lPtr->head = itemPtr;
206
        SetNext(infoPtr, lPtr->head, liPtr->curr);
207
        liPtr->last = itemPtr;
208
        liPtr->curr = itemPtr;
209
    }
210
    else {
211
        SetNext(infoPtr, liPtr->last, itemPtr);
212
        SetNext(infoPtr, itemPtr,     liPtr->curr);
213
        liPtr->last = itemPtr;
214
    }
215
    ++ lPtr->numItems;
216
}
217
 
218
/*----------------------------------------------------------------------
219
 * Tix_LinkListFindAndDelete --
220
 *
221
 *      Find an element and delete it.
222
 *
223
 * liPtr:
224
 *      Can be NULL.
225
 *      If non-NULL, the search will start from the current entry indexed
226
 *      by liPtr;
227
 *
228
 * Return value:
229
 *      1 if element is found and deleted
230
 *      0 if element is not found
231
 *----------------------------------------------------------------------
232
 */
233
int Tix_LinkListFindAndDelete(infoPtr, lPtr, itemPtr, liPtr)
234
    Tix_ListInfo * infoPtr;
235
    Tix_LinkList * lPtr;
236
    char * itemPtr;
237
    Tix_ListIterator * liPtr;
238
{
239
    Tix_ListIterator defIterator;
240
 
241
    if (liPtr == NULL) {
242
        Tix_LinkListIteratorInit(&defIterator);
243
        liPtr = &defIterator;
244
    }
245
 
246
    if (!liPtr->started) {
247
        Tix_LinkListStart(infoPtr, lPtr, liPtr);
248
    }
249
 
250
    if (Tix_LinkListFind(infoPtr, lPtr, itemPtr, liPtr)) {
251
        Tix_LinkListDelete(infoPtr, lPtr, liPtr);
252
        return 1;
253
    } else {
254
        return 0;
255
    }
256
}
257
 
258
int Tix_LinkListDeleteRange(infoPtr, lPtr, fromPtr, toPtr, liPtr)
259
    Tix_ListInfo * infoPtr;
260
    Tix_LinkList * lPtr;
261
    char * fromPtr;
262
    char * toPtr;
263
    Tix_ListIterator * liPtr;
264
{
265
    Tix_ListIterator defIterator;
266
    int start = 0;
267
    int deleted = 0;
268
 
269
    if (liPtr == NULL) {
270
        Tix_LinkListIteratorInit(&defIterator);
271
        liPtr = &defIterator;
272
    }
273
    if (!liPtr->started) {
274
        Tix_LinkListStart(infoPtr, lPtr, liPtr);
275
    }
276
 
277
    for (;
278
         !Tix_LinkListDone(liPtr);
279
         Tix_LinkListNext (infoPtr, lPtr, liPtr)) {
280
 
281
        if (liPtr->curr == fromPtr) {
282
            start = 1;
283
        }
284
        if (start) {
285
            Tix_LinkListDelete(infoPtr, lPtr, liPtr);
286
            ++ deleted;
287
        }
288
        if (liPtr->curr == toPtr) {
289
            break;
290
        }
291
    }
292
 
293
    return deleted;
294
}
295
 
296
int Tix_LinkListFind(infoPtr, lPtr, itemPtr, liPtr)
297
    Tix_ListInfo * infoPtr;
298
    Tix_LinkList * lPtr;
299
    char * itemPtr;
300
    Tix_ListIterator * liPtr;
301
{
302
    if (!liPtr->started) {
303
        Tix_LinkListStart(infoPtr, lPtr, liPtr);
304
    }
305
 
306
    for (Tix_LinkListStart(infoPtr, lPtr, liPtr);
307
         !Tix_LinkListDone(liPtr);
308
         Tix_LinkListNext (infoPtr, lPtr, liPtr)) {
309
 
310
        if (liPtr->curr == itemPtr) {
311
            return 1;                           /* found! */
312
        }
313
    }
314
 
315
    return 0;                                    /* Can't find */
316
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.