Ejemplo n.º 1
0
        static void Main()
        {
            var set = new RankedSet <int> {
                3, 5, 7
            };
            var arg = new int[] { 5, 7, 9 };

            var ew = new RankedSet <int> (set);
            var iw = new RankedSet <int> (set);
            var se = new RankedSet <int> (set);
            var uw = new RankedSet <int> (set);

            ew.ExceptWith(arg);
            iw.IntersectWith(arg);
            se.SymmetricExceptWith(arg);
            uw.UnionWith(arg);

            Console.WriteLine($"{Text(set)} ExceptWith {Text(arg)} = {Text(ew)}");
            Console.WriteLine($"{Text(set)} IntersectWith {Text(arg)} = {Text(iw)}");
            Console.WriteLine($"{Text(set)} SymmetricExceptWith {Text(arg)} = {Text(se)}");
            Console.WriteLine($"{Text(set)} UnionWith {Text(arg)} = {Text(uw)}");
        }
Ejemplo n.º 2
0
        static void Main()
        {
            var names1 = new string[] { "Falco", "Nico", "David Bowie", "Tom Petty", "Joni Mitchell", "Warren Zevon" };
            var names2 = new string[] { "Michelangelo", "Rembrandt", "Joni Mitchell", "David Bowie" };

            var musicians = new RankedSet <string> (names1);

            // Remove mononymous items.
            Console.WriteLine("Remove single names from the set...");
            Console.WriteLine($"  Count before: {musicians.Count}");
            musicians.RemoveWhere(IsMononymous);
            Console.WriteLine($"  Count after: {musicians.Count}\n");

            // List names starting with 'J'.
            Console.WriteLine("Musicians J-T");
            foreach (var name in musicians.ElementsBetween("J", "U"))
            {
                Console.WriteLine($"  {name}");
            }

            // Create another RankedSet.
            var painters = new RankedSet <string> (names2);

            // Remove elements in musicians that are also in painters.
            Console.WriteLine("\nRemove duplicates (of painters) from the musicians...");
            Console.WriteLine($"  Count before: {musicians.Count}");
            musicians.ExceptWith(painters);
            Console.WriteLine($"  Count after: {musicians.Count}\n");

            Console.WriteLine("List of musicians that are not painters:");
            foreach (string name in musicians)
            {
                Console.WriteLine($"  {name}");
            }

            var comp = RankedSet <string> .CreateSetComparer();

            HashSet <RankedSet <string> > setOfSets = new HashSet <RankedSet <string> > (comp);

            setOfSets.Add(musicians);
            setOfSets.Add(painters);

            Console.WriteLine("\nAll sets in hash set:");
            foreach (var set in setOfSets)
            {
                Console.WriteLine($"  {set.Count} items:");
                foreach (var item in set)
                {
                    Console.WriteLine($"    {item}");
                }
            }

            // Create a 3rd RankedSet.
            var people = new RankedSet <string> {
                "Tom Petty", "Warren Zevon"
            };

            // Create a set equality comparer.
            var comparer = RankedSet <string> .CreateSetComparer();

            Console.WriteLine($"\nSet comparison 1: {comparer.Equals (musicians, people)}");
            Console.WriteLine($"Set comparison 2: {comparer.Equals (painters, people)}");
        }