Skip to content

Commit

Permalink
Merge pull request #398 from unum-cloud/main-dev
Browse files Browse the repository at this point in the history
Introduce Swift Formatting
  • Loading branch information
ashvardanian committed Apr 15, 2024
2 parents eb67d1d + 28520e5 commit a6e63b4
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 27 deletions.
8 changes: 8 additions & 0 deletions .github/ISSUE_TEMPLATE/bug_report.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ body:
placeholder: email@example.com
validations:
required: false
- type: checkboxes
id: mentions
attributes:
label: Are you open to being tagged as a contributor?
description: Sometimes a good bug report is just as valuable as a patch 🤗
options:
- label: I am open to being mentioned in the project `.git` history as a contributor
required: false
- type: checkboxes
id: duplicate
attributes:
Expand Down
20 changes: 8 additions & 12 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -468,13 +468,13 @@ jobs:
- x86
os:
- macos-latest
- ubuntu-latest
- ubuntu-22.04
- windows-latest
exclude:
- arch: x86
os: macos-latest
- arch: x86
os: ubuntu-latest
os: ubuntu-22.04
runs-on: ${{ matrix.os }}
env:
CC: gcc-12
Expand All @@ -485,8 +485,7 @@ jobs:
uses: actions/checkout@v4
with:
ref: "main"
- name: Pull Git submodules
run: git submodule update --init --recursive
- run: git submodule update --init --recursive

- name: Set up Node.js
uses: actions/setup-node@v4
Expand All @@ -495,14 +494,12 @@ jobs:
architecture: ${{ matrix.arch }}

- name: Update compilers
if: matrix.os == 'ubuntu-latest'
if: matrix.os == 'ubuntu-22.04'
run: |
sudo apt update
sudo apt install -y cmake build-essential libjemalloc-dev libomp-dev gcc-12 g++-12
sudo apt install -y gcc-aarch64-linux-gnu binutils-aarch64-linux-gnu g++-aarch64-linux-gnu
- name: Install dependencies
run: npm ci --ignore-scripts
- run: npm ci --ignore-scripts
- run: npm run prebuild-single
if: matrix.os != 'macos-latest'
- run: npm run prebuild-darwin-x64+arm64
Expand All @@ -512,8 +509,8 @@ jobs:
if: matrix.os == 'macos-latest'
- run: file prebuilds/*/*
if: matrix.os == 'macos-latest'
- name: Build (Linux)
if: matrix.os == 'ubuntu-latest'
- name: Extra for cross build
if: matrix.os == 'ubuntu-22.04'
run: |
export CC=aarch64-linux-gnu-gcc
export CXX=aarch64-linux-gnu-g++
Expand All @@ -523,8 +520,7 @@ jobs:
export STRIP=aarch64-linux-gnu-strip
npm run prebuild-arm64
aarch64-linux-gnu-strip prebuilds/linux-arm64/*node
- name: Upload prebuilds
uses: actions/upload-artifact@v3
- uses: actions/upload-artifact@v3
with:
name: prebuilds
path: prebuilds
Expand Down
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
}
10 changes: 10 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"recommendations": [
"vknabel.vscode-apple-swift-format",
"sswg.swift-lang",
"ms-python.black-formatter",
"ms-vscode.cpptools-extension-pack",
"xaver.clang-format",
"swellaby.rust-pack"
]
}
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 a6e63b4

Please sign in to comment.