Lucene.Net  3.0.3
Lucene.Net is a port of the Lucene search engine library, written in C# and targeted at .NET runtime users.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
ArrayUtil.cs
Go to the documentation of this file.
1 /*
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
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17 
18 using System;
19 
20 namespace Lucene.Net.Util
21 {
22 
23  /// <summary> Methods for manipulating arrays.</summary>
24  public sealed class ArrayUtil
25  {
26  /*
27  Begin Apache Harmony code
28 
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
30 
31  */
32 
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
35  /// int quantity.
36  ///
37  /// </summary>
38  /// <param name="chars">a string representation of an int quantity.
39  /// </param>
40  /// <returns> int the value represented by the argument
41  /// </returns>
42  /// <throws> NumberFormatException if the argument could not be parsed as an int quantity. </throws>
43  public static int ParseInt(char[] chars)
44  {
45  return ParseInt(chars, 0, chars.Length, 10);
46  }
47 
48  /// <summary> Parses a char array into an int.</summary>
49  /// <param name="chars">the character array
50  /// </param>
51  /// <param name="offset">The offset into the array
52  /// </param>
53  /// <param name="len">The length
54  /// </param>
55  /// <returns> the int
56  /// </returns>
57  /// <throws> NumberFormatException if it can't parse </throws>
58  public static int ParseInt(char[] chars, int offset, int len)
59  {
60  return ParseInt(chars, offset, len, 10);
61  }
62 
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
66  /// the value.
67  ///
68  /// </summary>
69  /// <param name="chars">a string representation of an int quantity.
70  /// </param>
71  /// <param name="offset"></param>
72  /// <param name="len"></param>
73  /// <param name="radix">the base to use for conversion.
74  /// </param>
75  /// <returns> int the value represented by the argument
76  /// </returns>
77  /// <throws> NumberFormatException if the argument could not be parsed as an int quantity. </throws>
78  public static int ParseInt(char[] chars, int offset, int len, int radix)
79  {
80  if (chars == null || radix < 2 || radix > 36)
81  {
82  throw new System.FormatException();
83  }
84  int i = 0;
85  if (len == 0)
86  {
87  throw new System.FormatException("chars length is 0");
88  }
89  bool negative = chars[offset + i] == '-';
90  if (negative && ++i == len)
91  {
92  throw new System.FormatException("can't convert to an int");
93  }
94  if (negative == true)
95  {
96  offset++;
97  len--;
98  }
99  return Parse(chars, offset, len, radix, negative);
100  }
101 
102 
103  private static int Parse(char[] chars, int offset, int len, int radix, bool negative)
104  {
105  int max = System.Int32.MinValue / radix;
106  int result = 0;
107  for (int i = 0; i < len; i++)
108  {
109  int digit = (int) System.Char.GetNumericValue(chars[i + offset]);
110  if (digit == - 1)
111  {
112  throw new System.FormatException("Unable to parse");
113  }
114  if (max > result)
115  {
116  throw new System.FormatException("Unable to parse");
117  }
118  int next = result * radix - digit;
119  if (next > result)
120  {
121  throw new System.FormatException("Unable to parse");
122  }
123  result = next;
124  }
125  /*while (offset < len) {
126 
127  }*/
128  if (!negative)
129  {
130  result = - result;
131  if (result < 0)
132  {
133  throw new System.FormatException("Unable to parse");
134  }
135  }
136  return result;
137  }
138 
139 
140  /*
141 
142  END APACHE HARMONY CODE
143  */
144 
145 
146  public static int GetNextSize(int targetSize)
147  {
148  /* This over-allocates proportional to the list size, making room
149  * for additional growth. The over-allocation is mild, but is
150  * enough to give linear-time amortized behavior over a long
151  * sequence of appends() in the presence of a poorly-performing
152  * system realloc().
153  * The growth pattern is: 0, 4, 8, 16, 25, 35, 46, 58, 72, 88, ...
154  */
155  return (targetSize >> 3) + (targetSize < 9?3:6) + targetSize;
156  }
157 
158  public static int GetShrinkSize(int currentSize, int targetSize)
159  {
160  int newSize = GetNextSize(targetSize);
161  // Only reallocate if we are "substantially" smaller.
162  // This saves us from "running hot" (constantly making a
163  // bit bigger then a bit smaller, over and over):
164  if (newSize < currentSize / 2)
165  return newSize;
166  else
167  return currentSize;
168  }
169 
170  public static int[] Grow(int[] array, int minSize)
171  {
172  if (array.Length < minSize)
173  {
174  int[] newArray = new int[GetNextSize(minSize)];
175  Array.Copy(array, 0, newArray, 0, array.Length);
176  return newArray;
177  }
178  else
179  return array;
180  }
181 
182  public static int[] Grow(int[] array)
183  {
184  return Grow(array, 1 + array.Length);
185  }
186 
187  public static int[] Shrink(int[] array, int targetSize)
188  {
189  int newSize = GetShrinkSize(array.Length, targetSize);
190  if (newSize != array.Length)
191  {
192  int[] newArray = new int[newSize];
193  Array.Copy(array, 0, newArray, 0, newSize);
194  return newArray;
195  }
196  else
197  return array;
198  }
199 
200  public static long[] Grow(long[] array, int minSize)
201  {
202  if (array.Length < minSize)
203  {
204  long[] newArray = new long[GetNextSize(minSize)];
205  Array.Copy(array, 0, newArray, 0, array.Length);
206  return newArray;
207  }
208  else
209  return array;
210  }
211 
212  public static long[] Grow(long[] array)
213  {
214  return Grow(array, 1 + array.Length);
215  }
216 
217  public static long[] Shrink(long[] array, int targetSize)
218  {
219  int newSize = GetShrinkSize(array.Length, targetSize);
220  if (newSize != array.Length)
221  {
222  long[] newArray = new long[newSize];
223  Array.Copy(array, 0, newArray, 0, newSize);
224  return newArray;
225  }
226  else
227  return array;
228  }
229 
230  public static byte[] Grow(byte[] array, int minSize)
231  {
232  if (array.Length < minSize)
233  {
234  byte[] newArray = new byte[GetNextSize(minSize)];
235  Array.Copy(array, 0, newArray, 0, array.Length);
236  return newArray;
237  }
238  else
239  return array;
240  }
241 
242  public static byte[] Grow(byte[] array)
243  {
244  return Grow(array, 1 + array.Length);
245  }
246 
247  public static byte[] Shrink(byte[] array, int targetSize)
248  {
249  int newSize = GetShrinkSize(array.Length, targetSize);
250  if (newSize != array.Length)
251  {
252  byte[] newArray = new byte[newSize];
253  Array.Copy(array, 0, newArray, 0, newSize);
254  return newArray;
255  }
256  else
257  return array;
258  }
259 
260  /// <summary> Returns hash of chars in range start (inclusive) to
261  /// end (inclusive)
262  /// </summary>
263  public static int HashCode(char[] array, int start, int end)
264  {
265  int code = 0;
266  for (int i = end - 1; i >= start; i--)
267  code = code * 31 + array[i];
268  return code;
269  }
270 
271  /// <summary> Returns hash of chars in range start (inclusive) to
272  /// end (inclusive)
273  /// </summary>
274  public static int HashCode(byte[] array, int start, int end)
275  {
276  int code = 0;
277  for (int i = end - 1; i >= start; i--)
278  code = code * 31 + array[i];
279  return code;
280  }
281  }
282 }