Mercurial > hg > ucis.core
comparison Util/ArrayUtil.cs @ 0:3ab940a0c7a0
Initial commit
author | Ivo Smits <Ivo@UCIS.nl> |
---|---|
date | Tue, 11 Sep 2012 16:28:53 +0200 |
parents | |
children | 28dc7d535036 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:3ab940a0c7a0 |
---|---|
1 using System; | |
2 using System.Collections.Generic; | |
3 | |
4 namespace UCIS.Util { | |
5 public class ArrayUtil { | |
6 public static T[] Slice<T>(T[] input, int offset) { | |
7 if (offset < 0) offset = input.Length + offset; | |
8 return Slice(input, offset, input.Length - offset); | |
9 } | |
10 public static T[] Slice<T>(T[] input, int offset, int count) { | |
11 if (offset < 0) offset = input.Length + offset; | |
12 if (count < 0) count = input.Length + count - offset; | |
13 T[] output = new T[count]; | |
14 Array.Copy(input, offset, output, 0, count); | |
15 return output; | |
16 } | |
17 public static T[] ToArray<T>(ICollection<T> input) { | |
18 T[] output = new T[input.Count]; | |
19 input.CopyTo(output, 0); | |
20 return output; | |
21 } | |
22 public static IList<T> ToList<T>(IEnumerable<T> input) { | |
23 return new List<T>(input); | |
24 } | |
25 public static void GnomeSort<T>(IList<T> a, Comparison<T> comparer) { | |
26 int pos = 1; | |
27 while (pos < a.Count) { | |
28 if (comparer(a[pos], a[pos - 1]) >= 0) { | |
29 pos++; | |
30 } else { | |
31 T tmp = a[pos]; | |
32 a[pos] = a[pos - 1]; | |
33 a[pos - 1] = tmp; | |
34 if (pos > 1) pos--; else pos++; | |
35 } | |
36 } | |
37 } | |
38 //Array shuffle | |
39 //Array unique | |
40 public static T[] Merge<T>(params ArraySegment<T>[] parts) { | |
41 int count = 0; | |
42 foreach (ArraySegment<T> segment in parts) count += segment.Count; | |
43 T[] ret = new T[count]; | |
44 int offset = 0; | |
45 foreach (ArraySegment<T> segment in parts) { | |
46 Array.Copy(segment.Array, segment.Offset, ret, offset, segment.Count); | |
47 offset += segment.Count; | |
48 } | |
49 return ret; | |
50 } | |
51 public static T[] Merge<T>(params T[][] parts) { | |
52 int count = 0; | |
53 foreach (T[] segment in parts) count += segment.Length; | |
54 T[] ret = new T[count]; | |
55 int offset = 0; | |
56 foreach (T[] segment in parts) { | |
57 segment.CopyTo(ret, offset); | |
58 offset += segment.Length; | |
59 } | |
60 return ret; | |
61 } | |
62 public static Boolean Equal<T>(T[] a, T[] b, IEqualityComparer<T> comparer) { | |
63 if (ReferenceEquals(a, b)) return true; | |
64 if (a == null || b == null) return false; | |
65 if (a.Length != b.Length) return false; | |
66 for (int i = 0; i < a.Length; i++) if (!comparer.Equals(a[i], b[i])) return false; | |
67 return true; | |
68 } | |
69 public static Boolean Equal<T>(T[] a, T[] b) where T : IEquatable<Byte> { | |
70 if (ReferenceEquals(a, b)) return true; | |
71 if (a == null || b == null) return false; | |
72 if (a.Length != b.Length) return false; | |
73 for (int i = 0; i < a.Length; i++) { | |
74 if (ReferenceEquals(a[i], b[i])) continue; | |
75 if (ReferenceEquals(a[i], null) || ReferenceEquals(b[i], null)) continue; | |
76 if (!a[i].Equals(b[i])) return false; | |
77 } | |
78 return true; | |
79 } | |
80 public static Boolean Equal(Byte[] a, Byte[] b) { | |
81 if (ReferenceEquals(a, b)) return true; | |
82 if (a == null || b == null) return false; | |
83 if (a.Length != b.Length) return false; | |
84 for (int i = 0; i < a.Length; i++) if (a[i] != b[i]) return false; | |
85 return true; | |
86 } | |
87 } | |
88 } |