bijectFromUnsigned

Biject (Shift) an unsigned a "back down" to the corresponding signed type (for instance after radix sorting an array of a).

Examples

check that bijectToUnsigned is the opposite of bijectFromUnsigned

foreach (T; AliasSeq!(ubyte, ushort, uint, ulong))
{
    static assert(is(typeof(T.init.bijectToUnsigned) == T));
}
foreach (T; AliasSeq!(byte, short, int, long))
{
    static assert(is(typeof(T.init.bijectToUnsigned) == Unsigned!T));
}

static assert(is(typeof(char.init.bijectToUnsigned) == ubyte));
static assert(is(typeof(wchar.init.bijectToUnsigned) == ushort));
static assert(is(typeof(dchar.init.bijectToUnsigned) == uint));

const n = 1_000_000;
foreach (const i; 0 .. n)
{
    foreach (T; AliasSeq!(bool,
                          ubyte, ushort, uint, ulong,
                          byte, short, int, long,
                          char, wchar, dchar,
                          float, double))
    {
        const T x = cast(T)i;
        auto y = x.bijectToUnsigned;
        // pragma(msg, "T:", T);
        // pragma(msg, "typeof(x):", typeof(x));
        // pragma(msg, "typeof(y):", typeof(y));
        T z; y.bijectFromUnsigned(z);
        assert(x == z);
    }
}

Meta