forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrayHelper.cs
284 lines (244 loc) · 6.4 KB
/
ArrayHelper.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
using System;
using System.Collections;
using System.Linq;
using System.Text;
using System.Collections.Generic;
namespace NHibernate.Util
{
/// <summary>
/// Helper class that contains common array functions and
/// data structures used through out NHibernate.
/// </summary>
public static class ArrayHelper
{
//Since v5.1
[Obsolete("Please use System.Array.Empty<object>() instead")]
public static object[] EmptyObjectArray => Array.Empty<object>();
//Since v5.1
[Obsolete("Please use System.Array.Empty<int>() instead")]
public static int[] EmptyIntArray => Array.Empty<int>();
//Since v5.1
[Obsolete("Please use System.Array.Empty<bool>() instead")]
public static bool[] EmptyBoolArray => Array.Empty<bool>();
public static readonly bool[] True = new bool[] { true };
public static readonly bool[] False = new bool[] { false };
internal static bool IsNullOrEmpty(Array array)
{
return array == null || array.Length == 0;
}
public static bool IsAllNegative(int[] array)
{
return array.All(t => t < 0);
}
public static T[] Fill<T>(T value, int length)
{
var result = new T[length];
Fill(result, value);
return result;
}
public static void Fill<T>(T[] array, T value)
{
for (int i = 0; i < array.Length; i++)
{
array[i] = value;
}
}
public static T[] Slice<T>(T[] strings, int begin, int length)
{
var result = new T[length];
Array.Copy(strings, begin, result, 0, length);
return result;
}
public static T[] Join<T>(T[] x, T[] y, bool[] use)
{
var l = new List<T>(x);
for (int i = 0; i < y.Length; i++)
{
if (use[i])
l.Add(y[i]);
}
return l.ToArray();
}
public static T[] Join<T>(T[] x, T[] y)
{
var result = new T[x.Length + y.Length];
Array.Copy(x, 0, result, 0, x.Length);
Array.Copy(y, 0, result, x.Length, y.Length);
return result;
}
public static bool IsAllFalse(bool[] array)
{
return Array.IndexOf(array, true) < 0;
}
public static string ToString(object[] array)
{
StringBuilder sb = new StringBuilder();
sb.Append("[");
for (int i = 0; i < array.Length; i++)
{
sb.Append(array[i]);
if (i < array.Length - 1)
{
sb.Append(",");
}
}
sb.Append("]");
return sb.ToString();
}
/// <summary>
/// Append all elements in the 'from' list to the 'to' list.
/// </summary>
/// <param name="to"></param>
/// <param name="from"></param>
public static void AddAll(IList to, IList from)
{
foreach (object obj in from)
{
to.Add(obj);
}
}
// NH-specific
public static void AddAll<T>(IList<T> to, IList<T> from)
{
foreach (T obj in from)
to.Add(obj);
}
public static void AddAll<TKey, TValue>(IDictionary<TKey, TValue> to, IDictionary<TKey, TValue> from)
{
foreach (KeyValuePair<TKey, TValue> de in from)
{
// we want to override the values from to if they exists
to[de.Key] = de.Value;
}
}
public static IDictionary<TKey, TValue> AddOrOverride<TKey, TValue>(this IDictionary<TKey, TValue> destination, IDictionary<TKey, TValue> sourceOverride)
{
foreach (KeyValuePair<TKey, TValue> de in sourceOverride)
{
// we want to override the values from to if they exists
destination[de.Key] = de.Value;
}
return destination;
}
public static int[] GetBatchSizes(int maxBatchSize)
{
int batchSize = maxBatchSize;
int n = 1;
while (batchSize > 1)
{
batchSize = GetNextBatchSize(batchSize);
n++;
}
int[] result = new int[n];
batchSize = maxBatchSize;
for (int i = 0; i < n; i++)
{
result[i] = batchSize;
batchSize = GetNextBatchSize(batchSize);
}
return result;
}
private static int GetNextBatchSize(int batchSize)
{
if (batchSize <= 10)
{
return batchSize - 1; // allow 9,8,7,6,5,4,3,2,1
}
else if (batchSize / 2 < 10)
{
return 10;
}
else
{
return batchSize / 2;
}
}
public static int CountTrue(bool[] array)
{
return array.Count(t => t);
}
internal static IEnumerable<int> IndexesOf<T>(T[] array, T value)
{
for (int i = 0; i < array.Length; i++)
{
if (EqualityComparer<T>.Default.Equals(array[i], value))
yield return i;
}
}
public static bool ArrayEquals<T>(T[] a, T[] b)
{
return ArrayComparer<T>.Default.Equals(a, b);
}
public static bool ArrayEquals(byte[] a, byte[] b)
{
return ArrayComparer<byte>.Default.Equals(a, b);
}
/// <summary>
/// Calculate a hash code based on the length and contents of the array.
/// The algorithm is such that if ArrayHelper.ArrayEquals(a,b) returns true,
/// then ArrayGetHashCode(a) == ArrayGetHashCode(b).
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="array"></param>
/// <returns></returns>
public static int ArrayGetHashCode<T>(T[] array)
{
return ArrayComparer<T>.Default.GetHashCode(array);
}
/// <summary>
/// Append a value to an array.
/// </summary>
/// <remarks>
/// If <paramref name="array"/> is null, then return an array with length of 1 containing the <paramref name="value"/>.
/// </remarks>
/// <returns>A new array containing all elements from <paramref name="array"/> and a <paramref name="value"/> at the end.</returns>
internal static T[] Append<T>(T[] array, T value)
{
if (array == null)
{
return new[] {value};
}
else
{
var result = new T[array.Length + 1];
array.CopyTo(result, 0);
result[array.Length] = value;
return result;
}
}
internal class ArrayComparer<T> : IEqualityComparer<T[]>
{
private readonly IEqualityComparer<T> _elementComparer;
internal static ArrayComparer<T> Default { get; } = new ArrayComparer<T>();
internal ArrayComparer() : this(EqualityComparer<T>.Default) { }
internal ArrayComparer(IEqualityComparer<T> elementComparer)
{
_elementComparer = elementComparer ?? throw new ArgumentNullException(nameof(elementComparer));
}
public bool Equals(T[] a, T[] b)
{
if (a == b)
return true;
if (a == null || b == null)
return false;
if (a.Length != b.Length)
return false;
for (var i = 0; i < a.Length; i++)
{
if (!_elementComparer.Equals(a[i], b[i]))
return false;
}
return true;
}
public int GetHashCode(T[] array)
{
if (array == null)
return 0;
var hc = array.Length;
foreach (var e in array)
hc = unchecked(hc * 31 + _elementComparer.GetHashCode(e));
return hc;
}
}
}
}