-
Notifications
You must be signed in to change notification settings - Fork 621
/
IntsRef.cs
294 lines (272 loc) · 9.79 KB
/
IntsRef.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
using Lucene.Net.Diagnostics;
using Lucene.Net.Support;
using System;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Text;
namespace Lucene.Net.Util
{
/*
* 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.
*/
/// <summary>
/// Represents <see cref="T:int[]"/>, as a slice (offset + length) into an
/// existing <see cref="T:int[]"/>. The <see cref="Int32s"/> member should never be <c>null</c>; use
/// <see cref="EMPTY_INT32S"/> if necessary.
/// <para/>
/// NOTE: This was IntsRef in Lucene
/// <para/>
/// @lucene.internal
/// </summary>
#if FEATURE_SERIALIZABLE
[Serializable]
#endif
public sealed class Int32sRef : IComparable<Int32sRef> // LUCENENET specific: Not implementing ICloneable per Microsoft's recommendation
{
/// <summary>
/// An empty integer array for convenience.
/// <para/>
/// NOTE: This was EMPTY_INTS in Lucene
/// </summary>
public static readonly int[] EMPTY_INT32S = Arrays.Empty<int>();
/// <summary>
/// The contents of the <see cref="Int32sRef"/>. Should never be <c>null</c>.
/// <para/>
/// NOTE: This was ints (field) in Lucene
/// </summary>
[WritableArray]
[SuppressMessage("Microsoft.Performance", "CA1819", Justification = "Lucene's design requires some writable array properties")]
public int[] Int32s // LUCENENET TODO: API - change to indexer
{
get => ints;
set => ints = value ?? throw new ArgumentNullException(nameof(Int32s), "Int32s should never be null"); // LUCENENET specific - changed from IllegalArgumentException to ArgumentNullException (.NET convention)
}
private int[] ints;
/// <summary>
/// Offset of first valid integer. </summary>
public int Offset { get; set; }
/// <summary>
/// Length of used <see cref="int"/>s. </summary>
public int Length { get; set; }
/// <summary>
/// Create a <see cref="Int32sRef"/> with <see cref="EMPTY_INT32S"/>. </summary>
public Int32sRef()
{
ints = EMPTY_INT32S;
}
/// <summary>
/// Create a <see cref="Int32sRef"/> pointing to a new array of size <paramref name="capacity"/>.
/// Offset and length will both be zero.
/// </summary>
public Int32sRef(int capacity)
{
ints = new int[capacity];
}
/// <summary>
/// This instance will directly reference <paramref name="ints"/> w/o making a copy.
/// <paramref name="ints"/> should not be <c>null</c>.
/// </summary>
public Int32sRef(int[] ints, int offset, int length)
{
this.ints = ints;
this.Offset = offset;
this.Length = length;
if (Debugging.AssertsEnabled) Debugging.Assert(IsValid());
}
/// <summary>
/// Returns a shallow clone of this instance (the underlying <see cref="int"/>s are
/// <b>not</b> copied and will be shared by both the returned object and this
/// object.
/// </summary>
/// <seealso cref="DeepCopyOf(Int32sRef)"/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public object Clone()
{
return new Int32sRef(ints, Offset, Length);
}
public override int GetHashCode()
{
const int prime = 31;
int result = 0;
int end = Offset + Length;
for (int i = Offset; i < end; i++)
{
result = prime * result + ints[i];
}
return result;
}
public override bool Equals(object obj)
{
if (obj is null)
{
return false;
}
if (obj is Int32sRef other)
{
return this.Int32sEquals(other);
}
return false;
}
/// <summary>
/// NOTE: This was intsEquals() in Lucene
/// </summary>
public bool Int32sEquals(Int32sRef other)
{
if (Length == other.Length)
{
int otherUpto = other.Offset;
int[] otherInts = other.ints;
int end = Offset + Length;
for (int upto = Offset; upto < end; upto++, otherUpto++)
{
if (ints[upto] != otherInts[otherUpto])
{
return false;
}
}
return true;
}
else
{
return false;
}
}
/// <summary>
/// Signed <see cref="int"/> order comparison. </summary>
public int CompareTo(Int32sRef other)
{
if (this == other)
{
return 0;
}
int[] aInts = this.ints;
int aUpto = this.Offset;
int[] bInts = other.ints;
int bUpto = other.Offset;
int aStop = aUpto + Math.Min(this.Length, other.Length);
while (aUpto < aStop)
{
int aInt = aInts[aUpto++];
int bInt = bInts[bUpto++];
if (aInt > bInt)
{
return 1;
}
else if (aInt < bInt)
{
return -1;
}
}
// One is a prefix of the other, or, they are equal:
return this.Length - other.Length;
}
/// <summary>
/// NOTE: This was copyInts() in Lucene
/// </summary>
public void CopyInt32s(Int32sRef other)
{
if (ints.Length - Offset < other.Length)
{
ints = new int[other.Length];
Offset = 0;
}
Array.Copy(other.ints, other.Offset, ints, Offset, other.Length);
Length = other.Length;
}
/// <summary>
/// Used to grow the reference array.
/// <para/>
/// In general this should not be used as it does not take the offset into account.
/// <para/>
/// @lucene.internal
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Grow(int newLength)
{
if (Debugging.AssertsEnabled) Debugging.Assert(Offset == 0);
if (ints.Length < newLength)
{
ints = ArrayUtil.Grow(ints, newLength);
}
}
public override string ToString()
{
StringBuilder sb = new StringBuilder();
sb.Append('[');
int end = Offset + Length;
for (int i = Offset; i < end; i++)
{
if (i > Offset)
{
sb.Append(' ');
}
sb.Append(ints[i].ToString("x"));
}
sb.Append(']');
return sb.ToString();
}
/// <summary>
/// Creates a new <see cref="Int32sRef"/> that points to a copy of the <see cref="int"/>s from
/// <paramref name="other"/>
/// <para/>
/// The returned <see cref="Int32sRef"/> will have a length of <c>other.Length</c>
/// and an offset of zero.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static Int32sRef DeepCopyOf(Int32sRef other)
{
Int32sRef clone = new Int32sRef();
clone.CopyInt32s(other);
return clone;
}
/// <summary>
/// Performs internal consistency checks.
/// Always returns true (or throws <see cref="InvalidOperationException"/>)
/// </summary>
public bool IsValid()
{
if (ints is null)
{
throw IllegalStateException.Create("ints is null");
}
if (Length < 0)
{
throw IllegalStateException.Create("length is negative: " + Length);
}
if (Length > ints.Length)
{
throw IllegalStateException.Create("length is out of bounds: " + Length + ",ints.length=" + Int32s.Length);
}
if (Offset < 0)
{
throw IllegalStateException.Create("offset is negative: " + Offset);
}
if (Offset > ints.Length)
{
throw IllegalStateException.Create("offset out of bounds: " + Offset + ",ints.length=" + Int32s.Length);
}
if (Offset + Length < 0)
{
throw IllegalStateException.Create("offset+length is negative: offset=" + Offset + ",length=" + Length);
}
if (Offset + Length > Int32s.Length)
{
throw IllegalStateException.Create("offset+length out of bounds: offset=" + Offset + ",length=" + Length + ",ints.length=" + Int32s.Length);
}
return true;
}
}
}