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.base; 018 019import net.automatalib.automata.Automaton; 020import net.automatalib.automata.concepts.StateIDs; 021import net.automatalib.commons.util.array.ResizingObjectArray; 022import net.automatalib.commons.util.mappings.MutableMapping; 023 024public class StateIDGrowingMapping<S,V> implements MutableMapping<S,V> { 025 026 private final Automaton<S,?,?> automaton; 027 private final StateIDs<S> stateIds; 028 private final ResizingObjectArray storage; 029 030 public StateIDGrowingMapping(Automaton<S,?,?> automaton, StateIDs<S> stateIds) { 031 this.automaton = automaton; 032 this.stateIds = stateIds; 033 this.storage = new ResizingObjectArray(automaton.size()); 034 } 035 036 /* 037 * (non-Javadoc) 038 * @see net.automatalib.commons.util.mappings.Mapping#get(java.lang.Object) 039 */ 040 @Override 041 @SuppressWarnings("unchecked") 042 public V get(S elem) { 043 int id = stateIds.getStateId(elem); 044 if(id < storage.array.length) 045 return (V)storage.array[id]; 046 return null; 047 } 048 049 /* 050 * (non-Javadoc) 051 * @see net.automatalib.commons.util.mappings.MutableMapping#put(java.lang.Object, java.lang.Object) 052 */ 053 @Override 054 @SuppressWarnings("unchecked") 055 public V put(S key, V value) { 056 int id = stateIds.getStateId(key); 057 if(id >= storage.array.length) 058 storage.ensureCapacity(automaton.size()); 059 V old = (V)storage.array[id]; 060 storage.array[id] = value; 061 return old; 062 } 063 064}