/* * Created on 23-Sep-2004 by Ryan McNally */ package com.speckled.specksim.imp.state; import java.util.Arrays; import com.speckled.specksim.state.SpeckState; import com.speckled.specksim.state.StateProcessor; import com.speckled.specksim.state.StateSink; /** * This processor generates an array, each element of which is an int * array containing the indices of that speck's neighbours in the * state array. The indices will be sorted into ascending order * * @author ryanm */ public class NeighbourhoodProcessor extends StateProcessor { /** * The name of this processor */ public static final String NAME = "NeighbourhoodProcessor"; private int[][] neighbourIndices; @Override public String getName() { return NAME; } /** * Gets the output of this processor. The output is an array of * arrays, one for each NeighbourhoodAware speck in the simulator. * These arrays contain the indices of that speck's neighbours in * the simulatorState. Speck IDs that are not found in the * simulatorState will be represented with -1 * * @return an array of int arrays */ public int[][] getNeighbourIndices() { // first make sure our results are up to date... processState(); return neighbourIndices; } @Override protected void doProcessing( StateSink state ) { SpeckState[] specks = state.getState().getSpecks(); neighbourIndices = new int[ specks.length ][]; for( int i = 0; i < specks.length; i++ ) { if( specks[ i ] instanceof NeighbourhoodAware && ( ( NeighbourhoodAware ) specks[ i ] ).getNeighbourIDs() != null ) { int[] neighbourIDs = ( ( NeighbourhoodAware ) specks[ i ] ).getNeighbourIDs(); neighbourIndices[ i ] = new int[ neighbourIDs.length ]; for( int j = 0; j < neighbourIDs.length; j++ ) { neighbourIndices[ i ][ j ] = state.findID( neighbourIDs[ j ] ); } Arrays.sort( neighbourIndices[ i ] ); } else { neighbourIndices[ i ] = null; } } } }