From 4af6d2d498365a5ccc535554f9f2292b2e2eb3d5 Mon Sep 17 00:00:00 2001 From: Ash Vardanian <1983160+ashvardanian@users.noreply.github.com> Date: Mon, 15 Apr 2024 05:12:07 +0000 Subject: [PATCH] Make: Formatting Swift --- .releaserc | 1 + .swift-format | 13 +++++++++++++ CONTRIBUTING.md | 13 ++++++++++++- Package.swift | 4 ++-- swift/Index+Sugar.swift | 12 ++++++------ swift/Test.swift | 15 ++++++++++----- 6 files changed, 44 insertions(+), 14 deletions(-) create mode 100644 .swift-format diff --git a/.releaserc b/.releaserc index 10344419..c6e88843 100644 --- a/.releaserc +++ b/.releaserc @@ -79,6 +79,7 @@ "conanfile.py", "package.json", "Cargo.toml", + "Cargo.lock", "wasmer.toml", "./include/usearch/index.hpp", "./csharp/nuget/nuget-package.props", diff --git a/.swift-format b/.swift-format new file mode 100644 index 00000000..53bf6310 --- /dev/null +++ b/.swift-format @@ -0,0 +1,13 @@ +{ + "version": 1, + "lineLength": 120, + "indentation": { + "spaces": 4 + }, + "maximumBlankLines": 1, + "respectsExistingLineBreaks": true, + "lineBreakBeforeControlFlowKeywords": true, + "lineBreakBeforeEachArgument": true, + "multiElementCollectionTrailingCommas": true, + "spacesAroundRangeFormationOperators": true +} \ No newline at end of file diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 6152f041..0d589fc4 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -304,7 +304,18 @@ The compilation settings are controlled by the `Package.swift` and are independe swift build && swift test -v ``` -Those depend on Apple's `Foundation` library and can only run on Apple devices. +> Those depend on Apple's `Foundation` library and can only run on Apple devices. + +Swift formatting is enforced with `swift-format` default utility from Apple. +To install and run it on all the files in the project, use the following command: + +```bash +brew install swift-format +swift-format . -i -r +``` + +The style is controlled by the `.swift-format` JSON file in the root of the repository. +As there is no standard for Swift formatting, even Apple's own `swift-format` tool and Xcode differ in their formatting rules, and available settings. ## GoLang diff --git a/Package.swift b/Package.swift index f0aec20c..bd1dec33 100644 --- a/Package.swift +++ b/Package.swift @@ -19,7 +19,7 @@ let package = Package( cxxSettings: [ .headerSearchPath("../include/"), .headerSearchPath("../fp16/include/"), - .headerSearchPath("../simismd/include/") + .headerSearchPath("../simismd/include/"), ] ), .target( @@ -35,7 +35,7 @@ let package = Package( path: "swift", exclude: ["USearch.swift", "Index+Sugar.swift", "README.md"], sources: ["Test.swift"] - ) + ), ], cxxLanguageStandard: CXXLanguageStandard.cxx11 ) diff --git a/swift/Index+Sugar.swift b/swift/Index+Sugar.swift index 84f75c96..83569e46 100644 --- a/swift/Index+Sugar.swift +++ b/swift/Index+Sugar.swift @@ -25,7 +25,7 @@ extension USearchIndex { /// - Parameter key: Unique identifier for that object. /// - Parameter vector: Single-precision vector. /// - Throws: If runs out of memory. - public func add(key: USearchKey, vector: Array) { + public func add(key: USearchKey, vector: [Float32]) { add(key: key, vector: vector[...]) } @@ -50,7 +50,7 @@ extension USearchIndex { /// - Parameter count: Upper limit on the number of matches to retrieve. /// - Returns: Labels and distances to closest approximate matches in decreasing similarity order. /// - Throws: If runs out of memory. - public func search(vector: Array, count: Int) -> ([Key], [Float]) { + public func search(vector: [Float32], count: Int) -> ([Key], [Float]) { return search(vector: vector[...], count: count) } @@ -68,7 +68,7 @@ extension USearchIndex { /// - Parameter key: Unique identifier for that object. /// - Parameter vector: Double-precision vector. /// - Throws: If runs out of memory. - public func add(key: Key, vector: Array) { + public func add(key: Key, vector: [Float64]) { add(key: key, vector: vector[...]) } @@ -93,7 +93,7 @@ extension USearchIndex { /// - Parameter count: Upper limit on the number of matches to retrieve. /// - Returns: Labels and distances to closest approximate matches in decreasing similarity order. /// - Throws: If runs out of memory. - public func search(vector: Array, count: Int) -> ([Key], [Float]) { + public func search(vector: [Float64], count: Int) -> ([Key], [Float]) { search(vector: vector[...], count: count) } @@ -115,7 +115,7 @@ extension USearchIndex { /// - Parameter vector: Half-precision vector. /// - Throws: If runs out of memory. @available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *) - public func add(key: Key, vector: Array) { + public func add(key: Key, vector: [Float16]) { add(key: key, vector: vector[...]) } @@ -142,7 +142,7 @@ extension USearchIndex { /// - Returns: Labels and distances to closest approximate matches in decreasing similarity order. /// - Throws: If runs out of memory. @available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *) - public func search(vector: Array, count: Int) -> ([Key], [Float]) { + public func search(vector: [Float16], count: Int) -> ([Key], [Float]) { search(vector: vector[...], count: count) } diff --git a/swift/Test.swift b/swift/Test.swift index cae6db39..903ba2d5 100644 --- a/swift/Test.swift +++ b/swift/Test.swift @@ -12,20 +12,25 @@ import XCTest @available(iOS 13, macOS 10.15, tvOS 13.0, watchOS 6.0, *) class Test: XCTestCase { func testUnit() throws { - let index = USearchIndex.make(metric: USearchMetric.l2sq, dimensions: 4, connectivity: 8, quantization: USearchScalar.F32) + let index = USearchIndex.make( + metric: USearchMetric.l2sq, + dimensions: 4, + connectivity: 8, + quantization: USearchScalar.F32 + ) let vectorA: [Float32] = [0.3, 0.5, 1.2, 1.4] let vectorB: [Float32] = [0.4, 0.2, 1.2, 1.1] index.reserve(2) - + // Adding a slice index.add(key: 42, vector: vectorA[...]) - + // Adding a vector index.add(key: 43, vector: vectorB) - + let results = index.search(vector: vectorA, count: 10) assert(results.0[0] == 42) - + assert(index.contains(key: 42)) assert(index.count(key: 42) == 1) assert(index.count(key: 49) == 0)