001    /*
002     * MemoryCoOccurrenceFrequencyMatrix
003     * 
004     * Copyright (c) 2001, 2002, 2003 Marco Schmidt.
005     * All rights reserved.
006     */
007    
008    package net.sourceforge.jiu.color.data;
009    
010    import net.sourceforge.jiu.color.data.BaseCoOccurrenceFrequencyMatrix;
011    
012    /**
013     * Implements the {@link CoOccurrenceFrequencyMatrix} interface by using a large array
014     * of values in memory.
015     * @author Marco Schmidt
016     */
017    public class MemoryCoOccurrenceFrequencyMatrix extends BaseCoOccurrenceFrequencyMatrix
018    {
019            /** will be initialized in constructor and never changed */
020            private final int dimension;
021            /** total number of values, equals dimension * dimension and data.length */
022            private int numValues;
023            /** co occurrence frequency values */
024            private double[] data;
025    
026            /**
027             * Creates a co-occurrence frequency matrix of given dimension;
028             * allocates dimension times dimension double values for
029             * internal array;
030             * does not call clear() to set everything to zero, must be
031             * done by user (or automatically in init).
032             * Dimension should be number of colors in palette.
033             * @throws IllegalArgumentException if dimension is smaller than 1 
034             */
035            public MemoryCoOccurrenceFrequencyMatrix(int dimension) 
036            {
037                    if (dimension < 1)
038                    {
039                            throw new IllegalArgumentException("Dimension of co-occurrence frequency matrix must be >= 1.");
040                    }
041                    this.dimension = dimension;
042                    this.numValues = dimension * dimension;
043                    data = new double[numValues];
044            }
045    
046            /**
047             * Sets all values of this matrix to zero.
048             */
049            public void clear()
050            {
051                    if (data == null)
052                    {
053                            return;
054                    }
055                    for (int i = 0; i < numValues; i++)
056                    {
057                            data[i] = 0.0;
058                    }
059            }
060    
061            public int getDimension()
062            {
063                    return dimension;
064            }
065    
066            /**
067             * Returns the value of this matrix at row i, column i.
068             * Argument is zero-based, so make sure that
069             * 0 <= i < getDimension().
070             * Other values will raise an IllegalArgumentException.
071             * Simply calls getValue(i, i).
072             */
073            public double getValue(int i) throws IllegalArgumentException
074            {
075                    return getValue(i, i);
076            }
077    
078            /**
079             * Returns the value of this matrix at row j, column i.
080             * Both arguments are zero-based, so make sure that
081             * 0 <= i, j < getDimension().
082             * Other values will raise an IllegalArgumentException.
083             */
084            public double getValue(int i, int j) throws IllegalArgumentException
085            {
086                    if (i < 0 || i >= dimension || j < 0 || j >= dimension)
087                    {
088                            throw new IllegalArgumentException(
089                                    "i/j arguments out of bounds: " + i + "/" + j);
090                    }
091                    return data[j * dimension + i];
092            }
093    
094            /**
095             * Sets value at row j, column i to newValue.
096             * Both arguments are zero-based, so make sure that
097             * 0 <= i, j < getDimension().
098             * Other values will raise an IllegalArgumentException.
099             */
100            public void setValue(int i, int j, double newValue)
101                    throws IllegalArgumentException
102            {
103                    if (i < 0 || i >= dimension || j < 0 || j >= dimension)
104                    {
105                            throw new IllegalArgumentException("i/j coordinate out of bounds: " + i + "/" + j);
106                    }
107                    data[j * dimension + i] = newValue;
108            }
109    }