com.hfg.util.collection.SymmetricMatrix Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of com_hfg Show documentation
Show all versions of com_hfg Show documentation
com.hfg xml, html, svg, and bioinformatics utility library
package com.hfg.util.collection;
import java.util.*;
//------------------------------------------------------------------------------
/**
A symmetric matrix (a square matrix with values that are symmetric with respect to the diagonal).
Ex:
1 5 -3
5 2 9
-3 9 8
* @author J. Alex Taylor, hairyfatguy.com
*/
//------------------------------------------------------------------------------
// com.hfg Library
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
// [email protected]
//------------------------------------------------------------------------------
public class SymmetricMatrix extends AbstractSparseMatrix
{
private OrderedSet mOrderedKeys;
private static int sDefaultInitialCapacity = 99;
//##########################################################################
// CONSTRUCTORS
//##########################################################################
//--------------------------------------------------------------------------
public SymmetricMatrix()
{
super();
init(sDefaultInitialCapacity);
}
//--------------------------------------------------------------------------
public SymmetricMatrix(int inInitialCapacity)
{
super(inInitialCapacity, inInitialCapacity);
init(inInitialCapacity);
}
//--------------------------------------------------------------------------
private void init(int inInitialCapacity)
{
mOrderedKeys = new OrderedSet(inInitialCapacity);
}
//##########################################################################
// PUBLIC METHODS
//##########################################################################
//--------------------------------------------------------------------------
@Override
public void put(K inRowKey, K inColKey, V inValue)
{
mOrderedKeys.add(inRowKey);
mOrderedKeys.add(inColKey);
K rowKey = inRowKey;
K colKey = inColKey;
int rowIndex = mOrderedKeys.indexOf(inRowKey);
int colIndex = mOrderedKeys.indexOf(inColKey);
if (rowIndex < colIndex)
{
K tmp = colKey;
colKey = rowKey;
rowKey = tmp;
}
super.put(rowKey, colKey, inValue);
}
//--------------------------------------------------------------------------
@Override
public V get(K inRowKey, K inColKey)
{
K rowKey = inRowKey;
K colKey = inColKey;
int rowIndex = mOrderedKeys.indexOf(inRowKey);
int colIndex = mOrderedKeys.indexOf(inColKey);
if (rowIndex < colIndex)
{
K tmp = colKey;
colKey = rowKey;
rowKey = tmp;
}
return super.get(rowKey, colKey);
}
//--------------------------------------------------------------------------
@Override
public String toString()
{
StringBuilder buffer = new StringBuilder();
int maxKeyLength = getMaxKeyLength();
String keyFormat = "%-" + maxKeyLength + "." + maxKeyLength + "s";
for (int i = 0; i < mOrderedKeys.size(); i++)
{
K key1 = mOrderedKeys.get(i);
buffer.append(String.format(keyFormat, key1));
for (int j = 0; j <= i; j++)
{
K key2 = mOrderedKeys.get(j);
buffer.append(String.format(" %.4f", get(key1, key2)));
}
buffer.append(System.getProperty("line.separator"));
}
return buffer.toString();
}
//--------------------------------------------------------------------------
@Override
public Set rowKeySet()
{
return keySet();
}
//--------------------------------------------------------------------------
@Override
public Set colKeySet()
{
return keySet();
}
//--------------------------------------------------------------------------
public Set keySet()
{
return new OrderedSet(mOrderedKeys);
}
//--------------------------------------------------------------------------
public void addKey(K inKey)
{
mOrderedKeys.add(inKey);
super.addRow(inKey);
super.addCol(inKey);
}
//--------------------------------------------------------------------------
@Override
public boolean containsRow(K inKey)
{
return containsKey(inKey);
}
//--------------------------------------------------------------------------
@Override
public boolean containsCol(K inKey)
{
return containsKey(inKey);
}
//--------------------------------------------------------------------------
public boolean containsKey(K inKey)
{
return mOrderedKeys.contains(inKey);
}
//--------------------------------------------------------------------------
@Override
public Map removeRow(K inKey)
{
return removeKey(inKey);
}
//--------------------------------------------------------------------------
@Override
public void removeCol(K inKey)
{
removeKey(inKey);
}
//--------------------------------------------------------------------------
public Map removeKey(K inKey)
{
mOrderedKeys.remove(inKey);
Map rowMap = super.removeRow(inKey);
super.removeCol(inKey);
return rowMap;
}
//--------------------------------------------------------------------------
public void removeKeys(Set inKeys)
{
if (inKeys != null)
{
mOrderedKeys.removeAll(inKeys);
for (K key : inKeys)
{
super.removeRow(key);
}
super.removeCols(inKeys);
}
}
//--------------------------------------------------------------------------
@Override
public void changeRowKey(K inOldKey, K inNewKey)
{
changeKey(inOldKey, inNewKey);
}
//--------------------------------------------------------------------------
@Override
public void changeColKey(K inOldKey, K inNewKey)
{
changeKey(inOldKey, inNewKey);
}
//--------------------------------------------------------------------------
public void changeKey(K inOldKey, K inNewKey)
{
int index = mOrderedKeys.indexOf(inOldKey);
if (index >= 0)
{
super.changeRowKey(inOldKey, inNewKey);
super.changeColKey(inOldKey, inNewKey);
mOrderedKeys.remove(inOldKey);
mOrderedKeys.add(index, inNewKey);
}
}
//--------------------------------------------------------------------------
@Override
public SymmetricMatrix clone()
{
return (SymmetricMatrix) super.clone();
}
//--------------------------------------------------------------------------
@Override
public Map getRow(K inKey)
{
return super.getRow(inKey);
}
//--------------------------------------------------------------------------
private int getMaxKeyLength()
{
int maxLength = 0;
for (K key : keySet())
{
if (key.toString().length() > maxLength)
{
maxLength = key.toString().length();
}
}
return maxLength;
}
}