Skip to content

Commit

Permalink
Make: Formatting Swift
Browse files Browse the repository at this point in the history
  • Loading branch information
ashvardanian committed Apr 15, 2024
1 parent 3a33d9d commit 4af6d2d
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 14 deletions.
1 change: 1 addition & 0 deletions .releaserc
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
"conanfile.py",
"package.json",
"Cargo.toml",
"Cargo.lock",
"wasmer.toml",
"./include/usearch/index.hpp",
"./csharp/nuget/nuget-package.props",
Expand Down
13 changes: 13 additions & 0 deletions .swift-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"version": 1,
"lineLength": 120,
"indentation": {
"spaces": 4
},
"maximumBlankLines": 1,
"respectsExistingLineBreaks": true,
"lineBreakBeforeControlFlowKeywords": true,
"lineBreakBeforeEachArgument": true,
"multiElementCollectionTrailingCommas": true,
"spacesAroundRangeFormationOperators": true
}
13 changes: 12 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ let package = Package(
cxxSettings: [
.headerSearchPath("../include/"),
.headerSearchPath("../fp16/include/"),
.headerSearchPath("../simismd/include/")
.headerSearchPath("../simismd/include/"),
]
),
.target(
Expand All @@ -35,7 +35,7 @@ let package = Package(
path: "swift",
exclude: ["USearch.swift", "Index+Sugar.swift", "README.md"],
sources: ["Test.swift"]
)
),
],
cxxLanguageStandard: CXXLanguageStandard.cxx11
)
12 changes: 6 additions & 6 deletions swift/Index+Sugar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Float32>) {
public func add(key: USearchKey, vector: [Float32]) {
add(key: key, vector: vector[...])
}

Expand All @@ -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<Float32>, count: Int) -> ([Key], [Float]) {
public func search(vector: [Float32], count: Int) -> ([Key], [Float]) {
return search(vector: vector[...], count: count)
}

Expand All @@ -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<Float64>) {
public func add(key: Key, vector: [Float64]) {
add(key: key, vector: vector[...])
}

Expand All @@ -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<Float64>, count: Int) -> ([Key], [Float]) {
public func search(vector: [Float64], count: Int) -> ([Key], [Float]) {
search(vector: vector[...], count: count)
}

Expand All @@ -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<Float16>) {
public func add(key: Key, vector: [Float16]) {
add(key: key, vector: vector[...])
}

Expand All @@ -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<Float16>, count: Int) -> ([Key], [Float]) {
public func search(vector: [Float16], count: Int) -> ([Key], [Float]) {
search(vector: vector[...], count: count)
}

Expand Down
15 changes: 10 additions & 5 deletions swift/Test.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 4af6d2d

Please sign in to comment.