Exemplo n.º 1
0
	// Get the last index of a specific value within an array,
	// starting at a particular index.
	public static int LastIndexOf(Array array, Object value, int startIndex)
	{
		if(array == null)
		{
			throw new ArgumentNullException("array");
		}
		if(array.GetRank() != 1)
		{
			throw new RankException(_("Arg_RankMustBe1"));
		}
		if(startIndex < array.GetLowerBound(0) ||
		   startIndex > array.GetUpperBound(0) + 1)
		{
			throw new ArgumentOutOfRangeException
				("startIndex", _("Arg_InvalidArrayIndex"));
		}
		return InnerLastIndexOf(array, value, array.GetLowerBound(0),
								startIndex - array.GetLowerBound(0) + 1);
	}
Exemplo n.º 2
0
	// Get the last index of a specific value within an array,
	// starting at a particular index, searching for "count" items.
	public static int LastIndexOf(Array array, Object value,
							      int startIndex, int count)
	{
		if(array == null)
		{
			throw new ArgumentNullException("array");
		}
		if(array.GetRank() != 1)
		{
			throw new RankException(_("Arg_RankMustBe1"));
		}
		if(startIndex < array.GetLowerBound(0) ||
		   startIndex + count > array.GetUpperBound(0) + 1 ||
		   count < 0)
		{
			throw new ArgumentOutOfRangeException(_("Arg_InvalidArrayRange"));
		}
		return InnerLastIndexOf(array, value, startIndex - count + 1, count);
	}
Exemplo n.º 3
0
	// Implement the ICollection interface.
	public virtual void CopyTo(Array array, int index)
	{
		if(array == null)
		{
			throw new ArgumentNullException("array");
		}
		else if(GetRank() != 1)
		{
			throw new RankException(_("Arg_RankMustBe1"));
		}
		else if(array.GetRank() > 1)
		{
			throw new ArgumentException("array", _("Arg_RankMustBe1"));
		}
		else
		{
			Copy(this, GetLowerBound(0), array,
			     index + array.GetLowerBound(0), GetLength());
		}
	}
Exemplo n.º 4
0
	// Get the last index of a specific value within an array.
	public static int LastIndexOf(Array array, Object value)
	{
		if(array == null)
		{
			throw new ArgumentNullException("array");
		}
		if(array.GetRank() != 1)
		{
			throw new RankException(_("Arg_RankMustBe1"));
		}
		return InnerLastIndexOf(array, value, array.GetLowerBound(0),
					array.GetUpperBound(0) - array.GetLowerBound(0) + 1);
	}
Exemplo n.º 5
0
	// Perform a binary search within an array sub-range for a value,
	// using a specific element comparer.
	public static int BinarySearch(Array array, int index, int length,
								   Object value, IComparer comparer)
	{
		if(array == null)
		{
			throw new ArgumentNullException("array");
		}
		else if(array.GetRank() != 1)
		{
			throw new RankException(_("Arg_RankMustBe1"));
		}
		else if(index < array.GetLowerBound(0))
		{
			throw new ArgumentOutOfRangeException
				("index", _("ArgRange_Array"));
		}
		else if(length < 0)
		{
			throw new ArgumentOutOfRangeException
				("length", _("ArgRange_Array"));
		}
		else if((index - 1) > array.GetUpperBound(0) ||
		        length > (array.GetUpperBound(0) - index + 1))
		{
			throw new ArgumentException(_("Arg_InvalidArrayRange"));
		}
		return InnerBinarySearch(array, index, index + length - 1,
								 value, null);
	}
