001package com.hfg.util.collection;
002
003
004
005//------------------------------------------------------------------------------
006/**
007 Symmetric matrix specifically for Numbers.
008 <div>
009  @author J. Alex Taylor, hairyfatguy.com
010 </div>
011 */
012//------------------------------------------------------------------------------
013// com.hfg Library
014//
015// This library is free software; you can redistribute it and/or
016// modify it under the terms of the GNU Lesser General Public
017// License as published by the Free Software Foundation; either
018// version 2.1 of the License, or (at your option) any later version.
019//
020// This library is distributed in the hope that it will be useful,
021// but WITHOUT ANY WARRANTY; without even the implied warranty of
022// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
023// Lesser General Public License for more details.
024//
025// You should have received a copy of the GNU Lesser General Public
026// License along with this library; if not, write to the Free Software
027// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
028//
029// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
030// jataylor@hairyfatguy.com
031//------------------------------------------------------------------------------
032public class SymmetricNumberMatrix<K extends Comparable, V extends Number & Comparable> extends SymmetricMatrix<K, V>
033{
034
035   //##########################################################################
036   // CONSTRUCTORS
037   //##########################################################################
038
039   //--------------------------------------------------------------------------
040   public SymmetricNumberMatrix()
041   {
042      super();
043   }
044
045   //--------------------------------------------------------------------------
046   public SymmetricNumberMatrix(int inInitialCapacity)
047   {
048      super(inInitialCapacity);
049   }
050
051   //##########################################################################
052   // PUBLIC METHODS
053   //##########################################################################
054
055   //--------------------------------------------------------------------------
056   public SymmetricNumberMatrix<K, V> clone()
057   {
058      return (SymmetricNumberMatrix<K, V>) super.clone();
059   }
060
061   //--------------------------------------------------------------------------
062   public Float getSumForKey(K inKey)
063   {
064      // To find all values for this key we need to go over and down
065      int matrixRowIndex = getIndexForKey(inKey);
066
067      MatrixRow matrixRow = mMatrix.get(matrixRowIndex);
068
069      Float sum = 0.0f;
070      for (V value : matrixRow.getData())
071      {
072         if (value != null)
073         {
074            sum += value.floatValue();
075         }
076      }
077
078      // Now go down
079      for (int i = matrixRowIndex + 1; i < mMatrix.size(); i++)
080      {
081         MatrixRow additionalRow = mMatrix.get(i);
082
083         V value = additionalRow.getData().get(matrixRowIndex);
084         if (value != null)
085         {
086            sum += value.floatValue();
087         }
088      }
089
090      return sum;
091   }
092}