Changeset 1133:9fd485470fee in lemon-0.x
- Timestamp:
- 02/07/05 11:48:14 (20 years ago)
- Branch:
- default
- Phase:
- public
- Convert:
- svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@1532
- Location:
- src/work/deba
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
src/work/deba/graph_reader.h
r1115 r1133 74 74 75 75 76 // Readers and ReaderTraits77 76 /// \brief Standard ReaderTraits for the GraphReader class. 78 77 /// 79 /// 80 78 /// Standard ReaderTraits for the GraphReader class. 79 /// It defines standard reading method for all type of value. 81 80 struct DefaultReaderTraits { 82 81 82 /// \brief Template class for reading an value. 83 /// 84 /// Template class for reading an value. 83 85 template <typename _Value> 84 86 struct Reader { 87 /// The value type. 85 88 typedef _Value Value; 89 /// \brief Reads a value from the given stream. 90 /// 91 /// Reads a value from the given stream. 86 92 void read(std::istream& is, Value& value) { 87 93 if (!(is >> value)) … … 90 96 }; 91 97 98 /// The reader class for the not needed maps. 92 99 typedef Reader<std::string> DefaultReader; 93 100 94 101 }; 95 102 103 /// \brief Reader class for quoted strings. 104 /// 105 /// Reader class for quoted strings. It can process the escape 106 /// sequences in the string. 96 107 class QuotedStringReader { 97 108 public: 98 109 typedef std::string Value; 99 110 111 /// \brief Constructor for the reader. 112 /// 113 /// Constructor for the reader. If the given parameter is true 114 /// the reader processes the escape sequences. 100 115 QuotedStringReader(bool _escaped = true) : escaped(_escaped) {} 101 116 117 /// \brief Reads a quoted string from the given stream. 118 /// 119 /// Reads a quoted string from the given stream. 102 120 void read(std::istream& is, std::string& value) { 103 121 char c; … … 146 164 { 147 165 int code; 148 if (!is.get(c) || !isHex(c)) throw DataFormatException("Escape format exception"); 166 if (!is.get(c) || !isHex(c)) 167 throw DataFormatException("Escape format exception"); 149 168 else if (code = valueHex(c), !is.get(c) || !isHex(c)) is.putback(c); 150 169 else code = code * 16 + valueHex(c); … … 154 173 { 155 174 int code; 156 if (!isOct(c)) throw DataFormatException("Escape format exception"); 157 else if (code = valueOct(c), !is.get(c) || !isOct(c)) is.putback(c); 158 else if (code = code * 8 + valueOct(c), !is.get(c) || !isOct(c)) is.putback(c); 175 if (!isOct(c)) 176 throw DataFormatException("Escape format exception"); 177 else if (code = valueOct(c), !is.get(c) || !isOct(c)) 178 is.putback(c); 179 else if (code = code * 8 + valueOct(c), !is.get(c) || !isOct(c)) 180 is.putback(c); 159 181 else code = code * 8 + valueOct(c); 160 182 return code; … … 172 194 173 195 static bool isHex(char c) { 174 return ('0' <= c && c <= '9') || ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z'); 196 return ('0' <= c && c <= '9') || 197 ('a' <= c && c <= 'z') || 198 ('A' <= c && c <= 'Z'); 175 199 } 176 200 … … 184 208 }; 185 209 186 187 188 189 190 // Graph reader 191 210 /// \brief The graph reader class. 211 /// 212 /// The reader class for the graph input. 213 /// \see graph-io-page 192 214 template <typename _Graph, typename _ReaderTraits = DefaultReaderTraits> 193 215 class GraphReader { … … 201 223 typedef typename ReaderTraits::DefaultReader DefaultReader; 202 224 225 /// \brief Construct a new GraphReader. 226 /// 227 /// Construct a new GraphReader. It reads from the given map, 228 /// it constructs the given map and it use the given reader as the 229 /// default skipper. 203 230 GraphReader(std::istream& _is, Graph& _graph, 204 231 const DefaultReader& _reader = DefaultReader()) 205 232 : is(_is), graph(_graph), nodeSkipper(_reader), edgeSkipper(_reader) {} 206 233 207 234 /// \brief Destruct the graph reader. 235 /// 236 /// Destruct the graph reader. 208 237 ~GraphReader() { 209 238 … … 220 249 } 221 250 222 // Node map rules 223 251 /// \brief Add a new node map reader command for the reader. 252 /// 253 /// Add a new node map reader command for the reader. 224 254 template <typename Map> 225 255 GraphReader& addNodeMap(std::string name, Map& map) { … … 228 258 } 229 259 260 /// \brief Add a new node map reader command for the reader. 261 /// 262 /// Add a new node map reader command for the reader. 230 263 template <typename Reader, typename Map> 231 264 GraphReader& addNodeMap(std::string name, Map& map, … … 239 272 } 240 273 274 /// \brief Add a new node map skipper command for the reader. 275 /// 276 /// Add a new node map skipper command for the reader. 241 277 template <typename Reader> 242 278 GraphReader& skipNodeMap(std::string name, … … 250 286 } 251 287 252 // Edge map rules 253 288 /// \brief Add a new edge map reader command for the reader. 289 /// 290 /// Add a new edge map reader command for the reader. 254 291 template <typename Map> 255 292 GraphReader& addEdgeMap(std::string name, Map& map) { … … 259 296 260 297 298 /// \brief Add a new edge map reader command for the reader. 299 /// 300 /// Add a new edge map reader command for the reader. 261 301 template <typename Reader, typename Map> 262 302 GraphReader& addEdgeMap(std::string name, Map& map, … … 270 310 } 271 311 312 /// \brief Add a new edge map skipper command for the reader. 313 /// 314 /// Add a new edge map skipper command for the reader. 272 315 template <typename Reader> 273 316 GraphReader& skipEdgeMap(std::string name, … … 281 324 } 282 325 283 // Node rules 326 /// \brief Add a new labeled node reader for the reader. 327 /// 328 /// Add a new labeled node reader for the reader. 284 329 GraphReader& addNode(std::string name, Node& node) { 285 330 if (node_readers.find(name) != node_readers.end()) { … … 290 335 } 291 336 292 // Edge rules 293 337 /// \brief Add a new labeled edge reader for the reader. 338 /// 339 /// Add a new labeled edge reader for the reader. 294 340 GraphReader& addEdge(std::string name, Edge& edge) { 295 341 if (edge_readers.find(name) != edge_readers.end()) { … … 300 346 } 301 347 302 void read() { 348 /// \brief Executes the reader commands. 349 /// 350 /// Executes the reader commands. 351 void run() { 303 352 int line_num = 0; 304 353 std::auto_ptr<InverterBase<Node> > nodeInverter; -
src/work/deba/graph_writer.h
r1115 r1133 34 34 namespace lemon { 35 35 36 /// \brief Standard WriterTraits for the GraphWriter class. 37 /// 38 /// Standard WriterTraits for the GraphWriter class. 39 /// It defines standard writing method for all type of value. 36 40 struct DefaultWriterTraits { 37 41 42 /// \brief Template class for writing an value. 43 /// 44 /// Template class for writing an value. 38 45 template <typename _Value> 39 46 struct Writer { 47 /// The value type. 40 48 typedef _Value Value; 41 49 50 /// \brief Writes a value from the given stream. 51 /// 52 /// Writes a value from the given stream. 42 53 void write(std::ostream& os, const Value& value) { 43 54 os << value << '\t'; … … 48 59 49 60 61 /// \brief Writer class for quoted strings. 62 /// 63 /// Writer class for quoted strings. It can process the escape 64 /// sequences in the string. 50 65 class QuotedStringWriter { 51 66 public: 52 67 typedef std::string Value; 53 68 69 /// \brief Constructor for the writer. 70 /// 71 /// Constructor for the writer. If the given parameter is true 72 /// the writer creates escape sequences from special characters. 54 73 QuotedStringWriter(bool _escaped = true) : escaped(_escaped) {} 55 74 75 /// \brief Writes a quoted string from the given stream. 76 /// 77 /// Writes a quoted string from the given stream. 56 78 void write(std::ostream& os, const std::string& value) { 57 79 os << "\""; … … 118 140 }; 119 141 120 // Graph writer121 142 143 /// \brief The graph writer class. 144 /// 145 /// The writer class for the graph output. 146 /// \see graph-io-page 122 147 template <typename _Graph, typename _WriterTraits = DefaultWriterTraits> 123 148 class GraphWriter { … … 131 156 132 157 typedef _WriterTraits WriterTraits; 133 158 159 /// \brief Construct a new GraphWriter. 160 /// 161 /// Construct a new GraphWriter. It writes from the given map, 162 /// it constructs the given map and it use the given writer as the 163 /// default skipper. 134 164 GraphWriter(std::ostream& _os, Graph& _graph) : os(_os), graph(_graph) {} 135 165 136 166 167 /// \brief Destruct the graph writer. 168 /// 169 /// Destruct the graph writer. 137 170 ~GraphWriter() { 138 171 for (typename NodeMapWriters::iterator it = node_map_writers.begin(); … … 150 183 // Node map rules 151 184 185 /// \brief Add a new node map writer command for the writer. 186 /// 187 /// Add a new node map writer command for the writer. 152 188 template <typename Map> 153 189 GraphWriter& addNodeMap(std::string name, const Map& map) { … … 156 192 } 157 193 194 /// \brief Add a new node map writer command for the writer. 195 /// 196 /// Add a new node map writer command for the writer. 158 197 template <typename Writer, typename Map> 159 198 GraphWriter& addNodeMap(std::string name, const Map& map, 160 199 const Writer& writer = Writer()) { 161 // if (node_map_writers.find(name) != node_map_writers.end()) {162 // throw Exception() << "Multiple write rule for node map: "163 // << name;164 // }165 200 node_map_writers.push_back( 166 201 make_pair(name, new MapWriter<Node, Map, Writer>(map, writer))); … … 170 205 // Edge map rules 171 206 207 /// \brief Add a new edge map writer command for the writer. 208 /// 209 /// Add a new edge map writer command for the writer. 172 210 template <typename Map> 173 211 GraphWriter& addEdgeMap(std::string name, const Map& map) { 174 return addEdgeMap<typename WriterTraits::template Writer<typename Map::Value>, Map>(name, map); 175 } 176 177 212 return addEdgeMap<typename WriterTraits::template Writer< 213 typename Map::Value>, Map>(name, map); 214 } 215 216 217 /// \brief Add a new edge map writer command for the writer. 218 /// 219 /// Add a new edge map writer command for the writer. 178 220 template <typename Writer, typename Map> 179 GraphWriter& addEdgeMap(std::string name, const Map& map, const Writer& writer = Writer()) { 180 // if (edge_map_writers.find(name) != edge_map_writers.end()) { 181 // throw Exception() << "Multiple write rule for edge map: " << name; 182 // } 183 edge_map_writers.push_back(make_pair(name, new MapWriter<Edge, Map, Writer>(map, writer))); 221 GraphWriter& addEdgeMap(std::string name, 222 const Map& map, const Writer& writer = Writer()) { 223 edge_map_writers.push_back(make_pair(name, 224 new MapWriter<Edge, Map, Writer>(map, writer))); 184 225 return *this; 185 226 } 186 227 187 // Node rules 228 /// \brief Add a new labeled node writer for the writer. 229 /// 230 /// Add a new labeled node writer for the writer. 188 231 GraphWriter& addNode(std::string name, const Node& node) { 189 // if (node_writers.find(name) != node_writers.end()) {190 // throw Exception() << "Multiple write rule for node";191 // }192 232 node_writers.push_back(make_pair(name, node)); 193 233 return *this; 194 234 } 195 235 196 // Edge rules 197 236 /// \brief Add a new labeled edge writer for the writer. 237 /// 238 /// Add a new labeled edge writer for the writer. 198 239 GraphWriter& addEdge(std::string name, const Edge& edge) { 199 // if (edge_writers.find(name) != edge_writers.end()) {200 // throw Exception() << "Multiple write rule for edge";201 // }202 240 edge_writers.push_back(make_pair(name, edge)); 203 241 return *this; 204 242 } 205 243 206 void write() { 244 /// \brief Executes the writer commands. 245 /// 246 /// Executes the writer commands. 247 void run() { 207 248 writeNodeSet(); 208 249 writeEdgeSet(); -
src/work/deba/map_utils.h
r1115 r1133 24 24 namespace lemon { 25 25 26 /// \addtogroup gutils26 /// \addtogroup mutils 27 27 /// @{ 28 29 28 30 29 /// \brief General inversable map type. … … 34 33 /// and if a key is setted to a new value then store it 35 34 /// in the inverse map. 35 /// \param _Graph The graph type. 36 /// \param _Map The map to extend with inversable functionality. 36 37 template < 37 38 typename _Graph, … … 44 45 45 46 typedef _Map Map; 47 /// The key type of InversableMap (Node, Edge, UndirEdge). 46 48 typedef typename _Map::Key Key; 49 /// The value type of the InversableMap. 47 50 typedef typename _Map::Value Value; 48 51 typedef std::map<Value, Key> InverseMap; … … 61 64 void set(const Key& key, const Value& val) { 62 65 Value oldval = Map::operator[](key); 63 typename InverseMap::iterator it = inv _map.find(oldval);64 if (it != inv _map.end() && it->second == key) {65 inv _map.erase(it);66 typename InverseMap::iterator it = invMap.find(oldval); 67 if (it != invMap.end() && it->second == key) { 68 invMap.erase(it); 66 69 } 67 inv _map.insert(make_pair(val, key));70 invMap.insert(make_pair(val, key)); 68 71 Map::set(key, val); 69 72 } 70 73 71 ConstReference operator[](const Key&) const { 74 /// \brief The getter function of the map. 75 /// 76 /// It gives back the value associated with the key. 77 ConstReference operator[](const Key& key) const { 72 78 return Map::operator[](key); 73 79 } 74 80 75 virtual void add(const Key&) { 81 /// \brief Add a new key to the map. 82 /// 83 /// Add a new key to the map. It is called by the 84 /// \c AlterationNotifier. 85 virtual void add(const Key& key) { 76 86 Map::add(key); 77 87 } 78 88 79 virtual void erase(const Key&) { 89 /// \brief Erase the key from the map. 90 /// 91 /// Erase the key to the map. It is called by the 92 /// \c AlterationNotifier. 93 virtual void erase(const Key& key) { 80 94 Value val = Map::operator[](key); 81 typename InverseMap::iterator it = inv _map.find(val);82 if (it != inv _map.end() && it->second == key) {95 typename InverseMap::iterator it = invMap.find(val); 96 if (it != invMap.end() && it->second == key) { 83 97 invMap.erase(it); 84 98 } … … 86 100 } 87 101 102 /// \brief Clear the keys from the map and inverse map. 103 /// 104 /// Clear the keys from the map and inverse map. It is called by the 105 /// \c AlterationNotifier. 106 virtual void clear() { 107 invMap.clear(); 108 Map::clear(); 109 } 110 111 /// \brief It gives back the just readeable inverse map. 112 /// 113 /// It gives back the just readeable inverse map. 88 114 const InverseMap& inverse() const { 89 return inv _map;115 return invMap; 90 116 } 91 117 92 118 93 119 private: 94 InverseMap inv _map;120 InverseMap invMap; 95 121 }; 96 122 … … 136 162 } 137 163 164 /// \brief Add a new key to the map. 165 /// 166 /// Add a new key to the map. It is called by the 167 /// \c AlterationNotifier. 138 168 virtual void add(const Item& item) { 139 169 Map::add(item); 140 Map::set(item, inv_map.size()); 141 inv_map.push_back(item); 142 } 143 170 Map::set(item, invMap.size()); 171 invMap.push_back(item); 172 } 173 174 /// \brief Erase the key from the map. 175 /// 176 /// Erase the key to the map. It is called by the 177 /// \c AlterationNotifier. 144 178 virtual void erase(const Item& item) { 145 Map::set(inv _map.back(), Map::operator[](item));146 inv _map[Map::operator[](item)] = inv_map.back();179 Map::set(invMap.back(), Map::operator[](item)); 180 invMap[Map::operator[](item)] = invMap.back(); 147 181 Map::erase(item); 148 182 } 149 183 184 /// \brief Build the unique map. 185 /// 186 /// Build the unique map. It is called by the 187 /// \c AlterationNotifier. 150 188 virtual void build() { 151 189 Map::build(); 152 190 Item it; 153 191 for (getGraph()->first(it); it != INVALID; getGraph()->next(it)) { 154 Map::set(it, inv _map.size());155 inv _map.push_back(it);192 Map::set(it, invMap.size()); 193 invMap.push_back(it); 156 194 } 157 195 } 158 196 197 /// \brief Clear the keys from the map. 198 /// 199 /// Clear the keys from the map. It is called by the 200 /// \c AlterationNotifier. 159 201 virtual void clear() { 160 inv _map.clear();202 invMap.clear(); 161 203 Map::clear(); 162 204 } … … 173 215 /// Gives back the inverse of the map. 174 216 const InverseMap inverse() const { 175 return inv _map;217 return invMap; 176 218 } 177 219 178 220 private: 179 vector<Item> inv _map;221 vector<Item> invMap; 180 222 }; 181 223
Note: See TracChangeset
for help on using the changeset viewer.