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