Mercurial > hg > ucis.core
annotate Util/ArrayUtil.cs @ 90:57b4c2f895d1
USBLib: added DeviceNode.GetStatus
author | Ivo Smits <Ivo@UCIS.nl> |
---|---|
date | Wed, 02 Apr 2014 16:24:55 +0200 |
parents | c9da306e06c9 |
children | ebdff34b9e4f |
rev | line source |
---|---|
0 | 1 ???using System; |
56
7c15c12ef965
Added new ArrayUtil.ToArray functions
Ivo Smits <Ivo@UCIS.nl>
parents:
48
diff
changeset
|
2 using System.Collections; |
0 | 3 using System.Collections.Generic; |
4 | |
5 namespace UCIS.Util { | |
6 public class ArrayUtil { | |
7 public static T[] Slice<T>(T[] input, int offset) { | |
8 if (offset < 0) offset = input.Length + offset; | |
9 return Slice(input, offset, input.Length - offset); | |
10 } | |
11 public static T[] Slice<T>(T[] input, int offset, int count) { | |
12 if (offset < 0) offset = input.Length + offset; | |
13 if (count < 0) count = input.Length + count - offset; | |
14 T[] output = new T[count]; | |
15 Array.Copy(input, offset, output, 0, count); | |
16 return output; | |
17 } | |
56
7c15c12ef965
Added new ArrayUtil.ToArray functions
Ivo Smits <Ivo@UCIS.nl>
parents:
48
diff
changeset
|
18 public static Object[] ToArray(ICollection input) { |
7c15c12ef965
Added new ArrayUtil.ToArray functions
Ivo Smits <Ivo@UCIS.nl>
parents:
48
diff
changeset
|
19 Object[] output = new Object[input.Count]; |
7c15c12ef965
Added new ArrayUtil.ToArray functions
Ivo Smits <Ivo@UCIS.nl>
parents:
48
diff
changeset
|
20 input.CopyTo(output, 0); |
7c15c12ef965
Added new ArrayUtil.ToArray functions
Ivo Smits <Ivo@UCIS.nl>
parents:
48
diff
changeset
|
21 return output; |
7c15c12ef965
Added new ArrayUtil.ToArray functions
Ivo Smits <Ivo@UCIS.nl>
parents:
48
diff
changeset
|
22 } |
7c15c12ef965
Added new ArrayUtil.ToArray functions
Ivo Smits <Ivo@UCIS.nl>
parents:
48
diff
changeset
|
23 public static T[] ToArray<T>(ICollection input) { |
7c15c12ef965
Added new ArrayUtil.ToArray functions
Ivo Smits <Ivo@UCIS.nl>
parents:
48
diff
changeset
|
24 T[] output = new T[input.Count]; |
7c15c12ef965
Added new ArrayUtil.ToArray functions
Ivo Smits <Ivo@UCIS.nl>
parents:
48
diff
changeset
|
25 input.CopyTo(output, 0); |
7c15c12ef965
Added new ArrayUtil.ToArray functions
Ivo Smits <Ivo@UCIS.nl>
parents:
48
diff
changeset
|
26 return output; |
7c15c12ef965
Added new ArrayUtil.ToArray functions
Ivo Smits <Ivo@UCIS.nl>
parents:
48
diff
changeset
|
27 } |
0 | 28 public static T[] ToArray<T>(ICollection<T> input) { |
29 T[] output = new T[input.Count]; | |
30 input.CopyTo(output, 0); | |
31 return output; | |
32 } | |
9 | 33 public static T[] ToArray<T>(ArraySegment<T> input) { |
34 return Slice(input.Array, input.Offset, input.Count); | |
35 } | |
43
29cf42a12c34
Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents:
19
diff
changeset
|
36 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
|
37 return (T[])input.Clone(); |
29cf42a12c34
Added convenience functions in NaCl API and ArrayUtils
Ivo Smits <Ivo@UCIS.nl>
parents:
19
diff
changeset
|
38 } |
80 | 39 public static T[] Convert<T>(IList input, Converter<Object, T> converter) { |
40 T[] output = new T[input.Count]; | |
41 for (int i = 0; i < output.Length; i++) output[i] = converter(input[i]); | |
42 return output; | |
43 } | |
0 | 44 public static IList<T> ToList<T>(IEnumerable<T> input) { |
45 return new List<T>(input); | |
46 } | |
47 public static void GnomeSort<T>(IList<T> a, Comparison<T> comparer) { | |
48 int pos = 1; | |
49 while (pos < a.Count) { | |
50 if (comparer(a[pos], a[pos - 1]) >= 0) { | |
51 pos++; | |
52 } else { | |
53 T tmp = a[pos]; | |
54 a[pos] = a[pos - 1]; | |
55 a[pos - 1] = tmp; | |
56 if (pos > 1) pos--; else pos++; | |
57 } | |
58 } | |
59 } | |
60 //Array shuffle | |
61 //Array unique | |
62 public static T[] Merge<T>(params ArraySegment<T>[] parts) { | |
63 int count = 0; | |
64 foreach (ArraySegment<T> segment in parts) count += segment.Count; | |
65 T[] ret = new T[count]; | |
66 int offset = 0; | |
67 foreach (ArraySegment<T> segment in parts) { | |
68 Array.Copy(segment.Array, segment.Offset, ret, offset, segment.Count); | |
69 offset += segment.Count; | |
70 } | |
71 return ret; | |
72 } | |
73 public static T[] Merge<T>(params T[][] parts) { | |
74 int count = 0; | |
75 foreach (T[] segment in parts) count += segment.Length; | |
76 T[] ret = new T[count]; | |
77 int offset = 0; | |
78 foreach (T[] segment in parts) { | |
79 segment.CopyTo(ret, offset); | |
80 offset += segment.Length; | |
81 } | |
82 return ret; | |
83 } | |
48
f553f6e0a396
ArrayUtil: added Merge function to merge ILists
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
84 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
|
85 int count = 0; |
f553f6e0a396
ArrayUtil: added Merge function to merge ILists
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
86 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
|
87 T[] ret = new T[count]; |
f553f6e0a396
ArrayUtil: added Merge function to merge ILists
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
88 int offset = 0; |
f553f6e0a396
ArrayUtil: added Merge function to merge ILists
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
89 foreach (IList<T> segment in parts) { |
f553f6e0a396
ArrayUtil: added Merge function to merge ILists
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
90 segment.CopyTo(ret, offset); |
f553f6e0a396
ArrayUtil: added Merge function to merge ILists
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
91 offset += segment.Count; |
f553f6e0a396
ArrayUtil: added Merge function to merge ILists
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
92 } |
f553f6e0a396
ArrayUtil: added Merge function to merge ILists
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
93 return ret; |
f553f6e0a396
ArrayUtil: added Merge function to merge ILists
Ivo Smits <Ivo@UCIS.nl>
parents:
43
diff
changeset
|
94 } |
0 | 95 public static Boolean Equal<T>(T[] a, T[] b, IEqualityComparer<T> comparer) { |
96 if (ReferenceEquals(a, b)) return true; | |
97 if (a == null || b == null) return false; | |
98 if (a.Length != b.Length) return false; | |
99 for (int i = 0; i < a.Length; i++) if (!comparer.Equals(a[i], b[i])) return false; | |
100 return true; | |
101 } | |
102 public static Boolean Equal<T>(T[] a, T[] b) where T : IEquatable<Byte> { | |
103 if (ReferenceEquals(a, b)) return true; | |
104 if (a == null || b == null) return false; | |
105 if (a.Length != b.Length) return false; | |
106 for (int i = 0; i < a.Length; i++) { | |
107 if (ReferenceEquals(a[i], b[i])) continue; | |
108 if (ReferenceEquals(a[i], null) || ReferenceEquals(b[i], null)) continue; | |
109 if (!a[i].Equals(b[i])) return false; | |
110 } | |
111 return true; | |
112 } | |
113 public static Boolean Equal(Byte[] a, Byte[] b) { | |
114 if (ReferenceEquals(a, b)) return true; | |
115 if (a == null || b == null) return false; | |
116 if (a.Length != b.Length) return false; | |
117 for (int i = 0; i < a.Length; i++) if (a[i] != b[i]) return false; | |
118 return true; | |
119 } | |
1
28dc7d535036
Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
120 public static int GetHashCode<T>(T[] array) { |
28dc7d535036
Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
121 int h = 0; |
28dc7d535036
Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
122 foreach (T v in array) h ^= v.GetHashCode(); |
28dc7d535036
Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
123 return h; |
28dc7d535036
Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
124 } |
81
3352f89cf6f5
FBGUI ContainerControl fixes (client area, keyboard capture)
Ivo Smits <Ivo@UCIS.nl>
parents:
80
diff
changeset
|
125 public static int Add<T>(ref T[] array, params T[] items) { |
19 | 126 if (array == null) { |
127 array = new T[items.Length]; | |
128 items.CopyTo(array, 0); | |
81
3352f89cf6f5
FBGUI ContainerControl fixes (client area, keyboard capture)
Ivo Smits <Ivo@UCIS.nl>
parents:
80
diff
changeset
|
129 return 0; |
19 | 130 } else { |
131 int index = array.Length; | |
132 Array.Resize(ref array, index + items.Length); | |
133 items.CopyTo(array, index); | |
81
3352f89cf6f5
FBGUI ContainerControl fixes (client area, keyboard capture)
Ivo Smits <Ivo@UCIS.nl>
parents:
80
diff
changeset
|
134 return index; |
19 | 135 } |
136 } | |
81
3352f89cf6f5
FBGUI ContainerControl fixes (client area, keyboard capture)
Ivo Smits <Ivo@UCIS.nl>
parents:
80
diff
changeset
|
137 public static int Add<T>(ref T[] array, ICollection<T> items) { |
19 | 138 if (array == null) { |
139 array = new T[items.Count]; | |
140 items.CopyTo(array, 0); | |
81
3352f89cf6f5
FBGUI ContainerControl fixes (client area, keyboard capture)
Ivo Smits <Ivo@UCIS.nl>
parents:
80
diff
changeset
|
141 return 0; |
19 | 142 } else { |
143 int index = array.Length; | |
144 Array.Resize(ref array, index + items.Count); | |
145 items.CopyTo(array, index); | |
81
3352f89cf6f5
FBGUI ContainerControl fixes (client area, keyboard capture)
Ivo Smits <Ivo@UCIS.nl>
parents:
80
diff
changeset
|
146 return index; |
19 | 147 } |
148 } | |
81
3352f89cf6f5
FBGUI ContainerControl fixes (client area, keyboard capture)
Ivo Smits <Ivo@UCIS.nl>
parents:
80
diff
changeset
|
149 public static int Add<T>(ref T[] array, T item) { |
1
28dc7d535036
Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
150 if (array == null) { |
28dc7d535036
Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
151 array = new T[] { item }; |
81
3352f89cf6f5
FBGUI ContainerControl fixes (client area, keyboard capture)
Ivo Smits <Ivo@UCIS.nl>
parents:
80
diff
changeset
|
152 return 0; |
1
28dc7d535036
Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
153 } else { |
28dc7d535036
Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
154 int index = array.Length; |
28dc7d535036
Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
155 Array.Resize(ref array, index + 1); |
28dc7d535036
Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
156 array[index] = item; |
81
3352f89cf6f5
FBGUI ContainerControl fixes (client area, keyboard capture)
Ivo Smits <Ivo@UCIS.nl>
parents:
80
diff
changeset
|
157 return index; |
1
28dc7d535036
Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
158 } |
28dc7d535036
Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
159 } |
81
3352f89cf6f5
FBGUI ContainerControl fixes (client area, keyboard capture)
Ivo Smits <Ivo@UCIS.nl>
parents:
80
diff
changeset
|
160 public static int AddUnique<T>(ref T[] array, T item) { |
3352f89cf6f5
FBGUI ContainerControl fixes (client area, keyboard capture)
Ivo Smits <Ivo@UCIS.nl>
parents:
80
diff
changeset
|
161 int index = Array.IndexOf(array, item); |
3352f89cf6f5
FBGUI ContainerControl fixes (client area, keyboard capture)
Ivo Smits <Ivo@UCIS.nl>
parents:
80
diff
changeset
|
162 if (index == -1) index = Add(ref array, item); |
3352f89cf6f5
FBGUI ContainerControl fixes (client area, keyboard capture)
Ivo Smits <Ivo@UCIS.nl>
parents:
80
diff
changeset
|
163 return index; |
1
28dc7d535036
Small improvements, introduced PacketStream
Ivo Smits <Ivo@UCIS.nl>
parents:
0
diff
changeset
|
164 } |
88
c9da306e06c9
FBGUI: Improved FBGContainerControl thread safety
Ivo Smits <Ivo@UCIS.nl>
parents:
81
diff
changeset
|
165 public static Boolean Remove<T>(ref T[] array, T item) { |
c9da306e06c9
FBGUI: Improved FBGContainerControl thread safety
Ivo Smits <Ivo@UCIS.nl>
parents:
81
diff
changeset
|
166 if (array == null) return false; |
c9da306e06c9
FBGUI: Improved FBGContainerControl thread safety
Ivo Smits <Ivo@UCIS.nl>
parents:
81
diff
changeset
|
167 int index = Array.IndexOf(array, item); |
c9da306e06c9
FBGUI: Improved FBGContainerControl thread safety
Ivo Smits <Ivo@UCIS.nl>
parents:
81
diff
changeset
|
168 if (index == -1) return false; |
c9da306e06c9
FBGUI: Improved FBGContainerControl thread safety
Ivo Smits <Ivo@UCIS.nl>
parents:
81
diff
changeset
|
169 T[] newarray = new T[array.Length - 1]; |
c9da306e06c9
FBGUI: Improved FBGContainerControl thread safety
Ivo Smits <Ivo@UCIS.nl>
parents:
81
diff
changeset
|
170 if (index > 0) Array.Copy(array, 0, newarray, 0, index); |
c9da306e06c9
FBGUI: Improved FBGContainerControl thread safety
Ivo Smits <Ivo@UCIS.nl>
parents:
81
diff
changeset
|
171 if (index < array.Length - 1) Array.Copy(array, index + 1, newarray, index, array.Length - index - 1); |
c9da306e06c9
FBGUI: Improved FBGContainerControl thread safety
Ivo Smits <Ivo@UCIS.nl>
parents:
81
diff
changeset
|
172 array = newarray; |
c9da306e06c9
FBGUI: Improved FBGContainerControl thread safety
Ivo Smits <Ivo@UCIS.nl>
parents:
81
diff
changeset
|
173 return true; |
c9da306e06c9
FBGUI: Improved FBGContainerControl thread safety
Ivo Smits <Ivo@UCIS.nl>
parents:
81
diff
changeset
|
174 } |
0 | 175 } |
176 } |