Skip to content

Commit 43ba17e

Browse files
author
ctjoreilly
committed
- additional minor performance enhancements.
1 parent 95f8327 commit 43ba17e

File tree

1 file changed

+18
-17
lines changed

1 file changed

+18
-17
lines changed

src/aima/logic/fol/kb/data/Clause.java

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -359,8 +359,6 @@ private void recalculateIdentity() {
359359
// constant, function and variable.
360360
List<Literal> sortedLiterals = new ArrayList<Literal>(literals);
361361
Collections.sort(sortedLiterals, _literalSorter);
362-
363-
stringRep = sortedLiterals.toString();
364362

365363
// All variables are considered the same as regards
366364
// sorting. Therefore, to determine if two clauses
@@ -575,7 +573,7 @@ class ClauseEqualityIdentityConstructor implements FOLVisitor {
575573
private int noVarPositions = 0;
576574
private int[] clauseVarCounts = null;
577575
private int currentLiteral = 0;
578-
private Map<String, List<Integer>> varPositions = new LinkedHashMap<String, List<Integer>>();
576+
private Map<String, List<Integer>> varPositions = new HashMap<String, List<Integer>>();
579577

580578
public ClauseEqualityIdentityConstructor(List<Literal> literals,
581579
LiteralsSorter sorter) {
@@ -640,7 +638,7 @@ public ClauseEqualityIdentityConstructor(List<Literal> literals,
640638
if (pos >= min && pos < max) {
641639
int pPos = pos;
642640
int nPos = pos;
643-
for (int candSlot = 0; candSlot < (next - 1); candSlot++) {
641+
for (int candSlot = i; candSlot < (next - 1); candSlot++) {
644642
pPos += clauseVarCounts[i];
645643
if (pPos >= min && pPos < max) {
646644
if (!positions.contains(pPos)
@@ -664,33 +662,35 @@ public ClauseEqualityIdentityConstructor(List<Literal> literals,
664662
min = max;
665663
i = incITo;
666664
}
667-
668-
// Sort the individual position lists
669-
for (String key : varPositions.keySet()) {
670-
List<Integer> positions = varPositions.get(key);
671-
Collections.sort(positions);
672-
}
673-
665+
674666
// Determine the maxWidth
675667
int maxWidth = 1;
676668
while (noVarPositions >= 10) {
677669
noVarPositions = noVarPositions / 10;
678670
maxWidth++;
679671
}
680672
String format = "%0" + maxWidth + "d";
673+
674+
// Sort the individual position lists
675+
// And then add their string representations
676+
// together
681677
List<String> varOffsets = new ArrayList<String>();
682678
for (String key : varPositions.keySet()) {
683679
List<Integer> positions = varPositions.get(key);
680+
Collections.sort(positions);
684681
StringBuilder sb = new StringBuilder();
685-
sb.append("[");
686682
for (int pos : positions) {
687683
sb.append(String.format(format, pos));
688684
}
689-
sb.append("]");
690685
varOffsets.add(sb.toString());
691-
}
686+
}
692687
Collections.sort(varOffsets);
693-
identity.append(varOffsets.toString());
688+
for (int i = 0; i < varOffsets.size(); i++) {
689+
identity.append(varOffsets.get(i));
690+
if (i < (varOffsets.size() - 1)) {
691+
identity.append(",");
692+
}
693+
}
694694
}
695695

696696
public String getIdentity() {
@@ -705,9 +705,10 @@ public Object visitVariable(Variable var, Object arg) {
705705

706706
List<Integer> positions = varPositions.get(var.getValue());
707707
if (null == positions) {
708-
varPositions.put(var.getValue(), new ArrayList<Integer>());
708+
positions = new ArrayList<Integer>();
709+
varPositions.put(var.getValue(), positions);
709710
}
710-
varPositions.get(var.getValue()).add(noVarPositions);
711+
positions.add(noVarPositions);
711712

712713
noVarPositions++;
713714
clauseVarCounts[currentLiteral]++;

0 commit comments

Comments
 (0)