Open
Graph Drawing
Framework

 v. 2020.02 (Catalpa)
 

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