wasSharp – Diff between revs 1 and 5
?pathlinks?
Rev 1 | Rev 5 | |||
---|---|---|---|---|
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 class Arrays |
11 | public 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 wasGetElementAt<T>(T[] data, int index) |
23 | public static T GetElementAt<T>(T[] data, int index) |
|
24 | { |
24 | { |
|
25 | switch (index < 0) |
25 | switch (index < 0) |
|
26 | { |
26 | { |
|
27 | case true: |
27 | case true: |
|
28 | return data[((index%data.Length) + data.Length)%data.Length]; |
28 | return data[((index%data.Length) + data.Length)%data.Length]; |
|
29 | default: |
29 | default: |
|
30 | return data[index%data.Length]; |
30 | return data[index%data.Length]; |
|
31 | } |
31 | } |
|
32 | } |
32 | } |
|
33 | |
33 | |
|
34 | /////////////////////////////////////////////////////////////////////////// |
34 | /////////////////////////////////////////////////////////////////////////// |
|
35 | // Copyright (C) Wizardry and Steamworks 2014 - License: GNU GPLv3 // |
35 | // Copyright (C) Wizardry and Steamworks 2014 - License: GNU GPLv3 // |
|
36 | /////////////////////////////////////////////////////////////////////////// |
36 | /////////////////////////////////////////////////////////////////////////// |
|
37 | /// <summary> |
37 | /// <summary> |
|
38 | /// Gets a sub-array from an array. |
38 | /// Gets a sub-array from an array. |
|
39 | /// </summary> |
39 | /// </summary> |
|
40 | /// <typeparam name="T">the array type</typeparam> |
40 | /// <typeparam name="T">the array type</typeparam> |
|
41 | /// <param name="data">the array</param> |
41 | /// <param name="data">the array</param> |
|
42 | /// <param name="start">the start index</param> |
42 | /// <param name="start">the start index</param> |
|
43 | /// <param name="stop">the stop index (-1 denotes the end)</param> |
43 | /// <param name="stop">the stop index (-1 denotes the end)</param> |
|
44 | /// <returns>the array slice between start and stop</returns> |
44 | /// <returns>the array slice between start and stop</returns> |
|
45 | public static T[] wasGetSubArray<T>(T[] data, int start, int stop) |
45 | public static T[] GetSubArray<T>(T[] data, int start, int stop) |
|
46 | { |
46 | { |
|
47 | if (stop.Equals(-1)) |
47 | if (stop.Equals(-1)) |
|
48 | stop = data.Length - 1; |
48 | stop = data.Length - 1; |
|
49 | T[] result = new T[stop - start + 1]; |
49 | T[] result = new T[stop - start + 1]; |
|
50 | Array.Copy(data, start, result, 0, stop - start + 1); |
50 | Array.Copy(data, start, result, 0, stop - start + 1); |
|
51 | return result; |
51 | return result; |
|
52 | } |
52 | } |
|
53 | |
53 | |
|
54 | /////////////////////////////////////////////////////////////////////////// |
54 | /////////////////////////////////////////////////////////////////////////// |
|
55 | // Copyright (C) Wizardry and Steamworks 2014 - License: GNU GPLv3 // |
55 | // Copyright (C) Wizardry and Steamworks 2014 - License: GNU GPLv3 // |
|
56 | /////////////////////////////////////////////////////////////////////////// |
56 | /////////////////////////////////////////////////////////////////////////// |
|
57 | /// <summary> |
57 | /// <summary> |
|
58 | /// Delete a sub-array and return the result. |
58 | /// Delete a sub-array and return the result. |
|
59 | /// </summary> |
59 | /// </summary> |
|
60 | /// <typeparam name="T">the array type</typeparam> |
60 | /// <typeparam name="T">the array type</typeparam> |
|
61 | /// <param name="data">the array</param> |
61 | /// <param name="data">the array</param> |
|
62 | /// <param name="start">the start index</param> |
62 | /// <param name="start">the start index</param> |
|
63 | /// <param name="stop">the stop index (-1 denotes the end)</param> |
63 | /// <param name="stop">the stop index (-1 denotes the end)</param> |
|
64 | /// <returns>the array without elements between start and stop</returns> |
64 | /// <returns>the array without elements between start and stop</returns> |
|
65 | public static T[] wasDeleteSubArray<T>(T[] data, int start, int stop) |
65 | public static T[] DeleteSubArray<T>(T[] data, int start, int stop) |
|
66 | { |
66 | { |
|
67 | if (stop.Equals(-1)) |
67 | if (stop.Equals(-1)) |
|
68 | stop = data.Length - 1; |
68 | stop = data.Length - 1; |
|
69 | T[] result = new T[data.Length - (stop - start) - 1]; |
69 | T[] result = new T[data.Length - (stop - start) - 1]; |
|
70 | Array.Copy(data, 0, result, 0, start); |
70 | Array.Copy(data, 0, result, 0, start); |
|
71 | Array.Copy(data, stop + 1, result, start, data.Length - stop - 1); |
71 | Array.Copy(data, stop + 1, result, start, data.Length - stop - 1); |
|
72 | return result; |
72 | return result; |
|
73 | } |
73 | } |
|
74 | |
74 | |
|
75 | /////////////////////////////////////////////////////////////////////////// |
75 | /////////////////////////////////////////////////////////////////////////// |
|
76 | // Copyright (C) Wizardry and Steamworks 2014 - License: GNU GPLv3 // |
76 | // Copyright (C) Wizardry and Steamworks 2014 - License: GNU GPLv3 // |
|
77 | /////////////////////////////////////////////////////////////////////////// |
77 | /////////////////////////////////////////////////////////////////////////// |
|
78 | /// <summary> |
78 | /// <summary> |
|
79 | /// Concatenate multiple arrays. |
79 | /// Concatenate multiple arrays. |
|
80 | /// </summary> |
80 | /// </summary> |
|
81 | /// <typeparam name="T">the array type</typeparam> |
81 | /// <typeparam name="T">the array type</typeparam> |
|
82 | /// <param name="arrays">multiple arrays</param> |
82 | /// <param name="arrays">multiple arrays</param> |
|
83 | /// <returns>a flat array with all arrays concatenated</returns> |
83 | /// <returns>a flat array with all arrays concatenated</returns> |
|
84 | public static T[] wasConcatenateArrays<T>(params T[][] arrays) |
84 | public static T[] ConcatenateArrays<T>(params T[][] arrays) |
|
85 | { |
85 | { |
|
86 | int resultLength = 0; |
86 | int resultLength = 0; |
|
87 | foreach (T[] o in arrays) |
87 | foreach (T[] o in arrays) |
|
88 | { |
88 | { |
|
89 | resultLength += o.Length; |
89 | resultLength += o.Length; |
|
90 | } |
90 | } |
|
91 | T[] result = new T[resultLength]; |
91 | T[] result = new T[resultLength]; |
|
92 | int offset = 0; |
92 | int offset = 0; |
|
93 | for (int x = 0; x < arrays.Length; x++) |
93 | for (int x = 0; x < arrays.Length; x++) |
|
94 | { |
94 | { |
|
95 | arrays[x].CopyTo(result, offset); |
95 | arrays[x].CopyTo(result, offset); |
|
96 | offset += arrays[x].Length; |
96 | offset += arrays[x].Length; |
|
97 | } |
97 | } |
|
98 | return result; |
98 | return result; |
|
99 | } |
99 | } |
|
100 | |
100 | |
|
101 | /////////////////////////////////////////////////////////////////////////// |
101 | /////////////////////////////////////////////////////////////////////////// |
|
102 | // Copyright (C) Wizardry and Steamworks 2014 - License: GNU GPLv3 // |
102 | // Copyright (C) Wizardry and Steamworks 2014 - License: GNU GPLv3 // |
|
103 | /////////////////////////////////////////////////////////////////////////// |
103 | /////////////////////////////////////////////////////////////////////////// |
|
104 | /// <summary> |
104 | /// <summary> |
|
105 | /// Permutes an array in reverse a given number of times. |
105 | /// Permutes an array in reverse a given number of times. |
|
106 | /// </summary> |
106 | /// </summary> |
|
107 | /// <typeparam name="T">the array type</typeparam> |
107 | /// <typeparam name="T">the array type</typeparam> |
|
108 | /// <param name="input">the array</param> |
108 | /// <param name="input">the array</param> |
|
109 | /// <param name="times">the number of times to permute</param> |
109 | /// <param name="times">the number of times to permute</param> |
|
110 | /// <returns>the array with the elements permuted</returns> |
110 | /// <returns>the array with the elements permuted</returns> |
|
111 | public static T[] wasReversePermuteArrayElements<T>(T[] input, int times) |
111 | public static T[] ReversePermuteArrayElements<T>(T[] input, int times) |
|
112 | { |
112 | { |
|
113 | if (times.Equals(0)) return input; |
113 | if (times.Equals(0)) return input; |
|
114 | T[] slice = new T[input.Length]; |
114 | T[] slice = new T[input.Length]; |
|
115 | Array.Copy(input, 1, slice, 0, input.Length - 1); |
115 | Array.Copy(input, 1, slice, 0, input.Length - 1); |
|
116 | Array.Copy(input, 0, slice, input.Length - 1, 1); |
116 | Array.Copy(input, 0, slice, input.Length - 1, 1); |
|
117 | return wasReversePermuteArrayElements(slice, --times); |
117 | return ReversePermuteArrayElements(slice, --times); |
|
118 | } |
118 | } |
|
119 | |
119 | |
|
120 | /////////////////////////////////////////////////////////////////////////// |
120 | /////////////////////////////////////////////////////////////////////////// |
|
121 | // Copyright (C) Wizardry and Steamworks 2014 - License: GNU GPLv3 // |
121 | // Copyright (C) Wizardry and Steamworks 2014 - License: GNU GPLv3 // |
|
122 | /////////////////////////////////////////////////////////////////////////// |
122 | /////////////////////////////////////////////////////////////////////////// |
|
123 | /// <summary> |
123 | /// <summary> |
|
124 | /// Permutes an array forward a given number of times. |
124 | /// Permutes an array forward a given number of times. |
|
125 | /// </summary> |
125 | /// </summary> |
|
126 | /// <typeparam name="T">the array type</typeparam> |
126 | /// <typeparam name="T">the array type</typeparam> |
|
127 | /// <param name="input">the array</param> |
127 | /// <param name="input">the array</param> |
|
128 | /// <param name="times">the number of times to permute</param> |
128 | /// <param name="times">the number of times to permute</param> |
|
129 | /// <returns>the array with the elements permuted</returns> |
129 | /// <returns>the array with the elements permuted</returns> |
|
130 | public static T[] wasForwardPermuteArrayElements<T>(T[] input, int times) |
130 | public static T[] ForwardPermuteArrayElements<T>(T[] input, int times) |
|
131 | { |
131 | { |
|
132 | if (times.Equals(0)) return input; |
132 | if (times.Equals(0)) return input; |
|
133 | T[] slice = new T[input.Length]; |
133 | T[] slice = new T[input.Length]; |
|
134 | Array.Copy(input, input.Length - 1, slice, 0, 1); |
134 | Array.Copy(input, input.Length - 1, slice, 0, 1); |
|
135 | Array.Copy(input, 0, slice, 1, input.Length - 1); |
135 | Array.Copy(input, 0, slice, 1, input.Length - 1); |
|
136 | return wasForwardPermuteArrayElements(slice, --times); |
136 | return ForwardPermuteArrayElements(slice, --times); |
|
137 | } |
137 | } |
|
138 | } |
138 | } |
|
139 | } |
139 | } |
|
140 | |
140 | |
|
141 |
|
141 |
|
|
142 | |
142 | |
|
143 | |
143 | |
|
144 | |
144 | |