Exemplo n.º 6
0
	// Copy the contents of one array into another (general-purpose version).
	public static void Copy(Array sourceArray, int sourceIndex,
						    Array destinationArray,
						    int destinationIndex, int length)
	{
		// Validate the parameters.
		if(sourceArray == null)
		{
			throw new ArgumentNullException("sourceArray");
		}
		if(destinationArray == null)
		{
			throw new ArgumentNullException("destinationArray");
		}
		if(sourceArray.GetRank() != destinationArray.GetRank())
		{
			throw new RankException(_("Arg_MustBeSameRank"));
		}
		int srcLower = sourceArray.GetLowerBound(0);
		int srcLength = sourceArray.GetLength();
		int dstLower = destinationArray.GetLowerBound(0);
		int dstLength = destinationArray.GetLength();
		if(sourceIndex < srcLower)
		{
			throw new ArgumentOutOfRangeException
				("sourceIndex", _("ArgRange_Array"));
		}
		if(destinationIndex < dstLower)
		{
			throw new ArgumentOutOfRangeException
				("destinationIndex", _("ArgRange_Array"));
		}
		if(length < 0)
		{
			throw new ArgumentOutOfRangeException
				("length", _("ArgRange_NonNegative"));
		}
		int srcRelative = sourceIndex - srcLower;
		int dstRelative = destinationIndex - dstLower;
		if((srcLength - (srcRelative)) < length ||
		   (dstLength - (dstRelative)) < length)
		{
			throw new ArgumentException(_("Arg_InvalidArrayRange"));
		}

		// Get the array element types.
		Type arrayType1 = sourceArray.GetType().GetElementType();
		Type arrayType2 = destinationArray.GetType().GetElementType();

		// Is this a simple array copy of the same element type?
		if(arrayType1 == arrayType2)
		{
			InternalCopy
				(sourceArray, srcRelative,
				 destinationArray, dstRelative,
				 length);
			return;
		}

		// Check that casting between the types is possible,
		// without using a narrowing conversion.
		if(!ArrayTypeCompatible(arrayType1, arrayType2))
		{
			throw new ArrayTypeMismatchException
				(_("Exception_ArrayTypeMismatch")); 
		}

		// Copy the array contents the hard way.  We don't have to
		// worry about overlapping ranges because there is no way
		// to get here if the source and destination are the same.
		int index;
		for(index = 0; index < length; ++index)
		{
			try
			{
				destinationArray.SetRelative(
					Convert.ConvertObject(
						sourceArray.GetRelative(srcRelative + index), 
						arrayType2), dstRelative + index);
			}
			catch(FormatException e)
			{
				throw new InvalidCastException(String.Format(_("InvalidCast_FromTo"),
 						     arrayType1, arrayType2), e);
			}
		}
	}
Exemplo n.º 7
0
	// Sort an array sub-range of keys using a comparer.
	public static void Sort(Array array, int index, int length,
							IComparer comparer)
	{
		if(array == null)
		{
			throw new ArgumentNullException("array");
		}
		if(array.GetRank() != 1)
		{
			throw new RankException(_("Arg_RankMustBe1"));
		}
		if(index < array.GetLowerBound(0))
		{
			throw new ArgumentOutOfRangeException
				("index", _("ArgRange_Array"));
		}
		if(length < 0)
		{
			throw new ArgumentOutOfRangeException
				("length", _("ArgRange_Array"));
		}
		if(index > array.GetUpperBound(0) + 1 ||
		   length > (array.GetUpperBound(0) - index + 1))
		{
			throw new ArgumentException(_("Arg_InvalidArrayRange"));
		}
		if(comparer == null)
		{
			comparer = Comparer.Default;
		}
		InnerSort(array, null, index, index + length - 1, comparer);
	}
Exemplo n.º 8
0
	// Sort an array sub-range of keys and items using a comparer.
	public static void Sort(Array keys, Array items,
							int index, int length,
							IComparer comparer)
	{
		if(keys == null)
		{
			throw new ArgumentNullException("keys");
		}
		if(keys.GetRank() != 1)
		{
			throw new RankException(_("Arg_RankMustBe1"));
		}
		if(index < keys.GetLowerBound(0))
		{
			throw new ArgumentOutOfRangeException
				("index", _("ArgRange_Array"));
		}
		if(length < 0)
		{
			throw new ArgumentOutOfRangeException
				("length", _("ArgRange_Array"));
		}
		if(index > keys.GetUpperBound(0) ||
		   length > (keys.GetUpperBound(0) - index + 1))
		{
			throw new ArgumentException(_("Arg_InvalidArrayRange"));
		}
		if(items != null)
		{
			if(items.GetRank() != 1)
			{
				throw new RankException(_("Arg_RankMustBe1"));
			}
			if(index < items.GetLowerBound(0))
			{
				throw new ArgumentOutOfRangeException
					("index", _("ArgRange_Array"));
			}
			if(index > items.GetUpperBound(0) ||
			   length > (items.GetUpperBound(0) - index + 1))
			{
				throw new ArgumentException(_("Arg_InvalidArrayRange"));
			}
		}
		if(comparer == null)
		{
			comparer = Comparer.Default;
		}
		InnerSort(keys, items, index, index + length - 1, comparer);
	}
