Skip to content

buylibs/ann

Repository files navigation

BuyLibs ANN bindings to Annoy v1.17.3 for Object Pascal

Build Status

BuyLibs ANN is bindings for Delphi and Free Pascal that allow you to utilize Annoy (Approximate Nearest Neighbors Oh Yeah).

Overview

This guide provides instructions on how to integrate and use our bindings to Annoy for object pascal. This includes the necessary files, licensing details, tests, examples, and dependencies.

Delphi/FPC Example

var
  t, u: TAnnIndexAngularSingle;
  v: TAnnVectorSingle;
  r: TAnnVectorInteger;
  i, z: Integer;
const
  f = 40; // Dimensionality of the vectors stored in the index
begin
  t.Init(f); // Initialize Annoy index with vectors of dimension f

  // Generate 1000 random vectors and add them to the index
  for i := 0 to 999 do
  begin
    v.Init();

    // Fill the vector with random values drawn from N(0,1)
    for z := 0 to f - 1 do
      v.PushBack(RandG(0, 1));

    t.AddItem(i, v); // Add the vector with item id = i
  end;

  t.Build(10); // Build the Annoy index using 10 trees.

  t.Save('test.ann'); // Persist the index to disk

  // ------------------------------------------------------------------
  // Load the index from disk into a new instance
  // ------------------------------------------------------------------

  u.Init(f); // Initialize another index object with the same dimensionality

  // Load the index. Annoy uses memory-mapping so loading is very fast
  // and does not require fully copying the data into RAM.
  u.Load('test.ann');

  r.Init(); // Prepare container for nearest neighbor results

  // Query the index:
  // Find the 1000 nearest neighbors to item with id = 0
  u.GetNnsByItem(0, 1000, r);

  // ------------------------------------------------------------------
  // Print resulting neighbor item IDs
  // ------------------------------------------------------------------
  Write('[');
  for i := 0 to r.Size - 1 do
  begin
    if i > 0 then
      Write(', ');
    Write(r[i]);
  end;
  Writeln(']');
end;

Tests

Test files written in object pascal are provided to test Annoy.

The results for 81 tests that were successfully completed are as follows:

 Time:46.199 N:81 E:0 F:0 I:0
  TTestResults Time:00.000 N:5 E:0 F:0 I:0
    00.000  test_files_exist
    00.000  test_glove_25
    00.000  test_nytimes_16
    00.000  test_lastfm_dot
    00.000  test_lastfm_angular
  TTestResults Time:01.548 N:21 E:0 F:0 I:0
    00.000  test_not_found_tree
    00.001  test_binary_compatibility
    00.491  test_load_unload
    00.533  test_construct_load_destruct
    00.430  test_construct_destruct
    00.002  test_save_twice
    00.000  test_load_save
    00.000  test_save_without_build
    00.000  test_unbuild_with_loaded_tree
    00.000  test_seed
    00.000  test_metric
    00.001  test_item_vector_after_save
    00.000  test_prefault
    00.000  test_fail_save
    00.017  test_overwrite_index
    00.000  test_get_n_trees
    00.012  test_write_failed
    00.021  test_dimension_mismatch
    00.021  test_add_after_save
    00.019  test_build_twice
    00.000  test_very_large_index
  TTestResults Time:02.862 N:11 E:0 F:0 I:0
    00.000  test_get_nns_by_vector
    00.000  test_get_nns_by_item
    00.000  test_dist
    00.120  test_large_index
    00.669  test_precision_1
    00.685  test_precision_10
    00.675  test_precision_100
    00.685  test_precision_1000
    00.000  test_get_nns_with_distances
    00.000  test_include_dists
    00.028  test_distance_consistency
  TTestResults Time:00.000 N:1 E:0 F:0 I:0
    00.000  test_on_disk
  TTestResults Time:09.114 N:10 E:0 F:0 I:0
    00.000  test_get_nns_by_vector
    00.000  test_get_nns_by_item
    00.001  test_dist
    00.433  test_recall_at_10
    00.871  test_recall_at_100
    04.803  test_recall_at_1000
    02.976  test_recall_at_1000_fewer_trees
    00.001  test_get_nns_with_distances
    00.000  test_include_dists
    00.029  test_distance_consistency
  TTestResults Time:01.895 N:5 E:0 F:0 I:0
    01.744  test_random_holes
    00.031  test_root_one_child
    00.029  test_root_two_children
    00.030  test_root_some_children
    00.061  test_root_many_children
  TTestResults Time:00.257 N:1 E:0 F:0 I:0
    00.257  test_threads
  TTestResults Time:00.805 N:4 E:0 F:0 I:0
    00.390  test_one_thread
    00.211  test_two_threads
    00.117  test_four_threads
    00.087  test_eight_threads
  TTestResults Time:02.583 N:12 E:0 F:0 I:0
    00.000  test_get_nns_by_vector_euclidean
    00.000  test_get_nns_by_item_euclidean
    00.000  test_dist_euclidean
    00.113  test_large_index_euclidean
    00.598  test_precision_1_euclidean
    00.607  test_precision_10_euclidean
    00.611  test_precision_100_euclidean
    00.628  test_precision_1000_euclidean
    00.001  test_get_nns_with_distances_euclidean
    00.000  test_include_dists_euclidean
    00.025  test_distance_consistency_euclidean
    00.000  test_rounding_error
  TTestResults Time:26.093 N:4 E:0 F:0 I:0
    07.930  test_get_item_vector
    00.001  test_get_lots_of_nns
    00.311  test_build_unbuild
    17.851  test_include_distances
  TTestResults Time:00.005 N:2 E:0 F:0 I:0
    00.000  test_wrong_length
    00.005  test_range_errors
  TTestResults Time:00.021 N:1 E:0 F:0 I:0
    00.021  test_seeding
  TTestResults Time:01.016 N:4 E:0 F:0 I:0
    00.000  test_basic_conversion
    00.000  test_basic_nns
    00.000  test_save_load
    01.016  test_many_vectors

