### Extend Float with AnnoyOperable Source: https://github.com/jbadger3/swiftannoy/blob/master/docs/Extensions.html This extension allows Float types to conform to the AnnoyOperable protocol. No specific setup is required beyond importing the SwiftAnnoy library. ```swift extension Float: AnnoyOperable ``` -------------------------------- ### onDiskBuild(url:) Source: https://github.com/jbadger3/swiftannoy/blob/master/docs/Classes/AnnoyIndex.html Configures the index to be built on disk instead of in memory. ```APIDOC ## onDiskBuild(url:) ### Description Prepares the index to be built on disk rather than in RAM. Should be set before adding items to the index. ### Declaration Swift ```swift public func onDiskBuild(url: URL) ``` ### Parameters #### Path Parameters - **url** (URL) - A file destination URL for the index. ``` -------------------------------- ### save(url:prefault:) Source: https://github.com/jbadger3/swiftannoy/blob/master/docs/Classes/AnnoyIndex.html Saves the current index to a file and then immediately loads it from disk. ```APIDOC ## save(url:prefault:) ### Description Saves the current index to a file then immediately loads (memory maps) the index from disk. ### Throws `AnnoyIndexError.saveFailed` ### Parameters - **url** (URL) - The URL of the file to save the index to. - **prefault** (Bool) - Whether to prefault the index from disk. Defaults to false. ``` -------------------------------- ### build(numTrees:) Source: https://github.com/jbadger3/swiftannoy/blob/master/docs/Classes/AnnoyIndex.html Builds the index to allow for fast approximate nearest neighbors lookup. ```APIDOC ## build(numTrees:) ### Description Builds the index to allow for fast approximate nearest neighbors lookup. Using a larger number of trees to build the index will take longer, but will also lead to better accuracy during querying. You may need to experiment to find the right balance. ### Throws `AnnoyIndexError.buildFailed` ### Parameters - **numTrees** (Int) - The number of trees to use to build the index. ``` -------------------------------- ### AnnoyIndex Initialization Source: https://github.com/jbadger3/swiftannoy/blob/master/docs/Classes/AnnoyIndex.html Initializes a new AnnoyIndex with a specified item length and distance metric. ```APIDOC ## init(itemLength:metric:) ### Description Initializes a new AnnoyIndex with a specified item length and distance metric. ### Parameters - **itemLength** (Int) - The vector length (Array.count) of each item stored in the index . - **metric** (DistanceMetric) - The metric to be used to measure the distance between items. One of .angular, .dotProduct, .euclidean, or .manhattan. Defaults to .euclidean. ``` -------------------------------- ### Build the Annoy Index Source: https://github.com/jbadger3/swiftannoy/blob/master/docs/index.html Build the Annoy index to enable querying. The numTrees parameter affects accuracy, build time, space, and search time. ```swift try? index.build(numTrees: 1) ``` -------------------------------- ### load(url:) Source: https://github.com/jbadger3/swiftannoy/blob/master/docs/Classes/AnnoyIndex.html Loads a previously saved Annoy index from a given URL. ```APIDOC ## load(url:) ### Description Loads a previously saved index. ### Declaration Swift ```swift public func load(url: URL) throws ``` ### Throws AnnoyIndexError.loadFailed ``` -------------------------------- ### Load Annoy Index Source: https://github.com/jbadger3/swiftannoy/blob/master/README.md Loads a previously saved Annoy index from a file URL. The index must be built before saving. ```swift let fileURL = FileManager.default.temporaryDirectory.appending(path: "index.annoy") try? index.load(fileURL) ``` -------------------------------- ### Create an Annoy Index Source: https://github.com/jbadger3/swiftannoy/blob/master/docs/index.html Instantiate an AnnoyIndex with a specified item length and distance metric. Supported types are Float and Double. ```swift let index = AnnoyIndex(itemLength: 2, metric: .euclidean) ``` -------------------------------- ### addItems(items:) Source: https://github.com/jbadger3/swiftannoy/blob/master/docs/Classes/AnnoyIndex.html Adds multiple new items to the index. ```APIDOC ## addItems(items:) ### Description Adds multiple new items to the index. ### Throws `AnnoyIndexError.addIemFailed` ### Parameters - **items** ([[T]]) - An Array of vectors to add to the index. ``` -------------------------------- ### Add Items to Annoy Index Source: https://github.com/jbadger3/swiftannoy/blob/master/docs/index.html Populate an Annoy index using either addItem for single items or addItems for multiple items. Indices must be added in chronological order. ```swift var item0 = [1.0, 1.0] var item1 = [3.0, 4.0] var item2 = [6.0, 8.0] var items = [[item0, item1, item2]] // add one item try? index.addItem(index: 0, vector: &item0) // add multple items try? index.addItems(items: &items) ``` -------------------------------- ### getNNsForItem(item:neighbors:search_k:) Source: https://github.com/jbadger3/swiftannoy/blob/master/docs/Classes/AnnoyIndex.html Gathers the approximate nearest neighbors for a given item in the index. ```APIDOC ## getNNsForItem(item:neighbors:search_k:) ### Description Gathers the approximate nearest neighbors for an item. ### Declaration Swift ```swift public func getNNsForItem(item: Int, neighbors: Int, search_k: Int = -1) -> (indices: [Int], distances: [T])? ``` ### Parameters #### Path Parameters - **item** (Int) - The index of the item of interest. - **neighbors** (Int) - The number of neighbors to return. - **search_k** (Int) - The number of nodes to inspect during search. search_k defaults to n * n_trees * D where n is the number of approximate nearest neighbors and D is a constant determined by Annoy based on the distance metric. In general, increasing search_k increases search time, but will give more accurate results. ### Return Value A tuple of arrays containing the item indicies and distances. ``` -------------------------------- ### unbuild() Source: https://github.com/jbadger3/swiftannoy/blob/master/docs/Classes/AnnoyIndex.html Unbuilds the current AnnoyIndex, allowing additional items to be added or for the index to be rebuilt. ```APIDOC ## unbuild() ### Description Unbuilds the current AnnoyIndex which allows additional items to be added or for the index to be rebuilt with a different number of trees. ### Throws `AnnoyIndexError.UnbuildFailed` ``` -------------------------------- ### Save Annoy Index Source: https://github.com/jbadger3/swiftannoy/blob/master/README.md Saves the current state of the Annoy index to a specified file URL. Ensure the file path is valid. ```swift let fileURL = FileManager.default.temporaryDirectory.appending(path: "index.annoy") try? index.save(fileURL) ``` -------------------------------- ### setVerbos(boolVal:) Source: https://github.com/jbadger3/swiftannoy/blob/master/docs/Classes/AnnoyIndex.html Enables or disables verbose output for Annoy operations. ```APIDOC ## setVerbos(boolVal:) ### Description When set to true provides additional information about operations carried out by Annoy via stdout. ### Declaration Swift ```swift public func setVerbos(boolVal: Bool) ``` ``` -------------------------------- ### Query Annoy Index by Vector Source: https://github.com/jbadger3/swiftannoy/blob/master/README.md Finds the nearest neighbors for a given vector. Requires the index to be built first. ```swift // by vector var vector = [3.0, 4.0] let results2 = index.getNNsForVector(vector: &vector, neighbors: 3) print(results2) "Optional((indices: [2, 0, 1], distances: [0.0, 3.605551275463989, 3.605551275463989]))" ``` -------------------------------- ### Query Annoy Index by Item Source: https://github.com/jbadger3/swiftannoy/blob/master/README.md Finds the nearest neighbors for a given item index. Requires the index to be built first. ```swift // by item let results = index.getNNsForItem(item: 3, neighbors: 3) print(results) "Optional((indices: [3, 2, 0], distances: [0.0, 5.0, 8.602325267042627]))" ``` -------------------------------- ### AnnoyIndexError LocalizedError Implementation Source: https://github.com/jbadger3/swiftannoy/blob/master/docs/Enums/AnnoyIndexError.html Provides a localized description for AnnoyIndex errors, conforming to the LocalizedError protocol. ```swift public var errorDescription: String? { get } ``` -------------------------------- ### AnnoyIndex Source: https://github.com/jbadger3/swiftannoy/blob/master/docs/Classes.html The primary generic class used to create an index. It is generic over a type T that conforms to the AnnoyOperable protocol. ```APIDOC ## Class AnnoyIndex ### Description The primary generic class used to create an index. ### Declaration ```swift public class AnnoyIndex where T : AnnoyOperable ``` ``` -------------------------------- ### addItem(index:vector:) Source: https://github.com/jbadger3/swiftannoy/blob/master/docs/Classes/AnnoyIndex.html Adds a new item to the index with a specified index and vector. ```APIDOC ## addItem(index:vector:) ### Description Adds a new item to the index. ### Throws `AnnoyIndexError.addItemFailed` ### Parameters - **index** (Int) - The index (integer) to assign to the item. - **vector** ([T]) - Array representing the feature vector for the item. ``` -------------------------------- ### DistanceMetric Enumeration Source: https://github.com/jbadger3/swiftannoy/blob/master/docs/Classes/AnnoyIndex/DistanceMetric.html The DistanceMetric enumeration defines the supported distance metrics for AnnoyIndex. These include angular, dotProduct, euclidean, and manhattan distances. ```APIDOC ## DistanceMetric ### Description Supported distance metrics / similarity measures. ### Cases - **angular** The angular distance used is the Euclidean distance of normalized vectors, which is sqrt(2(1-cos(u,v))) - **dotProduct** The dot product also called the inner product. - **euclidean** The L2 or straight line distance as calculated using the Pythagorean formula - **manhattan** The L1 or city block distance ``` -------------------------------- ### Euclidean Distance Metric Source: https://github.com/jbadger3/swiftannoy/blob/master/docs/Classes/AnnoyIndex/DistanceMetric.html Represents the Euclidean (L2) distance metric, calculated using the Pythagorean formula. ```swift case euclidean ``` -------------------------------- ### setSeed(seedVal:) Source: https://github.com/jbadger3/swiftannoy/blob/master/docs/Classes/AnnoyIndex.html Sets the random seed for index generation to ensure reproducible results. ```APIDOC ## setSeed(seedVal:) ### Description Sets the random seed used for generating the index. Setting this value is useful when running performance testing to ensure consistent results. ### Declaration Swift ```swift public func setSeed(seedVal: Int) ``` ``` -------------------------------- ### getItem(index:) Source: https://github.com/jbadger3/swiftannoy/blob/master/docs/Classes/AnnoyIndex.html Retrieves the vector associated with a specific item index. ```APIDOC ## getItem(index:) ### Description Retrieves the vector for an item in the index. ### Declaration Swift ```swift public func getItem(index: Int) -> [T]? ``` ### Parameters #### Path Parameters - **index** (Int) - The index of the item to retrieve ### Return Value The vector associated with the item or nil if not found. ``` -------------------------------- ### Extend Double with AnnoyOperable Source: https://github.com/jbadger3/swiftannoy/blob/master/docs/Extensions.html This extension enables Double types to conform to the AnnoyOperable protocol. Ensure the SwiftAnnoy library is imported for this functionality. ```swift extension Double: AnnoyOperable ``` -------------------------------- ### Query Annoy Index by Vector Source: https://github.com/jbadger3/swiftannoy/blob/master/docs/index.html Retrieve nearest neighbors for a given vector. This method returns optional indices and distances. ```swift var vector = [3.0, 4.0] let results2 = index.getNNsForVector(vector: &vector, neighbors: 3) print(results2) "Optional((indices: [2, 0, 1], distances: [0.0, 3.605551275463989, 3.605551275463989]))" ``` -------------------------------- ### getDistance(item1:item2:) Source: https://github.com/jbadger3/swiftannoy/blob/master/docs/Classes/AnnoyIndex.html Calculates the distance between two items in the index. ```APIDOC ## getDistance(item1:item2:) ### Description Calculates the distance between two items in the index. ### Declaration Swift ```swift public func getDistance(item1: Int, item2: Int) -> T? ``` ### Parameters #### Path Parameters - **item1** (Int) - The index of first item of inerest. - **item2** (Int) - The index of the second item of interest. ### Return Value The distance between the two items or nil if one of the items is not in the index. ``` -------------------------------- ### AnnoyIndexError Cases Source: https://github.com/jbadger3/swiftannoy/blob/master/docs/Enums/AnnoyIndexError.html Defines the possible errors that can occur when using AnnoyIndex. ```swift case invalidVectorLength(message: String) ``` ```swift case addItemFailed ``` ```swift case buildFailed ``` ```swift case unbuildFailed ``` ```swift case saveFailed ``` ```swift case loadFailed ``` -------------------------------- ### Query Annoy Index by Item Source: https://github.com/jbadger3/swiftannoy/blob/master/docs/index.html Retrieve nearest neighbors for a given item index. This method returns optional indices and distances. ```swift let results = index.getNNsForItem(item: 3, neighbors: 3) print(results) "Optional((indices: [3, 2, 0], distances: [0.0, 5.0, 8.602325267042627]))" ``` -------------------------------- ### getNNsForVector(vector:neighbors:search_k:) Source: https://github.com/jbadger3/swiftannoy/blob/master/docs/Classes/AnnoyIndex.html Gathers the approximate nearest neighbors for a given vector. ```APIDOC ## getNNsForVector(vector:neighbors:search_k:) ### Description Gathers the approximate nearest neighbors for a vector. ### Declaration Swift ```swift public func getNNsForVector(vector: inout [T], neighbors: Int, search_k: Int = -1) -> (indices: [Int], distances: [T])? ``` ### Parameters #### Path Parameters - **vector** (inout [T]) - The vector of interest. - **neighbors** (Int) - The number of neighbors to return. - **search_k** (Int) - The number of nodes to inspect during search. search_k defaults to n * n_trees * D where n is the number of approximate nearest neighbors and D is a constant determined by Annoy based on the distance metric. In general, increasing search_k increases search time, but will give more accurate results. ### Return Value A tuple of arrays containing the item indicies and distances. ``` -------------------------------- ### Dot Product Distance Metric Source: https://github.com/jbadger3/swiftannoy/blob/master/docs/Classes/AnnoyIndex/DistanceMetric.html Represents the dot product (inner product) distance metric. ```swift case dotProduct ``` -------------------------------- ### unload() Source: https://github.com/jbadger3/swiftannoy/blob/master/docs/Classes/AnnoyIndex.html Unloads the underlying index and all associated items from memory. ```APIDOC ## unload() ### Description Unloads the underlying index and all associated items. ### Declaration Swift ```swift public func unload() ``` ``` -------------------------------- ### AnnoyIndexError Source: https://github.com/jbadger3/swiftannoy/blob/master/docs/Enums.html Errors thrown by AnnoyIndex operations. ```APIDOC ## AnnoyIndexError ### Description Errors thrown by AnnoyIndex. ### Values - `invalidVectorLenght` - `addItemFailed` - `buildFailed` - `unbuildFailed` - `saveFailed` - `loadFailed` ### Declaration Swift ```swift public enum AnnoyIndexError : Error, LocalizedError ``` ``` -------------------------------- ### AnnoyIndex Class Declaration Source: https://github.com/jbadger3/swiftannoy/blob/master/docs/Classes.html This is the declaration for the primary generic AnnoyIndex class. It is used to create an index and requires its generic type T to conform to the AnnoyOperable protocol. ```swift public class AnnoyIndex where T : AnnoyOperable ``` -------------------------------- ### Manhattan Distance Metric Source: https://github.com/jbadger3/swiftannoy/blob/master/docs/Classes/AnnoyIndex/DistanceMetric.html Represents the Manhattan (L1) distance metric, also known as the city block distance. ```swift case manhattan ``` -------------------------------- ### AnnoyIndexError Enumeration Source: https://github.com/jbadger3/swiftannoy/blob/master/docs/Enums/AnnoyIndexError.html The AnnoyIndexError enumeration defines the specific errors that can occur during operations with the AnnoyIndex. These errors include issues with vector lengths, adding items, building or unbuilding the index, and saving or loading the index. ```APIDOC ## AnnoyIndexError Enumeration ### Description This enumeration lists the errors that can be thrown by the AnnoyIndex class. It conforms to the `Error` and `LocalizedError` protocols. ### Cases - **`invalidVectorLength(message: String)`** - Description: Undocumented. This case indicates an issue with the provided vector length. - Declaration: ```swift case invalidVectorLength(message: String) ``` - **`addItemFailed`** - Description: Undocumented. This case indicates that adding an item to the index failed. - Declaration: ```swift case addItemFailed ``` - **`buildFailed`** - Description: Undocumented. This case indicates that the index build process failed. - Declaration: ```swift case buildFailed ``` - **`unbuildFailed`** - Description: Undocumented. This case indicates that the unbuild process for the index failed. - Declaration: ```swift case unbuildFailed ``` - **`saveFailed`** - Description: Undocumented. This case indicates that saving the index failed. - Declaration: ```swift case saveFailed ``` - **`loadFailed`** - Description: Undocumented. This case indicates that loading the index failed. - Declaration: ```swift case loadFailed ``` ### LocalizedError Conformance - **`errorDescription`** - Description: Returns a localized description of the error. - Declaration: ```swift public var errorDescription: String? { get } ``` ``` -------------------------------- ### AnnoyIndexError Declaration Source: https://github.com/jbadger3/swiftannoy/blob/master/docs/Enums.html Shows the declaration of the AnnoyIndexError enumeration, which conforms to the Error and LocalizedError protocols. This error type is used for exceptions thrown by AnnoyIndex operations. ```swift public enum AnnoyIndexError : Error, LocalizedError ``` -------------------------------- ### Angular Distance Metric Source: https://github.com/jbadger3/swiftannoy/blob/master/docs/Classes/AnnoyIndex/DistanceMetric.html Represents the angular distance metric. This is calculated as the Euclidean distance of normalized vectors. ```swift case angular ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.