001/* Copyright (C) 2013 TU Dortmund
002 * This file is part of AutomataLib, http://www.automatalib.net/.
003 * 
004 * AutomataLib is free software; you can redistribute it and/or
005 * modify it under the terms of the GNU Lesser General Public
006 * License version 3.0 as published by the Free Software Foundation.
007 * 
008 * AutomataLib is distributed in the hope that it will be useful,
009 * but WITHOUT ANY WARRANTY; without even the implied warranty of
010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
011 * Lesser General Public License for more details.
012 * 
013 * You should have received a copy of the GNU Lesser General Public
014 * License along with AutomataLib; if not, see
015 * http://www.gnu.de/documents/lgpl.en.html.
016 */
017package net.automatalib.graphs.abstractimpl;
018
019import java.util.ArrayList;
020import java.util.HashMap;
021import java.util.List;
022import java.util.Map;
023
024import net.automatalib.graphs.Graph;
025import net.automatalib.graphs.concepts.NodeIDs;
026
027public class SimpleNodeIDs<N> implements NodeIDs<N> {
028
029        private final Map<N,Integer> nodeIds;
030        private final List<N> nodes;
031        
032        public SimpleNodeIDs(Graph<N,?> graph) {
033                this.nodes = new ArrayList<N>(graph.getNodes());
034                int numNodes = this.nodes.size();
035                this.nodeIds = new HashMap<N,Integer>((int)(numNodes / 0.75) + 1);
036                
037                for(int i = 0; i < numNodes; i++) {
038                        N node = this.nodes.get(i);
039                        nodeIds.put(node, i);
040                }
041        }
042
043        @Override
044        public int getNodeId(N node) {
045                return nodeIds.get(node).intValue();
046        }
047
048        @Override
049        public N getNode(int id) {
050                return nodes.get(id);
051        }
052}