-
Notifications
You must be signed in to change notification settings - Fork 29
/
index.js
145 lines (124 loc) · 3.63 KB
/
index.js
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
var async = require('async');
var fs = require('fs');
var path = require('path');
var JSONStream = require('JSONStream');
var __ = require('highland');
var datasets = require('./datasets');
exports.TubeMap = TubeMap;
exports.Maps = Maps;
function Station(opts) {
this.id = opts.id;
this.conns = opts.conns || [];
this.name = opts.name || "Unknown";
this.display_name = opts.display_name || "Unknown";
this.rail = opts.rail ? parseInt(opts.rail, 10) : null;
this.total_lines = opts.total_lines ? parseInt(opts.total_lines, 10) : 1;
this.latitude = parseFloat(opts.latitude);
this.longitude = parseFloat(opts.longitude);
}
function TubeMap(opts) {
opts = opts || {};
this.stationsById = {};
this.stationsByName = {};
this.linesById = {};
this.linesByName = {};
this.stations = opts.stations || [];
this.connections = opts.connections || [];
this.lines = opts.lines || [];
this.make();
}
TubeMap.prototype.makeStation = function(s) {
s = new Station(s);
this.stationsById[s.id] = s;
this.stationsByName[s.name] = s;
};
TubeMap.prototype.getAdjacent = function (root, line) {
var conns = root.conns;
if (line) {
conns = conns.filter(function (c) {
return c.line == line;
});
}
return conns.map(function(c) {
c.station1.line = c.line;
c.station2.line = c.line;
return c.station1.id !== root.id ? c.station1 : c.station2;
});
};
TubeMap.prototype.constructPath = function (dict, path, start, destination){
if (path[path.length-1] && path[path.length-1].station2 == start) {
return path;
}
path.push({station1: destination, station2: dict[destination.id]});
return this.constructPath(dict, path, start, dict[destination.id]);
};
TubeMap.prototype.path = function(start, destination, line){
var Q = [destination];
var V = {};
var family = {};
family[destination.id] = null;
while (Q.length > 0) {
var currentStation = Q.shift();
if (currentStation.id in V) {
continue;
}
var conns = this.getAdjacent(currentStation, line);
conns.forEach(function(child) {
if (!(child.id in V)){
Q.push(child);
}
if (!(child.id in family)){
family[child.id] = currentStation;
}
});
V[currentStation.id] = currentStation;
if (currentStation.id === start.id) {
var path = this.constructPath(family, [], destination, start);
if (line) {
path = path.map(function(d) {
d.line = line;
return d;
});
}
return path;
}
}
return false;
};
TubeMap.prototype.line = function(id) {
return this.connections.filter(function(d) {
return d.line == id;
});
};
TubeMap.prototype.makeConnection = function(c) {
c.station1 = this.stationsById[c.station1];
c.station2 = this.stationsById[c.station2];
c.time = parseInt(c.time, 10);
c.station1.conns.push(c);
c.station2.conns.push(c);
};
TubeMap.prototype.makeLine = function(r) {
this.linesById[r.line] = r;
this.linesByName[r.name] = r;
};
TubeMap.prototype.getLine = function(id) {
return this.linesById[id];
};
TubeMap.prototype.getLineByName = function(name) {
return this.linesByName[name];
}
TubeMap.prototype.getStation = function(id) {
return this.stationsById[id];
};
TubeMap.prototype.getStationByName = function(display_name) {
return this.stationsByName[display_name];
};
TubeMap.prototype.make = function() {
this.stations.forEach(this.makeStation.bind(this));
this.connections.forEach(this.makeConnection.bind(this));
this.lines.forEach(this.makeLine.bind(this));
};
function Maps (city, cb) {
var dataset = require(datasets[city].json);
cb(null, new TubeMap(dataset));
};