예제 #1
0
        /// <summary>
        /// Instantiate the native resource wrapped
        /// </summary>
        /// <param name="args">The parameter is not used.</param>
        /// <returns>A pointer to the native resource.</returns>
        protected override IntPtr CreateNativeResources(params object[] args)
        {
            if (args.Length != 5)
            {
                throw new ArgumentException("Fuzzy matcher needs parameters to instantiate native resource.");
            }

            this.isAccelerated = (bool)args[0];
            var targets  = args[1] as IList <Target>;
            var distance = args[2] as DistanceFunc;
            var extractionToPronounceable = args[3] as Func <Extraction, Pronounceable>;
            var targetToExtraction        = args[4] as Func <Target, Extraction>;

            // Managed object needs to be initialized *before* creating the native fuzzy matcher.
            this.InitializeManaged(targets, distance, targetToExtraction, extractionToPronounceable);

            int targetsCount = targets.Count;

            IntPtr native = IntPtr.Zero;

            NativeResourceWrapper.CallNative((buffer) =>
            {
                int bufferSize = NativeResourceWrapper.BufferSize;
                var result     = FuzzyMatcherBase.FuzzyMatcher_Create(targetsCount, this.nativeDistanceDelegate, this.isAccelerated, out native, buffer, ref bufferSize);
                NativeResourceWrapper.BufferSize = bufferSize;
                return(result);
            });

            return(native);
        }
예제 #2
0
 /// <summary>
 /// Makes the native call to FindNearestWithin method virtual so we can use normal or accelerated version.
 /// </summary>
 /// <param name="native">Pointer to the native FuzzyMatcher object</param>
 /// <param name="count">maximum number of elements to retrieve</param>
 /// <param name="limit">threshold under which we have a match using the fuzzy matcher</param>
 /// <param name="nearestIdxs">array in which result elements are stored</param>
 /// <param name="distances">array in which result distances are stored</param>
 /// <param name="buffer">buffer for error message</param>
 /// <param name="bufferSize">size of the buffer</param>
 /// <returns>The result of the native operation.</returns>
 protected virtual NativeResult NativeFindNearestWithin(IntPtr native, int count, double limit, int[] nearestIdxs, double[] distances, StringBuilder buffer, ref int bufferSize)
 {
     if (this.isAccelerated)
     {
         return(FuzzyMatcherBase.AcceleratedFuzzyMatcher_FindNearestWithin(native, count, limit, nearestIdxs, distances, buffer, ref bufferSize));
     }
     else
     {
         return(FuzzyMatcherBase.FuzzyMatcher_FindNearestWithin(native, count, limit, nearestIdxs, distances, buffer, ref bufferSize));
     }
 }