Esempio n. 1
0
        static OrderedSet <T> GetSetUsingPreorderedSet <T>(T[] orderedSet)
        {
            //
            // Check if this set is already created
            //
            for (int i = 0; i < OrderedSet <T> .allCreatedSets.Count; i++)
            {
                OrderedSet <T> existingSet = OrderedSet <T> .allCreatedSets[i];
                if (orderedSet.Length == existingSet.orderedSet.Length)
                {
                    Boolean isMatch = true;
                    for (int j = 0; j < orderedSet.Length; j++)
                    {
                        if (!orderedSet[j].Equals(existingSet.orderedSet[j]))
                        {
                            isMatch = false;
                            break;
                        }
                    }
                    if (isMatch)
                    {
                        return(existingSet);
                    }
                }
            }

            //
            // Create a new ordered set
            //
            OrderedSet <T> newSet = new OrderedSet <T>(orderedSet);

            OrderedSet <T> .allCreatedSets.Add(newSet);

            return(newSet);
        }
Esempio n. 2
0
        public void TestVerifySorted()
        {
            OrderedSet <int> set;

            set = OrderedSet.VerifySortedAndGetSet(new int[] { });
            set = OrderedSet.VerifySortedAndGetSet(new int[] { 0 });

            TestArray(new int[] { 0, 1 });
            TestArray(new int[] { 0, 1, 2 });
            TestArray(new int[] { 0, 1, 2, 10, 100, 1774 });


            try
            {
                set = OrderedSet.VerifySortedAndGetSet(new int[] { 0, 0 });
                Assert.Fail();
            }
            catch (ArgumentException e)
            {
                Console.WriteLine("Got expected exception '{0}'", e.Message);
            }

            try
            {
                set = OrderedSet.VerifySortedAndGetSet(new int[] { 0, 1, 1, 2 });
                Assert.Fail();
            }
            catch (ArgumentException e)
            {
                Console.WriteLine("Got expected exception '{0}'", e.Message);
            }
        }
Esempio n. 3
0
        public void TestCombined2()
        {
            OrderedSet <Int32> First = OrderedSet.SortArrayAndGetSet(new Int32[] { 0, 1, 3 });

            AssertEquals(new Int32[] { 0, 1, 3 }, First.orderedSet);

            OrderedSet <Int32> Second = First.Combine(OrderedSet.SortArrayAndGetSet(new Int32[] { 0, 2 }));

            AssertEquals(new Int32[] { 0, 1, 2, 3 }, Second.orderedSet);
        }
Esempio n. 4
0
 void TestSet(OrderedSet <Int32> set)
 {
     Assert.AreEqual(-1, set.IndexOf(set.orderedSet[0] - 1));
     for (int i = 0; i < set.orderedSet.Length; i++)
     {
         //Console.WriteLine("Testing IndexOf({0}) == {1}", set.IndexOf(set.orderedSet[i]), i);
         Assert.AreEqual(i, set.IndexOf(set.orderedSet[i]));
     }
     Assert.AreEqual(-1, set.IndexOf(set.orderedSet[set.orderedSet.Length - 1] + 1));
 }
Esempio n. 5
0
        public void TestSortArray()
        {
            OrderedSet <int> set;

            try
            {
                set = OrderedSet.SortArrayAndGetSet(new int[] { 0, 0 });
                Assert.Fail();
            }
            catch (ArgumentException e)
            {
                Console.WriteLine("Got expected exception '{0}'", e.Message);
            }
        }
Esempio n. 6
0
        // NOTE: this can probably be made more efficient
        public static OrderedSet <T> Combine <T>(this IEnumerable <OrderedSet <T> > sets)
        {
            IEnumerator <OrderedSet <T> > enumerator = sets.GetEnumerator();

            if (!enumerator.MoveNext())
            {
                return(default(OrderedSet <T>));
            }

            OrderedSet <T> combined = enumerator.Current;

            while (enumerator.MoveNext())
            {
                combined = combined.Combine(enumerator.Current);
            }

            return(combined);
        }
Esempio n. 7
0
        void TestArray <T>(T[] array)
        {
            OrderedSet <T> set;

            set = OrderedSet.VerifySortedAndGetSet(array);

            Array.Reverse(array);
            try
            {
                set = OrderedSet.VerifySortedAndGetSet(array);
                Assert.Fail();
            }
            catch (ArgumentException e)
            {
                Console.WriteLine("Got expected exception '{0}'", e.Message);
            }

            set = OrderedSet.SortArrayAndGetSet(array);
            set = OrderedSet.VerifySortedAndGetSet(array);
        }
Esempio n. 8
0
 public void TestIndexOf()
 {
     TestSet(OrderedSet.VerifySortedAndGetSet(new Int32[] { 0, 1, 2, 3, 4 }));
     TestSet(OrderedSet.VerifySortedAndGetSet(new Int32[] { -14, 10, 20, 55, 98 }));
 }