本文整理匯總了TypeScript中typescript-collections.Set.forEach方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript Set.forEach方法的具體用法?TypeScript Set.forEach怎麽用?TypeScript Set.forEach使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類typescript-collections.Set
的用法示例。
在下文中一共展示了Set.forEach方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: dijkstra
function dijkstra(source, target) {
var unvisited = new Collections.Set(),
dist = {},
prev = {};
Graph.nodes.forEach(function (node) {
dist[node] = INFINITY;
prev[node] = null;
unvisited.add(node);
});
dist[source] = 0;
var currentNode = source,
pathExists = true;
while (currentNode != target) {
unvisited.remove(currentNode);
Graph.doWithConnectedNodes(currentNode, (node: string, weight: number) => {
var distFromCurrentNode = dist[currentNode] + weight;
if (dist[node] > distFromCurrentNode) {
dist[node] = distFromCurrentNode;
prev[node] = currentNode;
}
});
currentNode = null;
unvisited.forEach(function (node: string) {
if (!currentNode || dist[currentNode] > dist[node]) {
currentNode = node;
}
});
if (dist[currentNode] === INFINITY) {
console.log('No path was found');
pathExists = false;
break;
}
}
if (pathExists) {
while (currentNode != source) {
console.log(currentNode);
currentNode = prev[currentNode];
}
console.log(source);
}
}