-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgraphlib.cc
More file actions
1226 lines (1041 loc) · 33.1 KB
/
graphlib.cc
File metadata and controls
1226 lines (1041 loc) · 33.1 KB
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
#include "graphlib.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sccs.h"
// External libraries
#include "../_IntSets/intset.h"
const int MAJOR_VERSION = 3;
const int MINOR_VERSION = 0;
// #define DEBUG_DEFRAG
// #define DEBUG_TRANSPOSE_FROM
// ******************************************************************
// * *
// * Macros and such *
// * *
// ******************************************************************
inline void ShowArray(const char* name, const long* ptr, long N)
{
if (0==ptr) {
printf("%s: null\n", name);
return;
}
printf("%s: [%ld", name, ptr[0]);
for (long i=1; i<N; i++) printf(", %ld", ptr[i]);
printf("]\n");
}
// ******************************************************************
// * *
// * error methods *
// * *
// ******************************************************************
const char* GraphLib::error::getString() const
{
switch (errcode) {
case Not_Implemented: return "Not implemented";
case Bad_Index: return "Bad index";
case Out_Of_Memory: return "Out of memory";
case Format_Mismatch: return "Format mismatch";
case Finished_Mismatch: return "Finished graph";
case Miscellaneous: return "Misc. error";
};
return "Unknown error";
}
// ******************************************************************
// * *
// * timer_hook methods *
// * *
// ******************************************************************
GraphLib::timer_hook::timer_hook()
{
}
GraphLib::timer_hook::~timer_hook()
{
}
// ******************************************************************
// * *
// * BF_graph_traversal methods *
// * *
// ******************************************************************
GraphLib::BF_graph_traversal::BF_graph_traversal()
{
}
GraphLib::BF_graph_traversal::~BF_graph_traversal()
{
}
// ******************************************************************
// * *
// * BF_with_queue methods *
// * *
// ******************************************************************
GraphLib::BF_with_queue::BF_with_queue(long max_queue)
: BF_graph_traversal()
{
queue_alloc = max_queue;
queue = new long[max_queue];
queueReset();
}
GraphLib::BF_with_queue::~BF_with_queue()
{
delete[] queue;
}
bool GraphLib::BF_with_queue::hasNodesToExplore()
{
return !queueEmpty();
}
long GraphLib::BF_with_queue::getNextToExplore()
{
return queuePop();
}
// ******************************************************************
// * *
// * node_renumberer methods *
// * *
// ******************************************************************
GraphLib::node_renumberer::node_renumberer()
{
}
GraphLib::node_renumberer::~node_renumberer()
{
}
// ******************************************************************
// * *
// * nochange_renumberer methods *
// * *
// ******************************************************************
GraphLib::nochange_renumberer::nochange_renumberer()
{
}
GraphLib::nochange_renumberer::~nochange_renumberer()
{
}
long GraphLib::nochange_renumberer::new_number(long s) const
{
return s;
}
bool GraphLib::nochange_renumberer::changes_something() const
{
return false;
}
// ******************************************************************
// * *
// * array_renumberer methods *
// * *
// ******************************************************************
GraphLib::array_renumberer::array_renumberer(long* nn, long L)
{
newnumber = nn;
length = L;
not_identity = false;
for (long i=0; i<L; i++) {
if (nn[i] != i) {
not_identity = true;
break;
}
}
}
GraphLib::array_renumberer::~array_renumberer()
{
delete[] newnumber;
}
long GraphLib::array_renumberer::new_number(long s) const
{
CHECK_RANGE(0, s, length);
return newnumber[s];
}
bool GraphLib::array_renumberer::changes_something() const
{
return not_identity;
}
// ******************************************************************
// * *
// * static_classifier methods *
// * *
// ******************************************************************
GraphLib::static_classifier::static_classifier()
{
num_classes = 0;
class_start = 0;
}
GraphLib::static_classifier::static_classifier(const static_classifier &SC)
{
num_classes = 0;
class_start = 0;
replace(SC);
}
GraphLib::static_classifier::~static_classifier()
{
delete[] class_start;
}
long GraphLib::static_classifier::classOfNode(long s) const
{
// Insane values:
if (s<class_start[0]) return -1;
if (s>=class_start[num_classes]) return num_classes;
// Binary search
// Find c such that class_start[c] <= s < class_start[c+1].
// Invariant: class_start[low] <= s < class_start[high].
// Stop when low + 1 == high.
long low = 0;
long high = num_classes;
while (high>low+1) {
long mid = (high+low)/2;
if (s< class_start[mid]) high = mid;
else low = mid;
}
return low;
}
size_t GraphLib::static_classifier::getMemTotal() const
{
return (1+num_classes)*sizeof(long);
}
void GraphLib::static_classifier::rebuild(long nc, long* sizes)
{
delete[] class_start;
class_start = sizes;
num_classes = nc;
// Shift everything to the right,
// so that class_start[i] is the size of class i-1
for (long c=num_classes; c>0; c--) {
class_start[c] = class_start[c-1];
}
class_start[0] = 0;
//
// Accumulate, so that class_start[i] is equal to
// the sum from j=0 to i-1 of size of class j
//
for (long c=2; c<=num_classes; c++) {
class_start[c] += class_start[c-1];
}
}
void GraphLib::static_classifier::replace(long nc, long* starts)
{
delete[] class_start;
class_start = starts;
num_classes = nc;
// TBD - should we check the array?
}
void GraphLib::static_classifier::replace(const static_classifier &SC)
{
delete[] class_start;
num_classes = SC.num_classes;
class_start = new long[num_classes+1];
memcpy(class_start, SC.class_start, (1+num_classes)*sizeof(long));
}
// ******************************************************************
// * *
// * abstract_classifier methods *
// * *
// ******************************************************************
GraphLib::abstract_classifier::abstract_classifier(long ns, long nc)
{
num_nodes = ns;
num_classes = nc;
}
GraphLib::abstract_classifier::~abstract_classifier()
{
}
// ******************************************************************
// * *
// * general_classifier methods *
// * *
// ******************************************************************
GraphLib::general_classifier::general_classifier(long* C, long ns, long nc)
: abstract_classifier(ns, nc)
{
class_of_node = C;
}
GraphLib::general_classifier::~general_classifier()
{
delete[] class_of_node;
}
long GraphLib::general_classifier::classOfNode(long s) const
{
// TBD?
// CHECK_RANGE(0, s, getNumNodes());
return class_of_node[s];
}
GraphLib::node_renumberer*
GraphLib::general_classifier::buildRenumbererAndStatic(static_classifier &C) const
{
//
// First, count the number of states in each class.
// Use an extra element because eventually these will be converted
// to start indexes as needed for the static_classifier.
//
long* starts = new long[1+getNumClasses()];
for (long i=0; i<=getNumClasses(); i++) {
starts[i] = 0;
}
for (long i=0; i<getNumNodes(); i++) {
starts[class_of_node[i]]++;
}
//
// Determine starting index per class
//
for (long i=1; i<=getNumClasses(); i++) {
starts[i] += starts[i-1];
}
for (long i=getNumClasses(); i; i--) {
starts[i] = starts[i-1];
}
starts[0] = 0;
//
// Build the renumbering array.
// For node s, its new number is the current index of its class
// (and we increment the index).
//
long* renumber = new long[getNumNodes()];
for (long i=0; i<getNumNodes(); i++) {
renumber[i] = starts[ class_of_node[i] ]++;
}
//
// Shift the sizes array to get the correct "starts" array
//
for (long i=getNumClasses(); i; i--) {
starts[i] = starts[i-1];
}
starts[0] = 0;
//
// Package everything
//
replace_classifier(C, getNumClasses(), starts);
node_renumberer* R = new array_renumberer(renumber, getNumNodes());
if (R->changes_something()) return R;
// Clever bit here!
delete R;
return new nochange_renumberer;
}
// ******************************************************************
// * *
// * static_graph methods *
// * *
// ******************************************************************
GraphLib::static_graph::static_graph()
{
num_nodes = 0;
num_edges = 0;
row_pointer = 0;
column_index = 0;
edge_bytes = 0;
label = 0;
is_by_rows = true; // Arbitrary and useless
}
// ******************************************************************
GraphLib::static_graph::~static_graph()
{
free(row_pointer);
free(column_index);
free(label);
}
// ******************************************************************
void GraphLib::static_graph::transposeFrom(const static_graph &m)
{
#ifdef DEBUG_TRANSPOSE_FROM
printf("Inside transposeFrom\n");
printf(" Input graph:\n");
printf("\tis_by_rows: %s\n", m.is_by_rows ? "true" : "false");
printf("\tnum_nodes: %ld\n", m.num_nodes);
printf("\trow_pointer: ");
ShowArray("\trow_pointer", m.row_pointer, m.num_nodes+1);
ShowArray("\tcolumn_index: ", m.column_index, m.num_edges);
#endif
is_by_rows = !m.is_by_rows;
edge_bytes = m.edge_bytes;
// resize arrays
allocate(m.num_nodes, m.num_edges);
// Count entries in each transposed row
for (long r=0; r<=num_nodes; r++) row_pointer[r] = 0;
for (long e=0; e<m.row_pointer[m.num_nodes]; e++) {
++row_pointer[ m.column_index[e] ];
}
#ifdef DEBUG_TRANSPOSE_FROM
ShowArray(" index counts", row_pointer, num_nodes+1);
#endif
// Accumulate, so row_pointer[r] gives #entries in rows 0..r
for (long r=1; r<=num_nodes; r++) {
row_pointer[r] += row_pointer[r-1];
}
// Shift.
for (long r=num_nodes; r>0; r--) {
row_pointer[r] = row_pointer[r-1];
}
row_pointer[0] = 0;
#ifdef DEBUG_TRANSPOSE_FROM
ShowArray(" transposed row_pointer", row_pointer, num_nodes+1);
#endif
//
// Ok, right now row_pointer[r] is number of entries in rows 0..r-1.
// In other words, it's the starting location for row r.
// So we can start copying elements.
long e = 0;
for (long i=0; i<num_nodes; i++) {
for (; e<m.row_pointer[i+1]; e++) {
long j = m.column_index[e];
// add element i,j,value[e]
#ifdef DEBUG_TRANSPOSE_FROM
printf(" adding element [%ld, %ld]\n", i, j);
#endif
column_index[ row_pointer[j] ] = i;
if (edge_bytes) {
memcpy(label + edge_bytes * row_pointer[j], m.label + edge_bytes * e, edge_bytes);
}
++row_pointer[j];
} // for e
} // for i
// Right now, row_pointer[r] gives #entries in rows 0..r,
// so we need to shift again.
for (long r=num_nodes; r>0; r--) {
row_pointer[r] = row_pointer[r-1];
}
row_pointer[0] = 0;
#ifdef DEBUG_TRANSPOSE_FROM
printf(" Output matrix:\n");
printf("\tis_by_rows: %s\n", is_by_rows ? "true" : "false");
printf("\tnum_nodes: %ld\n", num_nodes);
ShowArray("\trow_pointer", row_pointer, num_nodes+1);
ShowArray("\tcolumn_index", column_index, num_edges);
#endif
}
// ******************************************************************
void GraphLib::static_graph::emptyRows(intset &x) const
{
x.removeAll();
for (long s=0; s<num_nodes; s++) {
if (row_pointer[s] == row_pointer[s+1]) {
x.addElement(s);
}
}
}
// ******************************************************************
bool GraphLib::static_graph::traverse(BF_graph_traversal &t) const
{
while (t.hasNodesToExplore()) {
long s = t.getNextToExplore();
if (s<0 || s>=num_nodes) {
throw GraphLib::error(GraphLib::error::Bad_Index);
}
// explore edges to/from s
for (long z=row_pointer[s]; z<row_pointer[s+1]; z++) {
if (t.visit(s, column_index[z], label + z*edge_bytes)) return true;
} // for z
} // while
return false;
}
// ******************************************************************
size_t GraphLib::static_graph::getMemTotal() const
{
long mem = 0;
if (row_pointer) mem += (num_nodes+1) * sizeof(long);
if (column_index) mem += num_edges * sizeof(long);
if (label) mem += num_edges * edge_bytes;
return mem;
}
// ******************************************************************
void GraphLib::static_graph::allocate(long nodes, long edges)
{
// row pointers
// hopefully realloc is fast if we give the same size?
long* nrp = (long*) realloc(row_pointer, (nodes+1)*sizeof(long));
if (0==nrp) {
throw error(error::Out_Of_Memory);
}
row_pointer = nrp;
num_nodes = nodes;
// column indexes
long* nci = 0;
if (0==edges) {
free(column_index);
} else {
nci = (long*) realloc(column_index, edges * sizeof(long));
if (0==nci) {
throw error(error::Out_Of_Memory);
}
}
column_index = nci;
num_edges = edges;
// labels
unsigned char* nl = 0;
if (0==edges*edge_bytes) {
free(label);
} else {
nl = (unsigned char*) realloc(label, edges * edge_bytes);
if (0==nl) {
throw error(error::Out_Of_Memory);
}
}
label = nl;
}
// ******************************************************************
// * *
// * dynamic_graph methods *
// * *
// ******************************************************************
GraphLib::dynamic_graph::dynamic_graph(unsigned char es, bool ksl, bool md)
{
edge_size = es;
keep_self_loops = ksl;
merge_duplicates = md;
is_by_rows = true;
nodes_alloc = 0;
row_pointer = 0;
edges_alloc = 0;
column_index = 0;
next = 0;
label = 0;
num_nodes = 0;
num_edges = 0;
}
// ******************************************************************
GraphLib::dynamic_graph::~dynamic_graph()
{
free(row_pointer);
free(column_index);
free(next);
free(label);
}
// ******************************************************************
void
GraphLib::dynamic_graph::addNodes(long count)
{
const int MAX_NODE_ADD = 1024;
long final_nodes = num_nodes+count;
if (final_nodes > nodes_alloc) {
long newnodes = nodes_alloc+1;
while (newnodes <= final_nodes) {
newnodes = MIN(2*newnodes, newnodes + MAX_NODE_ADD);
newnodes = MAX(newnodes, 8L);
}
long* foo = (long*) realloc(row_pointer, newnodes*sizeof(long));
if (0==foo) throw GraphLib::error(GraphLib::error::Out_Of_Memory);
row_pointer = foo;
nodes_alloc = newnodes-1;
}
for (; count; count--) {
row_pointer[num_nodes] = -1;
num_nodes++;
}
}
// ******************************************************************
bool
GraphLib::dynamic_graph::addEdge(long from, long to, const void* wt)
{
const int MAX_EDGE_ADD = 1024;
if (from == to && !keep_self_loops) return false;
if (!is_by_rows) SWAP(from, to);
if (from < 0 || from >= num_nodes || to < 0 || to >= num_nodes) {
throw GraphLib::error(GraphLib::error::Bad_Index);
}
// get a new edge from "end"; allocate more space if necessary
if (num_edges >= edges_alloc) {
long newedges = MIN(2*edges_alloc, edges_alloc + MAX_EDGE_ADD);
newedges = MAX(newedges, 16L);
long* nci = (long *) realloc(column_index, newedges*sizeof(long));
long* nn = (long *) realloc(next, newedges*sizeof(long));
void* nl = 0;
if (edge_size) {
nl = realloc(label, newedges*edge_size);
if (0==nl) throw GraphLib::error(GraphLib::error::Out_Of_Memory);
}
if ( (0==nci) || (0==nn) )
throw GraphLib::error(GraphLib::error::Out_Of_Memory);
column_index = nci;
next = nn;
label = (unsigned char*) nl;
edges_alloc = newedges;
}
// fix the edge
column_index[num_edges] = to;
if (edge_size) memcpy(label+num_edges*edge_size, wt, edge_size);
// add the new edge to the list.
if (merge_duplicates) {
if (!AddToMergedCircularList(row_pointer[from], num_edges))
return true;
} else {
AddToUnmergedCircularList(row_pointer[from], num_edges);
}
num_edges++;
return false;
}
// ******************************************************************
void
GraphLib::dynamic_graph::removeEdges(BF_graph_traversal &t)
{
while (t.hasNodesToExplore()) {
long s = t.getNextToExplore();
// Switch to linked lists, for simplicity
if (row_pointer[s]<0) continue; // it is empty
long tail = row_pointer[s];
row_pointer[s] = next[tail];
next[tail] = -1;
// traverse chain
long prev = -1;
long curr = row_pointer[s];
while (curr >= 0) {
if (t.visit(s, column_index[curr], label+curr*edge_size)) {
curr = next[curr];
if (prev>=0) next[prev] = curr;
else row_pointer[s] = curr;
} else {
prev = curr;
curr = next[curr];
}
} // while curr
// switch back to circular lists
if (row_pointer[s] < 0) continue; // empty list
if (prev < 0) {
// list has one element, close the circle.
next[row_pointer[s]] = row_pointer[s];
} else {
// prev is the tail, close the circle.
next[prev] = row_pointer[s];
row_pointer[s] = prev;
}
} // while
}
// ******************************************************************
void
GraphLib::dynamic_graph::renumberNodes(const node_renumberer &r)
{
//
// Important special case
//
if (!r.changes_something()) {
// we don't actually renumber anything
return;
}
//
// We need another row_pointer array; everything else is "in place"
//
long* tmp_row_lists = new long[num_nodes];
for (long s=0; s<num_nodes; s++) tmp_row_lists[s] = -1;
//
// Do a transpose + renumber.
// Convert old row lists into column lists, renumbering as we go.
//
for (long s=0; s<num_nodes; s++) {
if (row_pointer[s]<0) continue; // empty row; skip
// Convert current list from circular to linear
long tail = row_pointer[s];
row_pointer[s] = next[tail];
next[tail] = -1;
// Now, traverse the list, and do the transpose.
// We won't bother to order the lists;
// a second transpose will make that right.
long nc = r.new_number(s); // new column
long ptrnext = -1;
for (long ptr=row_pointer[s]; ptr>=0; ptr=ptrnext) {
ptrnext = next[ptr]; // we're going to clobber next[ptr], so save it
long nr = r.new_number(column_index[ptr]); // new row
//
// Change edge ptr from (colindex, value) in list s
// to (nc, value) in list nr
//
column_index[ptr] = nc;
// Ok, add this to list tmp_row_lists[nr]
next[ptr] = tmp_row_lists[nr];
tmp_row_lists[nr] = ptr;
}
row_pointer[s] = -1;
}
//
// Graph is renumbered, but transposed.
// Transpose back, which will ensure the edges are in order.
// Also, note that tmp_row_lists are (non-circular) linked lists.
//
long ptrnext = -1;
for (long s=0; s<num_nodes; s++) {
for (long ptr=tmp_row_lists[s]; ptr>=0; ptr=ptrnext) {
ptrnext = next[ptr];
// Current edge is (colindex[ptr], value[ptr]) in list s;
// Change it to (s, value[ptr]) in circular list colindex[ptr].
long nr = column_index[ptr];
column_index[ptr] = s;
// Edge is good; add it to the appropriate list
if (row_pointer[nr] < 0) {
// Empty row; build one
next[ptr] = ptr;
row_pointer[nr] = ptr;
} else {
// Non-empty row; add past the tail
long tail = row_pointer[nr];
next[ptr] = next[tail];
next[tail] = ptr;
row_pointer[nr] = ptr;
}
}
}
// Cleanup
delete[] tmp_row_lists;
}
// ******************************************************************
void GraphLib::dynamic_graph::transpose(timer_hook* sw)
{
// we need another row_pointer array; everything else is "in place"
long* old_row_pointer = (long *) malloc((num_nodes+1)*sizeof(long));
if (0==old_row_pointer)
throw GraphLib::error(GraphLib::error::Out_Of_Memory);
if (sw) sw->start("Transposing");
long s;
for (s=0; s<=num_nodes; s++) old_row_pointer[s] = -1;
SWAP(row_pointer, old_row_pointer);
nodes_alloc = num_nodes;
// Convert old row lists into column lists
for (s=0; s<num_nodes; s++) {
// convert old list from circular to linear
if (old_row_pointer[s]<0) continue; // it is empty
long tail = old_row_pointer[s];
old_row_pointer[s] = next[tail];
next[tail] = -1;
// traverse linear list
while (old_row_pointer[s]>=0) {
long ptr = old_row_pointer[s];
old_row_pointer[s] = next[ptr];
// change column index to row
long col = column_index[ptr];
column_index[ptr] = s;
AddToUnmergedCircularList(row_pointer[col], ptr);
} // while
} // for s
// done with old matrix
free(old_row_pointer);
// toggle
is_by_rows = !(is_by_rows);
if (sw) sw->stop();
}
// ******************************************************************
void
GraphLib::dynamic_graph::exportToStatic(static_graph &g, timer_hook *sw)
{
if (sw) sw->start("Defragmenting graph");
num_edges = Defragment(0);
if (sw) sw->stop();
if (sw) sw->start("Exporting graph");
// Make space for g
g.edge_bytes = edge_size;
g.allocate(num_nodes, num_edges);
// Copy everything over
g.is_by_rows = is_by_rows;
memcpy(g.row_pointer, row_pointer, (1+num_nodes) * sizeof(long));
memcpy(g.column_index, column_index, num_edges * sizeof(long));
if (edge_size) {
memcpy(g.label, label, num_edges * edge_size);
}
if (sw) sw->stop();
}
// ******************************************************************
void
GraphLib::dynamic_graph::exportAndDestroy(static_graph &g, timer_hook *sw)
{
if (sw) sw->start("Defragmenting graph");
num_edges = Defragment(0);
if (sw) sw->stop();
if (sw) sw->start("Exporting graph");
// Clean out any old g
g.allocate(0, 0);
// resize arrays
if (nodes_alloc > num_nodes) {
row_pointer = (long*) realloc(row_pointer, (1+num_nodes)*sizeof(long));
}
if (edges_alloc > num_edges) {
column_index = (long*) realloc(column_index, num_edges*sizeof(long));
label = (unsigned char*) realloc(label, num_edges*edge_size);
}
// Transfer things over
g.num_nodes = num_nodes;
g.num_edges = num_edges;
g.edge_bytes = edge_size;
g.is_by_rows = is_by_rows;
g.row_pointer = row_pointer;
g.column_index = column_index;
g.label = label;
// Destroy our pointers
row_pointer = 0;
column_index = 0;
label = 0;
free(next);
next = 0;
nodes_alloc = 0;
edges_alloc = 0;
num_nodes = 0;
num_edges = 0;
if (sw) sw->stop();
}
// ******************************************************************
void
GraphLib::dynamic_graph::splitAndExport(const static_classifier &C, bool ksl,
static_graph &g_diag, static_graph &g_off, timer_hook *sw)
{
if (sw) sw->start("Defragmenting graph");
num_edges = Defragment(0);
if (sw) sw->stop();
if (sw) sw->start("Exporting graphs");
//
// We know the total number of edges.
// Determine how many of these are "diagonal" edges
// (i.e., are between nodes in the same class)
// and how many are "off diagonal" edges.
//
// Do that by iterating over classes, then iterating
// over states in the class, and examining the outgoing
// edges for that state.
//
long diag_edges = 0;
long off_edges = 0;
for (long c=0; c<C.getNumClasses(); c++) {
for (long s=C.firstNodeOfClass(c); s<=C.lastNodeOfClass(c); s++) {
for (long edge=row_pointer[s]; edge<row_pointer[s+1]; edge++) {
if (!ksl && s == column_index[edge]) continue;
if (C.isNodeInClass(column_index[edge], c)) {
diag_edges++;
} else {
off_edges++;
}
} // for edge
} // for s
} // for c
DCASSERT(diag_edges + off_edges <= num_edges);
//
// Allocate space for g_diag and g_off
//
g_diag.is_by_rows = is_by_rows;
g_diag.edge_bytes = edge_size;
g_diag.allocate(num_nodes, diag_edges);
g_off.is_by_rows = is_by_rows;
g_off.edge_bytes = edge_size;
g_off.allocate(num_nodes, off_edges);
//
// Copy edges over, using an iteration similar to the one where we
// counted edges.
//
diag_edges = 0;
off_edges = 0;
for (long c=0; c<C.getNumClasses(); c++) {
for (long s=C.firstNodeOfClass(c); s<=C.lastNodeOfClass(c); s++) {
g_diag.row_pointer[s] = diag_edges;
g_off.row_pointer[s] = off_edges;
for (long edge=row_pointer[s]; edge<row_pointer[s+1]; edge++) {
if (!ksl && s == column_index[edge]) continue;
if (C.isNodeInClass(column_index[edge], c)) {
// copy the edge over to g_diag
g_diag.column_index[diag_edges] = column_index[edge];
if (edge_size) {
memcpy(g_diag.label+diag_edges*edge_size,
label+edge*edge_size, edge_size);
}
// and count it
diag_edges++;
} else {
// copy the edge over to g_off
g_off.column_index[off_edges] = column_index[edge];
if (edge_size) {
memcpy(g_off.label+off_edges*edge_size,
label+edge*edge_size, edge_size);
}
// and count it
off_edges++;
}
} // for edge
} // for s
} // for c
g_diag.row_pointer[num_nodes] = diag_edges;
g_off.row_pointer[num_nodes] = off_edges;
DCASSERT(g_diag.getNumEdges() == diag_edges);
DCASSERT(g_off.getNumEdges() == off_edges);
if (sw) sw->stop();
}