summaryrefslogtreecommitdiff
path: root/sources/scalac/symtab/SymSet.java
blob: f72b9764f5e12f3e54a5498559834837ce0111ac (plain) (blame)
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
/*     ____ ____  ____ ____  ______                                     *\
**    / __// __ \/ __// __ \/ ____/    SOcos COmpiles Scala             **
**  __\_ \/ /_/ / /__/ /_/ /\_ \       (c) 2002, LAMP/EPFL              **
** /_____/\____/\___/\____/____/                                        **
\*                                                                      */

// $Id$

package scalac.symtab;

/** Sets of symbols, implemented as binary trees.
 */
public class SymSet {
    private SymSet l, r;
    private Symbol sym;

    public SymSet() {}

    private SymSet(Symbol sym, SymSet l, SymSet r) {
	this.sym = sym; this.l = l; this.r = r;
    }

    /** Union of this set and `{sym}'.
     */
    public SymSet incl(Symbol sym) {
	if (this == EMPTY) {
	    return new SymSet(sym, EMPTY, EMPTY);
	} else if (sym == this.sym) {
	    return this;
	} else if (sym.isLess(this.sym)) {
	    return new SymSet(this.sym, l.incl(sym), r);
	} else {
	    assert this.sym.isLess(sym);
	    return new SymSet(this.sym, l, r.incl(sym));
	}
    }

    /** Is `sym' an element of this set?
     */
    public boolean contains(Symbol sym) {
	if (this == EMPTY) {
	    return false;
	} else if (sym == this.sym) {
	    return true;
	} else if (sym.isLess(this.sym)) {
	    return l.contains(sym);
	} else {
	    assert this.sym.isLess(sym);
	    return r.contains(sym);
	}
    }

    /** The number of elements in ths set.
     */
    public int size() {
	if (this == EMPTY) {
	    return 0;
	} else {
	    return 1 + l.size() + r.size();
	}
    }

    /** Copy elements of this set into `ss', starting at index `from'.
     *  Return index one past last element copied.
     */
    public int copyToArray(Symbol[] ss, int from) {
	if (this == EMPTY) {
	    return from;
	} else {
	    from = l.copyToArray(ss, from);
	    ss[from] = sym;
	    return r.copyToArray(ss, from + 1);
	}
    }

    /** Return all elements of this set as an array.
     */
    public Symbol[] toArray() {
	int s = size();
	if (s == 0) return Symbol.EMPTY_ARRAY;
	Symbol[] ss = new Symbol[s];
	copyToArray(ss, 0);
	return ss;
    }

    /** The empty set.
     */
    public final static SymSet EMPTY = new SymSet();
}