diff --git a/pmd/src/net/sourceforge/pmd/cpd/CPD.java b/pmd/src/net/sourceforge/pmd/cpd/CPD.java index 893e111e2d..3ae774aa34 100644 --- a/pmd/src/net/sourceforge/pmd/cpd/CPD.java +++ b/pmd/src/net/sourceforge/pmd/cpd/CPD.java @@ -12,7 +12,7 @@ public class CPD { private int minimumTileSize; private TokenSets tokenSets = new TokenSets(); - private MatchAlgorithm matchAlgorithm; + private MatchAlgorithm matchAlgorithm = new MatchAlgorithm(); private CPDListener listener = new CPDNullListener(); public CPD(int minimumTileSize) { @@ -24,12 +24,11 @@ public class CPD { } public void go() { - matchAlgorithm = new MatchAlgorithm(listener); for (Iterator i = tokenSets.iterator(); i.hasNext();) { TokenList tl = (TokenList)i.next(); for (Iterator j = tl.iterator();j.hasNext();) { TokenEntry te = (TokenEntry)j.next(); - matchAlgorithm.add(te, new Locator(te.getTokenSrcID(), te.getBeginLine(), te.getIndex())); + matchAlgorithm.add(te, new Locator(te.getTokenSrcID(), te.getBeginLine(), te.getIndex()), listener); } } matchAlgorithm.findMatches(minimumTileSize); diff --git a/pmd/src/net/sourceforge/pmd/cpd/MatchAlgorithm.java b/pmd/src/net/sourceforge/pmd/cpd/MatchAlgorithm.java index 99425d4637..c0be023b02 100644 --- a/pmd/src/net/sourceforge/pmd/cpd/MatchAlgorithm.java +++ b/pmd/src/net/sourceforge/pmd/cpd/MatchAlgorithm.java @@ -10,40 +10,20 @@ import java.util.Set; import java.util.TreeMap; public class MatchAlgorithm { - // using a treemap means I don't actually sort my flyweight tokens. + private Map pool = new TreeMap(); private List code = new ArrayList(); private List marks = new ArrayList(); - // separate what the token is from where it is. Locator is only used if we need to see the code at that location. - private List matchesList = new ArrayList(); - private CPDListener cpdListener; - public MatchAlgorithm(CPDListener cpdListener) { - this.cpdListener = cpdListener; - } - - public void add(TokenEntry token, Locator locator) { - pool.put(token, token); + public void add(TokenEntry token, Locator locator, CPDListener cpdListener) { + if (!pool.containsKey(token)) { + pool.put(token, token); + } code.add(token); marks.add(new Mark(code, code.size(), locator, cpdListener)); -/* - MyToken flyweight = (MyToken)pool.get(token); - if (flyweight == null) { - pool.put(token, token); - flyweight = token; - } - code.add(flyweight); - if (flyweight.isMarkToken()) { - marks.add(new Mark(code, code.size(), locator)); - } -*/ } - /** - Should return something, or notify someone with locators - that - kind of thing ;) - */ public void findMatches(int min) { /* Assign sort codes to all the pooled code. This should speed @@ -54,9 +34,8 @@ public class MatchAlgorithm { TokenEntry token = (TokenEntry)iter.next(); token.setSortCode(count++); } - // use quicksort on the marks, same as the perl version - Collections.sort(marks); + Collections.sort(marks); Set soFar = new HashSet(); for (int i = 1; i < marks.size(); i++) {