예제 #1
0
        /// <summary>
        /// Executes 'func' for each element in the GrowableArray and returns a GrowableArray
        /// for the result.
        /// </summary>
        public GrowableArray <T1> Foreach <T1>(Func <T, T1> func)
        {
            var ret = new GrowableArray <T1>();

            ret.Count = Count;

            for (int i = 0; i < Count; i++)
            {
                ret[i] = func(array[i]);
            }
            return(ret);
        }
예제 #2
0
        // Unit testing.  It is reasonable coverage, but concentrates on BinarySearch as that is the one that is
        // easy to get wrong.
#if TESTING
        public static void TestGrowableArray()
        {
            GrowableArray <float> testArray = new GrowableArray <float>();

            for (float i = 1.1F; i < 10; i += 2)
            {
                int successes = TestBinarySearch(testArray);
                Debug.Assert(successes == ((int)i) / 2);
                testArray.Add(i);
            }

            for (float i = 0.1F; i < 11; i += 2)
            {
                int index;
                bool result = testArray.BinarySearch(i, out index, delegate(float key, float elem) { return((int)key - (int)elem); });
                Debug.Assert(!result);
                testArray.InsertAt(index + 1, i);
            }

            int lastSuccesses = TestBinarySearch(testArray);

            Debug.Assert(lastSuccesses == 11);

            for (float i = 0; i < 11; i += 1)
            {
                int index;
                bool result = testArray.BinarySearch(i, out index, delegate(float key, float elem) { return((int)key - (int)elem); });
                Debug.Assert(result);
                testArray.InsertAt(index + 1, i);
            }

            lastSuccesses = TestBinarySearch(testArray);
            Debug.Assert(lastSuccesses == 11);

            // We always get the last one when the equality comparision allows multiple items to match.
            for (float i = 0; i < 11; i += 1)
            {
                int index;
                bool result = testArray.BinarySearch(i, out index, delegate(float key, float elem) { return((int)key - (int)elem); });
                Debug.Assert(result);
                Debug.Assert(i == testArray[index]);
            }
            Console.WriteLine("Done");
        }
예제 #3
0
        private static int TestBinarySearch(GrowableArray <float> testArray)
        {
            int successes = 0;

            for (int i = 0; i < 30; i++)
            {
                int index;
                if (testArray.BinarySearch(i, out index, delegate(float key, float elem) { return((int)key - (int)elem); }))
                {
                    successes++;
                    Debug.Assert((int)testArray[index] == i);
                }
                else
                {
                    Debug.Assert(index + 1 <= testArray.Count);
                }
            }
            return(successes);
        }
예제 #4
0
 internal GrowableArrayEnumerator(GrowableArray <T> growableArray)
 {
     cur   = -1;
     end   = growableArray.arrayLength;
     array = growableArray.array;
 }