usearch 2.8.15 β 2.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/ISSUE_TEMPLATE/bug_report.yml +1 -1
- package/.github/workflows/prerelease.yml +37 -11
- package/.github/workflows/release.yml +30 -28
- package/.gitmodules +4 -1
- package/CITATION.cff +1 -1
- package/CMakeLists.txt +6 -8
- package/CONTRIBUTING.md +20 -1
- package/Cargo.toml +1 -1
- package/README.md +8 -7
- package/VERSION +1 -1
- package/conanfile.py +1 -1
- package/cpp/test.cpp +9 -2
- package/include/usearch/index.hpp +11 -11
- package/include/usearch/index_plugins.hpp +4 -2
- package/javascript/usearch.js +1 -1
- package/package.json +1 -1
- package/pyproject.toml +2 -2
- package/setup.py +10 -4
- package/simsimd/.releaserc +3 -2
- package/simsimd/Cargo.lock +708 -0
- package/simsimd/Cargo.toml +36 -0
- package/simsimd/README.md +91 -25
- package/simsimd/binding.gyp +18 -0
- package/simsimd/build.rs +20 -0
- package/simsimd/include/simsimd/probability.h +5 -5
- package/simsimd/include/simsimd/simsimd.h +9 -5
- package/simsimd/include/simsimd/spatial.h +20 -21
- package/simsimd/include/simsimd/types.h +5 -1
- package/simsimd/javascript/bench.js +20 -10
- package/simsimd/javascript/dist-package-cjs.json +3 -0
- package/simsimd/javascript/dist-package-esm.json +3 -0
- package/simsimd/javascript/fallback.ts +193 -0
- package/simsimd/javascript/node-gyp-build.d.ts +1 -0
- package/simsimd/javascript/simsimd.ts +124 -0
- package/simsimd/javascript/test.mjs +153 -0
- package/simsimd/javascript/tsconfig-base.json +25 -0
- package/simsimd/javascript/tsconfig-cjs.json +8 -0
- package/simsimd/javascript/tsconfig-esm.json +8 -0
- package/simsimd/package-lock.json +1688 -0
- package/simsimd/package.json +26 -11
- package/simsimd/rust/benches/cosine.rs +27 -0
- package/simsimd/rust/benches/native.rs +27 -0
- package/simsimd/rust/benches/sqeuclidean.rs +27 -0
- package/simsimd/rust/lib.c +68 -0
- package/simsimd/rust/lib.rs +232 -0
- package/sqlite/README.md +178 -0
- package/sqlite/lib.cpp +325 -0
- package/stringzilla/CONTRIBUTING.md +429 -0
- package/stringzilla/Cargo.lock +25 -0
- package/stringzilla/Cargo.toml +26 -0
- package/stringzilla/LICENSE +201 -0
- package/stringzilla/README.md +1281 -0
- package/stringzilla/VERSION +1 -0
- package/stringzilla/assets/cover-strinzilla.jpeg +0 -0
- package/stringzilla/assets/meme-stringzilla-v2.jpeg +0 -0
- package/stringzilla/assets/meme-stringzilla-v3.jpeg +0 -0
- package/stringzilla/binding.gyp +16 -0
- package/stringzilla/build.rs +14 -0
- package/stringzilla/c/lib.c +304 -0
- package/stringzilla/cli/README.md +48 -0
- package/stringzilla/cli/split.py +72 -0
- package/stringzilla/cli/wc.py +37 -0
- package/stringzilla/include/stringzilla/experimental.h +724 -0
- package/stringzilla/include/stringzilla/module.modulemap +9 -0
- package/stringzilla/include/stringzilla/stringzilla.h +5240 -0
- package/stringzilla/include/stringzilla/stringzilla.hpp +3830 -0
- package/stringzilla/javascript/lib.c +125 -0
- package/stringzilla/javascript/stringzilla.js +22 -0
- package/stringzilla/package-ci.json +10 -0
- package/stringzilla/package.json +32 -0
- package/stringzilla/rust/lib.rs +362 -0
- package/stringzilla/swift/StringProtocol+StringZilla.swift +280 -0
- package/stringzilla/swift/Test.swift +60 -0
- package/wasmer.toml +1 -1
- package/simsimd/javascript/simsimd.js +0 -45
- package/simsimd/javascript/test/simsimd.test.js +0 -47
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
//
|
|
2
|
+
// StringProtocol+StringZilla.swift
|
|
3
|
+
//
|
|
4
|
+
// Created by Ash Vardanian on 18/1/24.
|
|
5
|
+
// Extension of StringProtocol to interface with StringZilla functionalities.
|
|
6
|
+
//
|
|
7
|
+
// Docs:
|
|
8
|
+
// - Accessing immutable UTF8-range:
|
|
9
|
+
// https://developer.apple.com/documentation/swift/string/utf8view
|
|
10
|
+
//
|
|
11
|
+
// More reading materials:
|
|
12
|
+
// - Stringβs ABI and UTF-8. Nov 2018
|
|
13
|
+
// https://forums.swift.org/t/string-s-abi-and-utf-8/17676
|
|
14
|
+
// - Stable pointer into a C string without copying it? Aug 2021
|
|
15
|
+
// https://forums.swift.org/t/stable-pointer-into-a-c-string-without-copying-it/51244/1
|
|
16
|
+
|
|
17
|
+
import StringZillaC
|
|
18
|
+
|
|
19
|
+
// We need to link the standard libraries.
|
|
20
|
+
#if os(Linux)
|
|
21
|
+
import Glibc
|
|
22
|
+
#else
|
|
23
|
+
import Darwin.C
|
|
24
|
+
#endif
|
|
25
|
+
|
|
26
|
+
/// Protocol defining a single-byte data type.
|
|
27
|
+
fileprivate protocol SingleByte {}
|
|
28
|
+
|
|
29
|
+
extension UInt8: SingleByte {}
|
|
30
|
+
extension Int8: SingleByte {} // This would match `CChar` as well.
|
|
31
|
+
|
|
32
|
+
@usableFromInline
|
|
33
|
+
enum StringZillaError: Error {
|
|
34
|
+
case contiguousStorageUnavailable
|
|
35
|
+
case memoryAllocationFailed
|
|
36
|
+
|
|
37
|
+
var localizedDescription: String {
|
|
38
|
+
switch self {
|
|
39
|
+
case .contiguousStorageUnavailable:
|
|
40
|
+
return "Contiguous storage for the sequence is unavailable."
|
|
41
|
+
case .memoryAllocationFailed:
|
|
42
|
+
return "Memory allocation failed."
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/// Protocol defining the interface for StringZilla-compatible byte-spans.
|
|
48
|
+
///
|
|
49
|
+
/// # Discussion:
|
|
50
|
+
/// The Swift documentation is extremely vague about the actual memory layout of a String
|
|
51
|
+
/// and the cost of obtaining the underlying UTF8 representation or any other raw pointers.
|
|
52
|
+
/// https://developer.apple.com/documentation/swift/stringprotocol/withcstring(_:)
|
|
53
|
+
/// https://developer.apple.com/documentation/swift/stringprotocol/withcstring(encodedas:_:)
|
|
54
|
+
/// https://developer.apple.com/documentation/swift/stringprotocol/data(using:allowlossyconversion:)
|
|
55
|
+
public protocol StringZillaViewable: Collection {
|
|
56
|
+
/// A type that represents a position in the collection.
|
|
57
|
+
///
|
|
58
|
+
/// Executes a closure with a pointer to the string's UTF8 C representation and its length.
|
|
59
|
+
///
|
|
60
|
+
/// - Parameters:
|
|
61
|
+
/// - body: A closure that takes a pointer to a C string and its length.
|
|
62
|
+
/// - Throws: Can throw an error.
|
|
63
|
+
/// - Returns: Returns a value of type R, which is the result of the closure.
|
|
64
|
+
func withStringZillaScope<R>(_ body: (sz_cptr_t, sz_size_t) throws -> R) rethrows -> R
|
|
65
|
+
|
|
66
|
+
/// Calculates the offset index for a given byte pointer relative to a start pointer.
|
|
67
|
+
///
|
|
68
|
+
/// - Parameters:
|
|
69
|
+
/// - bytePointer: A pointer to the byte for which the offset is calculated.
|
|
70
|
+
/// - startPointer: The starting pointer for the calculation, previously obtained from `szScope`.
|
|
71
|
+
/// - Returns: The calculated index offset.
|
|
72
|
+
func stringZillaByteOffset(forByte bytePointer: sz_cptr_t, after startPointer: sz_cptr_t) -> Index
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
extension String: StringZillaViewable {
|
|
76
|
+
public typealias Index = String.Index
|
|
77
|
+
|
|
78
|
+
@_transparent
|
|
79
|
+
public func withStringZillaScope<R>(_ body: (sz_cptr_t, sz_size_t) throws -> R) rethrows -> R {
|
|
80
|
+
let cLength = sz_size_t(utf8.count)
|
|
81
|
+
return try self.withCString { cString in
|
|
82
|
+
try body(cString, cLength)
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
@_transparent
|
|
87
|
+
public func stringZillaByteOffset(forByte bytePointer: sz_cptr_t, after startPointer: sz_cptr_t) -> Index {
|
|
88
|
+
self.utf8.index(self.utf8.startIndex, offsetBy: bytePointer - startPointer)
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
extension Substring.UTF8View: StringZillaViewable {
|
|
93
|
+
public typealias Index = Substring.UTF8View.Index
|
|
94
|
+
|
|
95
|
+
/// Executes a closure with a pointer to the UTF8View's contiguous storage of single-byte elements (UTF-8 code units).
|
|
96
|
+
/// - Parameters:
|
|
97
|
+
/// - body: A closure that takes a pointer to the contiguous storage and its size.
|
|
98
|
+
/// - Throws: An error if the storage is not contiguous.
|
|
99
|
+
@_transparent
|
|
100
|
+
public func withStringZillaScope<R>(_ body: (sz_cptr_t, sz_size_t) throws -> R) rethrows -> R {
|
|
101
|
+
return try withContiguousStorageIfAvailable { bufferPointer -> R in
|
|
102
|
+
let cLength = sz_size_t(bufferPointer.count)
|
|
103
|
+
let cString = UnsafeRawPointer(bufferPointer.baseAddress!).assumingMemoryBound(to: CChar.self)
|
|
104
|
+
return try body(cString, cLength)
|
|
105
|
+
} ?? {
|
|
106
|
+
throw StringZillaError.contiguousStorageUnavailable
|
|
107
|
+
}()
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/// Calculates the offset index for a given byte pointer relative to a start pointer.
|
|
111
|
+
/// - Parameters:
|
|
112
|
+
/// - bytePointer: A pointer to the byte for which the offset is calculated.
|
|
113
|
+
/// - startPointer: The starting pointer for the calculation, previously obtained from `szScope`.
|
|
114
|
+
/// - Returns: The calculated index offset.
|
|
115
|
+
@_transparent
|
|
116
|
+
public func stringZillaByteOffset(forByte bytePointer: sz_cptr_t, after startPointer: sz_cptr_t) -> Index {
|
|
117
|
+
return self.index(self.startIndex, offsetBy: bytePointer - startPointer)
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
extension String.UTF8View: StringZillaViewable {
|
|
122
|
+
public typealias Index = String.UTF8View.Index
|
|
123
|
+
|
|
124
|
+
/// Executes a closure with a pointer to the UTF8View's contiguous storage of single-byte elements (UTF-8 code units).
|
|
125
|
+
/// - Parameters:
|
|
126
|
+
/// - body: A closure that takes a pointer to the contiguous storage and its size.
|
|
127
|
+
/// - Throws: An error if the storage is not contiguous.
|
|
128
|
+
public func withStringZillaScope<R>(_ body: (sz_cptr_t, sz_size_t) throws -> R) rethrows -> R {
|
|
129
|
+
return try withContiguousStorageIfAvailable { bufferPointer -> R in
|
|
130
|
+
let cLength = sz_size_t(bufferPointer.count)
|
|
131
|
+
let cString = UnsafeRawPointer(bufferPointer.baseAddress!).assumingMemoryBound(to: CChar.self)
|
|
132
|
+
return try body(cString, cLength)
|
|
133
|
+
} ?? {
|
|
134
|
+
throw StringZillaError.contiguousStorageUnavailable
|
|
135
|
+
}()
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/// Calculates the offset index for a given byte pointer relative to a start pointer.
|
|
139
|
+
/// - Parameters:
|
|
140
|
+
/// - bytePointer: A pointer to the byte for which the offset is calculated.
|
|
141
|
+
/// - startPointer: The starting pointer for the calculation, previously obtained from `szScope`.
|
|
142
|
+
/// - Returns: The calculated index offset.
|
|
143
|
+
public func stringZillaByteOffset(forByte bytePointer: sz_cptr_t, after startPointer: sz_cptr_t) -> Index {
|
|
144
|
+
return self.index(self.startIndex, offsetBy: bytePointer - startPointer)
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
public extension StringZillaViewable {
|
|
149
|
+
|
|
150
|
+
/// Finds the first occurrence of the specified substring within the receiver.
|
|
151
|
+
/// - Parameter needle: The substring to search for.
|
|
152
|
+
/// - Returns: The index of the found occurrence, or `nil` if not found.
|
|
153
|
+
@_specialize(where Self == String, S == String)
|
|
154
|
+
@_specialize(where Self == String.UTF8View, S == String.UTF8View)
|
|
155
|
+
func findFirst<S: StringZillaViewable>(substring needle: S) -> Index? {
|
|
156
|
+
var result: Index?
|
|
157
|
+
withStringZillaScope { hPointer, hLength in
|
|
158
|
+
needle.withStringZillaScope { nPointer, nLength in
|
|
159
|
+
if let matchPointer = sz_find(hPointer, hLength, nPointer, nLength) {
|
|
160
|
+
result = self.stringZillaByteOffset(forByte: matchPointer, after: hPointer)
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return result
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/// Finds the last occurrence of the specified substring within the receiver.
|
|
168
|
+
/// - Parameter needle: The substring to search for.
|
|
169
|
+
/// - Returns: The index of the found occurrence, or `nil` if not found.
|
|
170
|
+
@_specialize(where Self == String, S == String)
|
|
171
|
+
@_specialize(where Self == String.UTF8View, S == String.UTF8View)
|
|
172
|
+
func findLast<S: StringZillaViewable>(substring needle: S) -> Index? {
|
|
173
|
+
var result: Index?
|
|
174
|
+
withStringZillaScope { hPointer, hLength in
|
|
175
|
+
needle.withStringZillaScope { nPointer, nLength in
|
|
176
|
+
if let matchPointer = sz_rfind(hPointer, hLength, nPointer, nLength) {
|
|
177
|
+
result = self.stringZillaByteOffset(forByte: matchPointer, after: hPointer)
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
return result
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/// Finds the first occurrence of the specified character-set members within the receiver.
|
|
185
|
+
/// - Parameter characters: A string-like collection of characters to match.
|
|
186
|
+
/// - Returns: The index of the found occurrence, or `nil` if not found.
|
|
187
|
+
@_specialize(where Self == String, S == String)
|
|
188
|
+
@_specialize(where Self == String.UTF8View, S == String.UTF8View)
|
|
189
|
+
func findFirst<S: StringZillaViewable>(characterFrom characters: S) -> Index? {
|
|
190
|
+
var result: Index?
|
|
191
|
+
withStringZillaScope { hPointer, hLength in
|
|
192
|
+
characters.withStringZillaScope { nPointer, nLength in
|
|
193
|
+
if let matchPointer = sz_find_char_from(hPointer, hLength, nPointer, nLength) {
|
|
194
|
+
result = self.stringZillaByteOffset(forByte: matchPointer, after: hPointer)
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
return result
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/// Finds the last occurrence of the specified character-set members within the receiver.
|
|
202
|
+
/// - Parameter characters: A string-like collection of characters to match.
|
|
203
|
+
/// - Returns: The index of the found occurrence, or `nil` if not found.
|
|
204
|
+
@_specialize(where Self == String, S == String)
|
|
205
|
+
@_specialize(where Self == String.UTF8View, S == String.UTF8View)
|
|
206
|
+
func findLast<S: StringZillaViewable>(characterFrom characters: S) -> Index? {
|
|
207
|
+
var result: Index?
|
|
208
|
+
withStringZillaScope { hPointer, hLength in
|
|
209
|
+
characters.withStringZillaScope { nPointer, nLength in
|
|
210
|
+
if let matchPointer = sz_rfind_char_from(hPointer, hLength, nPointer, nLength) {
|
|
211
|
+
result = self.stringZillaByteOffset(forByte: matchPointer, after: hPointer)
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
return result
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/// Finds the first occurrence of a character outside of the the given character-set within the receiver.
|
|
219
|
+
/// - Parameter characters: A string-like collection of characters to exclude.
|
|
220
|
+
/// - Returns: The index of the found occurrence, or `nil` if not found.
|
|
221
|
+
@_specialize(where Self == String, S == String)
|
|
222
|
+
@_specialize(where Self == String.UTF8View, S == String.UTF8View)
|
|
223
|
+
func findFirst<S: StringZillaViewable>(characterNotFrom characters: S) -> Index? {
|
|
224
|
+
var result: Index?
|
|
225
|
+
withStringZillaScope { hPointer, hLength in
|
|
226
|
+
characters.withStringZillaScope { nPointer, nLength in
|
|
227
|
+
if let matchPointer = sz_find_char_not_from(hPointer, hLength, nPointer, nLength) {
|
|
228
|
+
result = self.stringZillaByteOffset(forByte: matchPointer, after: hPointer)
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
return result
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/// Finds the last occurrence of a character outside of the the given character-set within the receiver.
|
|
236
|
+
/// - Parameter characters: A string-like collection of characters to exclude.
|
|
237
|
+
/// - Returns: The index of the found occurrence, or `nil` if not found.
|
|
238
|
+
@_specialize(where Self == String, S == String)
|
|
239
|
+
@_specialize(where Self == String.UTF8View, S == String.UTF8View)
|
|
240
|
+
func findLast<S: StringZillaViewable>(characterNotFrom characters: S) -> Index? {
|
|
241
|
+
var result: Index?
|
|
242
|
+
withStringZillaScope { hPointer, hLength in
|
|
243
|
+
characters.withStringZillaScope { nPointer, nLength in
|
|
244
|
+
if let matchPointer = sz_rfind_char_not_from(hPointer, hLength, nPointer, nLength) {
|
|
245
|
+
result = self.stringZillaByteOffset(forByte: matchPointer, after: hPointer)
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
return result
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/// Computes the Levenshtein edit distance between this and another string.
|
|
253
|
+
/// - Parameter other: A string-like collection of characters to exclude.
|
|
254
|
+
/// - Returns: The edit distance, as an unsigned integer.
|
|
255
|
+
/// - Throws: If a memory allocation error has happened.
|
|
256
|
+
@_specialize(where Self == String, S == String)
|
|
257
|
+
@_specialize(where Self == String.UTF8View, S == String.UTF8View)
|
|
258
|
+
func editDistance<S: StringZillaViewable>(from other: S, bound: UInt64 = 0) throws -> UInt64? {
|
|
259
|
+
var result: UInt64?
|
|
260
|
+
|
|
261
|
+
// Use a do-catch block to handle potential errors
|
|
262
|
+
do {
|
|
263
|
+
try withStringZillaScope { hPointer, hLength in
|
|
264
|
+
try other.withStringZillaScope { nPointer, nLength in
|
|
265
|
+
result = UInt64(sz_edit_distance(hPointer, hLength, nPointer, nLength, sz_size_t(bound), nil))
|
|
266
|
+
if result == SZ_SIZE_MAX {
|
|
267
|
+
result = nil
|
|
268
|
+
throw StringZillaError.memoryAllocationFailed
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
} catch {
|
|
273
|
+
// Handle or rethrow the error
|
|
274
|
+
throw error
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
return result
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Test.swift
|
|
3
|
+
//
|
|
4
|
+
//
|
|
5
|
+
// Created by Ash Vardanian on 18/1/24.
|
|
6
|
+
//
|
|
7
|
+
|
|
8
|
+
import XCTest
|
|
9
|
+
@testable import StringZilla
|
|
10
|
+
|
|
11
|
+
class StringZillaTests: XCTestCase {
|
|
12
|
+
|
|
13
|
+
var testString: String!
|
|
14
|
+
|
|
15
|
+
override func setUp() {
|
|
16
|
+
super.setUp()
|
|
17
|
+
testString = "Hello, world! Welcome to StringZilla. π"
|
|
18
|
+
XCTAssertEqual(testString.count, 39)
|
|
19
|
+
XCTAssertEqual(testString.utf8.count, 42)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
func testFindFirstSubstring() {
|
|
23
|
+
let index = testString.findFirst(substring: "world")!
|
|
24
|
+
XCTAssertEqual(testString[index...], "world! Welcome to StringZilla. π")
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
func testFindLastSubstring() {
|
|
28
|
+
let index = testString.findLast(substring: "o")!
|
|
29
|
+
XCTAssertEqual(testString[index...], "o StringZilla. π")
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
func testFindFirstCharacterFromSet() {
|
|
33
|
+
let index = testString.findFirst(characterFrom: "aeiou")!
|
|
34
|
+
XCTAssertEqual(testString[index...], "ello, world! Welcome to StringZilla. π")
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
func testFindLastCharacterFromSet() {
|
|
38
|
+
let index = testString.findLast(characterFrom: "aeiou")!
|
|
39
|
+
XCTAssertEqual(testString[index...], "a. π")
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
func testFindFirstCharacterNotFromSet() {
|
|
43
|
+
let index = testString.findFirst(characterNotFrom: "aeiou")!
|
|
44
|
+
XCTAssertEqual(testString[index...], "Hello, world! Welcome to StringZilla. π")
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// TODO: This fails!
|
|
48
|
+
// func testFindLastCharacterNotFromSet() {
|
|
49
|
+
// let index = testString.findLast(characterNotFrom: "aeiou")!
|
|
50
|
+
// XCTAssertEqual(testString.distance(from: testString.startIndex, to: index), 38)
|
|
51
|
+
// XCTAssertEqual(testString[index...], "π")
|
|
52
|
+
// }
|
|
53
|
+
|
|
54
|
+
func testEditDistance() {
|
|
55
|
+
let otherString = "Hello, world!"
|
|
56
|
+
let distance = try? testString.editDistance(from: otherString) // Using try?
|
|
57
|
+
XCTAssertNotNil(distance)
|
|
58
|
+
XCTAssertEqual(distance, 29)
|
|
59
|
+
}
|
|
60
|
+
}
|
package/wasmer.toml
CHANGED
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
const compiled = require('bindings')('simsimd');
|
|
2
|
-
|
|
3
|
-
module.exports = {
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* @brief Computes the squared Euclidean distance between two vectors.
|
|
7
|
-
* @param {Float32Array|Int8Array} a - The first vector.
|
|
8
|
-
* @param {Float32Array|Int8Array} b - The second vector.
|
|
9
|
-
* @returns {number} The squared Euclidean distance between vectors a and b.
|
|
10
|
-
*/
|
|
11
|
-
sqeuclidean: compiled.sqeuclidean,
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* @brief Computes the cosine similarity between two vectors.
|
|
15
|
-
* @param {Float32Array|Int8Array} a - The first vector.
|
|
16
|
-
* @param {Float32Array|Int8Array} b - The second vector.
|
|
17
|
-
* @returns {number} The cosine similarity between vectors a and b.
|
|
18
|
-
*/
|
|
19
|
-
cosine: compiled.cosine,
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* @brief Computes the inner product of two vectors.
|
|
23
|
-
* @param {Float32Array} a - The first vector.
|
|
24
|
-
* @param {Float32Array} b - The second vector.
|
|
25
|
-
* @returns {number} The inner product of vectors a and b.
|
|
26
|
-
*/
|
|
27
|
-
inner: compiled.inner,
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* @brief Computes the bitwise Hamming distance between two vectors.
|
|
31
|
-
* @param {Uint8Array} a - The first vector.
|
|
32
|
-
* @param {Uint8Array} b - The second vector.
|
|
33
|
-
* @returns {number} The Hamming distance between vectors a and b.
|
|
34
|
-
*/
|
|
35
|
-
hamming: compiled.hamming,
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* @brief Computes the bitwise Jaccard similarity coefficient between two vectors.
|
|
39
|
-
* @param {Uint8Array} a - The first vector.
|
|
40
|
-
* @param {Uint8Array} b - The second vector.
|
|
41
|
-
* @returns {number} The Jaccard similarity coefficient between vectors a and b.
|
|
42
|
-
*/
|
|
43
|
-
jaccard: compiled.jaccard,
|
|
44
|
-
|
|
45
|
-
};
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import test from 'node:test';
|
|
2
|
-
import bindings from 'bindings';
|
|
3
|
-
import assert from 'node:assert';
|
|
4
|
-
|
|
5
|
-
const simsimd = bindings('simsimd');
|
|
6
|
-
|
|
7
|
-
function assertAlmostEqual(actual, expected, tolerance = 1e-6) {
|
|
8
|
-
const lowerBound = expected - tolerance;
|
|
9
|
-
const upperBound = expected + tolerance;
|
|
10
|
-
assert(actual >= lowerBound && actual <= upperBound, `Expected ${actual} to be almost equal to ${expected}`);
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
test('Distance from itself', () => {
|
|
14
|
-
|
|
15
|
-
const f32s = new Float32Array([1.0, 2.0, 3.0]);
|
|
16
|
-
assertAlmostEqual(simsimd.sqeuclidean(f32s, f32s), 0.0, 0.01);
|
|
17
|
-
assertAlmostEqual(simsimd.cosine(f32s, f32s), 0.0, 0.01);
|
|
18
|
-
|
|
19
|
-
const f32sNormalized = new Float32Array([1.0 / Math.sqrt(14), 2.0 / Math.sqrt(14), 3.0 / Math.sqrt(14)]);
|
|
20
|
-
assertAlmostEqual(simsimd.inner(f32sNormalized, f32sNormalized), 0.0, 0.01);
|
|
21
|
-
|
|
22
|
-
const f32sDistribution = new Float32Array([1.0 / 6, 2.0 / 6, 3.0 / 6]);
|
|
23
|
-
assertAlmostEqual(simsimd.kullbackleibler(f32sDistribution, f32sDistribution), 0.0, 0.01);
|
|
24
|
-
assertAlmostEqual(simsimd.jensenshannon(f32sDistribution, f32sDistribution), 0.0, 0.01);
|
|
25
|
-
|
|
26
|
-
const u8s = new Uint8Array([1, 2, 3]);
|
|
27
|
-
assertAlmostEqual(simsimd.hamming(u8s, u8s), 0.0, 0.01);
|
|
28
|
-
assertAlmostEqual(simsimd.jaccard(u8s, u8s), 0.0, 0.01);
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
const f32Array1 = new Float32Array([1.0, 2.0, 3.0]);
|
|
32
|
-
const f32Array2 = new Float32Array([4.0, 5.0, 6.0]);
|
|
33
|
-
|
|
34
|
-
test('Squared Euclidean Distance', () => {
|
|
35
|
-
const result = simsimd.sqeuclidean(f32Array1, f32Array2);
|
|
36
|
-
assertAlmostEqual(result, 27.0, 0.01);
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
test('Inner Product', () => {
|
|
40
|
-
const result = simsimd.inner(f32Array1, f32Array2);
|
|
41
|
-
assertAlmostEqual(result, -31.0, 0.01);
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
test('Cosine Similarity', () => {
|
|
45
|
-
const result = simsimd.cosine(f32Array1, f32Array2);
|
|
46
|
-
assertAlmostEqual(result, 0.029, 0.01);
|
|
47
|
-
});
|