Guest User

Sparse Matrix realisation

a guest
Aug 13th, 2010
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.19 KB | None | 0 0
  1. package sparseMatrix;
  2.  
  3. public class Matrix {
  4.     protected MNode[] pointers;
  5.     protected short rows;
  6.  
  7.     public Matrix(short columns, short rows) {
  8.         pointers = new MNode[columns];
  9.         this.rows = rows;
  10.     }
  11.  
  12.     public MNode[] getNodes() {
  13.         return pointers;
  14.     }
  15.  
  16.     public short getRows() {
  17.         return rows;
  18.     }
  19.  
  20.     public MNode getNode(short row, short col) {
  21.         MNode root = pointers[col];
  22.         MNode tmp;
  23.         if (row < root.row)
  24.             return null;
  25.         else if (row == root.row)
  26.             return root;
  27.         tmp = root.up;
  28.         while (tmp != root) {
  29.             if (row == tmp.row)
  30.                 return tmp;
  31.             if (row < tmp.row)
  32.                 return null;
  33.             else if (row == tmp.row)
  34.                 return tmp;
  35.             tmp = root.up;
  36.         }
  37.         return null;
  38.     }
  39.     /**
  40.      * Find and delete the element in matrix
  41.      * @param element
  42.      */
  43.     public void delete(MNode element) {
  44.         MNode cur = pointers[element.col];
  45.         while (cur.up != element) {
  46.             cur = element.up;
  47.         }
  48.         if (cur == element) {
  49.             pointers[element.col] = null;
  50.             return;
  51.         }
  52.         cur.up = element.up;
  53.     }
  54.     /**
  55.      * Insert a new MNode into matrix.
  56.      * It's unsafe, lacking checking of boundaries, so safety is up to programmer.
  57.      * @param value
  58.      * @param row
  59.      * @param col
  60.      */
  61.     public void insert(float value, short row, short col) {
  62.         MNode cell = new MNode(value);
  63.         cell.col = col;
  64.         cell.row = row;
  65.         MNode lCell = null;
  66.         MNode rCell = null;
  67.         MNode tmpCell;
  68.         // two way finding closest cell in row
  69.         MNode receiver = null;
  70.         for (int j = 1; j < Math.ceil(pointers.length / 2); j++) {
  71.             if (pointers[j] != null) {
  72.                 tmpCell = pointers[j];
  73.                 while (tmpCell.up != pointers[j])
  74.                     if ((receiver == null) && (tmpCell.row == cell.row))
  75.                         receiver = tmpCell;
  76.             }
  77.             if ((col - j > -1) && (null == lCell))
  78.                 lCell = receiver;
  79.             else if ((col + j < pointers.length) && (null == rCell))
  80.                 rCell = receiver;
  81.             if((null != rCell) && (null != lCell))
  82.                 break;
  83.         }
  84.        
  85.         // looking for closest cells in column
  86.         tmpCell = pointers[col];
  87.         if (tmpCell == null) {
  88.             cell.up = cell;
  89.             return;
  90.         }
  91.         while (tmpCell.up != pointers[col]) {
  92.             if((tmpCell.up.row > row) && (tmpCell.row < row)) {
  93.                 cell.up = tmpCell.up;
  94.                 tmpCell.up = cell;
  95.                 break;
  96.             }
  97.         }
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment