@@ -3,7 +3,6 @@ Handles the writing of multiline adjacency lists after the graph is completed.
3
3
Also handles general file IO.
4
4
*/
5
5
var fs = require ( 'fs' ) ,
6
- pathMod = require ( 'path' ) ,
7
6
dialog = require ( 'nw-dialog' ) ;
8
7
//**************************************************************************
9
8
//WRITE GRAPHS
@@ -38,7 +37,7 @@ function makeAdjlist(nodeList){
38
37
return adjlist ;
39
38
}
40
39
41
- function writeGraph ( ) {
40
+ function writeGraphAdjlist ( ) {
42
41
/*
43
42
Writes a multiline adjlist of the user made graph to the specified filename.
44
43
*/
@@ -53,6 +52,65 @@ function writeGraph(){
53
52
} ) ;
54
53
}
55
54
55
+ function makeMatrix ( nodeList ) {
56
+ /*
57
+ Given a list of nodes in a graph, creates an adjacency matrix.
58
+ Note that the dimensions of the matrix are the first line of the file
59
+ and are formatted n,n.
60
+
61
+ INPUT
62
+ nodeList: A list of nodes in the user's graph
63
+
64
+ OUTPUT
65
+ matrix: A string with the first line being the dimensions of a matrix,
66
+ and then a jagged array to represent the matrix.
67
+ */
68
+
69
+ var matrix = nodeList . length . toString ( ) + ',' + nodeList . length . toString ( )
70
+ + '\r\n' ;
71
+ matrix = matrix + '[' ;
72
+
73
+ for ( var i = 0 ; i < nodeList . length ; i ++ ) {
74
+ console . log ( 'here' ) ;
75
+ var edges = nodes [ i ] . adjacencies ;
76
+
77
+ //Define our row and fill it with zeros
78
+ var matrixRow = new Array ( nodeList . length ) ;
79
+ matrixRow . fill ( 0 ) ;
80
+
81
+ //Increment every index which has an edge by one
82
+ for ( var j = 0 ; j < edges . length ; j ++ ) {
83
+ matrixRow [ edges [ j ] . index ] ++ ;
84
+ }
85
+
86
+ matrix = matrix + '[' + matrixRow . toString ( ) + ']' + ',\r\n' ;
87
+ }
88
+
89
+ //Remove the last ',\r\n'
90
+ matrix = matrix . split ( ',\r\n' ) ;
91
+ matrix . pop ( ) ;
92
+ matrix = matrix . join ( ',\r\n' ) ;
93
+
94
+ matrix = matrix + ']' ;
95
+
96
+ return matrix ;
97
+ }
98
+
99
+ function writeGraphMatrix ( ) {
100
+ /*
101
+ Writes an adjacency matrix for the usermade graph to the specified filename.
102
+ */
103
+
104
+ dialog . setContext ( document ) ;
105
+ dialog . saveFileDialog ( function ( fileName ) {
106
+ var matrixFile = makeMatrix ( nodes ) ;
107
+
108
+ fs . writeFile ( fileName , matrixFile , function ( err ) {
109
+ if ( err ) throw err ;
110
+ } ) ;
111
+ } ) ;
112
+ }
113
+
56
114
//**************************************************************************
57
115
//WRITE POSITIONS
58
116
//**************************************************************************
0 commit comments