Number of run tests: 81
Number of errors:    0
Number of failures:  0

Required Files for Delphi Development

To use Annoy in your pascal projects, the following files are required:

./ann_native.pas
./ann.pas

# For Delphi:

./bin/Win64/libannwin64.dll

# For FPC:

./bin/fpc-win64-x86_64/libannwin64.dll

Redistributable Components

The following runtime libraries can be duplicated and distributed with your applications if you have a registered license:

./bin/Win64/libannwin64.dll
./bin/fpc-win64-x86_64/libannwin64.dll

NOTE: Other files in this repository are not redistributable and may only be used by the licensee, i.e. the software developer(s).

License Key Registration

To use the binding functions, you must register your license key in your pascal code.

This repository contains a Lite version license key. The Lite version is intended solely for demonstration and evaluation purposes. It may have limited functionality, and certain features may be disabled.

Example of registration in Pascal:

ann.Registration('ANN Windows. Licensed to ORGANIZATION NAME. N Developer(s) Only.',
    6487215699604760467,
    11610087105836253481,
    ...);

This repository does not contain your entity's License Key Name and License Key Number. You need to provide your own when calling ann.Registration, for purposes other than evaluation.

Documentation

The Pascal API structure is designed to align with C++ Annoy as closely as possible. Please refer to examples_* and api tests as well as Annoy Documentation.

Examples

You can find examples demonstrating how to use the bindings in the following directories:

  • example_precision_test
  • example_mmap_test
  • example_simple_test
  • API tests are located in the api directory.

NOTE: When compiling the code not only for demonstation and evaluation purposes, ensure that you set your valid key for your entity in all ann.registration calls. Failure to do so may result in runtime exceptions.

Dependencies

BuyLibs ANN Binding Dependencies for Windows

The libannwin64.dll has the following dependencies:

$ objdump -p libannwin64.dll | grep "DLL Name:"
	DLL Name: USER32.dll
	DLL Name: WS2_32.dll
	DLL Name: KERNEL32.dll
	DLL Name: VCRUNTIME140.dll
	DLL Name: api-ms-win-crt-runtime-l1-1-0.dll
	DLL Name: api-ms-win-crt-heap-l1-1-0.dll
	DLL Name: api-ms-win-crt-convert-l1-1-0.dll
	DLL Name: api-ms-win-crt-math-l1-1-0.dll
	DLL Name: api-ms-win-crt-stdio-l1-1-0.dll
	DLL Name: api-ms-win-crt-filesystem-l1-1-0.dll
	DLL Name: api-ms-win-crt-string-l1-1-0.dll
	DLL Name: api-ms-win-crt-locale-l1-1-0.dll
	DLL Name: api-ms-win-crt-multibyte-l1-1-0.dll

CONCLUSION: Ensure that you distribute VCRUNTIME140.dll with your applications for Windows.

Copyright

Copyright © 2025-2026 BuyLibs. All rights reserved.

Portions of the software are under different copyright and license, as outlined in the NOTICE.md file.

No Sponsorship or Endorsement

BuyLibs is not sponsored, endorsed, or affiliated with the Spotify AB.

Contact

For more information, please visit our website at buylibs.com or reach out to us via email at support@buylibs.com.

About

Bindings to Annoy for Delphi and Free Pascal: Approximate Nearest Neighbors, optimized for efficient memory usage and fast disk loading and saving, enabling high-performance similarity search.

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors