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.util.minimizer; 018 019import java.util.Arrays; 020import java.util.Collection; 021import java.util.List; 022 023import net.automatalib.commons.util.mappings.MutableMapping; 024 025/** 026 * Class for associating arbitrary values with the blocks of a minimization 027 * result. 028 * <p> 029 * The storage and lookup are performed in constant time. 030 * 031 * @author Malte Isberner <malte.isberner@gmail.com> 032 * 033 * @param <V> value class. 034 */ 035public class BlockMap<V> implements MutableMapping<Block<?,?>,V> { 036 private final Object[] storage; 037 038 039 /** 040 * Constructor. 041 * @param minResult the result structure. 042 */ 043 public BlockMap(MinimizationResult<?, ?> minResult) { 044 this.storage = new Object[minResult.getNumBlocks()]; 045 } 046 047 /** 048 * Retrieves a value. 049 * @param block the block. 050 * @return the associated value. 051 */ 052 @SuppressWarnings("unchecked") 053 @Override 054 public V get(Block<?,?> block) { 055 return (V)storage[block.getId()]; 056 } 057 058 /** 059 * Stores a value. 060 * @param block the associated block. 061 * @param value the value. 062 */ 063 @SuppressWarnings("unchecked") 064 @Override 065 public V put(Block<?,?> block, V value) { 066 V old = (V)storage[block.getId()]; 067 storage[block.getId()] = value; 068 return old; 069 } 070 071 /** 072 * Retrieves all values that are stored in this map. 073 * @return the values that are stored in this map. 074 */ 075 @SuppressWarnings("unchecked") 076 public Collection<V> values() { 077 return (List<V>)Arrays.asList(storage); 078 } 079}