@@ -16,72 +16,73 @@ namespace lib_interval_tree
16
16
{
17
17
namespace
18
18
{
19
- template <unsigned Size>
20
- struct NumericalPointerEquivalent
21
- {};
22
-
23
- template <>
24
- struct NumericalPointerEquivalent <sizeof (std::uint32_t )>
25
- {
26
- using type = uint32_t ;
27
- };
28
-
29
- template <>
30
- struct NumericalPointerEquivalent <sizeof (std::uint64_t )>
31
- {
32
- using type = uint64_t ;
33
- };
34
-
35
- template <typename ... List>
36
- std::string iterCaption (typename lib_interval_tree::interval_tree < List...>::const_iterator iter)
19
+ template <unsigned Size>
20
+ struct NumericalPointerEquivalent
21
+ {};
22
+
23
+ template <>
24
+ struct NumericalPointerEquivalent <sizeof (std::uint32_t )>
25
+ {
26
+ using type = uint32_t ;
27
+ };
28
+
29
+ template <>
30
+ struct NumericalPointerEquivalent <sizeof (std::uint64_t )>
31
+ {
32
+ using type = uint64_t ;
33
+ };
34
+
35
+ template <typename IntervalT, typename ... List>
36
+ std::string iterCaption (typename lib_interval_tree::interval_tree<IntervalT, List...>::const_iterator iter)
37
37
{
38
38
auto ival = *iter.node ()->interval ();
39
39
std::stringstream sstr;
40
- sstr << ' [' << ival.low () << ' ,' << ival.high () << ' ]' ;
40
+ char leftBorder = [&ival]() {
41
+ if (ival.within (ival.low ()))
42
+ return ' [' ;
43
+ else
44
+ return ' (' ;
45
+ }();
46
+ char rightBorder = [&ival]() {
47
+ if (ival.within (ival.high ()))
48
+ return ' ]' ;
49
+ else
50
+ return ' )' ;
51
+ }();
52
+ sstr << leftBorder << ival.low () << ' ,' << ival.high () << rightBorder;
41
53
return sstr.str ();
42
54
}
43
55
44
56
std::string pointerString (void const * ptr)
45
57
{
46
58
std::stringstream sstr;
47
- sstr << " 0x" << std::hex << reinterpret_cast <NumericalPointerEquivalent<sizeof (void *)>::type> (ptr);
59
+ sstr << " 0x" << std::hex << reinterpret_cast <NumericalPointerEquivalent<sizeof (void *)>::type>(ptr);
48
60
return sstr.str ();
49
61
}
50
62
51
- constexpr double margin = 5 .;
52
- constexpr double gridMargin = 5 .;
53
- constexpr double yPadding = 0 .;
54
- constexpr double xPadding = 30 .;
55
- constexpr double leftPadding = 10 .;
56
- constexpr double rightPadding = 10 .;
57
- constexpr double topPadding = 10 .;
58
- constexpr double bottomPadding = 10 .;
59
- constexpr Cairo::Pen blackPen = {3 ., Cairo::Colors::Black};
60
- constexpr Cairo::Pen iterCaptionPen = {3 ., Cairo::Colors::Black};
61
- constexpr Cairo::Pen ptrPen = {3 ., Cairo::Colors::Red};
62
- constexpr Cairo::Pen edgePen = {8 ., Cairo::Colors::Black};
63
- constexpr auto whitePen = Cairo::Colors::White;
64
-
65
- auto getiterBounds ()
63
+ constexpr double margin = 5 .;
64
+ constexpr double gridMargin = 5 .;
65
+ constexpr double yPadding = 0 .;
66
+ constexpr double xPadding = 30 .;
67
+ constexpr double leftPadding = 10 .;
68
+ constexpr double rightPadding = 10 .;
69
+ constexpr double topPadding = 10 .;
70
+ constexpr double bottomPadding = 10 .;
71
+ constexpr Cairo::Pen blackPen = {3 ., Cairo::Colors::Black};
72
+ constexpr Cairo::Pen iterCaptionPen = {3 ., Cairo::Colors::Black};
73
+ constexpr Cairo::Pen ptrPen = {3 ., Cairo::Colors::Red};
74
+ constexpr Cairo::Pen edgePen = {8 ., Cairo::Colors::Black};
75
+ constexpr auto whitePen = Cairo::Colors::White;
76
+
77
+ auto getiterBounds ()
66
78
{
67
79
Cairo::Surface dummySurface (0 , 0 );
68
80
Cairo::DrawContext dummyContext (&dummySurface);
69
81
70
- auto const captionBoundProvider = Cairo::Text (
71
- &dummyContext,
72
- 0 ,
73
- 0 ,
74
- " [00,00]" ,
75
- {" Arial" , 18 , CAIRO_FONT_WEIGHT_BOLD}
76
- );
77
-
78
- auto ptr = Cairo::Text (
79
- &dummyContext,
80
- 0 ,
81
- 0 ,
82
- pointerString (nullptr ),
83
- {" Arial" , 10 }
84
- );
82
+ auto const captionBoundProvider =
83
+ Cairo::Text (&dummyContext, 0 , 0 , " [00,00]" , {" Arial" , 18 , CAIRO_FONT_WEIGHT_BOLD});
84
+
85
+ auto ptr = Cairo::Text (&dummyContext, 0 , 0 , pointerString (nullptr ), {" Arial" , 10 });
85
86
auto ptrBounds = ptr.calculateBounds (ptrPen);
86
87
87
88
auto bounds = captionBoundProvider.calculateBounds (iterCaptionPen);
@@ -96,43 +97,42 @@ namespace lib_interval_tree
96
97
return getiterBounds ().getWidth () / 2 . + margin * 2 .;
97
98
}
98
99
}
99
- // #####################################################################################################################
100
+ // #####################################################################################################################
100
101
template <typename ... List>
101
102
struct TreeGriditer
102
103
{
103
- typename lib_interval_tree::interval_tree <List...>::const_iterator iter;
104
- std::pair <int , int > parentCoords;
104
+ typename lib_interval_tree::interval_tree<List...>::const_iterator iter;
105
+ std::pair<int , int > parentCoords;
105
106
};
106
107
template <typename ... List>
107
108
struct TreeGrid
108
109
{
109
110
// (row-major)
110
- std::vector < // rows
111
- std::vector < // columns
112
- boost::optional <TreeGriditer<List...>>
113
- >
114
- > grid;
111
+ std::vector< // rows
112
+ std::vector< // columns
113
+ boost::optional<TreeGriditer<List...>>>>
114
+ grid;
115
115
int xMax = 0 ;
116
116
int xMin = 0 ;
117
117
int yMax = 0 ;
118
118
};
119
- // #####################################################################################################################
119
+ // #####################################################################################################################
120
120
template <typename ... List>
121
- void drawIterator (Cairo::DrawContext* ctx, typename lib_interval_tree::interval_tree <List...>::const_iterator iter, double x, double y, bool drawPointers)
121
+ void drawIterator (
122
+ Cairo::DrawContext* ctx,
123
+ typename lib_interval_tree::interval_tree<List...>::const_iterator iter,
124
+ double x,
125
+ double y,
126
+ bool drawPointers
127
+ )
122
128
{
123
- auto caption = Cairo::Text (
124
- ctx,
125
- 0 ,
126
- 0 ,
127
- iterCaption<List...>(iter),
128
- {" Arial" , 18 , CAIRO_FONT_WEIGHT_BOLD}
129
- );
129
+ auto caption = Cairo::Text (ctx, 0 , 0 , iterCaption<List...>(iter), {" Arial" , 18 , CAIRO_FONT_WEIGHT_BOLD});
130
130
131
131
auto max = Cairo::Text (
132
132
ctx,
133
133
0 ,
134
134
0 ,
135
- // pointerString(iter),
135
+ // pointerString(iter),
136
136
std::to_string (iter.max ()),
137
137
{" Arial" , 12 }
138
138
);
@@ -151,7 +151,7 @@ namespace lib_interval_tree
151
151
auto iterCaptionBounds = getiterBounds ();
152
152
auto bounds = iterCaptionBounds;
153
153
auto maxBounds = max.calculateBounds (ptrPen);
154
- // auto ptrBounds = ptr.calculateBounds(ptrPen);
154
+ // auto ptrBounds = ptr.calculateBounds(ptrPen);
155
155
156
156
iterCaptionBounds.setWidth (bounds.getWidth () - 30 .);
157
157
iterCaptionBounds.setHeight (bounds.getWidth () - maxBounds.getHeight () - 5 .);
@@ -166,37 +166,36 @@ namespace lib_interval_tree
166
166
maxBounds = max.calculateBounds (ptrPen);
167
167
}
168
168
169
- Cairo::Arc circle {
170
- ctx,
171
- circleX,
172
- circleY,
173
- circleRadius
174
- };
169
+ Cairo::Arc circle{ctx, circleX, circleY, circleRadius};
175
170
switch (iter.node ()->color ())
176
171
{
177
- case (rb_color::red):
178
- circle.draw (blackPen, Cairo::Colors::Red);
179
- break ;
180
- case (rb_color::black):
181
- circle.draw (blackPen, Cairo::Colors::Black);
182
- break ;
183
- case (rb_color::fail):
184
- circle.draw (blackPen, Cairo::Colors::White);
185
- break ;
186
- case (rb_color::double_black):
187
- circle.draw (blackPen, Cairo::Colors::Gray);
188
- break ;
172
+ case (rb_color::red):
173
+ circle.draw (blackPen, Cairo::Colors::Red);
174
+ break ;
175
+ case (rb_color::black):
176
+ circle.draw (blackPen, Cairo::Colors::Black);
177
+ break ;
178
+ case (rb_color::fail):
179
+ circle.draw (blackPen, Cairo::Colors::White);
180
+ break ;
181
+ case (rb_color::double_black):
182
+ circle.draw (blackPen, Cairo::Colors::Gray);
183
+ break ;
189
184
}
190
185
191
- caption.move (circleX - actualCaptionBounds.getWidth () / 2 ., circleY - actualCaptionBounds.getHeight () / 2 . - maxBounds.getHeight ());
186
+ caption.move (
187
+ circleX - actualCaptionBounds.getWidth () / 2 .,
188
+ circleY - actualCaptionBounds.getHeight () / 2 . - maxBounds.getHeight ()
189
+ );
192
190
193
191
if (iter.node ()->color () != rb_color::black)
194
192
caption.draw (iterCaptionPen);
195
193
else
196
194
caption.draw (Cairo::Colors::White);
197
195
198
196
max.move (circleX - maxBounds.getWidth () / 2 ., circleY - maxBounds.getHeight () / 2 . + 10 .);
199
- // ptr.move(circleX - ptrBounds.getWidth() / 2., circleY - ptrBounds.getHeight() / 2. + 10. + maxBounds.getHeight() + margin);
197
+ // ptr.move(circleX - ptrBounds.getWidth() / 2., circleY - ptrBounds.getHeight() / 2. + 10. +
198
+ // maxBounds.getHeight() + margin);
200
199
201
200
if (iter.node ()->color () != rb_color::red)
202
201
max.draw (ptrPen);
@@ -213,17 +212,17 @@ namespace lib_interval_tree
213
212
}
214
213
*/
215
214
}
216
- // ---------------------------------------------------------------------------------------------------------------------
215
+ // ---------------------------------------------------------------------------------------------------------------------
217
216
template <typename ... List>
218
- TreeGrid<List...> createGrid (lib_interval_tree::interval_tree <List...> const & tree)
217
+ TreeGrid<List...> createGrid (lib_interval_tree::interval_tree<List...> const & tree)
219
218
{
220
219
auto root = tree.root ();
221
220
if (root == std::end (tree))
222
221
return {};
223
222
224
223
TreeGrid<List...> grid;
225
224
226
- using tree_const_iterator = typename lib_interval_tree::interval_tree <List...>::const_iterator;
225
+ using tree_const_iterator = typename lib_interval_tree::interval_tree<List...>::const_iterator;
227
226
228
227
struct GridPoint
229
228
{
@@ -233,9 +232,9 @@ namespace lib_interval_tree
233
232
int y;
234
233
};
235
234
236
- std::vector <GridPoint> gridPoints;
235
+ std::vector<GridPoint> gridPoints;
237
236
238
- std::function <int (tree_const_iterator iter)> subtreeSize;
237
+ std::function<int (tree_const_iterator iter)> subtreeSize;
239
238
subtreeSize = [&](tree_const_iterator iter) {
240
239
if (iter == std::end (tree))
241
240
return 0 ;
@@ -250,9 +249,8 @@ namespace lib_interval_tree
250
249
return 0 ;
251
250
};
252
251
253
- std::function <void (tree_const_iterator, int pX, int pY)> deduceCoordinates;
254
- deduceCoordinates = [&](tree_const_iterator iter, int pX, int pY)
255
- {
252
+ std::function<void (tree_const_iterator, int pX, int pY)> deduceCoordinates;
253
+ deduceCoordinates = [&](tree_const_iterator iter, int pX, int pY) {
256
254
int y = pY;
257
255
int x = pX;
258
256
if (!iter.node ()->is_root ())
@@ -298,7 +296,7 @@ namespace lib_interval_tree
298
296
299
297
for (auto & i : gridPoints)
300
298
{
301
- std::pair <int , int > parentCoords = {-1 , -1 };
299
+ std::pair<int , int > parentCoords = {-1 , -1 };
302
300
for (auto const & j : gridPoints)
303
301
{
304
302
if (j.iter == i.parent )
@@ -313,15 +311,19 @@ namespace lib_interval_tree
313
311
314
312
return grid;
315
313
}
316
- // ---------------------------------------------------------------------------------------------------------------------
314
+ // ---------------------------------------------------------------------------------------------------------------------
317
315
template <typename ... List>
318
316
void drawGrid (Cairo::DrawContext* ctx, TreeGrid<List...> const & grid, bool drawPointers, bool drawEmpty)
319
317
{
320
318
auto iterRadius = getiterRadius ();
321
319
auto cellSize = iterRadius * 2 . + gridMargin;
322
320
323
- auto iterX = [&](auto x_) {return leftPadding + iterRadius + x_ * cellSize + x_ * xPadding;};
324
- auto iterY = [&](auto y_) {return topPadding + iterRadius + y_ * cellSize + y_ * yPadding;};
321
+ auto iterX = [&](auto x_) {
322
+ return leftPadding + iterRadius + x_ * cellSize + x_ * xPadding;
323
+ };
324
+ auto iterY = [&](auto y_) {
325
+ return topPadding + iterRadius + y_ * cellSize + y_ * yPadding;
326
+ };
325
327
326
328
// Draw Lines
327
329
int y = 0 ;
@@ -332,7 +334,7 @@ namespace lib_interval_tree
332
334
{
333
335
if (cell && cell.get ().parentCoords .first != -1 )
334
336
{
335
- auto line = Cairo::Line {
337
+ auto line = Cairo::Line{
336
338
ctx,
337
339
iterX (x),
338
340
iterY (y),
@@ -360,21 +362,15 @@ namespace lib_interval_tree
360
362
}
361
363
else if (drawEmpty)
362
364
{
363
- Cairo::Arc circle {
364
- ctx,
365
- iterX (x),
366
- iterY (y),
367
- iterRadius
368
- };
365
+ Cairo::Arc circle{ctx, iterX (x), iterY (y), iterRadius};
369
366
circle.draw (blackPen, Cairo::Colors::White);
370
-
371
367
}
372
368
++x;
373
369
}
374
370
++y;
375
371
}
376
372
}
377
- // ---------------------------------------------------------------------------------------------------------------------
373
+ // ---------------------------------------------------------------------------------------------------------------------
378
374
template <typename ... List>
379
375
Cairo::Surface createSurface (TreeGrid<List...> const & grid)
380
376
{
@@ -384,19 +380,24 @@ namespace lib_interval_tree
384
380
int height = grid.yMax + 1 ;
385
381
386
382
return {
387
- static_cast <int > (leftPadding + (width) * cellSize + (width- 1 ) * xPadding + rightPadding),
388
- static_cast <int > (topPadding + (height) * cellSize + (height- 1 ) * yPadding + bottomPadding)
383
+ static_cast <int >(leftPadding + (width)* cellSize + (width - 1 ) * xPadding + rightPadding),
384
+ static_cast <int >(topPadding + (height)* cellSize + (height - 1 ) * yPadding + bottomPadding)
389
385
};
390
386
}
391
- // ---------------------------------------------------------------------------------------------------------------------
387
+ // ---------------------------------------------------------------------------------------------------------------------
392
388
template <typename ... List>
393
- void drawTree (std::string const & fileName, lib_interval_tree::interval_tree <List...> const & tree, bool drawPointers = false , bool drawEmpty = false )
389
+ void drawTree (
390
+ std::string const & fileName,
391
+ lib_interval_tree::interval_tree<List...> const & tree,
392
+ bool drawPointers = false ,
393
+ bool drawEmpty = false
394
+ )
394
395
{
395
396
auto grid = createGrid (tree);
396
397
auto surface = createSurface (grid);
397
398
Cairo::DrawContext ctx (&surface);
398
399
drawGrid<List...>(&ctx, grid, drawPointers, drawEmpty);
399
400
surface.saveToFile (fileName);
400
401
}
401
- // ######################################################################################################
402
+ // ######################################################################################################
402
403
}
0 commit comments