Example #1
0
        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);
            }
        }
Example #2
0
        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);
        }
Example #3
0
        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);
            }
        }