/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
/* ================================================================
* About NPOI
* Author: Tony Qu
* Author's email: tonyqus (at) gmail.com
* Author's Blog: tonyqus.wordpress.com.cn (wp.tonyqus.cn)
* HomePage: http://www.codeplex.com/npoi
* Contributors:
*
* ==============================================================*/
using System;
using System.Collections;
using System.Text;
namespace HH.WMS.Utils.NPOI.Util
{
public class Arrays
{
///
/// Fills the specified array.
///
/// The array.
/// The default value.
public static void Fill(byte[] array,byte defaultValue)
{
for (int i = 0; i < array.Length; i++)
{
array[i] = defaultValue;
}
}
public static void Fill(char[] array, char defaultValue)
{
for (int i = 0; i < array.Length; i++)
{
array[i] = defaultValue;
}
}
public static void Fill(T[] array, T defaultValue)
{
for (int i = 0; i < array.Length; i++)
{
array[i] = defaultValue;
}
}
///
/// Assigns the specified byte value to each element of the specified
/// range of the specified array of bytes. The range to be filled
/// extends from index fromIndex, inclusive, to index
/// toIndex, exclusive. (If fromIndex==toIndex, the
/// range to be filled is empty.)
///
/// the array to be filled
/// the index of the first element (inclusive) to be filled with the specified value
/// the index of the last element (exclusive) to be filled with the specified value
/// the value to be stored in all elements of the array
/// if fromIndex > toIndex
/// if fromIndex < 0 or toIndex > a.length
public static void Fill(byte[] a, int fromIndex, int toIndex, byte val)
{
RangeCheck(a.Length, fromIndex, toIndex);
for (int i = fromIndex; i < toIndex; i++)
a[i] = val;
}
///
/// Checks that {@code fromIndex} and {@code toIndex} are in
/// the range and throws an appropriate exception, if they aren't.
///
///
///
///
private static void RangeCheck(int length, int fromIndex, int toIndex)
{
if (fromIndex > toIndex)
{
throw new ArgumentException(
"fromIndex(" + fromIndex + ") > toIndex(" + toIndex + ")");
}
if (fromIndex < 0)
{
throw new IndexOutOfRangeException("fromIndex(" + fromIndex + ")");
}
if (toIndex > length)
{
throw new IndexOutOfRangeException( "toIndex(" + toIndex + ")");
}
}
///
/// Convert Array to ArrayList
///
/// source array
///
public static ArrayList AsList(Array arr)
{
if (arr.Length <= 0)
return new ArrayList();
ArrayList al = new ArrayList(arr.Length);
for (int i = 0; i < arr.Length; i++)
{
al.Add(arr.GetValue(i));
}
return al;
}
///
/// Fills the specified array.
///
/// The array.
/// The default value.
public static void Fill(int[] array, byte defaultValue)
{
for (int i = 0; i < array.Length; i++)
{
array[i] = defaultValue;
}
}
///
/// Equals the specified a1.
///
/// The a1.
/// The b1.
///
public new static bool Equals(object a1, object b1)
{
if (a1 == null || b1 == null)
return false;
Array a = a1 as Array;
Array b = b1 as Array;
if (a.Length != b.Length)
return false;
for (int i = 0; i < a.Length; i++)
{
if (!a.GetValue(i).Equals(b.GetValue(i)))
return false;
}
return true;
}
/**
* Returns true if the two specified arrays of Objects are
* equal to one another. The two arrays are considered equal if
* both arrays contain the same number of elements, and all corresponding
* pairs of elements in the two arrays are equal. Two objects e1
* and e2 are considered equal if (e1==null ? e2==null
* : e1.equals(e2)). In other words, the two arrays are equal if
* they contain the same elements in the same order. Also, two array
* references are considered equal if both are null.
*
* @param a one array to be tested for equality
* @param a2 the other array to be tested for equality
* @return true if the two arrays are equal
*/
public static bool Equals(Object[] a, Object[] a2)
{
if (a == a2)
return true;
if (a == null || a2 == null)
return false;
int length = a.Length;
if (a2.Length != length)
return false;
for (int i = 0; i < length; i++)
{
Object o1 = a[i];
Object o2 = a2[i];
if (!(o1 == null ? o2 == null : o1.Equals(o2)))
return false;
}
return true;
}
///
/// Moves a number of entries in an array to another point in the array, shifting those inbetween as required.
///
/// The array to alter
/// The (0 based) index of the first entry to move
/// The (0 based) index of the positition to move to
/// The number of entries to move
public static void ArrayMoveWithin(Object[] array, int moveFrom, int moveTo, int numToMove)
{
// If we're not asked to do anything, return now
if (numToMove <= 0) { return; }
if (moveFrom == moveTo) { return; }
// Check that the values supplied are valid
if (moveFrom < 0 || moveFrom >= array.Length)
{
throw new ArgumentException("The moveFrom must be a valid array index");
}
if (moveTo < 0 || moveTo >= array.Length)
{
throw new ArgumentException("The moveTo must be a valid array index");
}
if (moveFrom + numToMove > array.Length)
{
throw new ArgumentException("Asked to move more entries than the array has");
}
if (moveTo + numToMove > array.Length)
{
throw new ArgumentException("Asked to move to a position that doesn't have enough space");
}
// Grab the bit to move
Object[] toMove = new Object[numToMove];
Array.Copy(array, moveFrom, toMove, 0, numToMove);
// Grab the bit to be shifted
Object[] toShift;
int shiftTo;
if (moveFrom > moveTo)
{
// Moving to an earlier point in the array
// Grab everything between the two points
toShift = new Object[(moveFrom - moveTo)];
Array.Copy(array, moveTo, toShift, 0, toShift.Length);
shiftTo = moveTo + numToMove;
}
else
{
// Moving to a later point in the array
// Grab everything from after the toMove block, to the new point
toShift = new Object[(moveTo - moveFrom)];
Array.Copy(array, moveFrom + numToMove, toShift, 0, toShift.Length);
shiftTo = moveFrom;
}
// Copy the moved block to its new location
Array.Copy(toMove, 0, array, moveTo, toMove.Length);
// And copy the shifted block to the shifted location
Array.Copy(toShift, 0, array, shiftTo, toShift.Length);
// We're done - array will now have everything moved as required
}
///
/// Copies the specified array, truncating or padding with zeros (if
/// necessary) so the copy has the specified length. This method is temporary
/// replace for Arrays.copyOf() until we start to require JDK 1.6.
///
/// the array to be copied
/// the length of the copy to be returned
/// a copy of the original array, truncated or padded with zeros to obtain the specified length
public static byte[] CopyOf(byte[] source, int newLength)
{
byte[] result = new byte[newLength];
Array.Copy(source, 0, result, 0,
Math.Min(source.Length, newLength));
return result;
}
internal static int[] CopyOfRange(int[] original, int from, int to)
{
int newLength = to - from;
if (newLength < 0)
throw new ArgumentException(from + " > " + to);
int[] copy = new int[newLength];
Array.Copy(original, from, copy, 0,
Math.Min(original.Length - from, newLength));
return copy;
}
}
}