wasSharp – Diff between revs 7 and 27
?pathlinks?
Rev 7 | Rev 27 | |||
---|---|---|---|---|
1 | /////////////////////////////////////////////////////////////////////////// |
1 | /////////////////////////////////////////////////////////////////////////// |
|
2 | // Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 // |
2 | // Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 // |
|
3 | // Please see: http://www.gnu.org/licenses/gpl.html for legal details, // |
3 | // Please see: http://www.gnu.org/licenses/gpl.html for legal details, // |
|
4 | // rights of fair usage, the disclaimer and warranty conditions. // |
4 | // rights of fair usage, the disclaimer and warranty conditions. // |
|
5 | /////////////////////////////////////////////////////////////////////////// |
5 | /////////////////////////////////////////////////////////////////////////// |
|
6 | |
6 | |
|
7 | using System; |
7 | using System; |
|
8 | |
8 | |
|
9 | namespace wasSharp |
9 | namespace wasSharp |
|
10 | { |
10 | { |
|
11 | public static class Arrays |
11 | public static class Arrays |
|
12 | { |
12 | { |
|
13 | /////////////////////////////////////////////////////////////////////////// |
13 | /////////////////////////////////////////////////////////////////////////// |
|
14 | // Copyright (C) Wizardry and Steamworks 2014 - License: GNU GPLv3 // |
14 | // Copyright (C) Wizardry and Steamworks 2014 - License: GNU GPLv3 // |
|
15 | /////////////////////////////////////////////////////////////////////////// |
15 | /////////////////////////////////////////////////////////////////////////// |
|
16 | /// <summary> |
16 | /// <summary> |
|
17 | /// Gets an array element at a given modulo index. |
17 | /// Gets an array element at a given modulo index. |
|
18 | /// </summary> |
18 | /// </summary> |
|
19 | /// <typeparam name="T">the array type</typeparam> |
19 | /// <typeparam name="T">the array type</typeparam> |
|
20 | /// <param name="index">a positive or negative index of the element</param> |
20 | /// <param name="index">a positive or negative index of the element</param> |
|
21 | /// <param name="data">the array</param> |
21 | /// <param name="data">the array</param> |
|
22 | /// <return>an array element</return> |
22 | /// <return>an array element</return> |
|
23 | public static T GetElementAt<T>(T[] data, int index) |
23 | public static T GetElementAt<T>(T[] data, int index) |
|
24 | { |
24 | { |
|
25 | return index < 0 ? data[(index%data.Length + data.Length)%data.Length] : data[index%data.Length]; |
25 | return index < 0 ? data[(index % data.Length + data.Length) % data.Length] : data[index % data.Length]; |
|
26 | } |
26 | } |
|
27 | |
27 | |
|
28 | /////////////////////////////////////////////////////////////////////////// |
28 | /////////////////////////////////////////////////////////////////////////// |
|
29 | // Copyright (C) Wizardry and Steamworks 2014 - License: GNU GPLv3 // |
29 | // Copyright (C) Wizardry and Steamworks 2014 - License: GNU GPLv3 // |
|
30 | /////////////////////////////////////////////////////////////////////////// |
30 | /////////////////////////////////////////////////////////////////////////// |
|
31 | /// <summary> |
31 | /// <summary> |
|
32 | /// Gets a sub-array from an array. |
32 | /// Gets a sub-array from an array. |
|
33 | /// </summary> |
33 | /// </summary> |
|
34 | /// <typeparam name="T">the array type</typeparam> |
34 | /// <typeparam name="T">the array type</typeparam> |
|
35 | /// <param name="data">the array</param> |
35 | /// <param name="data">the array</param> |
|
36 | /// <param name="start">the start index</param> |
36 | /// <param name="start">the start index</param> |
|
37 | /// <param name="stop">the stop index (-1 denotes the end)</param> |
37 | /// <param name="stop">the stop index (-1 denotes the end)</param> |
|
38 | /// <returns>the array slice between start and stop</returns> |
38 | /// <returns>the array slice between start and stop</returns> |
|
39 | public static T[] GetSubArray<T>(T[] data, int start, int stop) |
39 | public static T[] GetSubArray<T>(T[] data, int start, int stop) |
|
40 | { |
40 | { |
|
41 | if (stop.Equals(-1)) |
41 | if (stop.Equals(-1)) |
|
42 | stop = data.Length - 1; |
42 | stop = data.Length - 1; |
|
43 | var result = new T[stop - start + 1]; |
43 | var result = new T[stop - start + 1]; |
|
44 | Array.Copy(data, start, result, 0, stop - start + 1); |
44 | Array.Copy(data, start, result, 0, stop - start + 1); |
|
45 | return result; |
45 | return result; |
|
46 | } |
46 | } |
|
47 | |
47 | |
|
48 | /////////////////////////////////////////////////////////////////////////// |
48 | /////////////////////////////////////////////////////////////////////////// |
|
49 | // Copyright (C) Wizardry and Steamworks 2014 - License: GNU GPLv3 // |
49 | // Copyright (C) Wizardry and Steamworks 2014 - License: GNU GPLv3 // |
|
50 | /////////////////////////////////////////////////////////////////////////// |
50 | /////////////////////////////////////////////////////////////////////////// |
|
51 | /// <summary> |
51 | /// <summary> |
|
52 | /// Delete a sub-array and return the result. |
52 | /// Delete a sub-array and return the result. |
|
53 | /// </summary> |
53 | /// </summary> |
|
54 | /// <typeparam name="T">the array type</typeparam> |
54 | /// <typeparam name="T">the array type</typeparam> |
|
55 | /// <param name="data">the array</param> |
55 | /// <param name="data">the array</param> |
|
56 | /// <param name="start">the start index</param> |
56 | /// <param name="start">the start index</param> |
|
57 | /// <param name="stop">the stop index (-1 denotes the end)</param> |
57 | /// <param name="stop">the stop index (-1 denotes the end)</param> |
|
58 | /// <returns>the array without elements between start and stop</returns> |
58 | /// <returns>the array without elements between start and stop</returns> |
|
59 | public static T[] DeleteSubArray<T>(T[] data, int start, int stop) |
59 | public static T[] DeleteSubArray<T>(T[] data, int start, int stop) |
|
60 | { |
60 | { |
|
61 | if (stop.Equals(-1)) |
61 | if (stop.Equals(-1)) |
|
62 | stop = data.Length - 1; |
62 | stop = data.Length - 1; |
|
63 | var result = new T[data.Length - (stop - start) - 1]; |
63 | var result = new T[data.Length - (stop - start) - 1]; |
|
64 | Array.Copy(data, 0, result, 0, start); |
64 | Array.Copy(data, 0, result, 0, start); |
|
65 | Array.Copy(data, stop + 1, result, start, data.Length - stop - 1); |
65 | Array.Copy(data, stop + 1, result, start, data.Length - stop - 1); |
|
66 | return result; |
66 | return result; |
|
67 | } |
67 | } |
|
68 | |
68 | |
|
69 | /////////////////////////////////////////////////////////////////////////// |
69 | /////////////////////////////////////////////////////////////////////////// |
|
70 | // Copyright (C) Wizardry and Steamworks 2014 - License: GNU GPLv3 // |
70 | // Copyright (C) Wizardry and Steamworks 2014 - License: GNU GPLv3 // |
|
71 | /////////////////////////////////////////////////////////////////////////// |
71 | /////////////////////////////////////////////////////////////////////////// |
|
72 | /// <summary> |
72 | /// <summary> |
|
73 | /// Concatenate multiple arrays. |
73 | /// Concatenate multiple arrays. |
|
74 | /// </summary> |
74 | /// </summary> |
|
75 | /// <typeparam name="T">the array type</typeparam> |
75 | /// <typeparam name="T">the array type</typeparam> |
|
76 | /// <param name="arrays">multiple arrays</param> |
76 | /// <param name="arrays">multiple arrays</param> |
|
77 | /// <returns>a flat array with all arrays concatenated</returns> |
77 | /// <returns>a flat array with all arrays concatenated</returns> |
|
78 | public static T[] ConcatenateArrays<T>(params T[][] arrays) |
78 | public static T[] ConcatenateArrays<T>(params T[][] arrays) |
|
79 | { |
79 | { |
|
80 | var resultLength = 0; |
80 | var resultLength = 0; |
|
81 | foreach (var o in arrays) |
81 | foreach (var o in arrays) |
|
82 | { |
82 | { |
|
83 | resultLength += o.Length; |
83 | resultLength += o.Length; |
|
84 | } |
84 | } |
|
85 | var result = new T[resultLength]; |
85 | var result = new T[resultLength]; |
|
86 | var offset = 0; |
86 | var offset = 0; |
|
87 | for (var x = 0; x < arrays.Length; x++) |
87 | for (var x = 0; x < arrays.Length; x++) |
|
88 | { |
88 | { |
|
89 | arrays[x].CopyTo(result, offset); |
89 | arrays[x].CopyTo(result, offset); |
|
90 | offset += arrays[x].Length; |
90 | offset += arrays[x].Length; |
|
91 | } |
91 | } |
|
92 | return result; |
92 | return result; |
|
93 | } |
93 | } |
|
94 | |
94 | |
|
95 | /////////////////////////////////////////////////////////////////////////// |
95 | /////////////////////////////////////////////////////////////////////////// |
|
96 | // Copyright (C) Wizardry and Steamworks 2014 - License: GNU GPLv3 // |
96 | // Copyright (C) Wizardry and Steamworks 2014 - License: GNU GPLv3 // |
|
97 | /////////////////////////////////////////////////////////////////////////// |
97 | /////////////////////////////////////////////////////////////////////////// |
|
98 | /// <summary> |
98 | /// <summary> |
|
99 | /// Permutes an array in reverse a given number of times. |
99 | /// Permutes an array in reverse a given number of times. |
|
100 | /// </summary> |
100 | /// </summary> |
|
101 | /// <typeparam name="T">the array type</typeparam> |
101 | /// <typeparam name="T">the array type</typeparam> |
|
102 | /// <param name="input">the array</param> |
102 | /// <param name="input">the array</param> |
|
103 | /// <param name="times">the number of times to permute</param> |
103 | /// <param name="times">the number of times to permute</param> |
|
104 | /// <returns>the array with the elements permuted</returns> |
104 | /// <returns>the array with the elements permuted</returns> |
|
105 | public static T[] ReversePermuteArrayElements<T>(T[] input, int times) |
105 | public static T[] ReversePermuteArrayElements<T>(T[] input, int times) |
|
106 | { |
106 | { |
|
107 | if (times.Equals(0)) return input; |
107 | if (times.Equals(0)) return input; |
|
108 | var slice = new T[input.Length]; |
108 | var slice = new T[input.Length]; |
|
109 | Array.Copy(input, 1, slice, 0, input.Length - 1); |
109 | Array.Copy(input, 1, slice, 0, input.Length - 1); |
|
110 | Array.Copy(input, 0, slice, input.Length - 1, 1); |
110 | Array.Copy(input, 0, slice, input.Length - 1, 1); |
|
111 | return ReversePermuteArrayElements(slice, --times); |
111 | return ReversePermuteArrayElements(slice, --times); |
|
112 | } |
112 | } |
|
113 | |
113 | |
|
114 | /////////////////////////////////////////////////////////////////////////// |
114 | /////////////////////////////////////////////////////////////////////////// |
|
115 | // Copyright (C) Wizardry and Steamworks 2014 - License: GNU GPLv3 // |
115 | // Copyright (C) Wizardry and Steamworks 2014 - License: GNU GPLv3 // |
|
116 | /////////////////////////////////////////////////////////////////////////// |
116 | /////////////////////////////////////////////////////////////////////////// |
|
117 | /// <summary> |
117 | /// <summary> |
|
118 | /// Permutes an array forward a given number of times. |
118 | /// Permutes an array forward a given number of times. |
|
119 | /// </summary> |
119 | /// </summary> |
|
120 | /// <typeparam name="T">the array type</typeparam> |
120 | /// <typeparam name="T">the array type</typeparam> |
|
121 | /// <param name="input">the array</param> |
121 | /// <param name="input">the array</param> |
|
122 | /// <param name="times">the number of times to permute</param> |
122 | /// <param name="times">the number of times to permute</param> |
|
123 | /// <returns>the array with the elements permuted</returns> |
123 | /// <returns>the array with the elements permuted</returns> |
|
124 | public static T[] ForwardPermuteArrayElements<T>(T[] input, int times) |
124 | public static T[] ForwardPermuteArrayElements<T>(T[] input, int times) |
|
125 | { |
125 | { |
|
126 | if (times.Equals(0)) return input; |
126 | if (times.Equals(0)) return input; |
|
127 | var slice = new T[input.Length]; |
127 | var slice = new T[input.Length]; |
|
128 | Array.Copy(input, input.Length - 1, slice, 0, 1); |
128 | Array.Copy(input, input.Length - 1, slice, 0, 1); |
|
129 | Array.Copy(input, 0, slice, 1, input.Length - 1); |
129 | Array.Copy(input, 0, slice, 1, input.Length - 1); |
|
130 | return ForwardPermuteArrayElements(slice, --times); |
130 | return ForwardPermuteArrayElements(slice, --times); |
|
131 | } |
131 | } |
|
132 | } |
132 | } |
|
133 | } |
- | ||
134 | |
133 | } |
|
- | 134 | |
||
135 |
|
- | ||
136 | |
- | ||
137 | |
- | ||
138 | |
- |