Esempio n. 1
0
 private void AddTerms(IEnumerable <Term> newTerms)
 {
     Terms.AddRange(
         from newTerm in newTerms
         where Terms.All(t => t.ID != newTerm.ID)
         select newTerm);
 }
Esempio n. 2
0
        public IEnumerable <Hit> Search(params string[] terms)
        {
            var locker = new Object();

            Terms = terms.Select(t => Formatter.Format(t)).ToList();
            var revList = Terms.Select(t => Reverse(t)).ToArray();

            Terms.AddRange(revList);
            Limit = Terms.OrderByDescending(t => t.Length).FirstOrDefault().Length;
            var hitCache = new List <int>();

            for (var pos = Start; pos < Stop; pos++)
            {
                var args = new ProgressUpdatedEventArgs(pos + 1);
                OnProgressUpdated(args);
                if (args.Cancel)
                {
                    break;
                }
                var length = Stop - pos;

                Parallel.For(FromSkip, ToSkip, new ParallelOptions
                {
                    MaxDegreeOfParallelism = ToSkip - FromSkip
                }, skip =>
                {
                    var text = Range(Text, pos, Stop);
                    var size = (int)Math.Ceiling((double)(length / skip));
                    if (size < Limit)
                    {
                        return;
                    }
                    var sequence = Sequence(text, skip);

                    foreach (var term in Terms)
                    {
                        var indices = IndicesOf(sequence, term);
                        if (indices.Length == 0)
                        {
                            continue;
                        }

                        foreach (var index in indices)
                        {
                            var hindex = ((index * skip) + pos);

                            if (!hitCache.Contains(hindex))
                            {
                                lock (locker)
                                {
                                    hitCache.Add(hindex);
                                    Hits.Add(new Hit(term, hindex, pos, skip));
                                }
                            }
                        }
                    }
                });
            }

            hitCache.Clear();

            if (Proximity != -1)
            {
                Hits = GetProximalHits(Proximity).ToList();
            }

            Grid = OffsetHitsToGrid(Hits);
            return(Hits);
        }