Open
Graph Drawing
Framework

 v. snapshot-2017-02-16
 

utils.h
Go to the documentation of this file.
1 
29 #pragma once
30 
31 #include <cstddef>
32 
33 namespace ogdf {
34 namespace energybased {
35 namespace dtree {
36 
37 template<typename IntType, int Dim>
38 inline bool mortonComparerEqual(const IntType a[Dim], const IntType b[Dim])
39 {
40  // loop over the dimension
41  for (int d = Dim - 1; d >= 0; d--) {
42  // if the block is different
43  if (a[d] != b[d]) {
44  return false;
45  }
46  }
47  return true;
48 }
49 
50 // special tuned version for unsigned int and dim = 1
51 template<>
52 inline bool mortonComparerEqual<unsigned int, 1>(const unsigned int a[1], const unsigned int b[1])
53 {
54  return a[0] == b[0];
55 }
56 
57 // special tuned version for unsigned int and dim = 2
58 template<>
59 inline bool mortonComparerEqual<unsigned int, 2>(const unsigned int a[2], const unsigned int b[2])
60 {
61  return (a[0] == b[0]) && (a[1] == b[1]);
62 }
63 
64 template<typename IntType, int Dim>
65 inline bool mortonComparerLess(const IntType a[Dim], const IntType b[Dim])
66 {
67  // loop over the dimension
68  for (int d = Dim - 1; d >= 0; d--) {
69  // if the block is different
70  if (a[d] != b[d]) {
71  return a[d] < b[d];
72  }
73  }
74  return false;
75 }
76 
77 // special tuned version for unsigned int and dim = 1
78 template<>
79 inline bool mortonComparerLess<unsigned int, 1>(const unsigned int a[1], const unsigned int b[1])
80 {
81  return a[0] < b[0];
82 }
83 
84 // special tuned version for unsigned int and dim = 2
85 template<>
86 inline bool mortonComparerLess<unsigned int, 2>(const unsigned int a[2], const unsigned int b[2])
87 {
88  return (a[1] == b[1]) ? a[0] < b[0] : a[1] < b[1];
89 }
90 
91 template<typename IntType, int Dim>
92 inline void interleaveBits(const IntType coords[Dim],
93  IntType mnr[Dim])
94 {
95  // number of bits of the grid coord type
96  const int BitLength = sizeof(IntType) << 3;
97 
98  // loop over the dimension
99  for (int d = 0; d < Dim; d++) {
100  // reset the mnr
101  mnr[d] = 0x0;
102  }
103 
104  // counter for the res bit
105  int k = 0;
106 
107  // now loop over all bits
108  for (int i = 0; i < BitLength; ++i) {
109  // loop over the dimension
110  for (int d = 0; d < Dim; d++) {
111  // set the k-th bit in the correct block at the k % position
112  mnr[k / BitLength] |= ((coords[d] >> i) & 0x1) << ( k % BitLength);
113 
114  // stupid increment
115  k++;
116  }
117  };
118 }
119 
120 template<>
121 inline void interleaveBits<unsigned int, 1>(const unsigned int coords[1],
122  unsigned int mnr[1])
123 {
124  mnr[0] = coords[0];
125 }
126 
127 template<>
128 inline void interleaveBits<unsigned int, 2>(const unsigned int coords[2],
129  unsigned int mnr[2])
130 {
131  // half the bit length = #bytes * 4
132  const size_t HalfBitLength = sizeof(unsigned int) << 2;
133 
134  // reset the mnr
135  mnr[0] = 0x0;
136  mnr[1] = 0x0;
137 
138  // this variable is used to generate an alternating pattern for the
139  // lower half bits of both coords (the higher half will be shifted out later)
140  unsigned int x_lo[2] = { coords[0],
141  coords[1] };
142 
143  // this one is used for the higher half, thus, we shift them to right
144  unsigned int x_hi[2] = { coords[0] >> HalfBitLength,
145  coords[1] >> HalfBitLength };
146 
147  // a mask full of 1's
148  unsigned int mask = ~0x0;
149 
150  for (unsigned int i = (HalfBitLength);i > 0; i = i >> 1) {
151  // increase frequency
152  // generates step by step: ..., 11110000, 11001100, 10101010
153  mask = mask ^ (mask << i);
154 
155  // create an alternating 0x0x0x0x0x pattern for the lower bits
156  x_lo[0] = (x_lo[0] | (x_lo[0] << i)) & mask;
157  x_lo[1] = (x_lo[1] | (x_lo[1] << i)) & mask;
158  // and for the higher bits too
159  x_hi[0] = (x_hi[0] | (x_hi[0] << i)) & mask;
160  x_hi[1] = (x_hi[1] | (x_hi[1] << i)) & mask;
161  };
162 
163  // save the lower bits alternating in the first block
164  mnr[0] = x_lo[0] | (x_lo[1] << 1);
165 
166  // the higher go into the second block
167  mnr[1] = x_hi[0] | (x_hi[1] << 1);
168 }
169 
170 
171 template<typename IntType>
172 inline int mostSignificantBit(IntType x)
173 {
174  // number of bits of the int type
175  const size_t BitLength = sizeof(IntType) << 3;
176 
177  // the index 0... BitLength - 1 of the msb
178  int result = 0;
179 
180  // binary search on the bits of x
181  for (unsigned int i = (BitLength >> 1); i > 0; i = i >> 1) {
182  // check if anything left of i - 1 is set
183  if ( x >> i) {
184  // it is msb must be in that half
185  x = x >> i;
186 
187  // msb > i
188  result += i;
189  }
190  }
191 
192  // return the result
193  return result;
194 }
195 
196 template<typename IntType, int Dim>
197 int lowestCommonAncestorLevel(const IntType a[Dim], const IntType b[Dim])
198 {
199  // number of bits of the grid coord type
200  const size_t BitLength = sizeof(IntType) << 3;
201 
202  // loop over the dimension
203  for (int d = Dim - 1; d >= 0; d--) {
204  // if the block is different
205  if (a[d] != b[d]) {
206  int msb = (mostSignificantBit<IntType>(a[d] ^ b[d]) + (d * BitLength));
207 
208  // the lowest common ancestor level is msb / num coords
209  return msb / Dim;
210  }
211  }
212 
213  return 0;
214 }
215 
216 template<>
217 int lowestCommonAncestorLevel<unsigned int, 1>(const unsigned int a[1], const unsigned int b[1])
218 {
219  return mostSignificantBit<unsigned int>(a[0] ^ b[0]);
220 }
221 
222 }}}
void interleaveBits(const IntType coords[Dim], IntType mnr[Dim])
Definition: utils.h:92
bool mortonComparerLess< unsigned int, 2 >(const unsigned int a[2], const unsigned int b[2])
Definition: utils.h:86
int lowestCommonAncestorLevel(const IntType a[Dim], const IntType b[Dim])
Definition: utils.h:197
void interleaveBits< unsigned int, 2 >(const unsigned int coords[2], unsigned int mnr[2])
Definition: utils.h:128
int mostSignificantBit(IntType x)
Definition: utils.h:172
bool mortonComparerEqual< unsigned int, 2 >(const unsigned int a[2], const unsigned int b[2])
Definition: utils.h:59
bool mortonComparerEqual< unsigned int, 1 >(const unsigned int a[1], const unsigned int b[1])
Definition: utils.h:52
int lowestCommonAncestorLevel< unsigned int, 1 >(const unsigned int a[1], const unsigned int b[1])
Definition: utils.h:217
bool mortonComparerLess< unsigned int, 1 >(const unsigned int a[1], const unsigned int b[1])
Definition: utils.h:79
bool mortonComparerLess(const IntType a[Dim], const IntType b[Dim])
Definition: utils.h:65
The namespace for all OGDF objects.
bool mortonComparerEqual(const IntType a[Dim], const IntType b[Dim])
Definition: utils.h:38
void interleaveBits< unsigned int, 1 >(const unsigned int coords[1], unsigned int mnr[1])
Definition: utils.h:121