Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose vector retrieval by key in Swift & Objective-C libraries #455

Merged

Conversation

brittlewis12
Copy link
Contributor

Thank you for the fantastic library, @ashvardanian! I'm using it quite happily for a couple different projects, and I'm excited for the opportunity to contribute back in a small way.

This PR adds two new features to the Swift & Obj-C libraries:

  1. vector retrieval by key (get)
  2. constructing indexes with multi-vector keys enabled

& add tests to exercise the new get interfaces & multi configuration on the swift side.

a couple notes:

  • for the multi case:
    • the obj-c side doesn't do any work to break up the single large vector if a count greater than one is returned. this mirrors the cpp seam.
    • the swift seam returns a 2d array instead.
  • there's a bit of duplication in the get implementations, both on the objective-c and on the swift side
  • the swift 'sugar' interfaces are a bit less than ergonomic than they could be, requiring explicit typing on the expected return value at the callsite, since the parameters alone aren't enough to disambiguate. didn't think it was worth going further without feedback

tests are passing locally:
❯ swift build && swift test -v
Building for debugging...
[2/2] Emitting module USearch
Build complete! (0.87s)
Planning build
Building for debugging...
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc -module-name USearch -incremental -emit-dependencies -emit-module -emit-module-path /Users/tito/code/usearch/.build/arm64-apple-macosx/debug/USearch.swiftmodule -output-file-map /Users/tito/code/usearch/.build/arm64-apple-macosx/debug/USearch.build/output-file-map.json -parse-as-library -c /Users/tito/code/usearch/swift/Index+Sugar.swift /Users/tito/code/usearch/swift/USearch.swift -I /Users/tito/code/usearch/.build/arm64-apple-macosx/debug -target arm64-apple-macosx10.13 -swift-version 5 -v -enable-batch-mode -index-store-path /Users/tito/code/usearch/.build/arm64-apple-macosx/debug/index/store -Onone -enable-testing -g -j10 -DSWIFT_PACKAGE -DDEBUG -Xcc -fmodule-map-file=/Users/tito/code/usearch/.build/arm64-apple-macosx/debug/USearchObjective.build/module.modulemap -Xcc -I -Xcc /Users/tito/code/usearch/objc/include -module-cache-path /Users/tito/code/usearch/.build/arm64-apple-macosx/debug/ModuleCache -parseable-output -parse-as-library -emit-objc-header -emit-objc-header-path /Users/tito/code/usearch/.build/arm64-apple-macosx/debug/USearch.build/USearch-Swift.h -color-diagnostics -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.2.sdk -F /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks -I /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib -L /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib -Xcc -isysroot -Xcc /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.2.sdk -Xcc -F -Xcc /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks -Xcc -fPIC
Apple Swift version 5.9.2 (swiftlang-5.9.2.2.56 clang-1500.1.0.2.5)
Target: arm64-apple-macosx10.13
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swiftc -module-name USearchTests -incremental -emit-dependencies -emit-module -emit-module-path /Users/tito/code/usearch/.build/arm64-apple-macosx/debug/USearchTests.swiftmodule -output-file-map /Users/tito/code/usearch/.build/arm64-apple-macosx/debug/USearchTests.build/output-file-map.json -parse-as-library -c /Users/tito/code/usearch/swift/Test.swift -I /Users/tito/code/usearch/.build/arm64-apple-macosx/debug -target arm64-apple-macosx13.0 -swift-version 5 -v -enable-batch-mode -index-store-path /Users/tito/code/usearch/.build/arm64-apple-macosx/debug/index/store -Onone -enable-testing -g -j10 -DSWIFT_PACKAGE -DDEBUG -Xcc -fmodule-map-file=/Users/tito/code/usearch/.build/arm64-apple-macosx/debug/USearchObjective.build/module.modulemap -Xcc -I -Xcc /Users/tito/code/usearch/objc/include -module-cache-path /Users/tito/code/usearch/.build/arm64-apple-macosx/debug/ModuleCache -parseable-output -parse-as-library -color-diagnostics -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.2.sdk -F /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks -I /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib -L /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib -Xcc -isysroot -Xcc /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.2.sdk -Xcc -F -Xcc /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks -Xcc -fPIC
Apple Swift version 5.9.2 (swiftlang-5.9.2.2.56 clang-1500.1.0.2.5)
Target: arm64-apple-macosx13.0
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-frontend -frontend -c -primary-file /Users/tito/code/usearch/swift/Test.swift -emit-dependencies-path /Users/tito/code/usearch/.build/arm64-apple-macosx/debug/USearchTests.build/Test.d -emit-reference-dependencies-path /Users/tito/code/usearch/.build/arm64-apple-macosx/debug/USearchTests.build/Test.swiftdeps -target arm64-apple-macosx13.0 -Xllvm -aarch64-use-tbi -enable-objc-interop -stack-check -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.2.sdk -I /Users/tito/code/usearch/.build/arm64-apple-macosx/debug -I /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib -F /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks -color-diagnostics -enable-testing -g -module-cache-path /Users/tito/code/usearch/.build/arm64-apple-macosx/debug/ModuleCache -swift-version 5 -Onone -D SWIFT_PACKAGE -D DEBUG -new-driver-path /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-driver -empty-abi-descriptor -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift -enable-anonymous-context-mangled-names -Xcc -fmodule-map-file=/Users/tito/code/usearch/.build/arm64-apple-macosx/debug/USearchObjective.build/module.modulemap -Xcc -I -Xcc /Users/tito/code/usearch/objc/include -Xcc -isysroot -Xcc /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.2.sdk -Xcc -F -Xcc /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks -Xcc -fPIC -module-name USearchTests -disable-clang-spi -target-sdk-version 14.2 -target-sdk-name macosx14.2 -external-plugin-path /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.2.sdk/usr/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.2.sdk/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.2.sdk/usr/local/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.2.sdk/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/local/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server -plugin-path /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/host/plugins -plugin-path /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/local/lib/swift/host/plugins -parse-as-library -o /Users/tito/code/usearch/.build/arm64-apple-macosx/debug/USearchTests.build/Test.swift.o -index-store-path /Users/tito/code/usearch/.build/arm64-apple-macosx/debug/index/store -index-system-modules
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-frontend -frontend -emit-module -experimental-skip-non-inlinable-function-bodies-without-types /Users/tito/code/usearch/swift/Test.swift -target arm64-apple-macosx13.0 -Xllvm -aarch64-use-tbi -enable-objc-interop -stack-check -sdk /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.2.sdk -I /Users/tito/code/usearch/.build/arm64-apple-macosx/debug -I /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib -F /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks -color-diagnostics -enable-testing -g -module-cache-path /Users/tito/code/usearch/.build/arm64-apple-macosx/debug/ModuleCache -swift-version 5 -Onone -D SWIFT_PACKAGE -D DEBUG -new-driver-path /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-driver -empty-abi-descriptor -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift -enable-anonymous-context-mangled-names -Xcc -fmodule-map-file=/Users/tito/code/usearch/.build/arm64-apple-macosx/debug/USearchObjective.build/module.modulemap -Xcc -I -Xcc /Users/tito/code/usearch/objc/include -Xcc -isysroot -Xcc /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.2.sdk -Xcc -F -Xcc /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/Library/Frameworks -Xcc -fPIC -module-name USearchTests -disable-clang-spi -target-sdk-version 14.2 -target-sdk-name macosx14.2 -external-plugin-path /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.2.sdk/usr/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.2.sdk/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.2.sdk/usr/local/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.2.sdk/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server -external-plugin-path /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/local/lib/swift/host/plugins#/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/usr/bin/swift-plugin-server -plugin-path /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/host/plugins -plugin-path /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/local/lib/swift/host/plugins -emit-module-doc-path /Users/tito/code/usearch/.build/arm64-apple-macosx/debug/USearchTests.swiftdoc -emit-module-source-info-path /Users/tito/code/usearch/.build/arm64-apple-macosx/debug/USearchTests.swiftsourceinfo -emit-dependencies-path /Users/tito/code/usearch/.build/arm64-apple-macosx/debug/USearchTests.build/USearchTests.emit-module.d -parse-as-library -o /Users/tito/code/usearch/.build/arm64-apple-macosx/debug/USearchTests.swiftmodule -emit-abi-descriptor-path /Users/tito/code/usearch/.build/arm64-apple-macosx/debug/USearchTests.abi.json
Build complete! (0.56s)
Test Suite 'All tests' started at 2024-08-03 00:47:41.055.
Test Suite 'USearchPackageTests.xctest' started at 2024-08-03 00:47:41.057.
Test Suite 'Test' started at 2024-08-03 00:47:41.057.
Test Case '-[USearchTests.Test testUnit]' started.
Test Case '-[USearchTests.Test testUnit]' passed (0.001 seconds).
Test Case '-[USearchTests.Test testUnitMulti]' started.
Test Case '-[USearchTests.Test testUnitMulti]' passed (0.000 seconds).
Test Suite 'Test' passed at 2024-08-03 00:47:41.058.
	 Executed 2 tests, with 0 failures (0 unexpected) in 0.001 (0.001) seconds
Test Suite 'USearchPackageTests.xctest' passed at 2024-08-03 00:47:41.058.
	 Executed 2 tests, with 0 failures (0 unexpected) in 0.001 (0.001) seconds
Test Suite 'All tests' passed at 2024-08-03 00:47:41.058.
	 Executed 2 tests, with 0 failures (0 unexpected) in 0.001 (0.002) seconds

ran swift-format locally as well; more than happy to adjust anything as you see fit!

* enable constructing multi-key indexes in Swift & Objective-C
@ashvardanian ashvardanian changed the base branch from main to main-dev August 3, 2024 18:03
objc/USearchObjective.mm Outdated Show resolved Hide resolved
objc/USearchObjective.mm Outdated Show resolved Hide resolved
@ashvardanian ashvardanian self-assigned this Aug 19, 2024
@ashvardanian ashvardanian merged commit 7fb3da3 into unum-cloud:main-dev Aug 21, 2024
26 checks passed
@ashvardanian
Copy link
Contributor

Thanks for a great quality contribution @brittlewis12 🤗

@brittlewis12 brittlewis12 deleted the swift-objc-get-and-multi branch August 21, 2024 20:50
vardhan pushed a commit to vardhan/usearch that referenced this pull request Aug 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants