Example #1
0
File: MySet.cs Project: xJCM/Csharp
        public MySet <T> UnionWith(MySet <T> b)
        {
            var resultSet = new MySet <T>();

            foreach (var element in this)
            {
                resultSet.Add(element);
            }

            foreach (var element in b)
            {
                resultSet.Add(element);
            }

            return(resultSet);
        }
Example #2
0
File: MySet.cs Project: xJCM/Csharp
        public MySet <T> IntersectWith(MySet <T> b)
        {
            var resultSet = new MySet <T>();

            foreach (var element in this)
            {
                if (b.Contains(element))
                {
                    resultSet.Add(element);
                }
            }

            return(resultSet);
        }
Example #3
0
        /// <summary>
        /// Изменяет текущий набор, чтобы он содержал только элементы, которые также имеются в заданной коллекции
        /// </summary>
        public void IntersectWith(IEnumerable <T> other)
        {
            var newSet = new MySet <T>();

            foreach (var element in other)
            {
                if (FindNode(head, element))
                {
                    newSet.Add(element);
                }
            }
            head  = newSet.head;
            Count = newSet.Count;
            newSet.Clear();
        }
Example #4
0
        /// <summary>
        /// Определяет, является ли текущий набор подмножеством заданной коллекции
        /// </summary>
        public bool IsSubsetOf(IEnumerable <T> other)
        {
            var tempSet = new MySet <T>();

            foreach (var element in other)
            {
                tempSet.Add(element);
            }

            foreach (var element in this)
            {
                if (!tempSet.FindNode(tempSet.head, element))
                {
                    return(false);
                }
            }

            return(true);
        }
Example #5
0
        static void Main(string[] args)
        {
            MySet <string> set = new MySet <string>();

            set.Add("Test");
            set.Add("Another one");
            set.Add("Test");
            set.Add("12345");

            foreach (var s in set)
            {
                Console.WriteLine(s);
            }

            Console.WriteLine("Number of elements in set: {0}", set.Count);

            set.Remove("12345");

            foreach (var s in set)
            {
                Console.WriteLine(s);
            }

            Console.WriteLine("Is set empty? {0}", set.IsEmpty);
            set.Clear();
            Console.WriteLine("Is set empty? {0}", set.IsEmpty);

            MySet <string> a = new MySet <string>
            {
                "Test",
                "12345",
                "May the force be with you"
            };

            MySet <string> b = new MySet <string>
            {
                "May the force be with you",
                "Bazinga",
                "12345",
                "Test"
            };

            //Subset:
            Console.WriteLine("Is a subset of b? {0}", a.IsSubsetOf(b));
            Console.WriteLine("Is b subset of a? {0}", b.IsSubsetOf(a));

            //Union:
            var unionSet = a.UnionWith(b);

            Console.WriteLine("Union:");

            foreach (var element in unionSet)
            {
                Console.WriteLine(element);
            }

            //Intersection:
            var intersectSet = a.IntersectWith(b);

            Console.WriteLine("Intersection:");

            foreach (var element in intersectSet)
            {
                Console.WriteLine(element);
            }
        }