More suggestions from Brian; memory usage is down about 10%

git-svn-id: https://pmd.svn.sourceforge.net/svnroot/pmd/trunk@1646 51baf565-9d33-0410-a72c-fc3788e3496d
This commit is contained in:
Tom Copeland
2003-03-28 20:53:51 +00:00
parent 46b47eacaf
commit 3a8e0a8a54
5 changed files with 27 additions and 29 deletions

View File

@ -25,32 +25,32 @@ public class MarkComparatorTest extends TestCase {
public void testEqualMarksAreEqual() {
List code = getCode();
MarkComparator comp = new MarkComparator(new CPDNullListener(), code.size());
Mark mark1 = new Mark(code, code.size(), "/var/Foo.java", 0);
Mark mark6 = new Mark(code, code.size(), "/var/Foo.java", 5);
MarkComparator comp = new MarkComparator(new CPDNullListener(), code);
Mark mark1 = new Mark(code.size(), "/var/Foo.java", 0);
Mark mark6 = new Mark(code.size(), "/var/Foo.java", 5);
assertEquals(0, comp.compare(mark1, mark6));
}
public void testSameMarkIsEqual() {
List code = getCode();
MarkComparator comp = new MarkComparator(new CPDNullListener(), code.size());
Mark mark1 = new Mark(code, code.size(), "/var/Foo.java", 0);
MarkComparator comp = new MarkComparator(new CPDNullListener(), code);
Mark mark1 = new Mark(code.size(), "/var/Foo.java", 0);
assertEquals(0, comp.compare(mark1, mark1));
}
public void testUnuequalMarksAreUnequal() {
List code = getCode();
MarkComparator comp = new MarkComparator(new CPDNullListener(), code.size());
Mark mark1 = new Mark(code, 0, "/var/Foo.java", 0);
Mark mark5 = new Mark(code, 4, "/var/Foo.java", 4);
MarkComparator comp = new MarkComparator(new CPDNullListener(), code);
Mark mark1 = new Mark(0, "/var/Foo.java", 0);
Mark mark5 = new Mark(4, "/var/Foo.java", 4);
assertFalse(0 == comp.compare(mark1, mark5));
}
public void testcomparisonCountCallback() {
gotCallback = false;
List code = getCode();
MarkComparator comp = new MarkComparator(new MyListener(), code.size(), 3);
Mark mark1 = new Mark(code, code.size(), "/var/Foo.java", 0);
Mark mark2 = new Mark(code, code.size(), "/var/Foo.java", 1);
MarkComparator comp = new MarkComparator(new MyListener(), code, 3);
Mark mark1 = new Mark(code.size(), "/var/Foo.java", 0);
Mark mark2 = new Mark(code.size(), "/var/Foo.java", 1);
comp.compare(mark1, mark2);
comp.compare(mark1, mark2);
comp.compare(mark1, mark2);

View File

@ -8,7 +8,7 @@ import java.util.ArrayList;
public class MarkTest extends TestCase {
public void testSimple() {
Mark mark = new Mark(new ArrayList(), 0, "/var/Foo.java", 10);
Mark mark = new Mark(0, "/var/Foo.java", 10);
assertEquals(mark.getIndexIntoFile(), 10);
}
}

View File

@ -6,11 +6,9 @@ public class Mark {
private int indexIntoFile;
private int indexIntoTokenArray;
private List code;
private String file;
public Mark(List code, int offset, String file, int index) {
this.code = code;
public Mark(int offset, String file, int index) {
this.indexIntoTokenArray = offset;
this.indexIntoFile = index;
this.file = file;
@ -27,9 +25,4 @@ public class Mark {
public int getIndexIntoTokenArray() {
return indexIntoTokenArray;
}
public TokenEntry tokenAt(int index) {
return (TokenEntry)code.get((index + indexIntoTokenArray) % code.size());
}
}

View File

@ -1,19 +1,20 @@
package net.sourceforge.pmd.cpd;
import java.util.Comparator;
import java.util.List;
public class MarkComparator implements Comparator {
private final int comparisonUpdateInterval;
private CPDListener l;
private long comparisons;
private int code;
private List code;
public MarkComparator(CPDListener l, int code) {
public MarkComparator(CPDListener l, List code) {
this(l, code, 10000);
}
public MarkComparator(CPDListener l, int code, int comparisonUpdateInterval) {
public MarkComparator(CPDListener l, List code, int comparisonUpdateInterval) {
this.l = l;
this.code = code;
this.comparisonUpdateInterval = comparisonUpdateInterval;
@ -27,8 +28,8 @@ public class MarkComparator implements Comparator {
Mark mark1 = (Mark)o1;
Mark mark2 = (Mark)o2;
for (int i = 1; i < code; i++) {
int cmp = mark2.tokenAt(i).compareTo(mark1.tokenAt(i));
for (int i = 1; i < code.size(); i++) {
int cmp = tokenAt(i, mark1).compareTo(tokenAt(i, mark2));
if (cmp != 0) {
return cmp;
}
@ -36,4 +37,7 @@ public class MarkComparator implements Comparator {
return 0;
}
public TokenEntry tokenAt(int index, Mark mark) {
return (TokenEntry)code.get((index + mark.getIndexIntoTokenArray()) % code.size());
}
}

View File

@ -22,7 +22,7 @@ public class MatchAlgorithm {
pool.put(token, token);
}
code.add(pool.get(token));
marks.add(new Mark(code, code.size(), token.getTokenSrcID(), token.getIndex()));
marks.add(new Mark(code.size(), token.getTokenSrcID(), token.getIndex()));
this.cpdListener = cpdListener;
}
@ -37,7 +37,8 @@ public class MatchAlgorithm {
token.setSortCode(count++);
}
Collections.sort(marks, new MarkComparator(cpdListener, code.size()));
MarkComparator mc = new MarkComparator(cpdListener, code);
Collections.sort(marks, mc);
Set soFar = new HashSet();
for (int i = 1; i < marks.size(); i++) {
@ -45,8 +46,8 @@ public class MatchAlgorithm {
Mark mark1 = (Mark)marks.get(i);
Mark mark2 = (Mark)marks.get(i - 1);
for (int j = 0; j < code.size(); j++) {
TokenEntry token1 = mark1.tokenAt(j);
TokenEntry token2 = mark2.tokenAt(j);
TokenEntry token1 = mc.tokenAt(j, mark1);
TokenEntry token2 = mc.tokenAt(j, mark2);
if (!token1.equals(token2) || token1 == TokenEntry.EOF || token2 == TokenEntry.EOF) {
break;
}