Mercurial > hg > ucis.core
annotate Util/ArrayUtil.cs @ 54:ba4e2cb031e0
Added general purpose tar archive reader class
author | Ivo Smits <Ivo@UCIS.nl> |
---|---|
date | Wed, 02 Oct 2013 21:17:30 +0200 |
parents | f553f6e0a396 |
children | 7c15c12ef965 |
rev | line source |
---|---|
0 | 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 } | |
9 | 22 public static T[] ToArray<T>(ArraySegment<T> input) { |
23 return Slice(input.Array, input.Offset, input.Count); | |
24 } | |
43
29cf42a12c34
Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents:
19
diff
changeset
|
25 public static T[] ToArray<T>(T[] input) { |
29cf42a12c34
Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents:
19
diff
changeset
|
26 return (T[])input.Clone(); |
29cf42a12c34
Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents:
19
diff
changeset
|
27 } |
0 | 28 public static IList<T> ToList<T>(IEnumerable<T> input) { |
29 return new List<T>(input); | |
30 } | |
31 public static void GnomeSort<T>(IList<T> a, Comparison<T> comparer) { | |
32 int pos = 1; | |
33 while (pos < a.Count) { | |
34 if (comparer(a[pos], a[pos - 1]) >= 0) { | |
35 pos++; | |
36 } else { | |
37 T tmp = a[pos]; | |
38 a[pos] = a[pos - 1]; | |
39 a[pos - 1] = tmp; | |
40 if (pos > 1) pos--; else pos++; | |
41 } | |
42 } | |
43 } | |
44 //Array shuffle | |
45 //Array unique | |
46 public static T[] Merge<T>(params ArraySegment<T>[] parts) { | |
47 int count = 0; | |
48 foreach (ArraySegment<T> segment in parts) count += segment.Count; | |
49 T[] ret = new T[count]; | |
50 int offset = 0; | |
51 foreach (ArraySegment<T> segment in parts) { | |
52 Array.Copy(segment.Array, segment.Offset, ret, offset, segment.Count); | |
53 offset += segment.Count; | |
54 } | |
55 return ret; | |
56 } | |
57 public static T[] Merge<T>(params T[][] parts) { | |
58 int count = 0; | |
59 foreach (T[] segment in parts) count += segment.Length; | |
60 T[] ret = new T[count]; | |
61 int offset = 0; | |
62 foreach (T[] segment in parts) { | |
63 segment.CopyTo(ret, offset); | |
64 offset += segment.Length; | |
65 } | |
66 return ret; | |
67 } | |
48
f553f6e0a396
ArrayUtil: added Merge function to merge ILists
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
68 public static T[] Merge<T>(params IList<T>[] parts) { |
f553f6e0a396
ArrayUtil: added Merge function to merge ILists
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
69 int count = 0; |
f553f6e0a396
ArrayUtil: added Merge function to merge ILists
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
70 foreach (IList<T> segment in parts) count += segment.Count; |
f553f6e0a396
ArrayUtil: added Merge function to merge ILists
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
71 T[] ret = new T[count]; |
f553f6e0a396
ArrayUtil: added Merge function to merge ILists
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
72 int offset = 0; |
f553f6e0a396
ArrayUtil: added Merge function to merge ILists
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
73 foreach (IList<T> segment in parts) { |
f553f6e0a396
ArrayUtil: added Merge function to merge ILists
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
74 segment.CopyTo(ret, offset); |
f553f6e0a396
ArrayUtil: added Merge function to merge ILists
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
75 offset += segment.Count; |
f553f6e0a396
ArrayUtil: added Merge function to merge ILists
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
76 } |
f553f6e0a396
ArrayUtil: added Merge function to merge ILists
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
77 return ret; |
f553f6e0a396
ArrayUtil: added Merge function to merge ILists
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
78 } |
0 | 79 public static Boolean Equal<T>(T[] a, T[] b, IEqualityComparer<T> comparer) { |
80 if (ReferenceEquals(a, b)) return true; | |
81 if (a == null || b == null) return false; | |
82 if (a.Length != b.Length) return false; | |
83 for (int i = 0; i < a.Length; i++) if (!comparer.Equals(a[i], b[i])) return false; | |
84 return true; | |
85 } | |
86 public static Boolean Equal<T>(T[] a, T[] b) where T : IEquatable<Byte> { | |
87 if (ReferenceEquals(a, b)) return true; | |
88 if (a == null || b == null) return false; | |
89 if (a.Length != b.Length) return false; | |
90 for (int i = 0; i < a.Length; i++) { | |
91 if (ReferenceEquals(a[i], b[i])) continue; | |
92 if (ReferenceEquals(a[i], null) || ReferenceEquals(b[i], null)) continue; | |
93 if (!a[i].Equals(b[i])) return false; | |
94 } | |
95 return true; | |
96 } | |
97 public static Boolean Equal(Byte[] a, Byte[] b) { | |
98 if (ReferenceEquals(a, b)) return true; | |
99 if (a == null || b == null) return false; | |
100 if (a.Length != b.Length) return false; | |
101 for (int i = 0; i < a.Length; i++) if (a[i] != b[i]) return false; | |
102 return true; | |
103 } | |
1
28dc7d535036
Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
104 public static int GetHashCode<T>(T[] array) { |
28dc7d535036
Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
105 int h = 0; |
28dc7d535036
Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
106 foreach (T v in array) h ^= v.GetHashCode(); |
28dc7d535036
Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
107 return h; |
28dc7d535036
Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
108 } |
19 | 109 public static void Add<T>(ref T[] array, params T[] items) { |
110 if (array == null) { | |
111 array = new T[items.Length]; | |
112 items.CopyTo(array, 0); | |
113 } else { | |
114 int index = array.Length; | |
115 Array.Resize(ref array, index + items.Length); | |
116 items.CopyTo(array, index); | |
117 } | |
118 } | |
119 public static void Add<T>(ref T[] array, ICollection<T> items) { | |
120 if (array == null) { | |
121 array = new T[items.Count]; | |
122 items.CopyTo(array, 0); | |
123 } else { | |
124 int index = array.Length; | |
125 Array.Resize(ref array, index + items.Count); | |
126 items.CopyTo(array, index); | |
127 } | |
128 } | |
1
28dc7d535036
Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
129 public static void Add<T>(ref T[] array, T item) { |
28dc7d535036
Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
130 if (array == null) { |
28dc7d535036
Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
131 array = new T[] { item }; |
28dc7d535036
Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
132 } else { |
28dc7d535036
Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
133 int index = array.Length; |
28dc7d535036
Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
134 Array.Resize(ref array, index + 1); |
28dc7d535036
Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
135 array[index] = item; |
28dc7d535036
Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
136 } |
28dc7d535036
Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
137 } |
28dc7d535036
Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
138 public static void AddUnique<T>(ref T[] array, T item) { |
28dc7d535036
Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
139 if (Array.IndexOf(array, item) != -1) return; |
28dc7d535036
Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
140 Add(ref array, item); |
28dc7d535036
Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
141 } |
0 | 142 } |
143 } |