File: list.cpp

package info (click to toggle)
faust 2.81.10%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 431,496 kB
  • sloc: cpp: 283,941; ansic: 116,215; javascript: 18,529; sh: 14,356; vhdl: 14,052; java: 5,900; python: 5,091; objc: 3,852; makefile: 2,725; cs: 1,672; lisp: 1,146; ruby: 954; yacc: 586; xml: 471; lex: 247; awk: 111; tcl: 26
file content (580 lines) | stat: -rw-r--r-- 14,832 bytes parent folder | download | duplicates (2)
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
/************************************************************************
 ************************************************************************
    FAUST compiler
    Copyright (C) 2003-2018 GRAME, Centre National de Creation Musicale
    ---------------------------------------------------------------------
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation; either version 2.1 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 ************************************************************************
 ************************************************************************/

/*****************************************************************************
******************************************************************************
                                LIST
                        Y. Orlarey, (c) Grame 2002
------------------------------------------------------------------------------
This file contains several extensions to the tree library :
    - lists : based on a operations like cons, hd , tl, ...
    - environments : list of associations (key value)
    - property list : used to annotate trees


 API:
 ----

    List :
    -----

    nil                 = predefined empty list
    cons(x,l)           = create a new list of head x and tail l
    hd(cons(x,l))       = x,
    tl(cons(x,l))       = l
    nth(l,i)            = ith element of l (or nil)
    replace(l,i,e)      = a copy of l where the ith element is e
    len(l)              = number of elements of l
    isNil(nil)          = true (false otherwise)
    isList(cons(x,l))   = true (false otherwise)
    list(a,b,..)        = cons(a, list(b,...))

    lmap(f, cons(x,l))      = cons(f(x), lmap(f,l))
    reverse([a,b,..,z])     = [z,..,b,a]
    reverseall([a,b,..,z])  = [ra(z),..,ra(b),ra(a)] where ra is reverseall

    Set :
    -----
    (Sets are implemented as ordered lists of elements without duplication)

    isElement(e,s)          = true if e is an element of set s, false otherwise
    addElement(e,s)         = s U {e}
    remElement(e,s)         = s - {e}
    singleton(e)            = {e}
    list2set(l)             = convert a list into a set
    setUnion(s1,s2)         = s1 U s2
    setIntersection(s1,s2)  = s1 intersection s2
    setDifference(s1,s2)    = s1 - s2

    Environment :
    -------------

    An 'environment' is a stack of pairs (key x value) used to keep track of lexical bindings

    pushEnv (key, val, env) -> env' create a new environment
    searchEnv (key,&v,env) -> bool  search for key in env and set v accordingly

    search(k1,&v, push(k2,x,env))  = true and v is set to x if k1 == k2
                                   = search(k1,&v,env) if k1 != k2
    Property list :
    ---------------

    Every tree can be annotated with an 'attribut' field. This attribute field
    can be used to manage a property list (pl). A property list is a list of pairs
    key x value, with three basic operations :

    setProperty(t, key, val) -> t  add the association (key x val) to the pl of t
    getProperty(t, key, &val) ->   bool search the pp of t for the value associated to key
    remProperty(t, key) -> t       remove any association (key x ?) from the pl of t

 Warning :
 ---------
 Since reference counters are used for garbage collecting, one must be careful not to
 create cycles in trees. The only possible source of cycles is by setting the attribut
 of a tree t to a tree t' that contains t as a subtree.

 History :
 ---------
    2002-02-08 : First version
    2002-02-20 : New description of the API, non recursive lmap and reverse
    2002-03-29 : Added function remElement(e,set), corrected comment error

******************************************************************************
*****************************************************************************/

#include "list.hh"
#include <stdlib.h>
#include <cstdlib>
#include <map>
#include "compatibility.hh"
#include "global.hh"
#include "property.hh"

using namespace std;

Tree cons(Tree a, Tree b)
{
    return tree(gGlobal->CONS, a, b);
}
Tree list0()
{
    return gGlobal->nil;
}

LIBFAUST_API bool isNil(Tree l)
{
    return (l->node() == Node(gGlobal->NIL)) && (l->arity() == 0);
}
bool isList(Tree l)
{
    return (l->node() == Node(gGlobal->CONS)) && (l->arity() == 2);
}

//------------------------------------------------------------------------------
// Printing of trees with special case for lists
//------------------------------------------------------------------------------

static bool printlist(Tree l, FILE* out)
{
    if (isList(l)) {
        char sep = '(';

        do {
            fputc(sep, out);
            sep = ',';
            print(hd(l));
            l = tl(l);
        } while (isList(l));

        if (!isNil(l)) {
            fprintf(out, " . ");
            print(l, out);
        }

        fputc(')', out);
        return true;

    } else if (isNil(l)) {
        fprintf(out, "nil");
        return true;

    } else {
        return false;
    }
}

