// Carnegie Mellon University
//   Information Networking Institute and
//   School of Computer Science
//
// Master Thesis: A Monitoring Tool for Overlay Network
// By: TungFai Chan and Annie Cheng
//
// File: AdjacencyMatrix.java
// Path: evetbase/routing
// Description: Adjacency matrix is used as a representation of packet
//              route spanning tree
// Formal Definition:
//      Let |V| = n.  The adjacency matrix of G is an n x n matrix A such that
//      Aij = 1 iff (vi, vj) is in E.  The ith row of the matrix is thus an
//      array of size n which has 1 in the jth position if there is an edge
//      leading from Vi to Vj, and a 0 otherwize


package eventbase.routing;

public class AdjacencyMatrix {

  private boolean [][] matrix;
  private int matrix_size;

  public AdjacencyMatrix(int size) {
    matrix_size = size;
    matrix = new boolean[size][size];
    for (int i = 0; i < size; i++)
      for (int j = 0; j < size; j++)
        matrix[i][j] = false;
  }

  // setCoordinate (i, j) means that there is an edge leading from vertex i
  // to vertex j on the Graph
  // TODO: maybe resize if row and col are out of boundary?
  public void setCoordinate(int row, int col) {
    matrix[row][col] = true;
  }

  public void cleanRow(int row) {
    for (int i = 0; i < matrix_size; i++ )
      matrix[row][i] = false;
  }

  // TODO: maybe return false and resize when row and col are out of boundary?
  public boolean getCoordinate (int row, int col) {
    return matrix[row][col];
  }

  public int size() {
    return matrix_size;
  }

  public void resize(int newsize) {
    boolean [][] new_matrix = new boolean[newsize][newsize];
    // copy
    for (int i = 0; i < matrix_size; i++)
      for (int j = 0; j < matrix_size; j++)
        new_matrix[i][j] = matrix[i][j];
    matrix = new_matrix;
    matrix_size = newsize;
  }

  void print () {
    for (int i = 0; i < matrix_size; i++) {
      for (int j = 0; j < matrix_size; j++) {
        System.out.print(matrix[i][j] + " ");
      }
      System.out.println();
    }
  }

}