2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
20 namespace Mono.Lucene.Net.Util
23 /// <summary> Methods for manipulating arrays.</summary>
24 public sealed class ArrayUtil
27 Begin Apache Harmony code
29 Revision taken on Friday, June 12. https://svn.apache.org/repos/asf/harmony/enhanced/classlib/archive/java6/modules/luni/src/main/java/java/lang/Integer.java
33 /// <summary> Parses the string argument as if it was an int value and returns the
34 /// result. Throws NumberFormatException if the string does not represent an
38 /// <param name="chars">a string representation of an int quantity.
40 /// <returns> int the value represented by the argument
42 /// <throws> NumberFormatException if the argument could not be parsed as an int quantity. </throws>
43 public static int ParseInt(char[] chars)
45 return ParseInt(chars, 0, chars.Length, 10);
48 /// <summary> Parses a char array into an int.</summary>
49 /// <param name="chars">the character array
51 /// <param name="offset">The offset into the array
53 /// <param name="len">The length
57 /// <throws> NumberFormatException if it can't parse </throws>
58 public static int ParseInt(char[] chars, int offset, int len)
60 return ParseInt(chars, offset, len, 10);
63 /// <summary> Parses the string argument as if it was an int value and returns the
64 /// result. Throws NumberFormatException if the string does not represent an
65 /// int quantity. The second argument specifies the radix to use when parsing
69 /// <param name="chars">a string representation of an int quantity.
71 /// <param name="radix">the base to use for conversion.
73 /// <returns> int the value represented by the argument
75 /// <throws> NumberFormatException if the argument could not be parsed as an int quantity. </throws>
76 public static int ParseInt(char[] chars, int offset, int len, int radix)
78 if (chars == null || radix < 2 || radix > 36)
80 throw new System.FormatException();
85 throw new System.FormatException("chars length is 0");
87 bool negative = chars[offset + i] == '-';
88 if (negative && ++i == len)
90 throw new System.FormatException("can't convert to an int");
97 return Parse(chars, offset, len, radix, negative);
101 private static int Parse(char[] chars, int offset, int len, int radix, bool negative)
103 int max = System.Int32.MinValue / radix;
105 for (int i = 0; i < len; i++)
107 int digit = (int) System.Char.GetNumericValue(chars[i + offset]);
110 throw new System.FormatException("Unable to parse");
114 throw new System.FormatException("Unable to parse");
116 int next = result * radix - digit;
119 throw new System.FormatException("Unable to parse");
123 /*while (offset < len) {
131 throw new System.FormatException("Unable to parse");
140 END APACHE HARMONY CODE
144 public static int GetNextSize(int targetSize)
146 /* This over-allocates proportional to the list size, making room
147 * for additional growth. The over-allocation is mild, but is
148 * enough to give linear-time amortized behavior over a long
149 * sequence of appends() in the presence of a poorly-performing
151 * The growth pattern is: 0, 4, 8, 16, 25, 35, 46, 58, 72, 88, ...
153 return (targetSize >> 3) + (targetSize < 9?3:6) + targetSize;
156 public static int GetShrinkSize(int currentSize, int targetSize)
158 int newSize = GetNextSize(targetSize);
159 // Only reallocate if we are "substantially" smaller.
160 // This saves us from "running hot" (constantly making a
161 // bit bigger then a bit smaller, over and over):
162 if (newSize < currentSize / 2)
168 public static int[] Grow(int[] array, int minSize)
170 if (array.Length < minSize)
172 int[] newArray = new int[GetNextSize(minSize)];
173 Array.Copy(array, 0, newArray, 0, array.Length);
180 public static int[] Grow(int[] array)
182 return Grow(array, 1 + array.Length);
185 public static int[] Shrink(int[] array, int targetSize)
187 int newSize = GetShrinkSize(array.Length, targetSize);
188 if (newSize != array.Length)
190 int[] newArray = new int[newSize];
191 Array.Copy(array, 0, newArray, 0, newSize);
198 public static long[] Grow(long[] array, int minSize)
200 if (array.Length < minSize)
202 long[] newArray = new long[GetNextSize(minSize)];
203 Array.Copy(array, 0, newArray, 0, array.Length);
210 public static long[] Grow(long[] array)
212 return Grow(array, 1 + array.Length);
215 public static long[] Shrink(long[] array, int targetSize)
217 int newSize = GetShrinkSize(array.Length, targetSize);
218 if (newSize != array.Length)
220 long[] newArray = new long[newSize];
221 Array.Copy(array, 0, newArray, 0, newSize);
228 public static byte[] Grow(byte[] array, int minSize)
230 if (array.Length < minSize)
232 byte[] newArray = new byte[GetNextSize(minSize)];
233 Array.Copy(array, 0, newArray, 0, array.Length);
240 public static byte[] Grow(byte[] array)
242 return Grow(array, 1 + array.Length);
245 public static byte[] Shrink(byte[] array, int targetSize)
247 int newSize = GetShrinkSize(array.Length, targetSize);
248 if (newSize != array.Length)
250 byte[] newArray = new byte[newSize];
251 Array.Copy(array, 0, newArray, 0, newSize);
258 /// <summary> Returns hash of chars in range start (inclusive) to
261 public static int HashCode(char[] array, int start, int end)
264 for (int i = end - 1; i >= start; i--)
265 code = code * 31 + array[i];
269 /// <summary> Returns hash of chars in range start (inclusive) to
272 public static int HashCode(byte[] array, int start, int end)
275 for (int i = end - 1; i >= start; i--)
276 code = code * 31 + array[i];