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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
1 ???using System;
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
2 using System.Collections.Generic;
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
3
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
4 namespace UCIS.Util {
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
5 public class ArrayUtil {
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
6 public static T[] Slice<T>(T[] input, int offset) {
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
7 if (offset < 0) offset = input.Length + offset;
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
8 return Slice(input, offset, input.Length - offset);
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
9 }
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
10 public static T[] Slice<T>(T[] input, int offset, int count) {
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
11 if (offset < 0) offset = input.Length + offset;
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
12 if (count < 0) count = input.Length + count - offset;
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
13 T[] output = new T[count];
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
14 Array.Copy(input, offset, output, 0, count);
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
15 return output;
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
16 }
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
17 public static T[] ToArray<T>(ICollection<T> input) {
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
18 T[] output = new T[input.Count];
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
19 input.CopyTo(output, 0);
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
20 return output;
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
21 }
9
9533a87363f3 Fixed deadlock in QueuedPacketStream
Ivo Smits <Ivo@UCIS.nl>
parents: 1
diff changeset
22 public static T[] ToArray<T>(ArraySegment<T> input) {
9533a87363f3 Fixed deadlock in QueuedPacketStream
Ivo Smits <Ivo@UCIS.nl>
parents: 1
diff changeset
23 return Slice(input.Array, input.Offset, input.Count);
9533a87363f3 Fixed deadlock in QueuedPacketStream
Ivo Smits <Ivo@UCIS.nl>
parents: 1
diff changeset
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
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
28 public static IList<T> ToList<T>(IEnumerable<T> input) {
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
29 return new List<T>(input);
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
30 }
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
31 public static void GnomeSort<T>(IList<T> a, Comparison<T> comparer) {
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
32 int pos = 1;
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
33 while (pos < a.Count) {
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
34 if (comparer(a[pos], a[pos - 1]) >= 0) {
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
35 pos++;
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
36 } else {
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
37 T tmp = a[pos];
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
38 a[pos] = a[pos - 1];
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
39 a[pos - 1] = tmp;
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
40 if (pos > 1) pos--; else pos++;
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
41 }
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
42 }
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
43 }
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
44 //Array shuffle
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
45 //Array unique
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
46 public static T[] Merge<T>(params ArraySegment<T>[] parts) {
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
47 int count = 0;
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
48 foreach (ArraySegment<T> segment in parts) count += segment.Count;
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
49 T[] ret = new T[count];
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
50 int offset = 0;
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
51 foreach (ArraySegment<T> segment in parts) {
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
52 Array.Copy(segment.Array, segment.Offset, ret, offset, segment.Count);
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
53 offset += segment.Count;
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
54 }
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
55 return ret;
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
56 }
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
57 public static T[] Merge<T>(params T[][] parts) {
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
58 int count = 0;
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
59 foreach (T[] segment in parts) count += segment.Length;
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
60 T[] ret = new T[count];
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
61 int offset = 0;
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
62 foreach (T[] segment in parts) {
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
63 segment.CopyTo(ret, offset);
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
64 offset += segment.Length;
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
65 }
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
66 return ret;
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
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
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
79 public static Boolean Equal<T>(T[] a, T[] b, IEqualityComparer<T> comparer) {
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
80 if (ReferenceEquals(a, b)) return true;
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
81 if (a == null || b == null) return false;
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
82 if (a.Length != b.Length) return false;
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
83 for (int i = 0; i < a.Length; i++) if (!comparer.Equals(a[i], b[i])) return false;
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
84 return true;
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
85 }
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
86 public static Boolean Equal<T>(T[] a, T[] b) where T : IEquatable<Byte> {
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
87 if (ReferenceEquals(a, b)) return true;
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
88 if (a == null || b == null) return false;
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
89 if (a.Length != b.Length) return false;
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
90 for (int i = 0; i < a.Length; i++) {
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
91 if (ReferenceEquals(a[i], b[i])) continue;
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
92 if (ReferenceEquals(a[i], null) || ReferenceEquals(b[i], null)) continue;
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
93 if (!a[i].Equals(b[i])) return false;
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
94 }
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
95 return true;
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
96 }
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
97 public static Boolean Equal(Byte[] a, Byte[] b) {
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
98 if (ReferenceEquals(a, b)) return true;
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
99 if (a == null || b == null) return false;
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
100 if (a.Length != b.Length) return false;
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
101 for (int i = 0; i < a.Length; i++) if (a[i] != b[i]) return false;
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
102 return true;
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
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
b9ef273964fd Updated ArrayUtil
Ivo Smits <Ivo@UCIS.nl>
parents: 9
diff changeset
109 public static void Add<T>(ref T[] array, params T[] items) {
b9ef273964fd Updated ArrayUtil
Ivo Smits <Ivo@UCIS.nl>
parents: 9
diff changeset
110 if (array == null) {
b9ef273964fd Updated ArrayUtil
Ivo Smits <Ivo@UCIS.nl>
parents: 9
diff changeset
111 array = new T[items.Length];
b9ef273964fd Updated ArrayUtil
Ivo Smits <Ivo@UCIS.nl>
parents: 9
diff changeset
112 items.CopyTo(array, 0);
b9ef273964fd Updated ArrayUtil
Ivo Smits <Ivo@UCIS.nl>
parents: 9
diff changeset
113 } else {
b9ef273964fd Updated ArrayUtil
Ivo Smits <Ivo@UCIS.nl>
parents: 9
diff changeset
114 int index = array.Length;
b9ef273964fd Updated ArrayUtil
Ivo Smits <Ivo@UCIS.nl>
parents: 9
diff changeset
115 Array.Resize(ref array, index + items.Length);
b9ef273964fd Updated ArrayUtil
Ivo Smits <Ivo@UCIS.nl>
parents: 9
diff changeset
116 items.CopyTo(array, index);
b9ef273964fd Updated ArrayUtil
Ivo Smits <Ivo@UCIS.nl>
parents: 9
diff changeset
117 }
b9ef273964fd Updated ArrayUtil
Ivo Smits <Ivo@UCIS.nl>
parents: 9
diff changeset
118 }
b9ef273964fd Updated ArrayUtil
Ivo Smits <Ivo@UCIS.nl>
parents: 9
diff changeset
119 public static void Add<T>(ref T[] array, ICollection<T> items) {
b9ef273964fd Updated ArrayUtil
Ivo Smits <Ivo@UCIS.nl>
parents: 9
diff changeset
120 if (array == null) {
b9ef273964fd Updated ArrayUtil
Ivo Smits <Ivo@UCIS.nl>
parents: 9
diff changeset
121 array = new T[items.Count];
b9ef273964fd Updated ArrayUtil
Ivo Smits <Ivo@UCIS.nl>
parents: 9
diff changeset
122 items.CopyTo(array, 0);
b9ef273964fd Updated ArrayUtil
Ivo Smits <Ivo@UCIS.nl>
parents: 9
diff changeset
123 } else {
b9ef273964fd Updated ArrayUtil
Ivo Smits <Ivo@UCIS.nl>
parents: 9
diff changeset
124 int index = array.Length;
b9ef273964fd Updated ArrayUtil
Ivo Smits <Ivo@UCIS.nl>
parents: 9
diff changeset
125 Array.Resize(ref array, index + items.Count);
b9ef273964fd Updated ArrayUtil
Ivo Smits <Ivo@UCIS.nl>
parents: 9
diff changeset
126 items.CopyTo(array, index);
b9ef273964fd Updated ArrayUtil
Ivo Smits <Ivo@UCIS.nl>
parents: 9
diff changeset
127 }
b9ef273964fd Updated ArrayUtil
Ivo Smits <Ivo@UCIS.nl>
parents: 9
diff changeset
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
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
142 }
3ab940a0c7a0 Initial commit
Ivo Smits <Ivo@UCIS.nl>
parents:
diff changeset
143 }