當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript typescript-collections.Set類代碼示例

本文整理匯總了TypeScript中typescript-collections.Set的典型用法代碼示例。如果您正苦於以下問題:TypeScript Set類的具體用法?TypeScript Set怎麽用?TypeScript Set使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


在下文中一共展示了Set類的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的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);
    }
}
開發者ID:rustamli,項目名稱:le-livre-bleu,代碼行數:51,代碼來源:dijkstra.ts

示例2: test

test('Generate cards for a standard deck', t => {
  t.plan(2);

  const cards = deckFactory.generateCardsForDeckType(deck_type.standard);
  const cardSet = new Set<string>();

  // Add all cards to a set, verify uniqueness by length
  cards.forEach(function(card: Card): void {
    let key = card.face.toString() + card.suit.toString();
    cardSet.add(key);
  });

  t.equal(cards.length, 52);
  t.equal(cardSet.size(), 52);
});
開發者ID:swese44,項目名稱:deck,代碼行數:15,代碼來源:deckFactory.test.ts

示例3: dfs

export default function dfs(startNode: string, visit: Function) {
    
    var visited = new Collections.Set<string>(),
        search = (node: string) => {
            Graph.doWithConnectedNodes(node, function (connectedNode: string) {
                if (!visited.contains(connectedNode)) {
                    visit(connectedNode);
                    visited.add(connectedNode);
                    search(connectedNode);
                }
            });
        };

    visit(startNode);
    visited.add(startNode);
    search(startNode);
}
開發者ID:rustamli,項目名稱:le-livre-bleu,代碼行數:17,代碼來源:dfs.ts

示例4: bfs

export default function bfs(startNode: string, visit: Function) {
    
    var visited = new Collections.Set<string>(),
        queue = new Collections.Queue<string>();

    visit(startNode);
    visited.add(startNode);
    queue.enqueue(startNode);

    while (!queue.isEmpty()) {
        let current = queue.dequeue();

        Graph.doWithConnectedNodes(current, function (connectedNode: string) {
            if (!visited.contains(connectedNode)) {
                visit(connectedNode);
                visited.add(connectedNode);
                queue.enqueue(connectedNode);
            }
        });
    }
}
開發者ID:rustamli,項目名稱:le-livre-bleu,代碼行數:21,代碼來源:bfs.ts

示例5:

 cards.forEach(function(card: Card): void {
   let key = card.face.toString() + card.suit.toString();
   cardSet.add(key);
 });
開發者ID:swese44,項目名稱:deck,代碼行數:4,代碼來源:deckFactory.test.ts


注:本文中的typescript-collections.Set類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。