Skip to content

Commit

Permalink
Add try/throws everywhere necessary for new API
Browse files Browse the repository at this point in the history
  • Loading branch information
danpalmer committed Jan 19, 2025
1 parent 1aa66a0 commit 5685012
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 115 deletions.
98 changes: 49 additions & 49 deletions swift/Index+Sugar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,30 @@ 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: ArraySlice<Float32>) {
vector.withContiguousStorageIfAvailable {
addSingle(key: key, vector: $0.baseAddress!)
public func add(key: USearchKey, vector: ArraySlice<Float32>) throws {
try vector.withContiguousStorageIfAvailable {
try addSingle(key: key, vector: $0.baseAddress!)
}
}

/// Adds a labeled vector to the index.
/// - Parameter key: Unique identifier for that object.
/// - Parameter vector: Single-precision vector.
/// - Throws: If runs out of memory.
public func add(key: USearchKey, vector: [Float32]) {
add(key: key, vector: vector[...])
public func add(key: USearchKey, vector: [Float32]) throws {
try add(key: key, vector: vector[...])
}

/// Approximate nearest neighbors search.
/// - Parameter vector: Single-precision query vector.
/// - 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: ArraySlice<Float32>, count: Int) -> ([Key], [Float]) {
public func search(vector: ArraySlice<Float32>, count: Int) throws -> ([Key], [Float]) {
var matches: [Key] = Array(repeating: 0, count: count)
var distances: [Float] = Array(repeating: 0, count: count)
let results = vector.withContiguousStorageIfAvailable {
searchSingle(vector: $0.baseAddress!, count: CUnsignedInt(count), keys: &matches, distances: &distances)
let results = try vector.withContiguousStorageIfAvailable {
try searchSingle(vector: $0.baseAddress!, count: CUnsignedInt(count), keys: &matches, distances: &distances)
}
matches.removeLast(count - Int(results!))
distances.removeLast(count - Int(results!))
Expand All @@ -53,20 +53,20 @@ 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: [Float32], count: Int) -> ([Key], [Float]) {
return search(vector: vector[...], count: count)
public func search(vector: [Float32], count: Int) throws -> ([Key], [Float]) {
return try search(vector: vector[...], count: count)
}

/// Retrieve vectors for a given key.
/// - Parameter key: Unique identifier for that object.
/// - Parameter count: For multi-indexes, Number of vectors to retrieve. Defaults to 1.
/// - Returns: Two-dimensional array of Single-precision vectors.
/// - Throws: If runs out of memory.
public func get(key: USearchKey, count: Int = 1) -> [[Float]]? {
public func get(key: USearchKey, count: Int = 1) throws -> [[Float]]? {
var vector: [Float] = Array(repeating: 0.0, count: Int(self.dimensions) * count)
let returnedCount = vector.withContiguousMutableStorageIfAvailable { buf in
let returnedCount = try vector.withContiguousMutableStorageIfAvailable { buf in
guard let baseAddress = buf.baseAddress else { return UInt32(0) }
return getSingle(
return try getSingle(
key: key,
vector: baseAddress,
count: CUnsignedInt(count)
Expand All @@ -86,30 +86,30 @@ 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: ArraySlice<Float64>) {
vector.withContiguousStorageIfAvailable {
addDouble(key: key, vector: $0.baseAddress!)
public func add(key: Key, vector: ArraySlice<Float64>) throws {
try vector.withContiguousStorageIfAvailable {
try addDouble(key: key, vector: $0.baseAddress!)
}
}

/// Adds a labeled vector to the index.
/// - Parameter key: Unique identifier for that object.
/// - Parameter vector: Double-precision vector.
/// - Throws: If runs out of memory.
public func add(key: Key, vector: [Float64]) {
add(key: key, vector: vector[...])
public func add(key: Key, vector: [Float64]) throws {
try add(key: key, vector: vector[...])
}

/// Approximate nearest neighbors search.
/// - Parameter vector: Double-precision query vector.
/// - 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: ArraySlice<Float64>, count: Int) -> ([Key], [Float]) {
public func search(vector: ArraySlice<Float64>, count: Int) throws -> ([Key], [Float]) {
var matches: [Key] = Array(repeating: 0, count: count)
var distances: [Float] = Array(repeating: 0, count: count)
let results = vector.withContiguousStorageIfAvailable {
searchDouble(vector: $0.baseAddress!, count: CUnsignedInt(count), keys: &matches, distances: &distances)
let results = try vector.withContiguousStorageIfAvailable {
try searchDouble(vector: $0.baseAddress!, count: CUnsignedInt(count), keys: &matches, distances: &distances)
}
matches.removeLast(count - Int(results!))
distances.removeLast(count - Int(results!))
Expand All @@ -121,20 +121,20 @@ 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: [Float64], count: Int) -> ([Key], [Float]) {
search(vector: vector[...], count: count)
public func search(vector: [Float64], count: Int) throws -> ([Key], [Float]) {
try search(vector: vector[...], count: count)
}

/// Retrieve vectors for a given key.
/// - Parameter key: Unique identifier for that object.
/// - Parameter count: For multi-indexes, Number of vectors to retrieve. Defaults to 1.
/// - Returns: Two-dimensional array of Double-precision vectors.
/// - Throws: If runs out of memory.
public func get(key: USearchKey, count: Int = 1) -> [[Float64]]? {
public func get(key: USearchKey, count: Int = 1) throws -> [[Float64]]? {
var vector: [Float64] = Array(repeating: 0.0, count: Int(self.dimensions) * count)
let count = vector.withContiguousMutableStorageIfAvailable { buf in
let count = try vector.withContiguousMutableStorageIfAvailable { buf in
guard let baseAddress = buf.baseAddress else { return UInt32(0) }
return getDouble(
return try getDouble(
key: key,
vector: baseAddress,
count: CUnsignedInt(count)
Expand All @@ -156,12 +156,12 @@ extension USearchIndex {
/// - Parameter filter: Closure used to determine whether to skip a key in the results.
/// - Returns: Labels and distances to closest approximate matches in decreasing similarity order.
/// - Throws: If runs out of memory.
public func filteredSearch(vector: ArraySlice<Float32>, count: Int, filter: @escaping FilterFn) -> ([Key], [Float])
public func filteredSearch(vector: ArraySlice<Float32>, count: Int, filter: @escaping FilterFn) throws -> ([Key], [Float])
{
var matches: [Key] = Array(repeating: 0, count: count)
var distances: [Float] = Array(repeating: 0, count: count)
let results = vector.withContiguousStorageIfAvailable {
filteredSearchSingle(
let results = try vector.withContiguousStorageIfAvailable {
try filteredSearchSingle(
vector: $0.baseAddress!,
count:
CUnsignedInt(count),
Expand All @@ -181,8 +181,8 @@ extension USearchIndex {
/// - Parameter filter: Closure used to determine whether to skip a key in the results.
/// - Returns: Labels and distances to closest approximate matches in decreasing similarity order.
/// - Throws: If runs out of memory.
public func filteredSearch(vector: [Float32], count: Int, filter: @escaping FilterFn) -> ([Key], [Float]) {
filteredSearch(vector: vector[...], count: count, filter: filter)
public func filteredSearch(vector: [Float32], count: Int, filter: @escaping FilterFn) throws -> ([Key], [Float]) {
try filteredSearch(vector: vector[...], count: count, filter: filter)
}

/// Approximate nearest neighbors search.
Expand All @@ -191,12 +191,12 @@ extension USearchIndex {
/// - Parameter filter: Closure used to determine whether to skip a key in the results.
/// - Returns: Labels and distances to closest approximate matches in decreasing similarity order.
/// - Throws: If runs out of memory.
public func filteredSearch(vector: ArraySlice<Float64>, count: Int, filter: @escaping FilterFn) -> ([Key], [Float])
public func filteredSearch(vector: ArraySlice<Float64>, count: Int, filter: @escaping FilterFn) throws -> ([Key], [Float])
{
var matches: [Key] = Array(repeating: 0, count: count)
var distances: [Float] = Array(repeating: 0, count: count)
let results = vector.withContiguousStorageIfAvailable {
filteredSearchDouble(
let results = try vector.withContiguousStorageIfAvailable {
try filteredSearchDouble(
vector: $0.baseAddress!,
count:
CUnsignedInt(count),
Expand All @@ -216,8 +216,8 @@ extension USearchIndex {
/// - Parameter filter: Closure used to determine whether to skip a key in the results.
/// - Returns: Labels and distances to closest approximate matches in decreasing similarity order.
/// - Throws: If runs out of memory.
public func filteredSearch(vector: [Float64], count: Int, filter: @escaping FilterFn) -> ([Key], [Float]) {
filteredSearch(vector: vector[...], count: count, filter: filter)
public func filteredSearch(vector: [Float64], count: Int, filter: @escaping FilterFn) throws -> ([Key], [Float]) {
try filteredSearch(vector: vector[...], count: count, filter: filter)
}

#if arch(arm64)
Expand All @@ -227,9 +227,9 @@ 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: ArraySlice<Float16>) {
vector.withContiguousStorageIfAvailable { buffer in
addHalf(key: key, vector: buffer.baseAddress!)
public func add(key: Key, vector: ArraySlice<Float16>) throws {
try vector.withContiguousStorageIfAvailable { buffer in
try addHalf(key: key, vector: buffer.baseAddress!)
}
}

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

/// Approximate nearest neighbors search.
Expand All @@ -248,11 +248,11 @@ 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: ArraySlice<Float16>, count: Int) -> ([Key], [Float]) {
public func search(vector: ArraySlice<Float16>, count: Int) throws -> ([Key], [Float]) {
var matches: [Key] = Array(repeating: 0, count: count)
var distances: [Float] = Array(repeating: 0, count: count)
let results = vector.withContiguousStorageIfAvailable {
searchHalf(vector: $0.baseAddress!, count: CUnsignedInt(count), keys: &matches, distances: &distances)
let results = try vector.withContiguousStorageIfAvailable {
try searchHalf(vector: $0.baseAddress!, count: CUnsignedInt(count), keys: &matches, distances: &distances)
}
matches.removeLast(count - Int(results!))
distances.removeLast(count - Int(results!))
Expand All @@ -265,8 +265,8 @@ 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: [Float16], count: Int) -> ([Key], [Float]) {
search(vector: vector[...], count: count)
public func search(vector: [Float16], count: Int) throws -> ([Key], [Float]) {
try search(vector: vector[...], count: count)
}

/// Retrieve vectors for a given key.
Expand All @@ -275,11 +275,11 @@ extension USearchIndex {
/// - Returns: Two-dimensional array of Half-precision vectors.
/// - Throws: If runs out of memory.
@available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)
public func get(key: USearchKey, count: Int = 1) -> [[Float16]]? {
public func get(key: USearchKey, count: Int = 1) throws -> [[Float16]]? {
var vector: [Float16] = Array(repeating: 0.0, count: Int(self.dimensions) * count)
let count = vector.withContiguousMutableStorageIfAvailable { buf in
let count = try vector.withContiguousMutableStorageIfAvailable { buf in
guard let baseAddress = buf.baseAddress else { return UInt32(0) }
return getSingle(
return try getSingle(
key: key,
vector: baseAddress,
count: CUnsignedInt(count)
Expand Down
Loading

0 comments on commit 5685012

Please sign in to comment.