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.automata.abstractimpl;
018
019import java.util.ArrayList;
020import java.util.HashMap;
021import java.util.List;
022import java.util.Map;
023
024import net.automatalib.automata.Automaton;
025import net.automatalib.automata.concepts.StateIDs;
026
027public class SimpleStateIDs<S> implements StateIDs<S> {
028        
029        private final Map<S,Integer> stateIds;
030        private final List<S> states;
031        
032        public SimpleStateIDs(Automaton<S,?,?> automaton) {
033                this.states = new ArrayList<S>(automaton.getStates());
034                int numStates = this.states.size();
035                this.stateIds = new HashMap<S,Integer>(numStates);
036                
037                for(int i = 0; i < numStates; i++) {
038                        S state = this.states.get(i);
039                        stateIds.put(state, i);
040                }
041        }
042
043        /*
044         * (non-Javadoc)
045         * @see net.automatalib.automata.concepts.StateIDs#getStateId(java.lang.Object)
046         */
047        @Override
048        public int getStateId(S state) {
049                return stateIds.get(state).intValue();
050        }
051
052        /*
053         * (non-Javadoc)
054         * @see net.automatalib.automata.concepts.StateIDs#getState(int)
055         */
056        @Override
057        public S getState(int id) {
058                return states.get(id);
059        }
060
061}