home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / diverses / leda / src / graph / _g_inout.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-15  |  3.7 KB  |  181 lines

  1. /*******************************************************************************
  2. +
  3. +  LEDA  2.1.1                                                 11-15-1991
  4. +
  5. +
  6. +  _g_inout.c
  7. +
  8. +
  9. +  Copyright (c) 1991  by  Max-Planck-Institut fuer Informatik
  10. +  Im Stadtwald, 6600 Saarbruecken, FRG     
  11. +  All rights reserved.
  12. *******************************************************************************/
  13.  
  14.  
  15. #include <LEDA/graph.h>
  16.  
  17.  
  18. //--------------------------------------------------------------------------
  19. // graph i/o
  20. //--------------------------------------------------------------------------
  21.  
  22.  
  23. void put_int(filebuf& fb, int n) 
  24. { register char* A = (char*)&n;
  25.            char* E = A+4;
  26.  
  27.   while (A<E) fb.sputc(*(A++)); 
  28. }
  29.  
  30. int get_int(istream& from) 
  31. { int n;
  32.   register char* A = (char*)&n;
  33.            char* E = A+4;
  34.   while (A<E) from.get(*(A++));
  35.   return n;
  36. }
  37.  
  38.  
  39.  
  40. void graph::write(string s) const
  41. { filebuf fbuf;
  42.  
  43.   if (fbuf.open(s.cstring(),output)==0) 
  44.    error_handler(1,"write: cannot open file");
  45.     
  46.   ostream out(&fbuf);  // ~ostream: flushes file buffer
  47.  
  48.   int* A = new int[max_node_index+2];
  49.  
  50.   // nodes get numbers from 1 to |V| (trouble with 0)
  51.  
  52.   int count = 1;
  53.  
  54.   out << "LEDA.GRAPH\n"; 
  55.   out << node_type() << "\n";
  56.   out << edge_type() << "\n";
  57.   
  58.   //put_int(fbuf,V.length());
  59.  
  60.   out << V.length() << "\n";
  61.  
  62.   node v;
  63.   forall_nodes(v,*this)
  64.   { //put_int(fbuf,int(v->contents));  // node entry (integer)
  65.     write_node_entry(out,v->contents);
  66.     out << "\n";
  67.     A[v->i_name] = count++;
  68.    }
  69.  
  70.   //put_int(fbuf,E.length());
  71.   out << E.length() << "\n";
  72.   edge e;
  73.   forall_edges(e,*this)
  74.   { //put_int(fbuf,A[e->s->i_name]);
  75.     //put_int(fbuf,A[e->t->i_name]);
  76.     //put_int(fbuf,int(e->contents)); // edge entry (integer)
  77.     out << A[e->s->i_name] << " " << A[e->t->i_name] << " ";
  78.     write_edge_entry(out,e->contents);
  79.     out << "\n";
  80.    }
  81. delete A;
  82. }
  83.  
  84. int graph::read(string s)
  85. { filebuf fbuf;
  86.   int result = 0;
  87.  
  88.   if (fbuf.open(s.cstring(),input)==0)  return 1;
  89.  
  90.   istream in(&fbuf);
  91.  
  92.   clear();
  93.  
  94.   edge e;
  95.   int n,i,v,w;
  96.  
  97.   string d_type,n_type,e_type;
  98.  
  99.   in >> d_type;
  100.  
  101.  
  102.   if (d_type != "LEDA.GRAPH") return 3;
  103.  
  104.   in >> n_type >> e_type;
  105.  
  106.   in >> n;         // number of nodes
  107.   read_line(in);
  108.  
  109.   node* A = new node[n+1];
  110.  
  111.   if (n_type != node_type())
  112.   { if (node_type() != "void") result = 2;   // incompatible node types
  113.     for (i=1; i<=n; i++) 
  114.     { A[i] = new_node();
  115.       read_line(in);
  116.      }
  117.    }
  118.   else
  119.     for (i=1; i<=n; i++) 
  120.     { A[i] = new_node();
  121.       read_node_entry(in,A[i]->contents);
  122.      }
  123.   
  124.   in >> n;       // number of edges
  125.  
  126.   if (e_type != edge_type())
  127.   { if (edge_type() != "void") result = 2;   // incompatible edge types
  128.     while (n--) { in >> v >> w;
  129.                   e = new_edge(A[v],A[w]); 
  130.                   read_line(in);
  131.                  }
  132.    }
  133.   else
  134.    while (n--) { in >> v >> w;
  135.                  e = new_edge(A[v],A[w]); 
  136.                  read_edge_entry(in,e->contents);
  137.                 }
  138.  
  139.   delete A;
  140.   return result;
  141. }
  142.  
  143.  
  144. void graph::print_node(node v,ostream& o) const
  145. { if (super() != 0)
  146.      super()->print_node(node(graph::inf(v)),o);
  147.   else 
  148.      { o << "[" << index(v) <<"]" ;
  149.        print_node_entry(o,v->contents);
  150.       }
  151. }
  152.  
  153. void graph::print_edge(edge e,ostream& o) const
  154. { if (super() != 0)
  155.      super()->print_edge(edge(graph::inf(e)),o);
  156.   else 
  157.      { o <<   "[" << e->s->i_name << "]--";
  158.        print_edge_entry(o,e->contents);
  159.        o << "-->[" << e->t->i_name << "]";
  160.       }
  161. }
  162.  
  163. void graph::print(string s, ostream& out) const
  164. { node v; 
  165.   edge e;
  166.   out << s << "\n";
  167.   forall_nodes(v,*this)
  168.   { print_node(v,out);
  169.     out << " : ";
  170.     forall(e,v->adj_edges) 
  171.       { print_edge(e,out); 
  172.         out << " "; 
  173.        }
  174.     out << "\n"; 
  175.    }
  176.   // out << "\n space: " << space() << " bytes \n";
  177.   out << "\n";
  178. }
  179.  
  180.