⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 simplesymetricalmatrix.java

📁 dm s preparing process. In this case we use O distance.
💻 JAVA
字号:
/* created at 2005-12-23 */
package com.clustering.ds.matrix;

/**
 * 简单对称矩阵.
 * <p>
 * 这里将使用一维数组来存储对称矩阵,也就是说把矩阵的一 半元素存储在一个一维数组中
 * 
 * @author Avon
 * @version 0.9
 * @since 0.9
 * @see com.clustering.core.FuzzyMatrix
 */
public class SimpleSymetricalMatrix extends AbstractSimpleMatrix {
	protected Object values[];

	@Deprecated
	public SimpleSymetricalMatrix() {
		super();
		values = null;
	}

	public SimpleSymetricalMatrix(long bound) {
		super(bound);
		long size = bound * (bound + 1) / 2;
		if (size > Integer.MAX_VALUE)
			throw new IllegalArgumentException(
					"SimpleSymetricalMatrix is not enough for current application. Turn to ComplexSymetricalMatrix to resolve the problem.");
		values = new Object[(int) size];
	}

	public Object getElement(long row, long col) {
		return values[getIndex(col, row)];
	}

	public void setElement(long row, long col, Object value) {
		values[getIndex(row, col)] = value;
	}

	/*
	 * getElement(int col, int row)方法所返回的元素在一维数组中的的下标就是该方法所返回的值
	 */
	protected int getIndex(long row, long col) {
		if (null == values)
			throw new NullPointerException("Matrix is not initialized.");
		if (col >= bound || row >= bound)
			throw new ArrayIndexOutOfBoundsException("SimpleSymetricalMatrix is overflow while u access [".concat(
					new Long(col).toString()).concat(", ").concat(new Long(row).toString().concat("].")));
		// 当前的对称阵能使用一维数组表示
		int r = (int) row;
		int c = (int) col;
		if (r > c) {
			return r * (r + 1) / 2 + c;
		}
		return c * (c + 1) / 2 + r;
	}

	/**
	 * 最好不要使用这个方法,简单矩阵创建了就不要改变其大小,否则可能导致内存的重新分配和 大量数据记录的移动,替代的办法就是使用构造函数2
	 * 
	 * @param bound
	 */
	@Deprecated
	@Override
	public void setBound(long bound) {
		long size = bound * (bound + 1) / 2;
		if (size > Integer.MAX_VALUE)
			throw new IllegalArgumentException(
					"SimpleSymetricalMatrix is not enough for current application. Refer to ComplexSymetricalMatrix to resolve the problem.");
		// 如果尚未初始化过矩阵
		if (null == values) {
			values = new Object[(int) size];
			this.bound = bound;
			return;
		}
		/*
		 * 如果设置的bound小于原来的bound,那么保持不变,空间浪费就浪费点吧, 反正是SimpleMatrix,内存一定够
		 */

		if (this.bound > bound) {
			this.bound = bound;
			return;
		}
		/*
		 * 如果扩大了矩阵的空间,就得折腾了,还有一个办法就是把new Object[bound]放到
		 * set方法中,对于这里将使用的情况没有好处:(,一个很好的办法估计就是使用多个桶 来装,但是好像没有必要这么麻烦
		 */
		this.bound = bound;
		Object[] tmp = values;
		values = new Object[(int) size];
		for (int i = 0; i < tmp.length; i++) {
			values[i] = tmp;
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -