summaryrefslogtreecommitdiff
path: root/jam-files/engine/lists.c
blob: ebabb63e9093047a6b80d8f81a7a860dad4305d8 (plain)
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
 * Copyright 1993, 1995 Christopher Seiwald.
 *
 * This file is part of Jam - see jam.c for Copyright information.
 */

# include "jam.h"
# include "newstr.h"
# include "lists.h"

/*
 * lists.c - maintain lists of strings
 *
 * This implementation essentially uses a singly linked list, but
 * guarantees that the head element of every list has a valid pointer
 * to the tail of the list, so the new elements can efficiently and
 * properly be appended to the end of a list.
 *
 * To avoid massive allocation, list_free() just tacks the whole freed
 * chain onto freelist and list_new() looks on freelist first for an
 * available list struct.  list_free() does not free the strings in the
 * chain: it lazily lets list_new() do so.
 *
 * 08/23/94 (seiwald) - new list_append()
 * 09/07/00 (seiwald) - documented lol_*() functions
 */

static LIST *freelist = 0;  /* junkpile for list_free() */

/*
 * list_append() - append a list onto another one, returning total
 */

LIST * list_append( LIST * l, LIST * nl )
{
    if ( !nl )
    {
        /* Just return l */
    }
    else if ( !l )
    {
        l = nl;
    }
    else
    {
        /* Graft two non-empty lists. */
        l->tail->next = nl;
        l->tail = nl->tail;
    }

    return l;
}

/*
 * list_new() - tack a string onto the end of a list of strings
 */

LIST * list_new( LIST * head, char * string )
{
    LIST * l;

    if ( DEBUG_LISTS )
        printf( "list > %s <\n", string );

    /* Get list struct from freelist, if one available.  */
    /* Otherwise allocate. */
    /* If from freelist, must free string first */

    if ( freelist )
    {
        l = freelist;
        freestr( l->string );
        freelist = freelist->next;
    }
    else
    {
        l = (LIST *)BJAM_MALLOC( sizeof( LIST ) );
    }

    /* If first on chain, head points here. */
    /* If adding to chain, tack us on. */
    /* Tail must point to this new, last element. */

    if ( !head ) head = l;
    else head->tail->next = l;
    head->tail = l;
    l->next = 0;

    l->string = string;

    return head;
}


/*
 * list_copy() - copy a whole list of strings (nl) onto end of another (l).
 */

LIST * list_copy( LIST * l, LIST * nl )
{
    for ( ; nl; nl = list_next( nl ) )
        l = list_new( l, copystr( nl->string ) );
    return l;
}


/*
 * list_sublist() - copy a subset of a list of strings.
 */

LIST * list_sublist( LIST * l, int start, int count )
{
    LIST * nl = 0;
    for ( ; l && start--; l = list_next( l ) );
    for ( ; l && count--; l = list_next( l ) )
        nl = list_new( nl, copystr( l->string ) );
    return nl;
}


static int str_ptr_compare( void const * va, void const * vb )
{
    char * a = *( (char * *)va );
    char * b = *( (char * *)vb );
    return strcmp(a, b);
}


LIST * list_sort( LIST * l )
{
    int len;
    int ii;
    char * * strings;
    LIST * listp;
    LIST * result = 0;

    if ( !l )
        return L0;

    len = list_length( l );
    strings = (char * *)BJAM_MALLOC( len * sizeof(char*) );

    listp = l;
    for ( ii = 0; ii < len; ++ii )
    {
        strings[ ii ] = listp->string;
        listp = listp->next;
    }

    qsort( strings, len, sizeof( char * ), str_ptr_compare );

    for ( ii = 0; ii < len; ++ii )
        result = list_append( result, list_new( 0, strings[ ii ] ) );

    BJAM_FREE( strings );

    return result;
}


/*
 * list_free() - free a list of strings
 */

void list_free( LIST * head )
{
    /* Just tack onto freelist. */
    if ( head )
    {
        head->tail->next = freelist;
        freelist = head;
    }
}


/*
 * list_pop_front() - remove the front element from a list of strings
 */

LIST * list_pop_front( LIST * l )
{
    LIST * result = l->next;
    if ( result )
    {
        result->tail = l->tail;
        l->next = L0;
        l->tail = l;
    }
    list_free( l );
    return result;
}


/*
 * list_print() - print a list of strings to stdout
 */

void list_print( LIST * l )
{
    LIST * p = 0;
    for ( ; l; p = l, l = list_next( l ) )
        if ( p )
            printf( "%s ", p->string );
    if ( p )
        printf( "%s", p->string );
}


/*
 * list_length() - return the number of items in the list
 */

int list_length( LIST * l )
{
    int n = 0;
    for ( ; l; l = list_next( l ), ++n );
    return n;
}


int list_in( LIST * l, char * value )
{
    for ( ; l; l = l->next )
        if ( strcmp( l->string, value ) == 0 )
            return 1;
    return 0;
}


LIST * list_unique( LIST * sorted_list )
{
    LIST * result = 0;
    LIST * last_added = 0;

    for ( ; sorted_list; sorted_list = sorted_list->next )
    {
        if ( !last_added || strcmp( sorted_list->string, last_added->string ) != 0 )
        {
            result = list_new( result, sorted_list->string );
            last_added = sorted_list;
        }
    }
    return result;
}


/*
 * lol_init() - initialize a LOL (list of lists).
 */

void lol_init( LOL * lol )
{
    lol->count = 0;
}


/*
 * lol_add() - append a LIST onto an LOL.
 */

void lol_add( LOL * lol, LIST * l )
{
    if ( lol->count < LOL_MAX )
        lol->list[ lol->count++ ] = l;
}


/*
 * lol_free() - free the LOL and its LISTs.
 */

void lol_free( LOL * lol )
{
    int i;
    for ( i = 0; i < lol->count; ++i )
        list_free( lol->list[ i ] );
    lol->count = 0;
}


/*
 * lol_get() - return one of the LISTs in the LOL.
 */

LIST * lol_get( LOL * lol, int i )
{
    return i < lol->count ? lol->list[ i ] : 0;
}


/*
 * lol_print() - debug print LISTS separated by ":".
 */

void lol_print( LOL * lol )
{
    int i;

    for ( i = 0; i < lol->count; ++i )
    {
        if ( i )
            printf( " : " );
        list_print( lol->list[ i ] );
    }
}

#ifdef HAVE_PYTHON

PyObject *list_to_python(LIST *l)
{
    PyObject *result = PyList_New(0);

    for (; l; l = l->next)
    {
        PyObject* s = PyString_FromString(l->string);
        PyList_Append(result, s);
        Py_DECREF(s);
    }

    return result;
}

LIST *list_from_python(PyObject *l)
{
    LIST * result = 0;

    Py_ssize_t i, n;
    n = PySequence_Size(l);
    for (i = 0; i < n; ++i)
    {
        PyObject *v = PySequence_GetItem(l, i);        
        result = list_new (result, newstr (PyString_AsString(v)));
        Py_DECREF(v);
    }

    return result;
}

#endif