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