From 90e84a9189a86fb7b227785016557689f630d9d7 Mon Sep 17 00:00:00 2001 From: Joel Knighton Date: Tue, 3 Dec 2024 15:25:07 -0600 Subject: [PATCH] Don't use segment hashCode in MemorySegmentVectorFloat, as it depends on segment base/offset in the heap rather than contents. This breaks testing around PQVectors hashcodes. --- .../jbellis/jvector/vector/ArrayVectorFloat.java | 2 +- .../jbellis/jvector/vector/types/VectorFloat.java | 10 ++++++++++ .../jvector/vector/MemorySegmentVectorFloat.java | 2 +- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/jvector-base/src/main/java/io/github/jbellis/jvector/vector/ArrayVectorFloat.java b/jvector-base/src/main/java/io/github/jbellis/jvector/vector/ArrayVectorFloat.java index 771772bc3..573328337 100644 --- a/jvector-base/src/main/java/io/github/jbellis/jvector/vector/ArrayVectorFloat.java +++ b/jvector-base/src/main/java/io/github/jbellis/jvector/vector/ArrayVectorFloat.java @@ -114,7 +114,7 @@ public boolean equals(Object o) @Override public int hashCode() { - return Arrays.hashCode(data); + return this.getHashCode(); } } diff --git a/jvector-base/src/main/java/io/github/jbellis/jvector/vector/types/VectorFloat.java b/jvector-base/src/main/java/io/github/jbellis/jvector/vector/types/VectorFloat.java index c57fe0611..636fa7d4d 100644 --- a/jvector-base/src/main/java/io/github/jbellis/jvector/vector/types/VectorFloat.java +++ b/jvector-base/src/main/java/io/github/jbellis/jvector/vector/types/VectorFloat.java @@ -40,4 +40,14 @@ default int offset(int i) { void set(int i, float value); void zero(); + + default int getHashCode() { + int result = 1; + for (int i = 0; i < length(); i++) { + if (get(i) != 0) { + result = 31 * result + Float.hashCode(get(i)); + } + } + return result; + } } diff --git a/jvector-native/src/main/java/io/github/jbellis/jvector/vector/MemorySegmentVectorFloat.java b/jvector-native/src/main/java/io/github/jbellis/jvector/vector/MemorySegmentVectorFloat.java index c26415d11..27df8d8b5 100644 --- a/jvector-native/src/main/java/io/github/jbellis/jvector/vector/MemorySegmentVectorFloat.java +++ b/jvector-native/src/main/java/io/github/jbellis/jvector/vector/MemorySegmentVectorFloat.java @@ -130,6 +130,6 @@ public boolean equals(Object o) @Override public int hashCode() { - return segment.hashCode(); + return this.getHashCode(); } }