-
-
Notifications
You must be signed in to change notification settings - Fork 910
/
Utilities.cs
846 lines (767 loc) · 32 KB
/
Utilities.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
// Copyright (c) Stride contributors (https://stride3d.net) and Silicon Studio Corp. (https://www.siliconstudio.co.jp)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
//
// Copyright (c) 2010-2012 SharpDX - Alexandre Mutel
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#pragma warning disable SA1405 // Debug.Assert must provide message text
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using Stride.Core.Annotations;
using Stride.Core.Native;
namespace Stride.Core
{
/// <summary>
/// Utility class.
/// </summary>
public static class Utilities
{
/// <summary>
/// Copy memory.
/// </summary>
/// <param name="dest">The destination memory location</param>
/// <param name="src">The source memory location.</param>
/// <param name="sizeInBytesToCopy">The count.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void CopyMemory(IntPtr dest, IntPtr src, int sizeInBytesToCopy)
{
unsafe
{
Buffer.MemoryCopy((void*)src, (void*)dest, sizeInBytesToCopy, sizeInBytesToCopy);
}
}
/// <summary>
/// Compares two block of memory.
/// </summary>
/// <param name="from">The pointer to compare from.</param>
/// <param name="against">The pointer to compare against.</param>
/// <param name="sizeToCompare">The size in bytes to compare.</param>
/// <returns>True if the buffers are equivalent, false otherwise.</returns>
public static unsafe bool CompareMemory(IntPtr from, IntPtr against, int sizeToCompare)
{
var pSrc = (byte*)from;
var pDst = (byte*)against;
// Compare 8 bytes.
var numberOf = sizeToCompare >> 3;
while (numberOf > 0)
{
if (*(long*)pSrc != *(long*)pDst)
return false;
pSrc += 8;
pDst += 8;
numberOf--;
}
// Compare remaining bytes.
numberOf = sizeToCompare & 7;
while (numberOf > 0)
{
if (*pSrc != *pDst)
return false;
pSrc++;
pDst++;
numberOf--;
}
return true;
}
/// <summary>
/// Clears the memory.
/// </summary>
/// <param name="dest">The dest.</param>
/// <param name="value">The value.</param>
/// <param name="sizeInBytesToClear">The size in bytes to clear.</param>
public static void ClearMemory(IntPtr dest, byte value, int sizeInBytesToClear)
{
unsafe
{
Interop.memset((void*)dest, value, sizeInBytesToClear);
}
}
/// <summary>
/// Return the sizeof a struct from a CLR. Equivalent to sizeof operator but works on generics too.
/// </summary>
/// <typeparam name="T">a struct to evaluate</typeparam>
/// <returns>sizeof this struct</returns>
public static int SizeOf<T>() where T : struct
{
return Interop.SizeOf<T>();
}
/// <summary>
/// Return the sizeof an array of struct. Equivalent to sizeof operator but works on generics too.
/// </summary>
/// <typeparam name="T">a struct</typeparam>
/// <param name="array">The array of struct to evaluate.</param>
/// <returns>sizeof in bytes of this array of struct</returns>
public static int SizeOf<T>(T[] array) where T : struct
{
return array == null ? 0 : array.Length * Interop.SizeOf<T>();
}
/// <summary>
/// Pins the specified source and call an action with the pinned pointer.
/// </summary>
/// <typeparam name="T">The type of the structure to pin</typeparam>
/// <param name="source">The source.</param>
/// <param name="pinAction">The pin action to perform on the pinned pointer.</param>
public static void Pin<T>(ref T source, Action<IntPtr> pinAction) where T : struct
{
unsafe
{
pinAction((IntPtr)Interop.Fixed(ref source));
}
}
/// <summary>
/// Pins the specified source and call an action with the pinned pointer.
/// </summary>
/// <typeparam name="T">The type of the structure to pin</typeparam>
/// <param name="source">The source array.</param>
/// <param name="pinAction">The pin action to perform on the pinned pointer.</param>
public static void Pin<T>(T[] source, [NotNull] Action<IntPtr> pinAction) where T : struct
{
unsafe
{
pinAction(source == null ? IntPtr.Zero : (IntPtr)Interop.Fixed(source));
}
}
/// <summary>
/// Covnerts a structured array to an equivalent byte array.
/// </summary>
/// <param name="source">The source.</param>
/// <returns>The byte array.</returns>
public static byte[] ToByteArray<T>(T[] source) where T : struct
{
if (source == null) return null;
var buffer = new byte[SizeOf<T>() * source.Length];
if (source.Length == 0)
return buffer;
unsafe
{
fixed (void* pBuffer = buffer)
Interop.Write(pBuffer, source, 0, source.Length);
}
return buffer;
}
/// <summary>
/// Reads the specified T data from a memory location.
/// </summary>
/// <typeparam name="T">Type of a data to read</typeparam>
/// <param name="source">Memory location to read from.</param>
/// <returns>The data read from the memory location</returns>
public static T Read<T>(IntPtr source) where T : struct
{
unsafe
{
return Interop.ReadInline<T>((void*)source);
}
}
/// <summary>
/// Reads the specified T data from a memory location.
/// </summary>
/// <typeparam name="T">Type of a data to read</typeparam>
/// <param name="source">Memory location to read from.</param>
/// <param name="data">The data write to.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Read<T>(IntPtr source, ref T data) where T : struct
{
unsafe
{
Interop.CopyInline(ref data, (void*)source);
}
}
/// <summary>
/// Reads the specified T data from a memory location.
/// </summary>
/// <typeparam name="T">Type of a data to read</typeparam>
/// <param name="source">Memory location to read from.</param>
/// <param name="data">The data write to.</param>
public static void ReadOut<T>(IntPtr source, out T data) where T : struct
{
unsafe
{
Interop.CopyInlineOut(out data, (void*)source);
}
}
/// <summary>
/// Reads the specified T data from a memory location.
/// </summary>
/// <typeparam name="T">Type of a data to read</typeparam>
/// <param name="source">Memory location to read from.</param>
/// <param name="data">The data write to.</param>
/// <returns>source pointer + sizeof(T)</returns>
public static IntPtr ReadAndPosition<T>(IntPtr source, ref T data) where T : struct
{
unsafe
{
return (IntPtr)Interop.Read((void*)source, ref data);
}
}
/// <summary>
/// Reads the specified array T[] data from a memory location.
/// </summary>
/// <typeparam name="T">Type of a data to read</typeparam>
/// <param name="source">Memory location to read from.</param>
/// <param name="data">The data write to.</param>
/// <param name="offset">The offset in the array to write to.</param>
/// <param name="count">The number of T element to read from the memory location</param>
/// <returns>source pointer + sizeof(T) * count</returns>
public static IntPtr Read<T>(IntPtr source, T[] data, int offset, int count) where T : struct
{
unsafe
{
return (IntPtr)Interop.Read((void*)source, data, offset, count);
}
}
/// <summary>
/// Writes the specified T data to a memory location.
/// </summary>
/// <typeparam name="T">Type of a data to write</typeparam>
/// <param name="destination">Memory location to write to.</param>
/// <param name="data">The data to write.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Write<T>(IntPtr destination, ref T data) where T : struct
{
unsafe
{
Interop.CopyInline((void*)destination, ref data);
}
}
/// <summary>
/// Writes the specified T data to a memory location.
/// </summary>
/// <typeparam name="T">Type of a data to write</typeparam>
/// <param name="destination">Memory location to write to.</param>
/// <param name="data">The data to write.</param>
/// <returns>destination pointer + sizeof(T)</returns>
public static IntPtr WriteAndPosition<T>(IntPtr destination, ref T data) where T : struct
{
unsafe
{
return (IntPtr)Interop.Write((void*)destination, ref data);
}
}
/// <summary>
/// Writes the specified array T[] data to a memory location.
/// </summary>
/// <typeparam name="T">Type of a data to write</typeparam>
/// <param name="destination">Memory location to write to.</param>
/// <param name="data">The array of T data to write.</param>
/// <param name="offset">The offset in the array to read from.</param>
/// <param name="count">The number of T element to write to the memory location</param>
public static void Write<T>(byte[] destination, T[] data, int offset, int count) where T : struct
{
unsafe
{
fixed (void* pDest = destination)
{
Write((IntPtr)pDest, data, offset, count);
}
}
}
/// <summary>
/// Writes the specified array T[] data to a memory location.
/// </summary>
/// <typeparam name="T">Type of a data to write</typeparam>
/// <param name="destination">Memory location to write to.</param>
/// <param name="data">The array of T data to write.</param>
/// <param name="offset">The offset in the array to read from.</param>
/// <param name="count">The number of T element to write to the memory location</param>
/// <returns>destination pointer + sizeof(T) * count</returns>
public static IntPtr Write<T>(IntPtr destination, T[] data, int offset, int count) where T : struct
{
unsafe
{
return (IntPtr)Interop.Write((void*)destination, data, offset, count);
}
}
/// <summary>
/// Allocate an aligned memory buffer.
/// </summary>
/// <param name="sizeInBytes">Size of the buffer to allocate.</param>
/// <param name="align">Alignment, a positive value which is a power of 2. 16 bytes by default.</param>
/// <returns>A pointer to a buffer aligned.</returns>
/// <remarks>
/// To free this buffer, call <see cref="FreeMemory"/>
/// </remarks>
public static unsafe IntPtr AllocateMemory(int sizeInBytes, int align = 16)
{
var mask = align - 1;
if ((align & mask) != 0)
{
throw new ArgumentException("Alignment is not power of 2", nameof(align));
}
var memPtr = Marshal.AllocHGlobal(sizeInBytes + mask + sizeof(void*));
var ptr = (byte*)((ulong)(memPtr + sizeof(void*) + mask) & ~(ulong)mask);
((IntPtr*)ptr)[-1] = memPtr;
return new IntPtr(ptr);
}
/// <summary>
/// Allocate an aligned memory buffer and clear it with a specified value (0 by defaault).
/// </summary>
/// <param name="sizeInBytes">Size of the buffer to allocate.</param>
/// <param name="clearValue">Default value used to clear the buffer.</param>
/// <param name="align">Alignment, 16 bytes by default.</param>
/// <returns>A pointer to a buffer aligned.</returns>
/// <remarks>
/// To free this buffer, call <see cref="FreeMemory"/>
/// </remarks>
public static IntPtr AllocateClearedMemory(int sizeInBytes, byte clearValue = 0, int align = 16)
{
var ptr = AllocateMemory(sizeInBytes, align);
ClearMemory(ptr, clearValue, sizeInBytes);
return ptr;
}
/// <summary>
/// Determines whether the specified memory pointer is aligned in memory.
/// </summary>
/// <param name="memoryPtr">The memory pointer.</param>
/// <param name="align">The align.</param>
/// <returns><c>true</c> if the specified memory pointer is aligned in memory; otherwise, <c>false</c>.</returns>
public static bool IsMemoryAligned(IntPtr memoryPtr, int align = 16)
{
return (memoryPtr.ToInt64() & (align - 1)) == 0;
}
/// <summary>
/// Allocate an aligned memory buffer.
/// </summary>
/// <remarks>
/// The buffer must have been allocated with <see cref="AllocateMemory"/>
/// </remarks>
public static unsafe void FreeMemory(IntPtr alignedBuffer)
{
Marshal.FreeHGlobal(((IntPtr*)alignedBuffer)[-1]);
}
/// <summary>
/// If non-null, disposes the specified object and set it to null, otherwise do nothing.
/// </summary>
/// <param name="disposable">The disposable.</param>
public static void Dispose<T>(ref T disposable) where T : class, IDisposable
{
if (disposable != null)
{
disposable.Dispose();
disposable = null;
}
}
/// <summary>
/// String helper join method to display an array of object as a single string.
/// </summary>
/// <param name="separator">The separator.</param>
/// <param name="array">The array.</param>
/// <returns>a string with array elements serparated by the seperator</returns>
[NotNull]
public static string Join<T>(string separator, T[] array)
{
var text = new StringBuilder();
if (array != null)
{
for (var i = 0; i < array.Length; i++)
{
if (i > 0) text.Append(separator);
text.Append(array[i]);
}
}
return text.ToString();
}
/// <summary>
/// String helper join method to display an enumrable of object as a single string.
/// </summary>
/// <param name="separator">The separator.</param>
/// <param name="elements">The enumerable.</param>
/// <returns>a string with array elements serparated by the seperator</returns>
[NotNull]
public static string Join(string separator, [NotNull] IEnumerable elements)
{
var elementList = new List<string>();
foreach (var element in elements)
elementList.Add(element.ToString());
var text = new StringBuilder();
for (var i = 0; i < elementList.Count; i++)
{
var element = elementList[i];
if (i > 0) text.Append(separator);
text.Append(element);
}
return text.ToString();
}
/// <summary>
/// String helper join method to display an enumrable of object as a single string.
/// </summary>
/// <param name="separator">The separator.</param>
/// <param name="elements">The enumerable.</param>
/// <returns>a string with array elements serparated by the seperator</returns>
[NotNull]
public static string Join(string separator, [NotNull] IEnumerator elements)
{
var elementList = new List<string>();
while (elements.MoveNext())
elementList.Add(elements.Current.ToString());
var text = new StringBuilder();
for (var i = 0; i < elementList.Count; i++)
{
var element = elementList[i];
if (i > 0) text.Append(separator);
text.Append(element);
}
return text.ToString();
}
/// <summary>
/// Read stream to a byte[] buffer
/// </summary>
/// <param name = "stream">input stream</param>
/// <returns>a byte[] buffer</returns>
[NotNull]
public static byte[] ReadStream([NotNull] Stream stream)
{
var readLength = 0;
return ReadStream(stream, ref readLength);
}
/// <summary>
/// Read stream to a byte[] buffer
/// </summary>
/// <param name = "stream">input stream</param>
/// <param name = "readLength">length to read</param>
/// <returns>a byte[] buffer</returns>
[NotNull]
public static byte[] ReadStream([NotNull] Stream stream, ref int readLength)
{
System.Diagnostics.Debug.Assert(stream != null);
System.Diagnostics.Debug.Assert(stream.CanRead);
var num = readLength;
System.Diagnostics.Debug.Assert(num <= (stream.Length - stream.Position));
if (num == 0)
readLength = (int)(stream.Length - stream.Position);
num = readLength;
System.Diagnostics.Debug.Assert(num >= 0);
if (num == 0)
return new byte[0];
var buffer = new byte[num];
var bytesRead = 0;
if (num > 0)
{
do
{
bytesRead += stream.Read(buffer, bytesRead, readLength - bytesRead);
} while (bytesRead < readLength);
}
return buffer;
}
/// <summary>
/// Computes a hashcode for a dictionary.
/// </summary>
/// <returns>Hashcode for the list.</returns>
public static int GetHashCode(IDictionary dict)
{
if (dict == null)
return 0;
var hashCode = 0;
foreach (DictionaryEntry keyValue in dict)
{
hashCode = (hashCode * 397) ^ keyValue.Key.GetHashCode();
hashCode = (hashCode * 397) ^ (keyValue.Value?.GetHashCode() ?? 0);
}
return hashCode;
}
/// <summary>
/// Computes a hashcode for an enumeration
/// </summary>
/// <param name="it">An enumerator.</param>
/// <returns>Hashcode for the list.</returns>
public static int GetHashCode(IEnumerable it)
{
if (it == null)
return 0;
var hashCode = 0;
foreach (var current in it)
{
hashCode = (hashCode * 397) ^ (current?.GetHashCode() ?? 0);
}
return hashCode;
}
/// <summary>
/// Computes a hashcode for an enumeration
/// </summary>
/// <param name="it">An enumerator.</param>
/// <returns>Hashcode for the list.</returns>
public static int GetHashCode(IEnumerator it)
{
if (it == null)
return 0;
var hashCode = 0;
while (it.MoveNext())
{
var current = it.Current;
hashCode = (hashCode * 397) ^ (current?.GetHashCode() ?? 0);
}
return hashCode;
}
/// <summary>
/// Compares two collection, element by elements.
/// </summary>
/// <param name="left">A "from" enumerator.</param>
/// <param name="right">A "to" enumerator.</param>
/// <returns>True if lists are identical. False otherwise.</returns>
public static bool Compare(IEnumerable left, IEnumerable right)
{
if (ReferenceEquals(left, right))
return true;
if (ReferenceEquals(left, null) || ReferenceEquals(right, null))
return false;
return Compare(left.GetEnumerator(), right.GetEnumerator());
}
/// <summary>
/// Compares two collection, element by elements.
/// </summary>
/// <param name="leftIt">A "from" enumerator.</param>
/// <param name="rightIt">A "to" enumerator.</param>
/// <returns>True if lists are identical. False otherwise.</returns>
public static bool Compare(IEnumerator leftIt, IEnumerator rightIt)
{
if (ReferenceEquals(leftIt, rightIt))
return true;
if (ReferenceEquals(leftIt, null) || ReferenceEquals(rightIt, null))
return false;
bool hasLeftNext;
bool hasRightNext;
while (true)
{
hasLeftNext = leftIt.MoveNext();
hasRightNext = rightIt.MoveNext();
if (!hasLeftNext || !hasRightNext)
break;
if (!Equals(leftIt.Current, rightIt.Current))
return false;
}
// If there is any left element
if (hasLeftNext != hasRightNext)
return false;
return true;
}
/// <summary>
/// Compares two collection, element by elements.
/// </summary>
/// <param name="first">The collection to compare from.</param>
/// <param name="second">The colllection to compare to.</param>
/// <returns>True if lists are identical (but no necessarely of the same time). False otherwise.</returns>
public static bool Compare<TKey, TValue>(IDictionary<TKey, TValue> first, IDictionary<TKey, TValue> second)
{
if (ReferenceEquals(first, second)) return true;
if (ReferenceEquals(first, null) || ReferenceEquals(second, null)) return false;
if (first.Count != second.Count) return false;
var comparer = EqualityComparer<TValue>.Default;
foreach (var keyValue in first)
{
TValue secondValue;
if (!second.TryGetValue(keyValue.Key, out secondValue)) return false;
if (!comparer.Equals(keyValue.Value, secondValue)) return false;
}
// Check that all keys in second are in first
return second.Keys.All(first.ContainsKey);
}
/// <summary>
/// Compares two collection, element by elements.
/// </summary>
/// <param name="first">The collection to compare from.</param>
/// <param name="second">The colllection to compare to.</param>
/// <returns>True if lists are identical (but not necessarily in the same order). False otherwise.</returns>
/// <remarks>Concrete SortedList is favored over interface to avoid enumerator object allocation.</remarks>
public static bool Compare<TKey, TValue>(Collections.SortedList<TKey, TValue> first, Collections.SortedList<TKey, TValue> second)
{
if (ReferenceEquals(first, second)) return true;
if (ReferenceEquals(first, null) || ReferenceEquals(second, null)) return false;
if (first.Count != second.Count) return false;
var comparer = EqualityComparer<TValue>.Default;
foreach (var keyValue in first)
{
TValue secondValue;
if (!second.TryGetValue(keyValue.Key, out secondValue)) return false;
if (!comparer.Equals(keyValue.Value, secondValue)) return false;
}
return true;
}
public static bool Compare<T>(T[] left, T[] right)
{
if (ReferenceEquals(left, right))
return true;
if (ReferenceEquals(left, null) || ReferenceEquals(right, null))
return false;
if (left.Length != right.Length)
return false;
var comparer = EqualityComparer<T>.Default;
for (var i = 0; i < left.Length; ++i)
{
if (!comparer.Equals(left[i], right[i]))
return false;
}
return true;
}
/// <summary>
/// Compares two collection, element by elements.
/// </summary>
/// <param name="left">The collection to compare from.</param>
/// <param name="right">The colllection to compare to.</param>
/// <returns>True if lists are identical (but no necessarely of the same time). False otherwise.</returns>
public static bool Compare<T>(ICollection<T> left, ICollection<T> right)
{
if (ReferenceEquals(left, right))
return true;
if (ReferenceEquals(left, null) || ReferenceEquals(right, null))
return false;
if (left.Count != right.Count)
return false;
var count = 0;
var leftIt = left.GetEnumerator();
var rightIt = right.GetEnumerator();
var comparer = EqualityComparer<T>.Default;
while (leftIt.MoveNext() && rightIt.MoveNext())
{
if (!comparer.Equals(leftIt.Current, rightIt.Current))
return false;
count++;
}
// Just double check to make sure that the iterator actually returns
// the exact number of elements
if (count != left.Count)
return false;
return true;
}
/// <summary>
/// Compares two list, element by elements.
/// </summary>
/// <param name="left">The list to compare from.</param>
/// <param name="right">The colllection to compare to.</param>
/// <returns>True if lists are sequentially equal. False otherwise.</returns>
/// <remarks>Concrete List is favored over interface to avoid enumerator object allocation.</remarks>
public static bool Compare<T>(List<T> left, List<T> right)
{
if (ReferenceEquals(left, right))
return true;
if (ReferenceEquals(left, null) || ReferenceEquals(right, null))
return false;
if (left.Count != right.Count)
return false;
var comparer = EqualityComparer<T>.Default;
for (int i = 0; i < left.Count; i++)
{
if (!comparer.Equals(left[i], right[i]))
return false;
}
return true;
}
/// <summary>
/// Swaps the value between two references.
/// </summary>
/// <typeparam name="T">Type of a data to swap.</typeparam>
/// <param name="left">The left value.</param>
/// <param name="right">The right value.</param>
public static void Swap<T>(ref T left, ref T right)
{
var temp = left;
left = right;
right = temp;
}
/// <summary>
/// Suspends current thread for a <see cref="sleepTime"/>.
/// </summary>
/// <param name="sleepTime">The duration of sleep.</param>
public static void Sleep(TimeSpan sleepTime)
{
var ms = (long)sleepTime.TotalMilliseconds;
if (ms < 0 || ms > int.MaxValue)
{
throw new ArgumentOutOfRangeException(nameof(sleepTime), "Sleep time must be a duration less than '2^31 - 1' milliseconds.");
}
NativeInvoke.Sleep((int)ms);
}
/// <summary>
/// Suspends current thread for a <see cref="sleepTimeInMillis"/>.
/// </summary>
/// <param name="sleepTimeInMillis">The duration of sleep in milliseconds.</param>
public static void Sleep(int sleepTimeInMillis)
{
NativeInvoke.Sleep(sleepTimeInMillis);
}
/// <summary>
/// Writes the specified T data to a memory location.
/// </summary>
/// <typeparam name="T">Type of a data to write</typeparam>
/// <param name="destination">Memory location to write to.</param>
/// <param name="data">The data to write.</param>
internal static void UnsafeWrite<T>(IntPtr destination, ref T data)
{
unsafe
{
Interop.CopyInline((void*)destination, ref data);
}
}
/// <summary>
/// Reads the specified T data from a memory location.
/// </summary>
/// <typeparam name="T">Type of a data to read</typeparam>
/// <param name="source">Memory location to read from.</param>
/// <param name="data">The data write to.</param>
internal static void UnsafeReadOut<T>(IntPtr source, out T data)
{
unsafe
{
Interop.CopyInlineOut(out data, (void*)source);
}
}
/// <summary>
/// Return the sizeof a struct from a CLR. Equivalent to sizeof operator but works on generics too.
/// </summary>
/// <typeparam name="T">a struct to evaluate</typeparam>
/// <returns>sizeof this struct</returns>
internal static int UnsafeSizeOf<T>()
{
return Interop.SizeOf<T>();
}
/// <summary>
/// Linq assisted full tree iteration and collection in a single line.
/// Warning, could be slow.
/// </summary>
/// <typeparam name="T">The type to iterate.</typeparam>
/// <param name="root">The root item</param>
/// <param name="childrenF">The function to retrieve a child</param>
public static IEnumerable<T> IterateTree<T>(T root, Func<T, IEnumerable<T>> childrenF)
{
var q = new List<T> { root };
while (q.Any())
{
var c = q[0];
q.RemoveAt(0);
q.AddRange(childrenF(c) ?? Enumerable.Empty<T>());
yield return c;
}
}
/// <summary>
/// Converts a <see cref="Stopwatch" /> raw time to a <see cref="TimeSpan" />.
/// </summary>
/// <param name="delta">The delta.</param>
/// <returns>The <see cref="TimeSpan" />.</returns>
public static TimeSpan ConvertRawToTimestamp(long delta)
{
return new TimeSpan(delta == 0 ? 0 : (delta * TimeSpan.TicksPerSecond) / Stopwatch.Frequency);
}
}
}