Skip to content

Commit

Permalink
reject zero for max_elements in HNSWLib.Index.new/4 (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
cocoa-xu authored Nov 25, 2023
1 parent ab27564 commit 42ed9db
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/hnswlib_index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ defmodule HNSWLib.Index do
Number of dimensions for each vector.
- *max_elements*: `non_neg_integer()`.
- *max_elements*: `pos_integer()`.
Number of maximum elements.
Expand All @@ -42,15 +42,15 @@ defmodule HNSWLib.Index do
- *random_seed*: `non_neg_integer()`.
- *allow_replace_deleted*: `boolean()`.
"""
@spec new(:cosine | :ip | :l2, non_neg_integer(), non_neg_integer(), [
@spec new(:cosine | :ip | :l2, non_neg_integer(), pos_integer(), [
{:m, non_neg_integer()},
{:ef_construction, non_neg_integer()},
{:random_seed, non_neg_integer()},
{:allow_replace_deleted, boolean()}
]) :: {:ok, %T{}} | {:error, String.t()}
def new(space, dim, max_elements, opts \\ [])
when (space == :l2 or space == :ip or space == :cosine) and is_integer(dim) and dim >= 0 and
is_integer(max_elements) and max_elements >= 0 do
is_integer(max_elements) and max_elements > 0 do
m = Helper.get_keyword!(opts, :m, :non_neg_integer, 16)
ef_construction = Helper.get_keyword!(opts, :ef_construction, :non_neg_integer, 200)
random_seed = Helper.get_keyword!(opts, :random_seed, :non_neg_integer, 100)
Expand Down
9 changes: 9 additions & 0 deletions test/hnswlib_index_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ defmodule HNSWLib.Index.Test do
use ExUnit.Case
doctest HNSWLib.Index

test "HNSWLib.Index.new should not accept 0 for max_elements" do
space = :l2
dim = 8
max_elements = 0
assert_raise FunctionClauseError, "no function clause matching in HNSWLib.Index.new/4", fn ->
HNSWLib.Index.new(space, dim, max_elements)
end
end

test "HNSWLib.Index.new/3 with L2-space" do
space = :l2
dim = 8
Expand Down

0 comments on commit 42ed9db

Please sign in to comment.