Skip to content

Commit

Permalink
Go: fixed getItem call and added a test for it.
Browse files Browse the repository at this point in the history
  • Loading branch information
rosmo committed Mar 15, 2017
1 parent 079a678 commit e548d3d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
5 changes: 4 additions & 1 deletion README_GO.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ Install
To install, you'll need Swig (tested with Swig 3.0.6 on OS X), and then just::

swig -go -intgosize 64 -cgo -c++ src/annoygomodule.i
cp src/annoygomodule_wrap.cxx src/annoyindex.go src/annoygomodule.h src/annoylib.h src/kissrandom.h $GOPATH/src/annoyindex
mkdir -p $GOPATH/src/annoyindex
cp src/annoygomodule_wrap.cxx src/annoyindex.go src/annoygomodule.h src/annoylib.h src/kissrandom.h test/annoy_test.go $GOPATH/src/annoyindex
cd $GOPATH/src/annoyindex
go get -t ...
go test
go build

Background
Expand Down
9 changes: 7 additions & 2 deletions src/annoygomodule.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class AnnoyIndex {
protected:
::AnnoyIndexInterface<int32_t, float> *ptr;

int f;

public:
~AnnoyIndex() {
delete ptr;
Expand Down Expand Up @@ -48,8 +50,9 @@ class AnnoyIndex {
void verbose(bool v) {
ptr->verbose(v);
};
void getItem(int item, float* v) {
ptr->get_item(item, v);
void getItem(int item, vector<float> *v) {
v->resize(this->f);
ptr->get_item(item, &v->front());
};
};

Expand All @@ -58,13 +61,15 @@ class AnnoyIndexAngular : public AnnoyIndex
public:
AnnoyIndexAngular(int f) {
ptr = new ::AnnoyIndex<int32_t, float, ::Angular, ::Kiss64Random>(f);
this->f = f;
}
};

class AnnoyIndexEuclidean : public AnnoyIndex {
public:
AnnoyIndexEuclidean(int f) {
ptr = new ::AnnoyIndex<int32_t, float, ::Euclidean, ::Kiss64Random>(f);
this->f = f;
}
};

Expand Down
22 changes: 22 additions & 0 deletions test/annoy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,28 @@ func (suite *AnnoyTestSuite) TestGetNnsByItem() {
annoyindex.DeleteAnnoyIndexAngular(index)
}

func (suite *AnnoyTestSuite) TestGetItem() {
index := annoyindex.NewAnnoyIndexAngular(3)
index.AddItem(0, []float32{2, 1, 0})
index.AddItem(1, []float32{1, 2, 0})
index.AddItem(2, []float32{0, 0, 1})
index.Build(10)

var result []float32

index.GetItem(0, &result)
assert.Equal(suite.T(), []float32{2, 1, 0}, result)

index.GetItem(1, &result)
assert.Equal(suite.T(), []float32{1, 2, 0}, result)

index.GetItem(2, &result)
assert.Equal(suite.T(), []float32{0, 0, 1}, result)

annoyindex.DeleteAnnoyIndexAngular(index)
}


func (suite *AnnoyTestSuite) TestGetDistance() {
index := annoyindex.NewAnnoyIndexAngular(2)
index.AddItem(0, []float32{0, 1})
Expand Down

0 comments on commit e548d3d

Please sign in to comment.