wasSharp – Blame information for rev
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | zed | 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 | { |
||
7 | office | 11 | public static class Arrays |
1 | zed | 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> |
||
5 | eva | 23 | public static T GetElementAt<T>(T[] data, int index) |
1 | zed | 24 | { |
27 | office | 25 | return index < 0 ? data[(index % data.Length + data.Length) % data.Length] : data[index % data.Length]; |
1 | zed | 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> |
||
5 | eva | 39 | public static T[] GetSubArray<T>(T[] data, int start, int stop) |
1 | zed | 40 | { |
41 | if (stop.Equals(-1)) |
||
42 | stop = data.Length - 1; |
||
7 | office | 43 | var result = new T[stop - start + 1]; |
1 | zed | 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> |
||
5 | eva | 59 | public static T[] DeleteSubArray<T>(T[] data, int start, int stop) |
1 | zed | 60 | { |
61 | if (stop.Equals(-1)) |
||
62 | stop = data.Length - 1; |
||
7 | office | 63 | var result = new T[data.Length - (stop - start) - 1]; |
1 | zed | 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> |
||
5 | eva | 78 | public static T[] ConcatenateArrays<T>(params T[][] arrays) |
1 | zed | 79 | { |
7 | office | 80 | var resultLength = 0; |
81 | foreach (var o in arrays) |
||
1 | zed | 82 | { |
83 | resultLength += o.Length; |
||
84 | } |
||
7 | office | 85 | var result = new T[resultLength]; |
86 | var offset = 0; |
||
87 | for (var x = 0; x < arrays.Length; x++) |
||
1 | zed | 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> |
||
5 | eva | 105 | public static T[] ReversePermuteArrayElements<T>(T[] input, int times) |
1 | zed | 106 | { |
107 | if (times.Equals(0)) return input; |
||
7 | office | 108 | var slice = new T[input.Length]; |
1 | zed | 109 | Array.Copy(input, 1, slice, 0, input.Length - 1); |
110 | Array.Copy(input, 0, slice, input.Length - 1, 1); |
||
5 | eva | 111 | return ReversePermuteArrayElements(slice, --times); |
1 | zed | 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> |
||
5 | eva | 124 | public static T[] ForwardPermuteArrayElements<T>(T[] input, int times) |
1 | zed | 125 | { |
126 | if (times.Equals(0)) return input; |
||
7 | office | 127 | var slice = new T[input.Length]; |
1 | zed | 128 | Array.Copy(input, input.Length - 1, slice, 0, 1); |
129 | Array.Copy(input, 0, slice, 1, input.Length - 1); |
||
5 | eva | 130 | return ForwardPermuteArrayElements(slice, --times); |
1 | zed | 131 | } |
132 | } |
||
27 | office | 133 | } |