Guest User

Untitled

a guest
Apr 24th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.72 KB | None | 0 0
  1.     public E get(int i, int j){
  2.             /*
  3.              * I'm assuming that this XMatrix has the following:
  4.              * size: how big the matrix is
  5.              * top: array containing the elements at the top of the matrix
  6.              * diagonals: array containing the diagonal elements
  7.              * bottom: array containing the elements at the bottom of the matrix
  8.              *
  9.              * Also assuming row-major mapping for the diagonal elements.
  10.              */
  11.              
  12.             /* the return value */
  13.             E ret = null;
  14.            
  15.             /* make sure we're not given a 0 or something too big (no 0s because matrices start at 1) */
  16.             if(1 <= i && i <= size){
  17.                 if(1 <= j && j <= size){
  18.                     if(i == 1){
  19.                         /* on the top row */
  20.                         ret = top[j - 1];
  21.                     } else if(i == size){
  22.                         /* on the bottom row */
  23.                         ret = bottom[j - 1];
  24.                     } else{
  25.                         /* check to see if it's on the X */
  26.                         if((i == j) || (i * 2 == j)){
  27.                             /*
  28.                              * The diagonals matrix has matrix.size rows
  29.                              * and matrix.size - 2 columns. Each row has 2 elements,
  30.                              * except for the middle row which only has 1.
  31.                              * If an element is in the middle of the matrix,
  32.                              * then it is in the middle of the diagonals array.
  33.                              * Else it's on a diagonal. Since there are 2 elements in each row,
  34.                              * minus one for the middle row
  35.                              */
  36.                             int middle = (size + 1) / 2;
  37.                             if(i == middle && j == middle)
  38.                                 ret = diagonals[middle];
  39.                             /*
  40.                              * I know these next four lines aren't correct,
  41.                              * but I spent way to long thinking about them
  42.                              */
  43.                             else if(j < middle)
  44.                                 ret = diagonals[middle - ((i * 2) + (j - 1))];
  45.                             else if(j > middle)
  46.                                 ret = diagonals[((i * 2) + j - 1) - middle];
  47.                         }
  48.                     }
  49.                 }
  50.             }
  51.            
  52.             return ret;
  53.         }
Add Comment
Please, Sign In to add comment