1 
2 #ifndef HILBERT_HPP
3 #define HILBERT_HPP
4 
5 namespace hilbert {
rot(const int n,int & x,int & y,const int rx,const int ry)6   void rot(const int n, int &x, int &y, const int rx, const int ry) {
7     int t(0);
8     if (ry == 0) {
9       if (rx == 1) {
10         x = n - 1 - x;
11         y = n - 1 - y;
12       }
13       t = x;
14       x = y;
15       y = t;
16     }
17   }
18 
d2xy(const int m,const int d,int & x,int & y)19   void d2xy(const int m, const int d, int &x, int &y) {
20     int n(0), rx(0), ry(0), t(d);
21     n = std::pow(2,m);
22     x = 0; y = 0;
23     for (int s = 1; s < n; s *= 2) {
24       rx = (1 & (t/2));
25       ry = (1 & (t^rx));
26       rot(s,x,y,rx,ry);
27       x += s*rx;
28       y += s*ry;
29       t /= 4;
30     }
31   }
32 
xy2d(const int m,int x,int y,int & d)33   void xy2d(const int m, int x, int y, int &d) {
34     int n(0), rx(0), ry(0);
35     d = 0;
36     n = std::pow(2,m);
37     for (int s = n/2; s > 0; s /= 2) {
38       rx = (x&s) > 0;
39       ry = (y&s) > 0;
40       d += s*s*((3*rx)^ry);
41       rot(s,x,y,rx,ry);
42     }
43   }
44 }
45 
46 #endif
47