forked from mxgmn/MarkovJunior
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArrayHelper.cs
57 lines (50 loc) · 1.67 KB
/
ArrayHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Copyright (C) 2022 Maxim Gumin, The MIT License (MIT)
using System;
static class AH
{
public static T[][][] Array3D<T>(int MX, int MY, int MZ, T value)
{
T[][][] result = new T[MX][][];
for (int x = 0; x < result.Length; x++)
{
result[x] = new T[MY][];
T[][] resultx = result[x];
for (int y = 0; y < resultx.Length; y++)
{
resultx[y] = new T[MZ];
resultx[y].AsSpan().Fill(value);
}
}
return result;
}
public static T[][] Array2D<T>(int MX, int MY, T value)
{
T[][] result = new T[MX][];
for (int x = 0; x < result.Length; x++)
{
result[x] = new T[MY];
result[x].AsSpan().Fill(value);
}
return result;
}
public static T[] Array1D<T>(int length, Func<int, T> f)
{
T[] result = new T[length];
for (int i = 0; i < result.Length; i++) result[i] = f(i);
return result;
}
public static T[] Array1D<T>(int length, T value)
{
T[] result = new T[length];
result.AsSpan().Fill(value);
return result;
}
public static T[] FlatArray3D<T>(int MX, int MY, int MZ, Func<int, int, int, T> f)
{
T[] result = new T[MX * MY * MZ];
for (int z = 0; z < MZ; z++) for (int y = 0; y < MY; y++) for (int x = 0; x < MX; x++) result[z * MX * MY + y * MX + x] = f(x, y, z);
return result;
}
public static void Set2D<T>(this T[][] a, T value) { for (int y = 0; y < a.Length; y++) a[y].AsSpan().Fill(value); }
public static bool Same(byte[] t1, byte[] t2) => t1.AsSpan().SequenceEqual(t2);
}