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