/// <summary>
        /// Seeks a target string, starting from a start string, with a given grouped similarities that must be part of a given source
        /// </summary>
        /// <param name="start">The start string to search from</param>
        /// <param name="target">The target string to find</param>
        /// <param name="source">The allowed contents that the search can be done at</param>
        /// <param name="similarityGroups">The similarity groups that were already found</param>
        /// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is <see cref="CancellationToken.None"/></param>
        /// <returns>True if the target is found</returns>
        public virtual async Task <bool> SeekTarget(string start, string target, ConcurrentHashSet <string> source, ConcurrentDictionary <string, ICollection <string> > similarityGroups, CancellationToken cancellationToken = default)
        {
            var pathsContent = new List <string>();

            pathsContent.Add(start);
            var foundTarget = false;

            while (pathsContent.Any() && !foundTarget && !cancellationToken.IsCancellationRequested)
            {
                var alreadyKnown = new ConcurrentHashSet <string>();
                var tasks        = new List <Task <bool> >();
                var length       = pathsContent.Count;
                _log.LogDebug("Iterating all paths seeking for target. Size is {length}", length);
                for (int i = 0; i < length; i++)
                {
                    var group = pathsContent.First();
                    pathsContent.Remove(group);

                    tasks.Add(Task.Run(() => IterateLetters(group, target, source, similarityGroups, alreadyKnown, pathsContent)));
                }
                bool[] foundInSeries = await Task.WhenAll(tasks);

                if (foundInSeries.Any(f => f))
                {
                    foundTarget = true;
                }

                source.RemoveWhere(alreadyKnown.Contains);
            }
            return(foundTarget);
        }
