v4: Simple use of generics to clean up local impl code

Specify @Override

[git-p4: depot-paths = "//depot/code/antlr4/main/": change = 9332]
This commit is contained in:
sharwell 2011-11-17 16:00:40 -08:00
parent ddf4cadf06
commit ed32646f52
1 changed files with 39 additions and 27 deletions

View File

@ -88,6 +88,7 @@ public class IntervalSet implements IntSet {
/** Add a single element to the set. An isolated element is stored /** Add a single element to the set. An isolated element is stored
* as a range el..el. * as a range el..el.
*/ */
@Override
public void add(int el) { public void add(int el) {
add(el,el); add(el,el);
} }
@ -111,8 +112,8 @@ public class IntervalSet implements IntSet {
} }
// find position in list // find position in list
// Use iterators as we modify list in place // Use iterators as we modify list in place
for (ListIterator iter = intervals.listIterator(); iter.hasNext();) { for (ListIterator<Interval> iter = intervals.listIterator(); iter.hasNext();) {
Interval r = (Interval) iter.next(); Interval r = iter.next();
if ( addition.equals(r) ) { if ( addition.equals(r) ) {
return; return;
} }
@ -123,7 +124,7 @@ public class IntervalSet implements IntSet {
// make sure we didn't just create an interval that // make sure we didn't just create an interval that
// should be merged with next interval in list // should be merged with next interval in list
if ( iter.hasNext() ) { if ( iter.hasNext() ) {
Interval next = (Interval) iter.next(); Interval next = iter.next();
if ( bigger.adjacent(next)||!bigger.disjoint(next) ) { if ( bigger.adjacent(next)||!bigger.disjoint(next) ) {
// if we bump up against or overlap next, merge // if we bump up against or overlap next, merge
iter.remove(); // remove this one iter.remove(); // remove this one
@ -153,6 +154,7 @@ public class IntervalSet implements IntSet {
return r; return r;
} }
@Override
public IntervalSet addAll(IntSet set) { public IntervalSet addAll(IntSet set) {
if ( set==null ) { if ( set==null ) {
return this; return this;
@ -166,7 +168,7 @@ public class IntervalSet implements IntSet {
// walk set and add each interval // walk set and add each interval
int n = other.intervals.size(); int n = other.intervals.size();
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
Interval I = (Interval) other.intervals.get(i); Interval I = other.intervals.get(i);
this.add(I.a,I.b); this.add(I.a,I.b);
} }
return this; return this;
@ -182,6 +184,7 @@ public class IntervalSet implements IntSet {
* *
* 'this' is assumed to be either a subset or equal to vocabulary. * 'this' is assumed to be either a subset or equal to vocabulary.
*/ */
@Override
public IntervalSet complement(IntSet vocabulary) { public IntervalSet complement(IntSet vocabulary) {
if ( vocabulary==null ) { if ( vocabulary==null ) {
return null; // nothing in common with null set return null; // nothing in common with null set
@ -198,25 +201,25 @@ public class IntervalSet implements IntSet {
if ( n ==0 ) { if ( n ==0 ) {
return compl; return compl;
} }
Interval first = (Interval)intervals.get(0); Interval first = intervals.get(0);
// add a range from 0 to first.a constrained to vocab // add a range from 0 to first.a constrained to vocab
if ( first.a > 0 ) { if ( first.a > 0 ) {
IntervalSet s = IntervalSet.of(0, first.a-1); IntervalSet s = IntervalSet.of(0, first.a-1);
IntervalSet a = (IntervalSet)s.and(vocabularyIS); IntervalSet a = s.and(vocabularyIS);
compl.addAll(a); compl.addAll(a);
} }
for (int i=1; i<n; i++) { // from 2nd interval .. nth for (int i=1; i<n; i++) { // from 2nd interval .. nth
Interval previous = (Interval)intervals.get(i-1); Interval previous = intervals.get(i-1);
Interval current = (Interval)intervals.get(i); Interval current = intervals.get(i);
IntervalSet s = IntervalSet.of(previous.b+1, current.a-1); IntervalSet s = IntervalSet.of(previous.b+1, current.a-1);
IntervalSet a = (IntervalSet)s.and(vocabularyIS); IntervalSet a = s.and(vocabularyIS);
compl.addAll(a); compl.addAll(a);
} }
Interval last = (Interval)intervals.get(n -1); Interval last = intervals.get(n -1);
// add a range from last.b to maxElement constrained to vocab // add a range from last.b to maxElement constrained to vocab
if ( last.b < maxElement ) { if ( last.b < maxElement ) {
IntervalSet s = IntervalSet.of(last.b+1, maxElement); IntervalSet s = IntervalSet.of(last.b+1, maxElement);
IntervalSet a = (IntervalSet)s.and(vocabularyIS); IntervalSet a = s.and(vocabularyIS);
compl.addAll(a); compl.addAll(a);
} }
return compl; return compl;
@ -227,6 +230,7 @@ public class IntervalSet implements IntSet {
* other is assumed to be a subset of this; * other is assumed to be a subset of this;
* anything that is in other but not in this will be ignored. * anything that is in other but not in this will be ignored.
*/ */
@Override
public IntervalSet subtract(IntSet other) { public IntervalSet subtract(IntSet other) {
// assume the whole unicode range here for the complement // assume the whole unicode range here for the complement
// because it doesn't matter. Anything beyond the max of this' set // because it doesn't matter. Anything beyond the max of this' set
@ -237,6 +241,7 @@ public class IntervalSet implements IntSet {
return this.and(((IntervalSet)other).complement(COMPLETE_SET)); return this.and(((IntervalSet)other).complement(COMPLETE_SET));
} }
@Override
public IntervalSet or(IntSet a) { public IntervalSet or(IntSet a) {
IntervalSet o = new IntervalSet(); IntervalSet o = new IntervalSet();
o.addAll(this); o.addAll(this);
@ -249,13 +254,14 @@ public class IntervalSet implements IntSet {
* just walk them together. This is roughly O(min(n,m)) for interval * just walk them together. This is roughly O(min(n,m)) for interval
* list lengths n and m. * list lengths n and m.
*/ */
@Override
public IntervalSet and(IntSet other) { public IntervalSet and(IntSet other) {
if ( other==null ) { //|| !(other instanceof IntervalSet) ) { if ( other==null ) { //|| !(other instanceof IntervalSet) ) {
return null; // nothing in common with null set return null; // nothing in common with null set
} }
ArrayList myIntervals = (ArrayList)this.intervals; List<Interval> myIntervals = this.intervals;
ArrayList theirIntervals = (ArrayList)((IntervalSet)other).intervals; List<Interval> theirIntervals = ((IntervalSet)other).intervals;
IntervalSet intersection = null; IntervalSet intersection = null;
int mySize = myIntervals.size(); int mySize = myIntervals.size();
int theirSize = theirIntervals.size(); int theirSize = theirIntervals.size();
@ -263,8 +269,8 @@ public class IntervalSet implements IntSet {
int j = 0; int j = 0;
// iterate down both interval lists looking for nondisjoint intervals // iterate down both interval lists looking for nondisjoint intervals
while ( i<mySize && j<theirSize ) { while ( i<mySize && j<theirSize ) {
Interval mine = (Interval)myIntervals.get(i); Interval mine = myIntervals.get(i);
Interval theirs = (Interval)theirIntervals.get(j); Interval theirs = theirIntervals.get(j);
//System.out.println("mine="+mine+" and theirs="+theirs); //System.out.println("mine="+mine+" and theirs="+theirs);
if ( mine.startsBeforeDisjoint(theirs) ) { if ( mine.startsBeforeDisjoint(theirs) ) {
// move this iterator looking for interval that might overlap // move this iterator looking for interval that might overlap
@ -318,10 +324,11 @@ public class IntervalSet implements IntSet {
} }
/** Is el in any range of this set? */ /** Is el in any range of this set? */
@Override
public boolean contains(int el) { public boolean contains(int el) {
int n = intervals.size(); int n = intervals.size();
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
Interval I = (Interval) intervals.get(i); Interval I = intervals.get(i);
int a = I.a; int a = I.a;
int b = I.b; int b = I.b;
if ( el<a ) { if ( el<a ) {
@ -347,14 +354,16 @@ public class IntervalSet implements IntSet {
} }
/** return true if this set has no members */ /** return true if this set has no members */
@Override
public boolean isNil() { public boolean isNil() {
return intervals==null || intervals.size()==0; return intervals==null || intervals.size()==0;
} }
/** If this set is a single integer, return it otherwise Token.INVALID_TYPE */ /** If this set is a single integer, return it otherwise Token.INVALID_TYPE */
@Override
public int getSingleElement() { public int getSingleElement() {
if ( intervals!=null && intervals.size()==1 ) { if ( intervals!=null && intervals.size()==1 ) {
Interval I = (Interval)intervals.get(0); Interval I = intervals.get(0);
if ( I.a == I.b ) { if ( I.a == I.b ) {
return I.a; return I.a;
} }
@ -366,7 +375,7 @@ public class IntervalSet implements IntSet {
if ( isNil() ) { if ( isNil() ) {
return Token.INVALID_TYPE; return Token.INVALID_TYPE;
} }
Interval last = (Interval)intervals.get(intervals.size()-1); Interval last = intervals.get(intervals.size()-1);
return last.b; return last.b;
} }
@ -377,7 +386,7 @@ public class IntervalSet implements IntSet {
} }
int n = intervals.size(); int n = intervals.size();
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
Interval I = (Interval) intervals.get(i); Interval I = intervals.get(i);
int a = I.a; int a = I.a;
int b = I.b; int b = I.b;
for (int v=a; v<=b; v++) { for (int v=a; v<=b; v++) {
@ -424,9 +433,9 @@ public class IntervalSet implements IntSet {
if ( this.size()>1 ) { if ( this.size()>1 ) {
buf.append("{"); buf.append("{");
} }
Iterator iter = this.intervals.iterator(); Iterator<Interval> iter = this.intervals.iterator();
while (iter.hasNext()) { while (iter.hasNext()) {
Interval I = (Interval) iter.next(); Interval I = iter.next();
int a = I.a; int a = I.a;
int b = I.b; int b = I.b;
if ( a==b ) { if ( a==b ) {
@ -456,9 +465,9 @@ public class IntervalSet implements IntSet {
if ( this.size()>1 ) { if ( this.size()>1 ) {
buf.append("{"); buf.append("{");
} }
Iterator iter = this.intervals.iterator(); Iterator<Interval> iter = this.intervals.iterator();
while (iter.hasNext()) { while (iter.hasNext()) {
Interval I = (Interval) iter.next(); Interval I = iter.next();
int a = I.a; int a = I.a;
int b = I.b; int b = I.b;
if ( a==b ) { if ( a==b ) {
@ -481,6 +490,7 @@ public class IntervalSet implements IntSet {
return buf.toString(); return buf.toString();
} }
@Override
public int size() { public int size() {
int n = 0; int n = 0;
int numIntervals = intervals.size(); int numIntervals = intervals.size();
@ -489,17 +499,18 @@ public class IntervalSet implements IntSet {
return firstInterval.b-firstInterval.a+1; return firstInterval.b-firstInterval.a+1;
} }
for (int i = 0; i < numIntervals; i++) { for (int i = 0; i < numIntervals; i++) {
Interval I = (Interval) intervals.get(i); Interval I = intervals.get(i);
n += (I.b-I.a+1); n += (I.b-I.a+1);
} }
return n; return n;
} }
@Override
public List<Integer> toList() { public List<Integer> toList() {
List<Integer> values = new ArrayList<Integer>(); List<Integer> values = new ArrayList<Integer>();
int n = intervals.size(); int n = intervals.size();
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
Interval I = (Interval) intervals.get(i); Interval I = intervals.get(i);
int a = I.a; int a = I.a;
int b = I.b; int b = I.b;
for (int v=a; v<=b; v++) { for (int v=a; v<=b; v++) {
@ -517,7 +528,7 @@ public class IntervalSet implements IntSet {
int n = intervals.size(); int n = intervals.size();
int index = 0; int index = 0;
for (int j = 0; j < n; j++) { for (int j = 0; j < n; j++) {
Interval I = (Interval) intervals.get(j); Interval I = intervals.get(j);
int a = I.a; int a = I.a;
int b = I.b; int b = I.b;
for (int v=a; v<=b; v++) { for (int v=a; v<=b; v++) {
@ -535,7 +546,7 @@ public class IntervalSet implements IntSet {
int n = intervals.size(); int n = intervals.size();
int j = 0; int j = 0;
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
Interval I = (Interval) intervals.get(i); Interval I = intervals.get(i);
int a = I.a; int a = I.a;
int b = I.b; int b = I.b;
for (int v=a; v<=b; v++) { for (int v=a; v<=b; v++) {
@ -546,6 +557,7 @@ public class IntervalSet implements IntSet {
return values; return values;
} }
@Override
public void remove(int el) { public void remove(int el) {
throw new NoSuchMethodError("IntervalSet.remove() unimplemented"); throw new NoSuchMethodError("IntervalSet.remove() unimplemented");
} }