diff options
author | Martin Odersky <odersky@gmail.com> | 2003-02-13 14:41:36 +0000 |
---|---|---|
committer | Martin Odersky <odersky@gmail.com> | 2003-02-13 14:41:36 +0000 |
commit | 4177daab2f54bdb20c71f623296a8bb32616fd12 (patch) | |
tree | 23f08b43f3758e825d5965b336030603a65bbcf7 /sources/scalac/symtab/SymSet.java | |
parent | 33d6e170c97ca7b2f991896a0729941a7240b6d6 (diff) | |
download | scala-4177daab2f54bdb20c71f623296a8bb32616fd12.tar.gz scala-4177daab2f54bdb20c71f623296a8bb32616fd12.tar.bz2 scala-4177daab2f54bdb20c71f623296a8bb32616fd12.zip |
Initial version.
Diffstat (limited to 'sources/scalac/symtab/SymSet.java')
-rw-r--r-- | sources/scalac/symtab/SymSet.java | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/sources/scalac/symtab/SymSet.java b/sources/scalac/symtab/SymSet.java new file mode 100644 index 0000000000..f72b9764f5 --- /dev/null +++ b/sources/scalac/symtab/SymSet.java @@ -0,0 +1,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(); +} |