Пример #1
0
        public HashedSet <T> UnionWith(params ICollection <T>[] sets)
        {
            var result = new HashedSet <T>();

            foreach (var item in this)
            {
                if (!result.Contains(item))
                {
                    result.Add(item);
                }
            }

            foreach (var set in sets)
            {
                foreach (var item in set)
                {
                    if (!result.Contains(item))
                    {
                        result.Add(item);
                    }
                }
            }

            return(result);
        }
Пример #2
0
        public static void Main()
        {
            var firstSet = new HashedSet <int>()
            {
                1, 2, 3, 4, 5, 5, 5, 5, 3, 2, 2, 1, 2, 3
            };                                                                                // 1, 2, 3, 4, 5

            firstSet.Add(7);
            firstSet.Add(7);
            firstSet.Add(7);
            firstSet.Add(7);
            firstSet.Add(7);

            var secondSet = new HashedSet <int>()
            {
                2, 3, 4, 8
            };

            Console.WriteLine("First set before removing 5:");
            PrintSet(firstSet);

            Console.WriteLine("\nFirst set after removing 5:");
            firstSet.Remove(5);
            PrintSet(firstSet);

            var union = HashedSet <int> .Union(firstSet, secondSet);

            Console.WriteLine("\nUnion {{{0}}} and {{{1}}}", string.Join(", ", firstSet), string.Join(", ", secondSet));
            PrintSet(union);

            var intersect = HashedSet <int> .Intersect(firstSet, secondSet);

            Console.WriteLine("\nIntersect {{{0}}} and {{{1}}}", string.Join(", ", firstSet), string.Join(", ", secondSet));
            PrintSet(intersect);
        }
Пример #3
0
        public HashedSet <T> Union(HashedSet <T> other)
        {
            var result = new HashedSet <T>();

            foreach (var myNode in this)
            {
                result.Add(myNode);
            }

            foreach (var otherNode in other)
            {
                result.Add(otherNode);
            }

            return(result);
        }
Пример #4
0
        public static HashedSet <T> Union(HashedSet <T> firstSet, HashedSet <T> secondSet)
        {
            HashedSet <T> result = new HashedSet <T>();

            foreach (var item in firstSet)
            {
                result.Add(item);
            }

            foreach (var item in secondSet)
            {
                result.Add(item);
            }

            return(result);
        }
Пример #5
0
        public HashedSet <T> UnionWith(IEnumerable <T> otherSet)
        {
            var result = new HashedSet <T>();

            foreach (var item in this)
            {
                if (!result.Contains(item))
                {
                    result.Add(item);
                }
            }

            foreach (var item in otherSet)
            {
                if (!result.Contains(item))
                {
                    result.Add(item);
                }
            }

            return(result);
        }
Пример #6
0
        private static HashedSet <T> AddRepeatableItems(HashedSet <T> biggerSet, HashedSet <T> smallerSet)
        {
            var result = new HashedSet <T>();

            foreach (var item in smallerSet)
            {
                var foundItem = biggerSet.Find(item);

                if (foundItem.CompareTo(default(T)) != 0)
                {
                    result.Add(item);
                }
            }

            return(result);
        }
Пример #7
0
        public HashedSet <T> IntersectsWith(HashedSet <T> other)
        {
            var result = new HashedSet <T>();

            foreach (var myNode in this)
            {
                foreach (var otherNode in other)
                {
                    if (myNode.Equals(otherNode))
                    {
                        result.Add(myNode);
                    }
                }
            }

            return(result);
        }
Пример #8
0
        public HashedSet <T> IntersectWith(params ICollection <T>[] sets)
        {
            var result = new HashedSet <T>();

            foreach (var set in sets)
            {
                foreach (var item in set)
                {
                    if (this.Contains(item) && sets.All(s => s.Contains(item)))
                    {
                        result.Add(item);
                    }
                }
            }

            return(result);
        }