/
graph.c
4338 lines (3933 loc) · 107 KB
/
graph.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright radare2 - 2014-2018 - pancake, ret2libc */
#include <r_core.h>
#include <r_cons.h>
#include <ctype.h>
#include <limits.h>
static int mousemode = 0;
static int disMode = 0;
static int discroll = 0;
static const char *mousemodes[] = {
"canvas-y",
"canvas-x",
"node-y",
"node-x",
NULL
};
#define GRAPH_MERGE_FEATURE 0
#define BORDER 3
#define BORDER_WIDTH 4
#define BORDER_HEIGHT 3
#define MARGIN_TEXT_X 2
#define MARGIN_TEXT_Y 2
#define HORIZONTAL_NODE_SPACING 4
#define VERTICAL_NODE_SPACING 2
#define MIN_NODE_WIDTH 22
#define MIN_NODE_HEIGHT BORDER_HEIGHT
#define TITLE_LEN 128
#define DEFAULT_SPEED 1
#define PAGEKEY_SPEED (h / 2)
/* 15 */
#define MINIGRAPH_NODE_TEXT_CUR "<@@@@@@>"
#define MINIGRAPH_NODE_MIN_WIDTH 8
#define MINIGRAPH_NODE_TITLE_LEN 4
#define MINIGRAPH_NODE_CENTER_X 3
#define MININODE_MIN_WIDTH 16
#define ZOOM_STEP 10
#define ZOOM_DEFAULT 100
#define BODY_OFFSETS 0x1
#define BODY_SUMMARY 0x2
#define NORMALIZE_MOV(x) ((x) < 0 ? -1 : ((x) > 0 ? 1 : 0))
#define hash_set(sdb, k, v) (sdb_num_set (sdb, sdb_fmt ("%"PFMT64u, (ut64) (size_t) k), (ut64) (size_t) v, 0))
#define hash_get(sdb, k) (sdb_num_get (sdb, sdb_fmt ("%"PFMT64u, (ut64) (size_t) k), NULL))
#define hash_get_rnode(sdb, k) ((RGraphNode *) (size_t) hash_get (sdb, k))
#define hash_get_rlist(sdb, k) ((RList *) (size_t) hash_get (sdb, k))
#define hash_get_int(sdb, k) ((int) hash_get (sdb, k))
/* dont use macros for this */
#define get_anode(gn) (gn? (RANode *) gn->data: NULL)
#define graph_foreach_anode(list, it, pos, anode)\
if (list) for (it = list->head; it && (pos = it->data) && (pos) && (anode = (RANode *) pos->data); it = it->n)
struct len_pos_t {
int len;
int pos;
};
struct dist_t {
const RGraphNode *from;
const RGraphNode *to;
int dist;
};
struct g_cb {
RAGraph *graph;
RANodeCallback node_cb;
RAEdgeCallback edge_cb;
void *data;
};
typedef struct ascii_edge_t {
RANode *from;
RANode *to;
RList *x, *y;
int is_reversed;
} AEdge;
struct layer_t {
int n_nodes;
RGraphNode **nodes;
int position;
int height;
int width;
int gap;
};
struct agraph_refresh_data {
RCore *core;
RAGraph *g;
RAnalFunction **fcn;
int fs;
};
#define G(x, y) r_cons_canvas_gotoxy (g->can, x, y)
#define W(x) r_cons_canvas_write (g->can, x)
#define F(x, y, x2, y2, c) r_cons_canvas_fill (g->can, x, y, x2, y2, c, 0)
static bool is_offset(const RAGraph *g) {
return g->mode == R_AGRAPH_MODE_OFFSET;
}
static bool is_mini(const RAGraph *g) {
return g->mode == R_AGRAPH_MODE_MINI;
}
static bool is_tiny(const RAGraph *g) {
return g->is_tiny || g->mode == R_AGRAPH_MODE_TINY;
}
static bool is_summary(const RAGraph *g) {
return g->mode == R_AGRAPH_MODE_SUMMARY;
}
static int next_mode(int mode) {
return (mode + 1) % R_AGRAPH_MODE_MAX;
}
static int prev_mode(int mode) {
return (mode + R_AGRAPH_MODE_MAX - 1) % R_AGRAPH_MODE_MAX;
}
static const char *mode2str(const RAGraph *g, const char *prefix) {
static char m[20];
const char *submode;
if (is_tiny (g)) {
submode = "TINY";
} else if (is_mini (g)) {
submode = "MINI";
} else if (is_offset (g)) {
submode = "OFF";
} else if (is_summary (g)) {
submode = "SUMM";
} else {
submode = "NORM";
}
snprintf (m, sizeof (m), "%s-%s", prefix, submode);
return m;
}
static int mode2opts(const RAGraph *g) {
int opts = 0;
if (is_offset (g)) {
opts |= BODY_OFFSETS;
}
if (is_summary (g)) {
opts |= BODY_SUMMARY;
}
return opts;
}
// duplicated from visual.c
static void rotateAsmemu(RCore *core) {
const bool isEmuStr = r_config_get_i (core->config, "emu.str");
const bool isEmu = r_config_get_i (core->config, "asm.emu");
if (isEmu) {
if (isEmuStr) {
r_config_set (core->config, "emu.str", "false");
} else {
r_config_set (core->config, "asm.emu", "false");
}
} else {
r_config_set (core->config, "emu.str", "true");
}
}
static void showcursor(RCore *core, int x) {
if (!x) {
int wheel = r_config_get_i (core->config, "scr.wheel");
if (wheel) {
r_cons_enable_mouse (true);
}
} else {
r_cons_enable_mouse (false);
}
r_cons_show_cursor (x);
}
static char *get_title(ut64 addr) {
return r_str_newf ("0x%"PFMT64x, addr);
}
static int agraph_refresh(struct agraph_refresh_data *grd);
static void update_node_dimension(const RGraph *g, int is_mini, int zoom) {
const RList *nodes = r_graph_get_nodes (g);
RGraphNode *gn;
RListIter *it;
RANode *n;
graph_foreach_anode (nodes, it, gn, n) {
if (is_mini) {
n->h = 1;
n->w = MINIGRAPH_NODE_MIN_WIDTH;
} else if (n->is_mini) {
n->h = 1;
n->w = MININODE_MIN_WIDTH;
} else {
n->w = r_str_bounds (n->body, (int *) &n->h);
ut32 len = strlen (n->title) + MARGIN_TEXT_X;
if (len > INT_MAX) {
len = INT_MAX;
}
if (len > n->w) {
n->w = len;
}
// n->w = n->w; //R_MIN (n->w, (int)len);
n->w += BORDER_WIDTH;
n->h += BORDER_HEIGHT;
/* scale node by zoom */
n->w = R_MAX (MIN_NODE_WIDTH, (n->w * zoom) / 100);
n->h = R_MAX (MIN_NODE_HEIGHT, (n->h * zoom) / 100);
}
}
}
static void mini_RANode_print(const RAGraph *g, const RANode *n, int cur, bool details) {
char title[TITLE_LEN];
int x, delta_x = 0;
if (!G (n->x + MINIGRAPH_NODE_CENTER_X, n->y) &&
!G (n->x + MINIGRAPH_NODE_CENTER_X + n->w, n->y)) {
return;
}
x = n->x + MINIGRAPH_NODE_CENTER_X + g->can->sx;
if (x < 0) {
delta_x = -x;
}
if (!G (n->x + MINIGRAPH_NODE_CENTER_X + delta_x, n->y)) {
return;
}
if (details) {
if (cur) {
W (&MINIGRAPH_NODE_TEXT_CUR[delta_x]);
(void) G (-g->can->sx, -g->can->sy + 2);
snprintf (title, sizeof (title) - 1,
"[ %s ]", n->title);
W (title);
if (discroll > 0) {
char *body = r_str_ansi_crop (n->body, 0, discroll, -1, -1);
(void) G (-g->can->sx, -g->can->sy + 3);
W (body);
free (body);
} else {
(void) G (-g->can->sx, -g->can->sy + 3);
W (n->body);
}
} else {
char *str = "____";
if (n->title) {
int l = strlen (n->title);
str = n->title;
if (l > MINIGRAPH_NODE_TITLE_LEN) {
str += l - MINIGRAPH_NODE_TITLE_LEN;
}
}
snprintf (title, sizeof (title) - 1, "__%s__", str);
W (title + delta_x);
}
} else {
snprintf (title, sizeof (title) - 1,
cur? "[ %s ]": " %s ", n->title);
W (title);
}
return;
}
static void tiny_RANode_print(const RAGraph *g, const RANode *n, int cur) {
G (n->x, n->y);
if (cur) {
W ("##");
} else {
W ("()");
}
}
static void normal_RANode_print(const RAGraph *g, const RANode *n, int cur) {
ut32 center_x = 0, center_y = 0;
ut32 delta_x = 0, delta_txt_x = 0;
ut32 delta_y = 0, delta_txt_y = 0;
char title[TITLE_LEN];
char *body;
int x, y;
char *shortcut;
x = n->x + g->can->sx;
y = n->y + g->can->sy;
if (x + MARGIN_TEXT_X < 0) {
delta_x = -(x + MARGIN_TEXT_X);
}
if (x + n->w < -MARGIN_TEXT_X) {
return;
}
if (y < -1) {
delta_y = R_MIN (n->h - BORDER_HEIGHT - 1, -y - MARGIN_TEXT_Y);
}
shortcut = sdb_get (g->db, sdb_fmt ("agraph.nodes.%s.shortcut", n->title), 0);
/* print the title */
if (cur) {
snprintf (title, sizeof (title) - 1, "[%s]", n->title);
} else {
snprintf (title, sizeof (title) - 1, " %s", n->title);
}
if (shortcut) {
strncat (title, sdb_fmt (" ;[g%s]", shortcut), sizeof (title) - strlen (title) - 1);
free (shortcut);
}
if ((delta_x < strlen (title)) && G (n->x + MARGIN_TEXT_X + delta_x, n->y + 1)) {
W (title + delta_x);
}
/* print the body */
if (g->zoom > ZOOM_DEFAULT) {
center_x = (g->zoom - ZOOM_DEFAULT) / 10;
center_y = (g->zoom - ZOOM_DEFAULT) / 30;
delta_txt_x = R_MIN (delta_x, center_x);
delta_txt_y = R_MIN (delta_y, center_y);
}
if (G (n->x + MARGIN_TEXT_X + delta_x + center_x - delta_txt_x,
n->y + MARGIN_TEXT_Y + delta_y + center_y - delta_txt_y)) {
ut32 body_x = center_x >= delta_x? 0: delta_x - center_x;
ut32 body_y = center_y >= delta_y? 0: delta_y - center_y;
ut32 body_h = BORDER_HEIGHT >= n->h? 1: n->h - BORDER_HEIGHT;
if (g->zoom < ZOOM_DEFAULT) {
body_h--;
}
if (body_y + 1 <= body_h) {
body = r_str_ansi_crop (n->body,
body_x, body_y,
n->w - BORDER_WIDTH,
body_h);
if (body) {
W (body);
if (g->zoom < ZOOM_DEFAULT) {
W ("\n");
}
free (body);
} else {
W (n->body);
}
}
/* print some dots when the body is cropped because of zoom */
if (body_y <= body_h && g->zoom < ZOOM_DEFAULT) {
char *dots = "...";
if (delta_x < strlen (dots)) {
dots += delta_x;
W (dots);
}
}
}
// TODO: check if node is traced or not and show proper color
// This info must be stored inside RANode* from RCore*
RCons *cons = r_cons_singleton ();
if (cur) {
r_cons_canvas_box (g->can, n->x, n->y, n->w, n->h, cons->pal.graph_box2);
} else {
r_cons_canvas_box (g->can, n->x, n->y, n->w, n->h, cons->pal.graph_box);
}
}
static int **get_crossing_matrix(const RGraph *g,
const struct layer_t layers[],
int maxlayer, int i, int from_up,
int *n_rows) {
int j, **m, len = layers[i].n_nodes;
m = R_NEWS0 (int *, len);
if (!m) {
return NULL;
}
for (j = 0; j < len; j++) {
m[j] = R_NEWS0 (int, len);
if (!m[j]) {
goto err_row;
}
}
/* calculate crossings between layer i and layer i-1 */
/* consider the crossings generated by each pair of edges */
if (i > 0 && from_up) {
for (j = 0; j < layers[i - 1].n_nodes; j++) {
const RGraphNode *gj = layers[i - 1].nodes[j];
const RList *neigh = r_graph_get_neighbours (g, gj);
RGraphNode *gk;
RListIter *itk;
r_list_foreach (neigh, itk, gk) {
int s;
// skip self-loop
if (gj == gk) {
continue;
}
for (s = 0; s < j; ++s) {
const RGraphNode *gs = layers[i - 1].nodes[s];
const RList *neigh_s = r_graph_get_neighbours (g, gs);
RGraphNode *gt;
RListIter *itt;
r_list_foreach (neigh_s, itt, gt) {
const RANode *ak, *at; /* k and t should be "indexes" on layer i */
if (gt == gk || gt == gs) {
continue;
}
ak = get_anode (gk);
at = get_anode (gt);
if (ak->layer != i || at->layer != i) {
// this should never happen
// but it happens if we do graph.dummy = false, so better hide it for now
#if 0
eprintf ("(WARNING) \"%s\" (%d) or \"%s\" (%d) are not on the right layer (%d)\n",
ak->title, ak->layer,
at->title, at->layer,
i);
#endif
continue;
}
m[ak->pos_in_layer][at->pos_in_layer]++;
}
}
}
}
}
/* calculate crossings between layer i and layer i+1 */
if (i < maxlayer - 1 && !from_up) {
for (j = 0; j < layers[i].n_nodes; ++j) {
const RGraphNode *gj = layers[i].nodes[j];
const RList *neigh = r_graph_get_neighbours (g, gj);
const RANode *ak, *aj = get_anode (gj);
RGraphNode *gk;
RListIter *itk;
graph_foreach_anode (neigh, itk, gk, ak) {
int s;
for (s = 0; s < layers[i].n_nodes; ++s) {
const RGraphNode *gs = layers[i].nodes[s];
const RList *neigh_s;
RGraphNode *gt;
RListIter *itt;
const RANode *at, *as = get_anode (gs);
if (gs == gj) {
continue;
}
neigh_s = r_graph_get_neighbours (g, gs);
graph_foreach_anode (neigh_s, itt, gt, at) {
if (at->pos_in_layer < ak->pos_in_layer) {
m[aj->pos_in_layer][as->pos_in_layer]++;
}
}
}
}
}
}
if (n_rows) {
*n_rows = len;
}
return m;
err_row:
for (i = 0; i < len; i++) {
free (m[i]);
}
free (m);
return NULL;
}
static int layer_sweep(const RGraph *g, const struct layer_t layers[],
int maxlayer, int i, int from_up) {
int **cross_matrix;
RGraphNode *u, *v;
const RANode *au, *av;
int n_rows, j, changed = false;
int len = layers[i].n_nodes;
cross_matrix = get_crossing_matrix (g, layers, maxlayer, i, from_up, &n_rows);
if (!cross_matrix) {
return false;
}
for (j = 0; j < len - 1; ++j) {
int auidx, avidx;
u = layers[i].nodes[j];
v = layers[i].nodes[j + 1];
au = get_anode (u);
av = get_anode (v);
auidx = au->pos_in_layer;
avidx = av->pos_in_layer;
if (cross_matrix[auidx][avidx] > cross_matrix[avidx][auidx]) {
/* swap elements */
layers[i].nodes[j] = v;
layers[i].nodes[j + 1] = u;
changed = true;
}
}
/* update position in the layer of each node. During the swap of some
* elements we didn't swap also the pos_in_layer because the cross_matrix
* is indexed by it, so do it now! */
for (j = 0; j < layers[i].n_nodes; ++j) {
RANode *n = get_anode (layers[i].nodes[j]);
n->pos_in_layer = j;
}
for (j = 0; j < n_rows; ++j) {
free (cross_matrix[j]);
}
free (cross_matrix);
return changed;
}
static void view_cyclic_edge(const RGraphEdge *e, const RGraphVisitor *vis) {
const RAGraph *g = (RAGraph *) vis->data;
RGraphEdge *new_e = R_NEW0 (RGraphEdge);
if (!new_e) {
return;
}
new_e->from = e->from;
new_e->to = e->to;
new_e->nth = e->nth;
r_list_append (g->back_edges, new_e);
}
static void view_dummy(const RGraphEdge *e, const RGraphVisitor *vis) {
const RANode *a = get_anode (e->from);
const RANode *b = get_anode (e->to);
RList *long_edges = (RList *) vis->data;
if (!a || !b) {
return;
}
if (R_ABS (a->layer - b->layer) > 1) {
RGraphEdge *new_e = R_NEW0 (RGraphEdge);
if (!new_e) {
return;
}
new_e->from = e->from;
new_e->to = e->to;
new_e->nth = e->nth;
r_list_append (long_edges, new_e);
}
}
/* find a set of edges that, removed, makes the graph acyclic */
/* invert the edges identified in the previous step */
static void remove_cycles(RAGraph *g) {
RGraphVisitor cyclic_vis = {
NULL, NULL, NULL, NULL, NULL, NULL
};
const RGraphEdge *e;
const RListIter *it;
g->back_edges = r_list_new ();
cyclic_vis.back_edge = (RGraphEdgeCallback) view_cyclic_edge;
cyclic_vis.data = g;
r_graph_dfs (g->graph, &cyclic_vis);
r_list_foreach (g->back_edges, it, e) {
RANode *from, *to;
from = e->from? get_anode (e->from): NULL;
to = e->to? get_anode (e->to): NULL;
r_agraph_del_edge (g, from, to);
r_agraph_add_edge_at (g, to, from, e->nth);
}
}
static void add_sorted(RGraphNode *n, RGraphVisitor *vis) {
RList *l = (RList *) vis->data;
r_list_prepend (l, n);
}
/* assign a layer to each node of the graph.
*
* It visits the nodes of the graph in the topological sort, so that every time
* you visit a node, you can be sure that you have already visited all nodes
* that can lead to that node and thus you can easily compute the layer based
* on the layer of these "parent" nodes. */
static void assign_layers(const RAGraph *g) {
RGraphVisitor layer_vis = {
NULL, NULL, NULL, NULL, NULL, NULL
};
const RGraphNode *gn;
const RListIter *it;
RANode *n;
RList *topological_sort = r_list_new ();
layer_vis.data = topological_sort;
layer_vis.finish_node = (RGraphNodeCallback) add_sorted;
r_graph_dfs (g->graph, &layer_vis);
graph_foreach_anode (topological_sort, it, gn, n) {
const RList *innodes = r_graph_innodes (g->graph, gn);
RListIter *it;
RGraphNode *prev;
RANode *preva;
n->layer = 0;
graph_foreach_anode (innodes, it, prev, preva) {
if (preva->layer + 1 > n->layer) {
n->layer = preva->layer + 1;
}
}
}
r_list_free (topological_sort);
}
static int find_edge(const RGraphEdge *a, const RGraphEdge *b) {
return a->from == b->to && a->to == b->from? 0: 1;
}
static int is_reversed(const RAGraph *g, const RGraphEdge *e) {
return r_list_find (g->back_edges, e, (RListComparator) find_edge)? true: false;
}
/* add dummy nodes when there are edges that span multiple layers */
static void create_dummy_nodes(RAGraph *g) {
RGraphVisitor dummy_vis = {
NULL, NULL, NULL, NULL, NULL, NULL
};
const RListIter *it;
const RGraphEdge *e;
g->long_edges = r_list_newf ((RListFree)free);
dummy_vis.data = g->long_edges;
dummy_vis.tree_edge = (RGraphEdgeCallback) view_dummy;
dummy_vis.fcross_edge = (RGraphEdgeCallback) view_dummy;
r_graph_dfs (g->graph, &dummy_vis);
r_list_foreach (g->long_edges, it, e) {
RANode *from = get_anode (e->from);
RANode *to = get_anode (e->to);
int diff_layer = R_ABS (from->layer - to->layer);
RANode *prev = get_anode (e->from);
int i, nth = e->nth;
r_agraph_del_edge (g, from, to);
for (i = 1; i < diff_layer; ++i) {
RANode *dummy = r_agraph_add_node (g, NULL, NULL);
if (!dummy) {
return;
}
dummy->is_dummy = true;
dummy->layer = from->layer + i;
dummy->is_reversed = is_reversed (g, e);
dummy->w = 1;
r_agraph_add_edge_at (g, prev, dummy, nth);
prev = dummy;
nth = -1;
}
r_graph_add_edge (g->graph, prev->gnode, e->to);
}
}
/* create layers and assign an initial ordering of the nodes into them */
static void create_layers(RAGraph *g) {
const RList *nodes = r_graph_get_nodes (g->graph);
RGraphNode *gn;
const RListIter *it;
RANode *n;
int i;
/* identify max layer */
g->n_layers = 0;
graph_foreach_anode (nodes, it, gn, n) {
if (n->layer > g->n_layers) {
g->n_layers = n->layer;
}
}
/* create a starting ordering of nodes for each layer */
g->n_layers++;
if (sizeof (struct layer_t) * g->n_layers < g->n_layers) {
return;
}
g->layers = R_NEWS0 (struct layer_t, g->n_layers);
graph_foreach_anode (nodes, it, gn, n) {
g->layers[n->layer].n_nodes++;
}
for (i = 0; i < g->n_layers; ++i) {
if (sizeof (RGraphNode *) * g->layers[i].n_nodes < g->layers[i].n_nodes) {
continue;
}
g->layers[i].nodes = R_NEWS0 (RGraphNode *,
1 + g->layers[i].n_nodes);
g->layers[i].position = 0;
}
graph_foreach_anode (nodes, it, gn, n) {
n->pos_in_layer = g->layers[n->layer].position;
g->layers[n->layer].nodes[g->layers[n->layer].position++] = gn;
}
}
/* layer-by-layer sweep */
/* it permutes each layer, trying to find the best ordering for each layer
* to minimize the number of crossing edges */
static void minimize_crossings(const RAGraph *g) {
int i, cross_changed, max_changes = 4096;
do {
cross_changed = false;
--max_changes;
for (i = 0; i < g->n_layers; ++i) {
cross_changed |= layer_sweep (g->graph, g->layers, g->n_layers, i, true);
}
} while (cross_changed && max_changes);
max_changes = 4096;
do {
cross_changed = false;
--max_changes;
for (i = g->n_layers - 1; i >= 0; --i) {
cross_changed |= layer_sweep (g->graph, g->layers, g->n_layers, i, false);
}
} while (cross_changed && max_changes);
}
static int find_dist(const struct dist_t *a, const struct dist_t *b) {
return a->from == b->from && a->to == b->to? 0: 1;
}
/* returns the distance between two nodes */
/* if the distance between two nodes were explicitly set, returns that;
* otherwise calculate the distance of two nodes on the same layer */
static int dist_nodes(const RAGraph *g, const RGraphNode *a, const RGraphNode *b) {
struct dist_t d;
const RANode *aa, *ab;
RListIter *it;
int res = 0;
if (g->dists) {
d.from = a;
d.to = b;
it = r_list_find (g->dists, &d, (RListComparator) find_dist);
if (it) {
struct dist_t *old = (struct dist_t *) r_list_iter_get_data (it);
return old->dist;
}
}
aa = get_anode (a);
ab = get_anode (b);
if (aa && ab && aa->layer == ab->layer) {
int i;
res = aa == ab && !aa->is_reversed? HORIZONTAL_NODE_SPACING: 0;
for (i = aa->pos_in_layer; i < ab->pos_in_layer; ++i) {
const RGraphNode *cur = g->layers[aa->layer].nodes[i];
const RGraphNode *next = g->layers[aa->layer].nodes[i + 1];
const RANode *anext = get_anode (next);
const RANode *acur = get_anode (cur);
int found = false;
if (g->dists) {
d.from = cur;
d.to = next;
it = r_list_find (g->dists, &d, (RListComparator) find_dist);
if (it) {
struct dist_t *old = (struct dist_t *) r_list_iter_get_data (it);
res += old->dist;
found = true;
}
}
if (acur && anext && !found) {
int space = HORIZONTAL_NODE_SPACING;
if (acur->is_reversed && anext->is_reversed) {
if (!acur->is_reversed) {
res += acur->w / 2;
} else if (!anext->is_reversed) {
res += anext->w / 2;
}
res += 1;
} else {
res += acur->w / 2 + anext->w / 2 + space;
}
}
}
}
return res;
}
/* explictly set the distance between two nodes on the same layer */
static void set_dist_nodes(const RAGraph *g, int l, int cur, int next) {
struct dist_t *d, find_el;
const RGraphNode *vi, *vip;
const RANode *avi, *avip;
RListIter *it;
if (!g->dists) {
return;
}
vi = g->layers[l].nodes[cur];
vip = g->layers[l].nodes[next];
avi = get_anode (vi);
avip = get_anode (vip);
find_el.from = vi;
find_el.to = vip;
it = r_list_find (g->dists, &find_el, (RListComparator) find_dist);
d = it? (struct dist_t *) r_list_iter_get_data (it): R_NEW0 (struct dist_t);
d->from = vi;
d->to = vip;
d->dist = (avip && avi)? avip->x - avi->x: 0;
if (!it) {
r_list_push (g->dists, d);
}
}
static int is_valid_pos(const RAGraph *g, int l, int pos) {
return pos >= 0 && pos < g->layers[l].n_nodes;
}
/* computes the set of vertical classes in the graph */
/* if v is an original node, L(v) = { v }
* if v is a dummy node, L(v) is the set of all the dummies node that belongs
* to the same long edge */
static Sdb *compute_vertical_nodes(const RAGraph *g) {
Sdb *res = sdb_new0 ();
int i, j;
for (i = 0; i < g->n_layers; ++i) {
for (j = 0; j < g->layers[i].n_nodes; ++j) {
RGraphNode *gn = g->layers[i].nodes[j];
const RList *Ln = hash_get_rlist (res, gn);
const RANode *an = get_anode (gn);
if (!Ln) {
RList *vert = r_list_new ();
hash_set (res, gn, vert);
if (an->is_dummy) {
RGraphNode *next = gn;
const RANode *anext = get_anode (next);
while (anext->is_dummy) {
r_list_append (vert, next);
next = r_graph_nth_neighbour (g->graph, next, 0);
if (!next) {
break;
}
anext = get_anode (next);
}
} else {
r_list_append (vert, gn);
}
}
}
}
return res;
}
/* computes left or right classes, used to place dummies node */
/* classes respect three properties:
* - v E C
* - w E C => L(v) is a subset of C
* - w E C, the s+(w) exists and is not in any class yet => s+(w) E C */
static RList **compute_classes(const RAGraph *g, Sdb *v_nodes, int is_left, int *n_classes) {
int i, j, c;
RList **res = R_NEWS0 (RList *, g->n_layers);
RGraphNode *gn;
const RListIter *it;
RANode *n;
graph_foreach_anode (r_graph_get_nodes (g->graph), it, gn, n) {
n->klass = -1;
}
for (i = 0; i < g->n_layers; ++i) {
c = i;
for (j = is_left? 0: g->layers[i].n_nodes - 1;
(is_left && j < g->layers[i].n_nodes) || (!is_left && j >= 0);
j = is_left? j + 1: j - 1) {
const RGraphNode *gj = g->layers[i].nodes[j];
const RANode *aj = get_anode (gj);
if (aj->klass == -1) {
const RList *laj = hash_get_rlist (v_nodes, gj);
if (!res[c]) {
res[c] = r_list_new ();
}
graph_foreach_anode (laj, it, gn, n) {
r_list_append (res[c], gn);
n->klass = c;
}
} else {
c = aj->klass;
}
}
}
if (n_classes) {
*n_classes = g->n_layers;
}
return res;
}
static int cmp_dist(const size_t a, const size_t b) {
return (int) a < (int) b;
}
static RGraphNode *get_sibling(const RAGraph *g, const RANode *n, int is_left, int is_adjust_class) {
RGraphNode *res = NULL;
int pos = n->pos_in_layer;
if ((is_left && is_adjust_class) || (!is_left && !is_adjust_class)) {
pos++;
} else {
pos--;
}
if (is_valid_pos (g, n->layer, pos)) {
res = g->layers[n->layer].nodes[pos];
}
return res;
}
static int adjust_class_val(const RAGraph *g, const RGraphNode *gn, const RGraphNode *sibl, Sdb *res, int is_left) {
if (is_left) {
return hash_get_int (res, sibl) - hash_get_int (res, gn) - dist_nodes (g, gn, sibl);
}
return hash_get_int (res, gn) - hash_get_int (res, sibl) - dist_nodes (g, sibl, gn);
}
/* adjusts the position of previously placed left/right classes */
/* tries to place classes as close as possible */
static void adjust_class(const RAGraph *g, int is_left, RList **classes, Sdb *res, int c) {
const RGraphNode *gn;
const RListIter *it;
const RANode *an;
int dist, v, is_first = true;
graph_foreach_anode (classes[c], it, gn, an) {
const RGraphNode *sibling;
const RANode *sibl_anode;
sibling = get_sibling (g, an, is_left, true);
if (!sibling) {
continue;
}
sibl_anode = get_anode (sibling);
if (sibl_anode->klass == c) {
continue;
}
v = adjust_class_val (g, gn, sibling, res, is_left);
dist = is_first? v: R_MIN (dist, v);
is_first = false;
}
if (is_first) {
RList *heap = r_list_new ();
int len;
graph_foreach_anode (classes[c], it, gn, an) {
const RList *neigh = r_graph_all_neighbours (g->graph, gn);
const RGraphNode *gk;
const RListIter *itk;
const RANode *ak;
graph_foreach_anode (neigh, itk, gk, ak) {
if (ak->klass < c) {
r_list_append (heap, (void *) (size_t) (ak->x - an->x));
}
}
}
len = r_list_length (heap);
if (len == 0) {
dist = 0;
} else {
r_list_sort (heap, (RListComparator) cmp_dist);
dist = (int) (size_t) r_list_get_n (heap, len / 2);
}
r_list_free (heap);
}
graph_foreach_anode (classes[c], it, gn, an) {