Пример #1
0
        /// <summary>
        /// Демонстрация работы EasySet.
        /// </summary>
        public void EasySet()
        {
            var easyset1 = new EasySet <int>(new int[] { 1, 2, 3, 4, 5 });
            var easyset2 = new EasySet <int>(new int[] { 4, 5, 6, 7, 8 });
            var easyset3 = new EasySet <int>(new int[] { 3, 4, 5 });

            Console.WriteLine("Basic strings:");
            DisplayResult(easyset1);
            DisplayResult(easyset2);
            DisplayResult(easyset3);

            Console.Write("Union: ");
            DisplayResult(easyset1.Union(easyset2));

            Console.Write("Intersection: ");
            DisplayResult(easyset1.Intersection(easyset2));

            Console.Write("Difference A \\ B: ");
            DisplayResult(easyset1.Difference(easyset2));

            Console.Write("Difference B \\ A: ");
            DisplayResult(easyset2.Difference(easyset1));

            Console.Write("A Subset C: ");
            Console.Write($"{easyset1.Subset(easyset3)} \n");

            Console.Write("C Subset A: ");
            Console.Write($"{easyset3.Subset(easyset1)} \n");

            Console.Write("C Subset B: ");
            Console.Write($"{easyset1.Subset(easyset3)} \n");

            Console.Write("Symmetric Difference: ");
            DisplayResult(easyset1.SymmetricDifference(easyset2));
        }
Пример #2
0
        /// <summary>
        /// Пересечение.
        /// </summary>
        /// <param name="set">Данныех.</param>
        /// <returns>Результат пересечения.</returns>
        public EasySet <T> Intersection(EasySet <T> set)
        {
            var         result = new EasySet <T>();
            EasySet <T> big;
            EasySet <T> small;

            if (Count >= set.Count)
            {
                big   = this;
                small = set;
            }
            else
            {
                big   = set;
                small = this;
            }

            foreach (var item1 in small.items)
            {
                foreach (var item2 in big.items)
                {
                    if (item1.Equals(item2))
                    {
                        result.Add(item1);
                        break;
                    }
                }
            }

            return(result);
        }
Пример #3
0
 private void DisplayResult(EasySet <int> easySet)
 {
     foreach (var set in easySet)
     {
         Console.Write($"{set} ");
     }
     Console.WriteLine();
 }
Пример #4
0
        /// <summary>
        /// Разность.
        /// </summary>
        /// <param name="set">Данные.</param>
        /// <returns>Результат разности.</returns>
        public EasySet <T> Difference(EasySet <T> set)
        {
            var result = new EasySet <T>(items);

            foreach (var item in set.items)
            {
                result.Remove(item);
            }

            return(result);
        }
Пример #5
0
        /// <summary>
        /// Объединить.
        /// </summary>
        /// <param name="set">Данные.</param>
        /// <returns>Результат объединения.</returns>
        public EasySet <T> Union(EasySet <T> set)
        {
            EasySet <T> result = new EasySet <T>();

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

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

            return(result);
        }
Пример #6
0
        /// <summary>
        /// Симметричная разность.
        /// </summary>
        /// <param name="set">Данные.</param>
        /// <returns>Результат симметричной разности.</returns>
        public EasySet <T> SymmetricDifference(EasySet <T> set)
        {
            var result = new EasySet <T>();

            foreach (var item1 in items)
            {
                var equals = false;

                foreach (var item2 in set.items)
                {
                    if (item1.Equals(item2))
                    {
                        equals = true;

                        break;
                    }
                }

                if (equals == false)
                {
                    result.Add(item1);
                }
            }

            foreach (var item1 in set.items)
            {
                var equals = false;

                foreach (var item2 in items)
                {
                    if (item1.Equals(item2))
                    {
                        equals = true;

                        break;
                    }
                }

                if (equals == false)
                {
                    result.Add(item1);
                }
            }

            return(result);
        }
Пример #7
0
        /// <summary>
        /// Подмножество.
        /// </summary>
        /// <param name="set">Данные.</param>
        /// <returns>Результат подмножества.</returns>
        public bool Subset(EasySet <T> set)
        {
            foreach (var item1 in items)
            {
                var equals = false;

                foreach (var item2 in set.items)
                {
                    if (item1.Equals(item2))
                    {
                        equals = true;
                        break;
                    }
                }

                if (!equals)
                {
                    return(false);
                }
            }

            return(true);
        }