angel_io.cpp

Go to the documentation of this file.
00001 // $Id: angel_io.cpp,v 1.6 2008/02/28 16:21:06 gottschling Exp $
00002 
00003 #include "angel/include/angel_io.hpp"
00004 
00005 #include <fstream>
00006 #include <string.h>
00007 #include <cstdio>
00008 #include <vector>
00009 
00010 #include "angel/include/angel_tools.hpp"
00011 #include "angel/include/angel_exceptions.hpp"
00012 
00013 namespace angel {
00014 
00015   using namespace std;
00016   using namespace boost;
00017 
00018 int read_graph_eliad (const string& file_name_in, c_graph_t& cg, bool retry) {
00019 
00020   typedef c_graph_t::vertex_t vertex_t;
00021 
00022   string file_name(file_name_in);
00023   FILE* fin= fopen (file_name.c_str(), "r");
00024   while (fin == NULL) {
00025     string error_message= "File " + file_name + " with c-graph not found"; 
00026     if (retry) {
00027       cout << "File " << file_name << " of c-graph does not exist. \n"
00028            << "Please enter other filename (or \"abort\" to abort). \n";
00029       cin >> file_name; }
00030     if (!retry || file_name == "abort") 
00031       THROW_EXCEPT_MACRO (true, io_exception, error_message);
00032     fin= fopen (file_name.c_str(), "r"); }
00033 
00034   char line [80]; 
00035   c_graph_t::ew_t ew= get(edge_weight, cg); 
00036 
00037   fgets (line, 80, fin);
00038   while (!feof (fin) && !strpbrk (line, "0123456789")) fgets (line, 80, fin); // 1st line with numbers 
00039   char* cp= strpbrk (line, "0123456789"); 
00040   int nv, read_values= sscanf (cp, "%i", &nv); // number of vertices
00041   THROW_EXCEPT_MACRO (read_values != 1, io_exception, "Number of vertices expected");
00042   c_graph_t gtmp (0, nv, 0); // all vertices as intermediate for now
00043 
00044   fgets (line, 80, fin); // read 'CG = ['
00045 
00046   int edge_number= 0; // to give numbers to the edges
00047   while (!feof (fin)) {
00048     fgets (line, 80, fin);
00049     if (strchr (line, ']')) break; // end found
00050     int target, source, triviality; 
00051     read_values= sscanf (line, "%i %i %i", &target, &source, &triviality);
00052     THROW_EXCEPT_MACRO (read_values != 3, io_exception, "Three values per line expected");
00053     c_graph_t::edge_t edge; bool added; 
00054     tie (edge, added)= add_edge (source-1, target-1, edge_number++, gtmp); // fortran to c re-numbering
00055     ew [edge]= triviality;
00056   }
00057 
00058   // vertices with in-degree 0 are independent and with out-degree 0 dependent
00059   c_graph_t::vi_t vi, v_end;
00060   vector<vertex_t> indeps;
00061   for (boost::tie (vi, v_end)= vertices (gtmp); vi != v_end; ++vi) {
00062     int ind= in_degree (*vi, gtmp), outd= out_degree (*vi, gtmp);
00063     THROW_EXCEPT_MACRO (ind == 0 && outd == 0, io_exception, 
00064                      "Unconnected vertex in input graph");
00065     if (ind == 0) indeps.push_back (*vi);
00066     if (outd == 0) gtmp.dependents.push_back (*vi); }
00067 
00068   gtmp.X= indeps.size(); 
00069 
00070   c_graph_t gtmp2;
00071   independent_vertices_to_front (gtmp, indeps, gtmp2);
00072 
00073   gtmp2.check_initial ();
00074   cg.swap (gtmp2);
00075   return 0;
00076 }
00077 
00078 void write_face (std::ostream& stream, 
00079                  line_graph_t::face_t face,
00080                  const line_graph_t& lg) {
00081 
00082   line_graph_t::edge_t vij= source (face, lg), vjk= target (face, lg);
00083 
00084   line_graph_t::const_evn_t evn= get(vertex_name, lg);
00085   int i= evn[vij].first, j= evn[vij].second, k= evn[vjk].second;
00086   THROW_DEBUG_EXCEPT_MACRO (j != evn[vjk].first, consistency_exception,
00087                          "Adjacency corrupted in line graph"); 
00088 
00089   stream << '(' << vij << ", " << vjk
00090          << ")=(" << i << ", " << j << ", " << k << ')';
00091 }
00092 
00093 void write_face_vector (std::ostream& stream, const std::string& s, 
00094                         const std::vector<line_graph_t::face_t>& v,
00095                         const line_graph_t& lg) {
00096   stream << s << " (size = " << v.size() << ") is {";
00097 
00098   std::vector<line_graph_t::face_t>::const_iterator i= v.begin();
00099   // write first if exist
00100   if (i != v.end()) write_face (stream, *i++, lg); 
00101 
00102   // from second to last (if exist)
00103   for (; i != v.end(); ++i) {
00104     stream << ", ";
00105     write_face (stream, *i, lg); }
00106   stream << '}' << std::endl;
00107 }
00108 
00109 ofstream log_file;
00110 
00111 string numbered_filename (const string& basename, const string& suffix, 
00112                         int number, int width) {
00113   ostringstream ost;
00114   ost << basename; 
00115   ost.width(width); ost.fill('0'); ost << number;
00116   ost << '.' << suffix;
00117   return ost.str();
00118 }
00119 
00120 no_output_t no_output;
00121 
00122 string_stream_output_t cout_string_output (std::cout);
00123 
00124 vis_display_output_t cout_vis_display_output (std::cout);
00125 
00126 #ifdef USEXAIFBOOSTER
00127 
00128 void write_refillDependences (ostream& stream,
00129                               const refillDependenceMap_t& refillDependences) {
00130   stream << "current contents of refillDependences: " << endl;
00131   for (refillDependenceMap_t::const_iterator di = refillDependences.begin(); di != refillDependences.end(); di++) {
00132     stream <<  "(" << di->first.first << "," << di->first.second << ") -> { ";
00133     for (vertex_set_t::const_iterator vsi = di->second.begin(); vsi != di->second.end(); vsi++)
00134       stream << *vsi << " ";
00135     stream << "}" << endl;
00136   }
00137   stream << endl;
00138 } // end write_refillDependences()
00139 
00140 void writeVertexAndEdgeTypes (ostream& stream,
00141                               c_graph_t& angelLCG) {
00142   c_graph_t::vi_t vi, v_end;
00143   for (tie (vi, v_end) = vertices(angelLCG); vi != v_end; ++vi) {
00144     stream << "vertex " << *vi;
00145     if (vertex_type(*vi, angelLCG) == dependent) stream << " IS"; else stream << " is NOT";
00146     stream << " a dependent" << endl;
00147   }
00148   boost::property_map<c_graph_t, EdgeType>::type eType = get(EdgeType(), angelLCG);
00149   c_graph_t::ei_t ei, e_end;
00150   for (tie(ei, e_end) = edges(angelLCG); ei != e_end; ++ei) {
00151     stream << "edge " << *ei << " is a ";
00152     if (eType[*ei] == UNIT_EDGE)                stream << "UNIT edge" << endl;
00153     else if (eType[*ei] == CONSTANT_EDGE)       stream << "CONSTANT edge" << endl;
00154     else if (eType[*ei] == VARIABLE_EDGE)       stream << "VARIABLE edge" << endl;
00155   }
00156 } // end writeVertexAndEdgeTypes()
00157 
00158 #endif // USEXAIFBOOSTER
00159 
00160 } // namespace angel
00161 

Generated on Wed Mar 11 10:33:11 2009 for angel by  doxygen 1.5.3