001 /* 002 * MemoryCoOccurrenceMatrix 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.CoOccurrenceMatrix; 011 012 /** 013 * This class stores a co-occurrence matrix, a two-dimensional array of int counters. 014 * The dimension is given to the constructor which allocates a corresponding array. 015 * @author Marco Schmidt 016 */ 017 public class MemoryCoOccurrenceMatrix implements CoOccurrenceMatrix 018 { 019 private int dimension; 020 private int dimSquare; 021 private int[] data; 022 023 /** 024 * Creates a new matrix that stores dimension times dimension int values in memory. 025 * Given that array index values are of type int, this limits dimension to about 46000 026 * (sqrt(Integer.MAX_VALUE). 027 * In practice, dimension leads to dimension times dimenstion times 4 bytes being 028 * allocated, so that memory available to the JVM may become a decisive factor. 029 * @param dimension the matrix' dimension, which is both the number of rows and columns 030 */ 031 public MemoryCoOccurrenceMatrix(int dimension) 032 { 033 if (dimension < 1) 034 { 035 throw new IllegalArgumentException("Dimension of co-occurrence matrix must be >= 1."); 036 } 037 this.dimension = dimension; 038 long longDimSquare = (long)dimension * (long)dimension; 039 if (longDimSquare > Integer.MAX_VALUE) 040 { 041 throw new IllegalArgumentException("Dimension " + dimension + " leads to an array exceeding the maximum size of 2^31 entries."); 042 } 043 dimSquare = dimension * dimension; 044 data = new int[dimSquare]; 045 } 046 047 public void clear() 048 { 049 for (int i = 0; i < dimSquare; i++) 050 { 051 data[i] = 0; 052 } 053 } 054 055 public int getDimension() 056 { 057 return dimension; 058 } 059 060 061 public int getValue(int i, int j) 062 { 063 if (i < 0 || i >= dimension || j < 0 || j >= dimension) 064 { 065 throw new IllegalArgumentException("co-occ matrix i/j arguments out of bounds: " + i + "/" + j); 066 } 067 return data[j * dimension + i]; 068 } 069 070 public void incValue(int i, int j) throws IllegalArgumentException 071 { 072 data[j * dimension + i]++; 073 } 074 075 public void setValue(int i, int j, int newValue) 076 { 077 if (i < 0 || i >= dimension || j < 0 || j >= dimension) 078 { 079 throw new IllegalArgumentException("co-occ matrix setValue, i/j coordinate out of bounds: " + i + "/" + j); 080 } 081 data[j * dimension + i] = newValue; 082 } 083 }