From b374f5ffa29f81a59c36e0d9d53db1ad8b10725f Mon Sep 17 00:00:00 2001 From: Yonik Seeley Date: Mon, 26 Dec 2005 17:12:16 +0000 Subject: [PATCH] implement, correct, improve hashcodes: LUCENE-460 git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@359078 13f79535-47bb-0310-9956-ffa450edef68 --- .../lucene/search/regex/SpanRegexQuery.java | 6 ++---- .../lucene/search/spans/SpanFirstQuery.java | 19 ++++++++++++++++++ .../lucene/search/spans/SpanNearQuery.java | 13 +++++++----- .../lucene/search/spans/SpanNotQuery.java | 20 +++++++++++++++++++ .../lucene/search/spans/SpanOrQuery.java | 8 ++++---- .../lucene/search/spans/TestBasics.java | 7 ++++++- 6 files changed, 59 insertions(+), 14 deletions(-) diff --git a/src/java/org/apache/lucene/search/regex/SpanRegexQuery.java b/src/java/org/apache/lucene/search/regex/SpanRegexQuery.java index 974a32760d9..eef905e1c1b 100644 --- a/src/java/org/apache/lucene/search/regex/SpanRegexQuery.java +++ b/src/java/org/apache/lucene/search/regex/SpanRegexQuery.java @@ -65,15 +65,13 @@ public class SpanRegexQuery extends SpanQuery { public boolean equals(Object o) { if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; - + if (!(o instanceof TermQuery)) return false; final SpanRegexQuery that = (SpanRegexQuery) o; - return term.equals(that.term) && getBoost() == that.getBoost(); } public int hashCode() { - return term.hashCode(); + return term.hashCode() ^ Float.floatToRawIntBits(getBoost()) ^ 0x4BCEF3A9; } public String toString(String field) { diff --git a/src/java/org/apache/lucene/search/spans/SpanFirstQuery.java b/src/java/org/apache/lucene/search/spans/SpanFirstQuery.java index 48248826257..dde4fb47a0e 100644 --- a/src/java/org/apache/lucene/search/spans/SpanFirstQuery.java +++ b/src/java/org/apache/lucene/search/spans/SpanFirstQuery.java @@ -105,4 +105,23 @@ public class SpanFirstQuery extends SpanQuery { return this; // no clauses rewrote } } + + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof SpanFirstQuery)) return false; + + SpanFirstQuery other = (SpanFirstQuery)o; + return this.end == other.end + && this.match.equals(other.match) + && this.getBoost() == other.getBoost(); + } + + public int hashCode() { + int h = match.hashCode(); + h ^= (h << 8) | (h >>> 25); // reversible + h ^= Float.floatToRawIntBits(getBoost()) ^ end; + return h; + } + + } diff --git a/src/java/org/apache/lucene/search/spans/SpanNearQuery.java b/src/java/org/apache/lucene/search/spans/SpanNearQuery.java index 6481b4171f1..01816a26c8a 100644 --- a/src/java/org/apache/lucene/search/spans/SpanNearQuery.java +++ b/src/java/org/apache/lucene/search/spans/SpanNearQuery.java @@ -134,14 +134,13 @@ public class SpanNearQuery extends SpanQuery { /** Returns true iff o is equal to this. */ public boolean equals(Object o) { if (this == o) return true; - if (o == null || getClass() != o.getClass()) return false; + if (!(o instanceof SpanNearQuery)) return false; final SpanNearQuery spanNearQuery = (SpanNearQuery) o; if (inOrder != spanNearQuery.inOrder) return false; if (slop != spanNearQuery.slop) return false; if (!clauses.equals(spanNearQuery.clauses)) return false; - if (!field.equals(spanNearQuery.field)) return false; return getBoost() == spanNearQuery.getBoost(); } @@ -149,9 +148,13 @@ public class SpanNearQuery extends SpanQuery { public int hashCode() { int result; result = clauses.hashCode(); - result += slop * 29; - result += (inOrder ? 1 : 0); - result ^= field.hashCode(); + // Mix bits before folding in things like boost, since it could cancel the + // last element of clauses. This particular mix also serves to + // differentiate SpanNearQuery hashcodes from others. + result ^= (result << 14) | (result >>> 19); // reversible + result += Float.floatToRawIntBits(getBoost()); + result += slop; + result ^= (inOrder ? 0x99AFD3BD : 0); return result; } } diff --git a/src/java/org/apache/lucene/search/spans/SpanNotQuery.java b/src/java/org/apache/lucene/search/spans/SpanNotQuery.java index 49455f60fcd..e3bc4c6dba2 100644 --- a/src/java/org/apache/lucene/search/spans/SpanNotQuery.java +++ b/src/java/org/apache/lucene/search/spans/SpanNotQuery.java @@ -151,4 +151,24 @@ public class SpanNotQuery extends SpanQuery { } } + /** Returns true iff o is equal to this. */ + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof SpanNotQuery)) return false; + + SpanNotQuery other = (SpanNotQuery)o; + return this.include.equals(other.include) + && this.exclude.equals(other.exclude) + && this.getBoost() == other.getBoost(); + } + + public int hashCode() { + int h = include.hashCode(); + h = (h<<1) | (h >>> 31); // rotate left + h ^= include.hashCode(); + h = (h<<1) | (h >>> 31); // rotate left + h ^= Float.floatToRawIntBits(getBoost()); + return h; + } + } diff --git a/src/java/org/apache/lucene/search/spans/SpanOrQuery.java b/src/java/org/apache/lucene/search/spans/SpanOrQuery.java index 59df8b64a3d..1b7aa07ebd7 100644 --- a/src/java/org/apache/lucene/search/spans/SpanOrQuery.java +++ b/src/java/org/apache/lucene/search/spans/SpanOrQuery.java @@ -113,10 +113,10 @@ public class SpanOrQuery extends SpanQuery { } public int hashCode() { - int result; - result = clauses.hashCode(); - result = 29 * result + field.hashCode(); - return result; + int h = clauses.hashCode(); + h ^= (h << 10) | (h >>> 23); + h ^= Float.floatToRawIntBits(getBoost()); + return h; } private class SpanQueue extends PriorityQueue { diff --git a/src/test/org/apache/lucene/search/spans/TestBasics.java b/src/test/org/apache/lucene/search/spans/TestBasics.java index f740413ed05..d7787c08a5a 100644 --- a/src/test/org/apache/lucene/search/spans/TestBasics.java +++ b/src/test/org/apache/lucene/search/spans/TestBasics.java @@ -72,7 +72,7 @@ public class TestBasics extends TestCase { 578, 579, 670, 671, 672, 673, 674, 675, 676, 677, 678, 679, 770, 771, 772, 773, 774, 775, 776, 777, 778, 779, 870, 871, 872, 873, 874, 875, 876, 877, 878, 879, 970, 971, 972, 973, 974, 975, 976, 977, 978, 979}); - } + } public void testTerm2() throws Exception { Query query = new TermQuery(new Term("field", "seventish")); @@ -120,6 +120,10 @@ public class TestBasics extends TestCase { assertTrue(searcher.explain(query, 77).getValue() > 0.0f); assertTrue(searcher.explain(query, 977).getValue() > 0.0f); + + QueryUtils.check(term1); + QueryUtils.check(term2); + QueryUtils.checkUnequal(term1,term2); } public void testSpanNearUnordered() throws Exception { @@ -264,5 +268,6 @@ public class TestBasics extends TestCase { private void checkHits(Query query, int[] results) throws IOException { CheckHits.checkHits(query, "field", searcher, results); + QueryUtils.check(query); } }