#include "AdjacencyList.h"
#include "Logger.h"

#include <string>
#include <boost/format.hpp>

AdjacencyList::AdjacencyList( const AdjacencyMatrix adj ) {
  try {

    filename = adj.getFilename();
    if ( !( adj.isGood() ) )
      throw 10;
    dimension = adj.getDimension();
    std::vector<std::vector<bool> > adjData = adj.getData();
    data.assign( dimension, ListType() );
    for ( unsigned int row = 0; row < dimension; row++ )
      for ( unsigned int col = 0;  col < dimension;  col++ )
        if ( adjData[row][col] == true )
          data[row].push_back( col );
    good = true;

  } catch ( int exception ) {
    LOG( Logger::LOGLEVEL_LOW,
         "Could not convert adjacency matrix to adjacency list! Error code :: %d",
         exception );
    good = false;
    data.clear();
    dimension = -1;
    return;
  }
}

void AdjacencyList::print() const {
  int v = 0;
  std::string buffer;
  for ( AdjListType::const_iterator vertex = data.begin() ; vertex != data.end(); vertex++ ) {
    buffer.clear();
    buffer += boost::str( boost::format( "Adjacency List\t%d\t" ) % ( ++v ) );
    for ( ListType::const_iterator it = vertex->begin(); it != vertex->end(); it++ )
      buffer += boost::str( boost::format( "%6d" ) % ( *it + 1 ) );
    LOG( Logger::LOGLEVEL_HIGH, buffer.c_str() );
  }
}
