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

1 foreach (T; AliasSeq!(ubyte, ushort, uint, ulong))
2 {
3     static assert(is(typeof(T.init.bijectToUnsigned) == T));
4 }
5 foreach (T; AliasSeq!(byte, short, int, long))
6 {
7     static assert(is(typeof(T.init.bijectToUnsigned) == Unsigned!T));
8 }
9 
10 static assert(is(typeof(char.init.bijectToUnsigned) == ubyte));
11 static assert(is(typeof(wchar.init.bijectToUnsigned) == ushort));
12 static assert(is(typeof(dchar.init.bijectToUnsigned) == uint));
13 
14 const n = 1_000_000;
15 foreach (const i; 0 .. n)
16 {
17     foreach (T; AliasSeq!(bool,
18                           ubyte, ushort, uint, ulong,
19                           byte, short, int, long,
20                           char, wchar, dchar,
21                           float, double))
22     {
23         const T x = cast(T)i;
24         auto y = x.bijectToUnsigned;
25         // pragma(msg, "T:", T);
26         // pragma(msg, "typeof(x):", typeof(x));
27         // pragma(msg, "typeof(y):", typeof(y));
28         T z; y.bijectFromUnsigned(z);
29         assert(x == z);
30     }
31 }

Meta