Пример #2
0
        public void RemoveWhere_Success()
        {
            var hashSet = new ConcurrentHashSet <string>();

            Parallel.For(0, 50000, i =>
            {
                hashSet.Add(i.ToString());
            });


            Parallel.For(0, 50000, i =>
            {
                if (i % 2 == 0)
                {
                    hashSet.RemoveWhere(ind => ind.Equals(i.ToString()));
                }
            });

            var count = 0;

            foreach (var index in hashSet)
            {
                count++;
            }
            Assert.AreEqual(25000, count);
        }
        public void ConcurrentHashSet()
        {
            ConcurrentHashSet <int> playSet  = new ConcurrentHashSet <int>();
            ConcurrentHashSet <int> smallSet = new ConcurrentHashSet <int>(new int[] { 0, 1 });
            ConcurrentHashSet <int> bigSet   = new ConcurrentHashSet <int>(new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, EqualityComparer <int> .Default);
            ConcurrentHashSet <int> nullSet  = null;

            Assert.AreEqual(EqualityComparer <int> .Default, bigSet.Comparer);
            Assert.IsFalse(string.IsNullOrEmpty(smallSet.ToString()));
            Assert.IsTrue(smallSet.IsProperSubsetOf(bigSet));
            Assert.IsTrue(smallSet.IsSubsetOf(bigSet));
            Assert.IsTrue(bigSet.IsProperSupersetOf(smallSet));
            Assert.IsTrue(bigSet.IsSupersetOf(smallSet));
            playSet.TryAdd(0);
            ((ICollection <int>)playSet).Add(1);
            Assert.IsTrue(playSet.Contains(0));
            Assert.IsTrue(smallSet.SetEquals(playSet));
            Assert.IsFalse(smallSet.SetEquals(bigSet));
            Assert.IsFalse(smallSet.SetEquals(null));
            Assert.IsTrue(smallSet.IsSubsetOf(playSet));
            Assert.IsFalse(bigSet.IsSubsetOf(playSet));
            Assert.IsFalse(bigSet.IsSubsetOf(null));
            Assert.IsFalse(playSet.IsSupersetOf(bigSet));
            Assert.IsTrue(playSet.IsSupersetOf(null));
            Assert.IsFalse(bigSet.IsSubsetOf(playSet));
            Assert.IsFalse(bigSet.IsSubsetOf(null));
            Assert.IsFalse(smallSet.IsProperSubsetOf(playSet));
            Assert.IsFalse(playSet.IsProperSupersetOf(smallSet));
            Assert.IsTrue(playSet.IsProperSupersetOf(null));
            Assert.IsFalse(smallSet.IsProperSubsetOf(null));
            Assert.IsFalse(smallSet.IsProperSubsetOf(new ConcurrentHashSet <int>(new int[] { 3, 4, 5 })));
            Assert.IsFalse(smallSet.IsSupersetOf(new ConcurrentHashSet <int>(new int[] { 3, 4, 5 })));
            Assert.IsFalse(smallSet.IsSubsetOf(new ConcurrentHashSet <int>(new int[] { 3, 4, 5 })));
            Assert.IsFalse(new ConcurrentHashSet <int>(new int[] { 3, 4, 5 }).IsProperSupersetOf(smallSet));
            Assert.IsFalse(new ConcurrentHashSet <int>(new int[] { 3, 4, 5 }).IsSupersetOf(smallSet));
            Assert.IsFalse(new ConcurrentHashSet <int>(new int[] { 3, 4, 5 }).Overlaps(smallSet));
            IEqualityComparer <ConcurrentHashSet <int> > setComparer = ConcurrentHashSet <int> .CreateSetComparer();

            Assert.IsTrue(setComparer.Equals(smallSet, playSet));
            Assert.IsFalse(setComparer.Equals(bigSet, playSet));
            Assert.AreEqual(setComparer.GetHashCode(smallSet), setComparer.GetHashCode(playSet));
            Assert.IsTrue(setComparer.Equals(smallSet, smallSet));
            Assert.IsFalse(setComparer.Equals(smallSet, nullSet));
            Assert.IsFalse(setComparer.Equals(nullSet, smallSet));
            Assert.IsFalse(smallSet.IsReadOnly);

            Assert.IsTrue(smallSet.Overlaps(bigSet));
            Assert.IsFalse(smallSet.Overlaps(null));

            int[] test = new int[2];
            smallSet.CopyTo(test);
            smallSet.CopyTo(test, 0);

            smallSet.Add(2);

            playSet.Remove(1);
            bigSet.ExceptWith(playSet);
            bigSet.ExceptWith(null);
            Assert.IsFalse(bigSet.Contains(0));
            Assert.IsTrue(bigSet.Contains(1));
            smallSet.IntersectWith(playSet);
            Assert.IsTrue(smallSet.Contains(0));
            Assert.IsFalse(smallSet.Contains(1));

            ConcurrentHashSet <int> niSet = new ConcurrentHashSet <int>();

            niSet.Add(0);
            Assert.IsFalse(niSet.IsEmpty);
            niSet.IntersectWith(null);
            Assert.IsTrue(niSet.IsEmpty);
            niSet.Add(0);
            Assert.AreEqual(1, niSet.Count);
            niSet.RemoveWhere(null);
            Assert.AreEqual(1, niSet.Count);

            smallSet.IntersectWith(playSet);

            playSet.Add(0);
            playSet.Add(2);
            playSet.RemoveWhere(n => n != 0);
            Assert.IsTrue(playSet.Contains(0));
            Assert.IsFalse(playSet.Contains(1));

            playSet.Add(0);
            playSet.Add(2);
            smallSet.Add(1);
            playSet.SymmetricExceptWith(smallSet);
            playSet.SymmetricExceptWith(null);
            Assert.IsFalse(playSet.Contains(0));
            Assert.IsTrue(playSet.Contains(1));
            Assert.IsTrue(playSet.Contains(2));

            playSet.UnionWith(smallSet);
            playSet.UnionWith(null);
            Assert.IsTrue(playSet.Contains(0));
            Assert.IsTrue(playSet.Contains(1));
            Assert.IsTrue(playSet.Contains(2));
            Assert.IsFalse(playSet.Contains(3));

            playSet.Clear();
            Assert.IsFalse(smallSet.IsSubsetOf(playSet));

            foreach (object o in ((System.Collections.IEnumerable)smallSet))
            {
                Assert.IsTrue(o is int);
                Assert.IsTrue((int)o < 10);
            }
        }