Skip to content
Snippets Groups Projects
Commit f4b01ad6 authored by Mingchung Xia's avatar Mingchung Xia
Browse files

Fixed CartesianDistanceMetric to run in reasonable compiler time

parent c1f31cfd
No related branches found
No related tags found
1 merge request!13HNSW Implementation with Testcases
...@@ -46,7 +46,8 @@ class HNSWCorpus<Scalar: BinaryFloatingPoint & Codable>: SNLPCorpus { ...@@ -46,7 +46,8 @@ class HNSWCorpus<Scalar: BinaryFloatingPoint & Codable>: SNLPCorpus {
@inlinable @inlinable
func addUntokenizedDocument(_ document: String) { func addUntokenizedDocument(_ document: String) {
encodedDocuments.insert((_documentEncoder.encodeSentence(document) as! [Scalar])) // Forced cast to [Scalar] is unnecessary
encodedDocuments.insert((_documentEncoder.encodeSentence(document)))
} }
} }
...@@ -86,9 +87,10 @@ public struct DeterministicSampleVectorIndex<Vector: Collection & Codable> where ...@@ -86,9 +87,10 @@ public struct DeterministicSampleVectorIndex<Vector: Collection & Codable> where
public struct CartesianDistanceMetric<Vector: Collection & Codable>: SimilarityMetric where Vector.Element: BinaryFloatingPoint { public struct CartesianDistanceMetric<Vector: Collection & Codable>: SimilarityMetric where Vector.Element: BinaryFloatingPoint {
public func similarity(between someItem: Vector, _ otherItem: Vector) -> Vector.Element { public func similarity(between someItem: Vector, _ otherItem: Vector) -> Vector.Element {
let squaredSum = zip(someItem, otherItem)
.map { (x, y) in (x - y) * (x - y) } let squaredDifferences = zip(someItem, otherItem).map { (x, y) in (x - y) * (x - y) }
.reduce(0, +) let squaredSum = squaredDifferences.reduce(0, +)
return sqrt(squaredSum) return sqrt(squaredSum)
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment