Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,22 @@
uint grayEncode(in uint n) pure nothrow @nogc {
return n ^ (n >> 1);
}
uint grayDecode(uint n) pure nothrow @nogc {
auto p = n;
while (n >>= 1)
p ^= n;
return p;
}
void main() {
import std.stdio;
" N N2 enc dec2 dec".writeln;
foreach (immutable n; 0 .. 32) {
immutable g = n.grayEncode;
immutable d = g.grayDecode;
writefln("%2d: %5b => %5b => %5b: %2d", n, n, g, d, d);
assert(d == n);
}
}

View file

@ -0,0 +1,38 @@
import std.stdio, std.algorithm;
T[] gray(int N : 1, T)() pure nothrow {
return [T(0), 1];
}
/// Recursively generate gray encoding mapping table.
T[] gray(int N, T)() pure nothrow if (N <= T.sizeof * 8) {
enum T M = T(2) ^^ (N - 1);
T[] g = gray!(N - 1, T)();
foreach (immutable i; 0 .. M)
g ~= M + g[M - i - 1];
return g;
}
T[][] grayDict(int N, T)() pure nothrow {
T[][] dict = [gray!(N, T)(), [0]];
// Append inversed gray encoding mapping.
foreach (immutable i; 1 .. dict[0].length)
dict[1] ~= cast(T)countUntil(dict[0], i);
return dict;
}
enum M { Encode = 0, Decode = 1 }
T gray(int N, T)(in T n, in int mode=M.Encode) pure nothrow {
// Generated at compile time.
enum dict = grayDict!(N, T)();
return dict[mode][n];
}
void main() {
foreach (immutable i; 0 .. 32) {
immutable encoded = gray!(5)(i, M.Encode);
immutable decoded = gray!(5)(encoded, M.Decode);
writefln("%2d: %5b => %5b : %2d", i, i, encoded, decoded);
}
}

View file

@ -0,0 +1,11 @@
import std.stdio, std.algorithm, std.range;
string[] g(in uint n) pure nothrow {
return n ? g(n - 1).map!q{'0' ~ a}.array ~
g(n - 1).retro.map!q{'1' ~ a}.array
: [""];
}
void main() {
4.g.writeln;
}