Fix skip*() functions is GraphWriters (Ticket #107)
authorBalazs Dezso <deba@inf.elte.hu>
Tue, 01 Jul 2008 21:21:49 +0200
changeset 18533e45a9b868c
parent 184 716b220697a0
child 186 9159de5e9657
Fix skip*() functions is GraphWriters (Ticket #107)
lemon/lgf_writer.h
     1.1 --- a/lemon/lgf_writer.h	Thu Mar 27 16:27:23 2008 +0100
     1.2 +++ b/lemon/lgf_writer.h	Tue Jul 01 21:21:49 2008 +0200
     1.3 @@ -603,6 +603,7 @@
     1.4      /// The \c \@nodes section will be not written to the stream.
     1.5      DigraphWriter& skipNodes() {
     1.6        LEMON_ASSERT(!_skip_nodes, "Multiple usage of skipNodes() member");
     1.7 +      _skip_nodes = true;
     1.8        return *this;
     1.9      }
    1.10  
    1.11 @@ -611,6 +612,7 @@
    1.12      /// The \c \@arcs section will be not written to the stream.
    1.13      DigraphWriter& skipArcs() {
    1.14        LEMON_ASSERT(!_skip_arcs, "Multiple usage of skipArcs() member");
    1.15 +      _skip_arcs = true;
    1.16        return *this;
    1.17      }
    1.18  
    1.19 @@ -678,6 +680,30 @@
    1.20        }
    1.21      }
    1.22  
    1.23 +    void createNodeIndex() {
    1.24 +      _writer_bits::MapStorageBase<Node>* label = 0;
    1.25 +      for (typename NodeMaps::iterator it = _node_maps.begin();
    1.26 +	   it != _node_maps.end(); ++it) {
    1.27 +        if (it->first == "label") {
    1.28 +	  label = it->second;
    1.29 +	  break;
    1.30 +	}
    1.31 +      }
    1.32 +
    1.33 +      if (label == 0) {
    1.34 +	for (NodeIt n(_digraph); n != INVALID; ++n) {
    1.35 +	  std::ostringstream os;
    1.36 +	  os << _digraph.id(n);
    1.37 +	  _node_index.insert(std::make_pair(n, os.str()));	  
    1.38 +	}	
    1.39 +      } else {
    1.40 +	for (NodeIt n(_digraph); n != INVALID; ++n) {
    1.41 +	  std::string value = label->get(n);	  
    1.42 +	  _node_index.insert(std::make_pair(n, value));
    1.43 +	}
    1.44 +      }
    1.45 +    }
    1.46 +
    1.47      void writeArcs() {
    1.48        _writer_bits::MapStorageBase<Arc>* label = 0;
    1.49        for (typename ArcMaps::iterator it = _arc_maps.begin();
    1.50 @@ -745,6 +771,30 @@
    1.51        }
    1.52      }
    1.53  
    1.54 +    void createArcIndex() {
    1.55 +      _writer_bits::MapStorageBase<Arc>* label = 0;
    1.56 +      for (typename ArcMaps::iterator it = _arc_maps.begin();
    1.57 +	   it != _arc_maps.end(); ++it) {
    1.58 +        if (it->first == "label") {
    1.59 +	  label = it->second;
    1.60 +	  break;
    1.61 +	}
    1.62 +      }
    1.63 +
    1.64 +      if (label == 0) {
    1.65 +	for (ArcIt a(_digraph); a != INVALID; ++a) {
    1.66 +	  std::ostringstream os;
    1.67 +	  os << _digraph.id(a);
    1.68 +	  _arc_index.insert(std::make_pair(a, os.str()));	  
    1.69 +	}	
    1.70 +      } else {
    1.71 +	for (ArcIt a(_digraph); a != INVALID; ++a) {
    1.72 +	  std::string value = label->get(a);	  
    1.73 +	  _arc_index.insert(std::make_pair(a, value));
    1.74 +	}
    1.75 +      }
    1.76 +    }
    1.77 +
    1.78      void writeAttributes() {
    1.79        if (_attributes.empty()) return;
    1.80        *_os << "@attributes";
    1.81 @@ -771,9 +821,13 @@
    1.82      void run() {
    1.83        if (!_skip_nodes) {
    1.84  	writeNodes();
    1.85 +      } else {
    1.86 +	createNodeIndex();
    1.87        }
    1.88        if (!_skip_arcs) {      
    1.89  	writeArcs();
    1.90 +      } else {
    1.91 +	createArcIndex();
    1.92        }
    1.93        writeAttributes();
    1.94      }
    1.95 @@ -1115,6 +1169,7 @@
    1.96      /// The \c \@nodes section will be not written to the stream.
    1.97      GraphWriter& skipNodes() {
    1.98        LEMON_ASSERT(!_skip_nodes, "Multiple usage of skipNodes() member");
    1.99 +      _skip_nodes = true;
   1.100        return *this;
   1.101      }
   1.102  
   1.103 @@ -1123,6 +1178,7 @@
   1.104      /// The \c \@edges section will be not written to the stream.
   1.105      GraphWriter& skipEdges() {
   1.106        LEMON_ASSERT(!_skip_edges, "Multiple usage of skipEdges() member");
   1.107 +      _skip_edges = true;
   1.108        return *this;
   1.109      }
   1.110  
   1.111 @@ -1190,6 +1246,30 @@
   1.112        }
   1.113      }
   1.114  
   1.115 +    void createNodeIndex() {
   1.116 +      _writer_bits::MapStorageBase<Node>* label = 0;
   1.117 +      for (typename NodeMaps::iterator it = _node_maps.begin();
   1.118 +	   it != _node_maps.end(); ++it) {
   1.119 +        if (it->first == "label") {
   1.120 +	  label = it->second;
   1.121 +	  break;
   1.122 +	}
   1.123 +      }
   1.124 +
   1.125 +      if (label == 0) {
   1.126 +	for (NodeIt n(_graph); n != INVALID; ++n) {
   1.127 +	  std::ostringstream os;
   1.128 +	  os << _graph.id(n);
   1.129 +	  _node_index.insert(std::make_pair(n, os.str()));	  
   1.130 +	}	
   1.131 +      } else {
   1.132 +	for (NodeIt n(_graph); n != INVALID; ++n) {
   1.133 +	  std::string value = label->get(n);	  
   1.134 +	  _node_index.insert(std::make_pair(n, value));
   1.135 +	}
   1.136 +      }
   1.137 +    }
   1.138 +
   1.139      void writeEdges() {
   1.140        _writer_bits::MapStorageBase<Edge>* label = 0;
   1.141        for (typename EdgeMaps::iterator it = _edge_maps.begin();
   1.142 @@ -1257,6 +1337,30 @@
   1.143        }
   1.144      }
   1.145  
   1.146 +    void createEdgeIndex() {
   1.147 +      _writer_bits::MapStorageBase<Edge>* label = 0;
   1.148 +      for (typename EdgeMaps::iterator it = _edge_maps.begin();
   1.149 +	   it != _edge_maps.end(); ++it) {
   1.150 +        if (it->first == "label") {
   1.151 +	  label = it->second;
   1.152 +	  break;
   1.153 +	}
   1.154 +      }
   1.155 +
   1.156 +      if (label == 0) {
   1.157 +	for (EdgeIt e(_graph); e != INVALID; ++e) {
   1.158 +	  std::ostringstream os;
   1.159 +	  os << _graph.id(e);
   1.160 +	  _edge_index.insert(std::make_pair(e, os.str()));	  
   1.161 +	}	
   1.162 +      } else {
   1.163 +	for (EdgeIt e(_graph); e != INVALID; ++e) {
   1.164 +	  std::string value = label->get(e);	  
   1.165 +	  _edge_index.insert(std::make_pair(e, value));
   1.166 +	}
   1.167 +      }
   1.168 +    }
   1.169 +
   1.170      void writeAttributes() {
   1.171        if (_attributes.empty()) return;
   1.172        *_os << "@attributes";
   1.173 @@ -1283,9 +1387,13 @@
   1.174      void run() {
   1.175        if (!_skip_nodes) {
   1.176  	writeNodes();
   1.177 +      } else {
   1.178 +	createNodeIndex();
   1.179        }
   1.180        if (!_skip_edges) {      
   1.181  	writeEdges();
   1.182 +      } else {
   1.183 +	createEdgeIndex();
   1.184        }
   1.185        writeAttributes();
   1.186      }