void print(Tree t, FILE* out)
{
    int    i;
    double f;
    Sym    s;
    void*  p;

    if (printlist(t, out)) {
        return;
    }

    Node n = t->node();
    if (isInt(n, &i)) {
        fprintf(out, "%d", i);
    } else if (isDouble(n, &f)) {
        fprintf(out, "%f", f);
    } else if (isSym(n, &s)) {
        fprintf(out, "%s", name(s));
    } else if (isPointer(n, &p)) {
        fprintf(out, "#%p", p);
    }

    int k = t->arity();
    if (k > 0) {
        char sep = '[';
        for (int i1 = 0; i1 < k; i1++) {
            fputc(sep, out);
            sep = ',';
            print(t->branch(i1), out);
        }
        fputc(']', out);
    }
}

//------------------------------------------------------------------------------
// Elements of list
//------------------------------------------------------------------------------

Tree nth(Tree l, int i)
{
    while (isList(l)) {
        if (i == 0) {
            return hd(l);
        }
        l = tl(l);
        i--;
    }
    return gGlobal->nil;
}

Tree replace(Tree l, int i, Tree e)
{
    return (i == 0) ? cons(e, tl(l)) : cons(hd(l), replace(tl(l), i - 1, e));
}

int len(Tree l)
{
    int n = 0;
    while (isList(l)) {
        l = tl(l);
        n++;
    }
    return n;
}

//------------------------------------------------------------------------------
// Mapping and reversing
//------------------------------------------------------------------------------

Tree rconcat(Tree l, Tree q)
{
    while (isList(l)) {
        q = cons(hd(l), q);
        l = tl(l);
    }
    return q;
}

Tree concat(Tree l, Tree q)
{
    return rconcat(reverse(l), q);
}

Tree lrange(Tree l, int i, int j)
{
    Tree r = gGlobal->nil;
    int  c = j;
    while (c > i) {
        r = cons(nth(l, --c), r);
    }
    return r;
}

//------------------------------------------------------------------------------
// Mapping and reversing
//------------------------------------------------------------------------------

static Tree rmap(tfun f, Tree l)
{
    Tree r = gGlobal->nil;
    while (isList(l)) {
        r = cons(f(hd(l)), r);
        l = tl(l);
    }
    return r;
}

Tree reverse(Tree l)
{
    Tree r = gGlobal->nil;
    while (isList(l)) {
        r = cons(hd(l), r);
        l = tl(l);
    }
    return r;
}

Tree lmap(tfun f, Tree l)
{
    return reverse(rmap(f, l));
}

Tree reverseall(Tree l)
{
    return isList(l) ? rmap(reverseall, l) : l;
}

//------------------------------------------------------------------------------
// Sets : implemented as ordered list
//------------------------------------------------------------------------------

bool isElement(Tree e, Tree l)
{
    while (isList(l)) {
        if (hd(l) == e) {
            return true;
        }
        if (hd(l)->serial() > e->serial()) {
            return false;
        }
        l = tl(l);
    }
    return false;
}

Tree addElement(Tree e, Tree l)
{
    if (isList(l)) {
        if (e->serial() < hd(l)->serial()) {
            return cons(e, l);
        } else if (e == hd(l)) {
            return l;
        } else {
            return cons(hd(l), addElement(e, tl(l)));
        }
    } else {
        return cons(e, gGlobal->nil);
    }
}

Tree remElement(Tree e, Tree l)
{
    if (isList(l)) {
        if (e->serial() < hd(l)->serial()) {
            return l;
        } else if (e == hd(l)) {
            return tl(l);
        } else {
            return cons(hd(l), remElement(e, tl(l)));
        }
    } else {
        return gGlobal->nil;
    }
}

Tree singleton(Tree e)
{
    return list1(e);
}

Tree list2set(Tree l)
{
    Tree s = gGlobal->nil;
    while (isList(l)) {
        s = addElement(hd(l), s);
        l = tl(l);
    }
    return s;
}

Tree setUnion(Tree A, Tree B)
{
    if (isNil(A)) {
        return B;
    }
    if (isNil(B)) {
        return A;
    }

    if (hd(A) == hd(B)) {
        return cons(hd(A), setUnion(tl(A), tl(B)));
    }
    if (hd(A)->serial() < hd(B)->serial()) {
        return cons(hd(A), setUnion(tl(A), B));
    }
    /* hd(A) > hd(B) */ return cons(hd(B), setUnion(A, tl(B)));
}

Tree setIntersection(Tree A, Tree B)
{
    if (isNil(A)) {
        return A;
    }
    if (isNil(B)) {
        return B;
    }
    if (hd(A) == hd(B)) {
        return cons(hd(A), setIntersection(tl(A), tl(B)));
    }
    if (hd(A)->serial() < hd(B)->serial()) {
        return setIntersection(tl(A), B);
    }
    /* (hd(A) > hd(B)*/ return setIntersection(A, tl(B));
}

