diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/pdom/tests/DBTest.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/pdom/tests/DBTest.java index cb7f52904ee..9fcf58d4d31 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/pdom/tests/DBTest.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/pdom/tests/DBTest.java @@ -236,7 +236,7 @@ public class DBTest extends BaseTestCase { doTrials(600, ShortString.MAX_LENGTH+1, ShortString.MAX_LENGTH*2, r, true); doTrials(600, ShortString.MAX_LENGTH+1, ShortString.MAX_LENGTH*2, r, false); } - + private void doTrials(int n, int min, int max, Random r, boolean caseSensitive) throws CoreException { long start = System.currentTimeMillis(); for(int i=0; i NUM_CHARSN) { @@ -125,11 +126,57 @@ public class LongString implements IString { } public boolean equals(Object obj) { - throw new PDOMNotImplementedError(); + if (obj == this) + return true; + try { + if (obj instanceof LongString) { + LongString lstr = (LongString)obj; + if (db == lstr.db && record == lstr.record) + return true; + return compare(lstr, true) == 0; + } + if (obj instanceof char[]) { + return compare((char[]) obj, true) == 0; + } + if (obj instanceof String) { + return compare((String) obj, true) == 0; + } + } catch (CoreException e) { + CCorePlugin.log(e); + } + return false; } + + private class HashCodeComputer implements IReader { + private int fHashcode= 0; + + public int getHashcode() { + return fHashcode; + } + + public void appendChar(char c) { + fHashcode = 31*fHashcode + c; + } + } + + /** + * Compatible with {@link String#hashCode()} + */ public int hashCode() { - return record1; + int h = hash; + if (h == 0) { + HashCodeComputer hcc; + try { + int length = db.getInt(record + LENGTH); + hcc = new HashCodeComputer(); + readChars(length, hcc); + h= hcc.getHashcode(); + hash = h; + } catch (CoreException e) { + } + } + return h; } public int compare(IString string, boolean caseSensitive) throws CoreException { @@ -246,13 +293,13 @@ public class LongString implements IString { private void readChars(int length, IReader reader) throws CoreException { // First record - int p = record1 + CHARS1; + int p = record + CHARS1; for (int i = 0; i < NUM_CHARS1; ++i) { reader.appendChar(db.getChar(p)); p += 2; } length -= NUM_CHARS1; - int nextRecord = db.getInt(record1 + NEXT1); + int nextRecord = db.getInt(record + NEXT1); // Middle records while (length > NUM_CHARSN) { @@ -282,8 +329,8 @@ public class LongString implements IString { int length; public CharIterator() throws CoreException { - p = record1 + CHARS1; - length = db.getInt(record1 + LENGTH); + p = record + CHARS1; + length = db.getInt(record + LENGTH); } public char next() throws CoreException { @@ -294,10 +341,10 @@ public class LongString implements IString { throw new NoSuchElementException(); } if(count == NUM_CHARS1) { - p = db.getInt(record1 + NEXT1) + CHARSN; + p = db.getInt(record + NEXT1) + CHARSN; } if(count > NUM_CHARS1 && ((count-NUM_CHARS1) % NUM_CHARSN)==0) { - p = db.getInt(record1 + NEXTN) + CHARSN; + p = db.getInt(p-(NUM_CHARSN*2)-4) + CHARSN; } return result; } @@ -308,7 +355,7 @@ public class LongString implements IString { } public char[] getChars() throws CoreException { - int length = db.getInt(record1 + LENGTH); + int length = db.getInt(record + LENGTH); final char[] chars = new char[length]; readChars(length, new IReader() { int cp = 0; @@ -320,7 +367,7 @@ public class LongString implements IString { } public String getString() throws CoreException { - int length = db.getInt(record1 + LENGTH); + int length = db.getInt(record + LENGTH); final StringBuffer buffer = new StringBuffer(length); readChars(length, new IReader() { public void appendChar(char c) { diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/db/ShortString.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/db/ShortString.java index f632da91e3f..cb10ec93137 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/db/ShortString.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/pdom/db/ShortString.java @@ -25,6 +25,7 @@ public class ShortString implements IString { private final Database db; private final int record; + private int hash; private static final int LENGTH = 0; private static final int CHARS = 4; @@ -152,9 +153,24 @@ public class ShortString implements IString { return false; } + /** + * Compatible with {@link String#hashCode()} + */ public int hashCode() { - // Custom hash code function to allow DBStrings in hashmaps. - return record; + int h = hash; + if (h == 0) { + char chars[]; + try { + chars = getChars(); + final int len = chars.length; + for (int i = 0; i < len; i++) { + h = 31*h + chars[i]; + } + } catch (CoreException e) { + } + hash = h; + } + return h; } public int compare(char[] other, boolean caseSensitive) throws CoreException {