Example #1
0
    /// <summary>
    /// Returns a new array containing the input array's elements in reverse order.
    /// </summary>
    /// <param name="input">The input array.</param>
    /// <returns>A reversed clone of the input array.</returns>
    public static Array ReverseArray(Array input)
    {
        Array clone = (Array)input.Clone();
        for (int g = 0; g < clone.Length; g++)
        {
            clone.SetValue(input.GetValue(input.Length - g - 1), g);
        }

        return clone;
    }
Example #2
0
        public static Array Add(Array aFirst, Array aSecond)
        {
            if (aFirst ==  null)
            {
                return aSecond.Clone() as Array;
            }

            if (aSecond == null)
            {
                return aFirst.Clone() as Array;
            }

            Type typeFirst = aFirst.GetType().GetElementType();
            Type typeSecond = aSecond.GetType().GetElementType();

            System.Diagnostics.Debug.Assert(typeFirst == typeSecond);

            Array aNewArray = Array.CreateInstance(typeFirst, aFirst.Length + aSecond.Length);
            aFirst.CopyTo(aNewArray, 0);
            aSecond.CopyTo(aNewArray, aFirst.Length);

            return aNewArray;
        }
Example #3
0
		private static void AssertArrayEqualsSorted (Array o1, Array o2)
		{
			Array s1 = (Array) o1.Clone ();
			Array s2 = (Array) o2.Clone ();

			Array.Sort (s1);
			Array.Sort (s2);

			Assert.AreEqual (s1.Length, s2.Length, "#1");
			for (int i = 0; i < s1.Length; ++i)
				Assert.AreEqual (s1.GetValue (i), s2.GetValue (i), "#2: " + i);
		}
Example #4
0
		/// <summary>
		/// The actual implementation
		/// </summary>
		/// <param name="primary"></param>
		/// <param name="left"></param>
		/// <param name="right"></param>
		/// <param name="compare"></param>
		protected void InternalSort(
		ref Array primary,
		int left,
		int right,
		System.Collections.IComparer compare)
		{
			if (secondary == null || secondary.Length != primary.Length)
				secondary = (Array)primary.Clone();

			if (right > left)
			{
				int middle = (left + right) / 2;
				InternalSort(ref primary, left, middle, compare);
				InternalSort(ref primary, middle + 1, right, compare);

				int i, j, k;
				for (i = middle + 1; i > left; i--)
					secondary.SetValue(primary.GetValue(i - 1), i - 1);
				for (j = middle; j < right; j++)
					secondary.SetValue(primary.GetValue(j + 1), right + middle - j);
				for (k = left; k <= right; k++)
					primary.SetValue(
					(compare.Compare(secondary.GetValue(i), secondary.GetValue(j)) < 0) ?
					secondary.GetValue(i++) :
					secondary.GetValue(j--), k);
			}
		}
        private static Array GetMergedArray(object selected, Array oldArray, Array newArray, bool[] equalComponents)
        {
            Array result = newArray;
            if (oldArray != null)
            {
                result = (Array)oldArray.Clone();
                for (int i = 0; i < newArray.Length; i++)
                    if (!equalComponents[i])
                        result.SetValue(newArray.GetValue(i), i);
            }

            return result;
        }
Example #6
0
        /// <summary>Writes the data to the wrapped array starting with the specified origin indices.
        /// </summary>
        /// <param name="origin">Indices to start adding of data. Null means all zeros.</param>
        /// <param name="a">Data to add to the variable.</param>
        public virtual void PutData(int[] origin, Array a)
        {
            if (a == null) return;
            if (rank == 0)
            {
                if (a.Rank != 1)
                    throw new Exception("Wrong rank");
                array.SetValue(a.GetValue(0), 0);
                return;
            }

            if (rank != a.Rank)
                throw new Exception("Wrong rank");

            int[] shape = null;

            if (array == null)
            {
                if (origin == null || IsZero(origin))
                {
                    array = (Array)a.Clone();
                    return;
                }

                shape = new int[rank];
                for (int i = 0; i < rank; i++)
                    shape[i] = origin[i] + a.GetLength(i);

                array = Array.CreateInstance(type, shape);
                CopyArray(a, array, new int[origin.Length], origin, true);
            }
            else
            {
                Array oldArr = array;

                if (origin == null)
                    origin = new int[rank];

                shape = new int[array.Rank];
                bool sufficient = true;
                for (int i = 0; i < array.Rank; i++)
                {
                    int size = origin[i] + a.GetLength(i);
                    sufficient = sufficient && (size <= oldArr.GetLength(i));

                    shape[i] = Math.Max(size, oldArr.GetLength(i));
                }

                Array newArr;
                if (sufficient)
                {
                    newArr = oldArr;
                }
                else
                {
                    newArr = Array.CreateInstance(type, shape);
                    CopyArray(oldArr, newArr, new int[array.Rank], new int[array.Rank],true);
                }

                if (origin == null)
                    origin = new int[array.Rank];
                CopyArray(a, newArr, new int[origin.Length], origin, true);
                array = newArr;
            }

            return;// shape;
        }