Tree setDifference(Tree A, Tree B)
{
    if (isNil(A)) {
        return A;
    }
    if (isNil(B)) {
        return A;
    }
    if (hd(A) == hd(B)) {
        return setDifference(tl(A), tl(B));
    }
    if (hd(A)->serial() < hd(B)->serial()) {
        return cons(hd(A), setDifference(tl(A), B));
    }
    /* (hd(A) > hd(B)*/ return setDifference(A, tl(B));
}

//------------------------------------------------------------------------------
// Environments
//------------------------------------------------------------------------------

Tree pushEnv(Tree key, Tree val, Tree env)
{
    return cons(cons(key, val), env);
}

bool searchEnv(Tree key, Tree& v, Tree env)
{
    while (isList(env)) {
        if (hd(hd(env)) == key) {
            v = tl(hd(env));
            return true;
        }
        env = tl(env);
    }
    return false;
}

#if 0

//------------------------------------------------------------------------------
// Property list
//------------------------------------------------------------------------------

static bool findKey (Tree pl, Tree key, Tree& val)
{
    if (isNil(pl))                return false;
    if (left(hd(pl)) == key)      { val = right(hd(pl)); return true; }
    /*  left(hd(pl)) != key    */ return findKey(tl(pl), key, val);
}

static Tree updateKey (Tree pl, Tree key, Tree val)
{
    if (isNil(pl))                return cons(cons(key,val), gGlobal->nil);
    if (left(hd(pl)) == key)      return cons(cons(key,val), tl(pl));
    /*  left(hd(pl)) != key    */ return cons(hd(pl), updateKey(tl(pl), key, val));
}

static Tree removeKey(Tree pl, Tree key)
{
    if (isNil(pl))                return gGlobal->nil;
    if (left(hd(pl)) == key)      return tl(pl);
    /*  left(hd(pl)) != key    */ return cons(hd(pl), removeKey(tl(pl), key));
}

#endif

#if 0
void setProperty(Tree t, Tree key, Tree val)
{
    Tree pl = t->attribut();
    if (pl) t->attribut(updateKey(pl, key, val));
    else t->attribut(updateKey(gGlobal->nil, key, val));
}

void remProperty(Tree t, Tree key)
{
    Tree pl = t->attribut();
    if (pl) t->attribut(removeKey(pl, key));
}

bool getProperty(Tree t, Tree key, Tree& val)
{
    Tree pl = t->attribut();
    if (pl) return findKey(pl, key, val);
    else return false;
}

#else
// new implementation
void setProperty(Tree t, Tree key, Tree val)
{
    t->setProperty(key, val);
}

bool getProperty(Tree t, Tree key, Tree& val)
{
    Tree pl = t->getProperty(key);
    if (pl) {
        val = pl;
        return true;
    } else {
        return false;
    }
}

void remProperty(Tree t, Tree key)
{
    cerr << "ASSERT : remProperty not implemented\n";
    faustassert(false);
}
#endif

//------------------------------------------------------------------------------
// Bottom Up Tree Mapping
//------------------------------------------------------------------------------

Tree tmap(Tree key, tfun f, Tree t)
{
    // printf("start tmap\n");
    Tree p;

    if (getProperty(t, key, p)) {
        return (isNil(p)) ? t : p;  // truc pour eviter les boucles

    } else {
        tvec br;
        int  n = t->arity();
        for (int i = 0; i < n; i++) {
            br.push_back(tmap(key, f, t->branch(i)));
        }

        Tree r1 = tree(t->node(), br);

        Tree r2 = f(r1);
        if (r2 == t) {
            setProperty(t, key, gGlobal->nil);
        } else {
            setProperty(t, key, r2);
        }
        return r2;
    }
}

//------------------------------------------------------------------------------
// substitute: replaces all occurrences of 'id' with 'val' in 't'
//------------------------------------------------------------------------------

// generates a unique key specific to this substitution
static Tree substkey(Tree t, Tree id, Tree val)
{
    char name[256];
    snprintf(name, 255, "SUBST<%p,%p,%p> : ", t, id, val);
    return tree(unique(name));
}

// performs the actual substitution while updating the property
// to avoid having to calculate it twice
static Tree subst(Tree t, Tree propkey, Tree id, Tree val)
{
    Tree p;

    if (t == id) {
        return val;

    } else if (t->arity() == 0) {
        return t;
    } else if (getProperty(t, propkey, p)) {
        return (isNil(p)) ? t : p;
    } else {
        tvec br;
        int  n = t->arity();
        for (int i = 0; i < n; i++) {
            br.push_back(subst(t->branch(i), propkey, id, val));
        }

        Tree r = tree(t->node(), br);

        if (r == t) {
            setProperty(t, propkey, gGlobal->nil);
        } else {
            setProperty(t, propkey, r);
        }
        return r;
    }
}

// remplace all  occurences of 'id' with 'val' in 't'
Tree substitute(Tree t, Tree id, Tree val)
{
    return subst(t, substkey(t, id, val), id, val);
}