Swift wrapper around hnswlib
  • Swift 73.4%
  • C++ 16.7%
  • C 9.9%
Find a file
2026-03-22 14:36:40 +01:00
hnswlib@3f34296611 chore: add submodule 2025-04-29 01:36:26 +02:00
Sources style: format 2026-03-22 14:36:40 +01:00
Tests/HNSWTests style: format 2026-03-22 14:36:40 +01:00
.gitignore init 2025-04-29 01:36:26 +02:00
.gitmodules chore: add submodule 2025-04-29 01:36:26 +02:00
.swiftformat style: format 2026-03-22 14:36:40 +01:00
LICENSE Initial commit 2025-04-29 01:35:10 +02:00
Package.swift style: format 2026-03-22 14:36:40 +01:00
README.md chore: add README.md 2025-05-07 02:32:55 +02:00

HNSW Swift Package

A Swift package that provides Swift bindings for hnswlib, a header-only C++ library for fast approximate nearest neighbor search using the Hierarchical Navigable Small World (HNSW) algorithm. This package enables high-performance vector similarity search in Swift applications.

Features

  • Swift bindings for the lightweight, header-only hnswlib C++ library
  • Support for multiple distance metrics:
    • Squared L2 (Euclidean) distance
    • Inner product
    • Cosine similarity
  • Full support for incremental index construction and updates
  • Support for element deletions and memory reuse
  • Thread-safe implementation
  • Support for macOS 11+ and iOS 16+

Requirements

  • Swift 6.0 or later
  • macOS 11.0+ / iOS 16.0+

Installation

Swift Package Manager

Add the following to your Package.swift file:

dependencies: [
    .package(url: "https://github.com/yourusername/HNSW.git", branch: "main")
]

Usage

import HNSW

// Create an HNSW index
let dimension = 128
let maxElements = 10_000
let index = try HNSWIndex(dimension: dimension, maxElements: maxElements)

// Add vectors to the index
let vector = [Float](repeating: 0.0, count: dimension)
try index.add(vector: vector, id: 0)

// Search for nearest neighbors
let queryVector = [Float](repeating: 0.0, count: dimension)
let results = try index.search(vector: queryVector, k: 10)

You can checkout the Tests/ directory for an example on how to use NLEmbeddings

Project Structure

  • Sources/HNSW/ - Swift implementation and public API
  • Sources/CHNSWLib/ - C++ interop layer
  • Tests/ - Unit tests

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the terms of the license included in the repository.

Acknowledgments

  • hnswlib - The lightweight, header-only C++ implementation of HNSW algorithm