예제 #1
0
        static void Main()
        {
            var musicians = new RankedSet <string> (StringComparer.InvariantCultureIgnoreCase);

            foreach (var m1 in new string[] { "Falco", "k.d. lang", "Madonna", "Tom Petty",
                                              "Joni Mitchell", "Grimes", "Warren Zevon" })
            {
                musicians.Add(m1);
            }

            Console.WriteLine("Candidates:");
            foreach (var item in musicians)
            {
                Console.WriteLine($"  {item}");
            }

            musicians.Remove("Falco");
            musicians.RemoveWhere(IsPolynymous);
            musicians.RemoveRange(1, musicians.Count - 1);

            Console.WriteLine("\nFavorite:");
            foreach (var item in musicians)
            {
                Console.WriteLine($"  {item}");
            }
        }
예제 #2
0
        static void Main()
        {
            int  maxN    = 500000;
            var  watch   = new Stopwatch();
            bool isPass1 = true;

            Console.WriteLine("Removals in Thousands,Set Size in Thousands,RankedSet,SortedSet");

            for (int n = maxN / 10; n <= maxN;)
            {
                var c1 = new RankedSet <int>();
                for (int i = 0; i < n; ++i)
                {
                    c1.Add(i);
                }
                watch.Reset(); watch.Start();
                c1.RemoveWhere(IsCull);
                var c1Time  = watch.ElapsedMilliseconds;
                int c1Count = c1.Count; c1.Clear();

                var c2 = new SortedSet <int>();
                for (int i = 0; i < n; ++i)
                {
                    c2.Add(i);
                }
                watch.Reset(); watch.Start();
                c2.RemoveWhere(IsCull);
                var c2Time  = watch.ElapsedMilliseconds;
                int c2Count = c2.Count; c2.Clear();

                int removes = n * division / divisionCount;
                if (c1Count != n - removes || c2Count != n - removes)
                {
                    Console.WriteLine($"*** ERROR *** {c1Count}, {c2Count}");
                }

                if (isPass1)
                {
                    isPass1 = false;
                }
                else
                {
                    Console.WriteLine($"{removes/1000},{n/1000},{c1Time},{c2Time}");
                    n += maxN / 10;
                }
            }
        }
예제 #3
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)}");
        }