Exemplo n.º 9
0
	// Sort an array of keys and items using a comparer.
	public static void Sort(Array keys, Array items, IComparer comparer)
	{
		if(keys == null)
		{
			throw new ArgumentNullException("keys");
		}
		if(keys.GetRank() != 1)
		{
			throw new RankException(_("Arg_RankMustBe1"));
		}
		if(items != null)
		{
			if(items.GetRank() != 1)
			{
				throw new RankException(_("Arg_RankMustBe1"));
			}
			if(items.GetLowerBound(0) != keys.GetLowerBound(0))
			{
				throw new ArgumentException(_("Arg_LowBoundsMustMatch"));
			}
			if(items.GetLength() < keys.GetLength())
			{
				throw new ArgumentException(_("Arg_ShortItemsArray"));
			}
		}
		if(comparer == null)
		{
			comparer = Comparer.Default;
		}
		InnerSort(keys, items, keys.GetLowerBound(0),
				  keys.GetUpperBound(0), comparer);
	}
Exemplo n.º 10
0
	// Perform a binary search within an array for a value,
	// using a specific element comparer.
	public static int BinarySearch(Array array, Object value,
								   IComparer comparer)
	{
		if(array == null)
		{
			throw new ArgumentNullException("array");
		}
		else if(array.GetRank() != 1)
		{
			throw new RankException(_("Arg_RankMustBe1"));
		}
		return InnerBinarySearch(array, array.GetLowerBound(0),
								 array.GetUpperBound(0), value, comparer);
	}
Exemplo n.º 11
0
	// Sort an array of keys using a comparer.
	public static void Sort(Array array, IComparer comparer)
	{
		if(array == null)
		{
			throw new ArgumentNullException("array");
		}
		if(array.GetRank() != 1)
		{
			throw new RankException(_("Arg_RankMustBe1"));
		}
		if(comparer == null)
		{
			comparer = Comparer.Default;
		}
		InnerSort(array, null, array.GetLowerBound(0),
				  array.GetUpperBound(0), comparer);
	}
Exemplo n.º 12
0
	// Reverse the order of elements in an array sub-range.
	public static void Reverse(Array array, int index, int length)
	{
		if(array == null)
		{
			throw new ArgumentNullException("array");
		}
		if(array.GetRank() != 1)
		{
			throw new RankException(_("Arg_RankMustBe1"));
		}
		if(index < array.GetLowerBound(0))
		{
			throw new ArgumentOutOfRangeException
				("index", _("ArgRange_Array"));
		}
		if(length < 0)
		{
			throw new ArgumentOutOfRangeException
				("length", _("ArgRange_Array"));
		}
		if(index > array.GetUpperBound(0) ||
	       length > (array.GetUpperBound(0) - index + 1))
		{
			throw new ArgumentException(_("Arg_InvalidArrayRange"));
		}
		InnerReverse(array, index, index + length - 1);
	}
Exemplo n.º 13
0
	// Reverse the order of elements in an array.
	public static void Reverse(Array array)
	{
		if(array == null)
		{
			throw new ArgumentNullException("array");
		}
		if(array.GetRank() != 1)
		{
			throw new RankException(_("Arg_RankMustBe1"));
		}
		InnerReverse(array, array.GetLowerBound(0), array.GetUpperBound(0));
	}