Example #1
0
            internal Core(Func <TItem, TItem, TDistance> distance, SmallWorld <TItem, TDistance> .Parameters parameters)
            {
                Distance   = distance;
                Parameters = parameters;

                var initialSize = Math.Max(1024, parameters.InitialItemsSize);

                Nodes = new List <Node>(initialSize);
                Items = new List <TItem>(initialSize);

                switch (Parameters.NeighbourHeuristic)
                {
                case NeighbourSelectionHeuristic.SelectSimple:
                {
                    Algorithm = new Algorithms.Algorithm3 <TItem, TDistance>(this);
                    break;
                }

                case NeighbourSelectionHeuristic.SelectHeuristic:
                {
                    Algorithm = new Algorithms.Algorithm4 <TItem, TDistance>(this);
                    break;
                }
                }

                if (Parameters.EnableDistanceCacheForConstruction)
                {
                    DistanceCache = new DistanceCache <TDistance>();
                    DistanceCache.Resize(parameters.InitialDistanceCacheSize, false);
                }

                DistanceCalculationsCount = 0;
            }
Example #2
0
            internal IReadOnlyList <int> AddItems(IReadOnlyList <TItem> items, IProvideRandomValues generator, CancellationToken cancellationToken)
            {
                int newCount = items.Count;

                var newIDs = new List <int>();

                Items.AddRange(items);
                DistanceCache?.Resize(newCount, false);

                int id0 = Nodes.Count;

                for (int id = 0; id < newCount; ++id)
                {
                    Nodes.Add(Algorithm.NewNode(id0 + id, RandomLayer(generator, Parameters.LevelLambda)));
                    newIDs.Add(id0 + id);
                    cancellationToken.ThrowIfCancellationRequested();
                }
                return(newIDs);
            }