// file: GraphIO.h
// pg 21, Algorithms in C++, Part 5, Robert Sedgewick
// Modified by Dawn Lawrie

// Packages related graph-processing functions together in a single class
// Uses standard in and standard out to scan and display graphs

#include <iostream>
#include <fstream>
#include <cmath>
#include "Graph.h"
#include "EdgeException.h"

using namespace std;

#ifndef IO_H
#define IO_H

template <class GraphType>
class GraphIO {
public:
  // Write the upper triangle of the graph to the output stream as
  // vertex pairs
  // @param data - the output stream to write to
  static void outputUpperTri(const GraphType &graph, ofstream &data);

  // Displays graph in a format consistent with Figure 17.7
  // @param graph - the graph to display
  static void show (const GraphType &graph);

  // Returns a graph in the specified format by reading edges (pairs of 
  // integers between 0 and V-1 (number vertices in the graph) from
  // standard input
  static void scanEZ(GraphType& graph, ifstream &input);

  // Returns a graph in the specified format by reading edges (pairs of 
  // symbols) from standard input. The symbol-table ADT function index
  // associates an integer with each symbol: on unsucessful search in the
  // table of size N it adds the symbol to the table with associated integer
  // N+1; on successful search, it simply returns the integer previously
  // associated with the symbol. 
  static void scan(GraphType& graph /*, SymbolTable &st*/);
};

// Returns a graph in the specified format by reading edges (pairs of 
// integers between 0 and V-1 (number vertices in the graph) from
// standard input
template <class GraphType>
void GraphIO<GraphType>::scanEZ(GraphType& graph, ifstream &input) {
  int min = 0;
  int max = graph.V()-1;
  int v, w;

  for (input >> v >> w; input && !input.eof(); input >> v >> w) {
    if ( min <= v && v <= max && min <= w && w <= max) 
      graph.insert(Edge(v,w));
    else {
      throw EdgeException("Vertex out of bounds");
    }
  }
}

// Displays graph in a format consistent with Figure 17.7
// @param graph - the graph to display
template <class GraphType>
void GraphIO<GraphType>::show(const GraphType &gr) {
  for (int s = 0; s < gr.V(); s++) {
    cout << s << ": ";

    // Iterator over each of the vertices
    typename GraphType::AdjIterator itr(gr, s);
    for (int t = itr.beg(); !itr.end(); t = itr.nxt()) {
      cout << t << " "; 
    }
    cout << endl;
  }
}


#endif
