public AnnoyIndexSearchResult GetNearestToItem( ulong itemIndex, uint nResult, int searchK, bool shouldIncludeDistance) { if (_indexPtr == IntPtr.Zero) { throw new ObjectDisposedException("index"); } var searchResultPtr = NativeMethods.GetNearestToItem( _indexPtr, itemIndex, nResult, searchK, shouldIncludeDistance); try { return(AnnoyIndexSearchResult.LoadFromPtr(searchResultPtr, shouldIncludeDistance)); } finally { NativeMethods.FreeSearchResult(searchResultPtr); } }
internal static AnnoyIndexSearchResult LoadFromPtr(IntPtr searchResult, bool isDistanceIncluded) { var count = (int)NativeMethods.GetResultCount(searchResult); var result = new AnnoyIndexSearchResult { Count = count, IsDistanceIncluded = isDistanceIncluded, }; if (count > 0) { var idList = new long[count]; var idListPtr = NativeMethods.GetIdList(searchResult); Marshal.Copy(idListPtr, idList, 0, count); result.IdList = idList; if (isDistanceIncluded) { var distanceList = new float[count]; var distanceListPtr = NativeMethods.GetDistanceList(searchResult); Marshal.Copy(distanceListPtr, distanceList, 0, count); result.DistanceList = distanceList; } } return(result); }
public AnnoyIndexSearchResult GetNearest( IReadOnlyList <float> queryVector, uint nResult, int searchK, bool shouldIncludeDistance) { if (_indexPtr == IntPtr.Zero) { throw new ObjectDisposedException("index"); } var searchResultPtr = NativeMethods.GetNearest( _indexPtr, queryVector.ToArray(), nResult, searchK, shouldIncludeDistance); try { return(AnnoyIndexSearchResult.LoadFromPtr(searchResultPtr, shouldIncludeDistance)); } finally { NativeMethods.FreeSearchResult(searchResultPtr); } }