#include "AdjacencyMatrix.h"
#include "Logger.h"
#include <fstream>

AdjacencyMatrix::AdjacencyMatrix( std::string _filename ) : filename( _filename ) {
  try {

    std::ifstream inputFile( filename.c_str() );
    if ( !inputFile )
      throw 1;
    inputFile >> std::skipws >> std::noboolalpha >> dimension;
    if ( inputFile.fail() )
      throw 2;
    if ( dimension < 1 )
      throw 3;

    data.assign( dimension, RowType( dimension ) ); //Allocate space
    for ( unsigned int row = 0; row < dimension; row++ )
      for ( unsigned int col = 0;  col < dimension;  col++ )
        if ( inputFile.good() ) {
          bool foo;
          inputFile >> foo;
          data[row][col] = foo;
        } else
          throw( 4 );
    good = true;

  } catch ( int exception ) {
    LOG( Logger::LOGLEVEL_LOW,
         "Could not open data file <%s>! Error code :: %d",
         filename.c_str(), exception );
    good = false;
    data.clear();
    dimension = -1;
    return;
  }
}

AdjacencyMatrix::MatrixType AdjacencyMatrix::getData() const {
  return(( good ) ? data : MatrixType() );
}

bool AdjacencyMatrix::isGood() const {
  return good;
}

unsigned int AdjacencyMatrix::getDimension() const {
  return dimension;
}

std::string AdjacencyMatrix::getFilename() const {
  return filename;
}
