gravatar
deba@inf.elte.hu
deba@inf.elte.hu
New implementation of GRAPH_TYPEDEFS
0 3 0
default
3 files changed with 119 insertions and 14 deletions:
↑ Collapse diff ↑
Ignore white space 96 line context
1 1
/* -*- C++ -*-
2 2
 *
3 3
 * This file is a part of LEMON, a generic C++ optimization library
4 4
 *
5 5
 * Copyright (C) 2003-2008
6 6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 8
 *
9 9
 * Permission to use, modify and distribute this software is granted
10 10
 * provided that this copyright notice appears in all copies. For
11 11
 * precise terms see the accompanying LICENSE file.
12 12
 *
13 13
 * This software is provided "AS IS" with no warranty of any kind,
14 14
 * express or implied, and with no claim as to its suitability for any
15 15
 * purpose.
16 16
 *
17 17
 */
18 18

	
19 19
#ifndef LEMON_GRAPH_UTILS_H
20 20
#define LEMON_GRAPH_UTILS_H
21 21

	
22 22
#include <iterator>
23 23
#include <vector>
24 24
#include <map>
25 25
#include <cmath>
26 26
#include <algorithm>
27 27

	
28 28
#include <lemon/bits/invalid.h>
29 29
#include <lemon/bits/utility.h>
30 30
#include <lemon/maps.h>
31 31
#include <lemon/bits/traits.h>
32 32

	
33 33
#include <lemon/bits/alteration_notifier.h>
34 34
#include <lemon/bits/default_map.h>
35 35

	
36 36
///\ingroup gutils
37 37
///\file
38 38
///\brief Graph utilities.
39 39

	
40 40
namespace lemon {
41 41

	
42 42
  /// \addtogroup gutils
43 43
  /// @{
44 44

	
45
  namespace _graph_utils_bits {
46
    template <typename Graph>
47
    struct Node { typedef typename Graph::Node type; };
48

	
49
    template <typename Graph>
50
    struct NodeIt { typedef typename Graph::NodeIt type; };
51

	
52
    template <typename Graph>
53
    struct Arc { typedef typename Graph::Arc type; };
54

	
55
    template <typename Graph>
56
    struct ArcIt { typedef typename Graph::ArcIt type; };
57

	
58
    template <typename Graph>
59
    struct Edge { typedef typename Graph::Edge type; };
60

	
61
    template <typename Graph>
62
    struct EdgeIt { typedef typename Graph::EdgeIt type; };
63

	
64
    template <typename Graph>
65
    struct OutArcIt { typedef typename Graph::OutArcIt type; };
66

	
67
    template <typename Graph>
68
    struct InArcIt { typedef typename Graph::InArcIt type; };
69

	
70
    template <typename Graph>
71
    struct IncEdgeIt { typedef typename Graph::IncEdgeIt type; };
72

	
73
    template <typename Graph>
74
    struct BoolNodeMap { 
75
      typedef typename Graph::template NodeMap<bool> type; 
76
    };
77

	
78
    template <typename Graph>
79
    struct IntNodeMap { 
80
      typedef typename Graph::template NodeMap<int> type; 
81
    };
82

	
83
    template <typename Graph>
84
    struct DoubleNodeMap { 
85
      typedef typename Graph::template NodeMap<double> type; 
86
    };
87

	
88
    template <typename Graph>
89
    struct BoolArcMap { 
90
      typedef typename Graph::template ArcMap<bool> type; 
91
    };
92

	
93
    template <typename Graph>
94
    struct IntArcMap { 
95
      typedef typename Graph::template ArcMap<int> type; 
96
    };
97

	
98
    template <typename Graph>
99
    struct DoubleArcMap { 
100
      typedef typename Graph::template ArcMap<double> type; 
101
    };
102

	
103
    template <typename Graph>
104
    struct BoolEdgeMap { 
105
      typedef typename Graph::template EdgeMap<bool> type; 
106
    };
107

	
108
    template <typename Graph>
109
    struct IntEdgeMap { 
110
      typedef typename Graph::template EdgeMap<int> type; 
111
    };
112

	
113
    template <typename Graph>
114
    struct DoubleEdgeMap { 
115
      typedef typename Graph::template EdgeMap<double> type; 
116
    };
117

	
118
    
119
  }
120

	
45 121
  ///Creates convenience typedefs for the digraph types and iterators
46 122

	
47 123
  ///This \c \#define creates convenience typedefs for the following types
48 124
  ///of \c Digraph: \c Node,  \c NodeIt, \c Arc, \c ArcIt, \c InArcIt,
49 125
  ///\c OutArcIt, \c BoolNodeMap, \c IntNodeMap, \c DoubleNodeMap, 
50 126
  ///\c BoolArcMap, \c IntArcMap, \c DoubleArcMap. 
51 127
#define DIGRAPH_TYPEDEFS(Digraph)					\
52
  typedef Digraph::Node Node;						\
53
  typedef Digraph::NodeIt NodeIt;					\
54
  typedef Digraph::Arc Arc;						\
55
  typedef Digraph::ArcIt ArcIt;						\
56
  typedef Digraph::InArcIt InArcIt;					\
57
  typedef Digraph::OutArcIt OutArcIt
128
  typedef typename ::lemon::_graph_utils_bits::				\
129
  Node<Digraph>::type Node;						\
130
  typedef typename ::lemon::_graph_utils_bits::				\
131
  NodeIt<Digraph>::type	NodeIt;						\
132
  typedef typename ::lemon::_graph_utils_bits::				\
133
  Arc<Digraph>::type Arc;						\
134
  typedef typename ::lemon::_graph_utils_bits::				\
135
  ArcIt<Digraph>::type ArcIt;						\
136
  typedef typename ::lemon::_graph_utils_bits::				\
137
  OutArcIt<Digraph>::type OutArcIt;					\
138
  typedef typename ::lemon::_graph_utils_bits::				\
139
  InArcIt<Digraph>::type InArcIt;					\
140
  typedef typename ::lemon::_graph_utils_bits::				\
141
  BoolNodeMap<Digraph>::type BoolNodeMap;				\
142
  typedef typename ::lemon::_graph_utils_bits::				\
143
  IntNodeMap<Digraph>::type IntNodeMap;					\
144
  typedef typename ::lemon::_graph_utils_bits::				\
145
  DoubleNodeMap<Digraph>::type DoubleNodeMap;				\
146
  typedef typename ::lemon::_graph_utils_bits::				\
147
  BoolArcMap<Digraph>::type BoolArcMap;					\
148
  typedef typename ::lemon::_graph_utils_bits::				\
149
  IntArcMap<Digraph>::type IntArcMap;					\
150
  typedef typename ::lemon::_graph_utils_bits::				\
151
  DoubleArcMap<Digraph>::type DoubleArcMap
152

	
58 153

	
59 154
  ///Creates convenience typedefs for the graph types and iterators
60 155

	
61 156
  ///This \c \#define creates the same convenience typedefs as defined
62 157
  ///by \ref DIGRAPH_TYPEDEFS(Graph) and six more, namely it creates
63 158
  ///\c Edge, \c EdgeIt, \c IncEdgeIt, \c BoolEdgeMap, \c IntEdgeMap,
64 159
  ///\c DoubleEdgeMap.
65 160
#define GRAPH_TYPEDEFS(Graph)						\
66 161
  DIGRAPH_TYPEDEFS(Graph);						\
67
  typedef Graph::Edge Edge;						\
68
  typedef Graph::EdgeIt EdgeIt;						\
69
  typedef Graph::IncEdgeIt IncEdgeIt
162
  typedef typename ::lemon::_graph_utils_bits::				\
163
  Edge<Graph>::type Edge;						\
164
  typedef typename ::lemon::_graph_utils_bits::				\
165
  EdgeIt<Graph>::type EdgeIt;						\
166
  typedef typename ::lemon::_graph_utils_bits::				\
167
  IncEdgeIt<Graph>::type IncEdgeIt					\
168
  typedef typename ::lemon::_graph_utils_bits::				\
169
  BoolEdgeMap<Graph>::type BoolEdgeMap;					\
170
  typedef typename ::lemon::_graph_utils_bits::				\
171
  IntEdgeMap<Graph>::type IntEdgeMap;					\
172
  typedef typename ::lemon::_graph_utils_bits::				\
173
  DoubleEdgeMap<Graph>::type DoubleEdgeMap
174

	
70 175

	
71 176
  /// \brief Function to count the items in the graph.
72 177
  ///
73 178
  /// This function counts the items (nodes, arcs etc) in the graph.
74 179
  /// The complexity of the function is O(n) because
75 180
  /// it iterates on all of the items.
76 181
  template <typename Graph, typename Item>
77 182
  inline int countItems(const Graph& g) {
78 183
    typedef typename ItemSetTraits<Graph, Item>::ItemIt ItemIt;
79 184
    int num = 0;
80 185
    for (ItemIt it(g); it != INVALID; ++it) {
81 186
      ++num;
82 187
    }
83 188
    return num;
84 189
  }
85 190

	
86 191
  // Node counting:
87 192

	
88 193
  namespace _graph_utils_bits {
89 194
    
90 195
    template <typename Graph, typename Enable = void>
91 196
    struct CountNodesSelector {
92 197
      static int count(const Graph &g) {
93 198
        return countItems<Graph, typename Graph::Node>(g);
94 199
      }
95 200
    };
96 201

	
97 202
    template <typename Graph>
98 203
    struct CountNodesSelector<
99 204
      Graph, typename 
100 205
      enable_if<typename Graph::NodeNumTag, void>::type> 
101 206
    {
102 207
      static int count(const Graph &g) {
103 208
        return g.nodeNum();
104 209
      }
105 210
    };    
106 211
  }
107 212

	
108 213
  /// \brief Function to count the nodes in the graph.
109 214
  ///
110 215
  /// This function counts the nodes in the graph.
111 216
  /// The complexity of the function is O(n) but for some
112 217
  /// graph structures it is specialized to run in O(1).
113 218
  ///
114 219
  /// If the graph contains a \e nodeNum() member function and a 
115 220
  /// \e NodeNumTag tag then this function calls directly the member
116 221
  /// function to query the cardinality of the node set.
117 222
  template <typename Graph>
... ...
@@ -2011,97 +2116,97 @@
2011 2116
      for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
2012 2117
	_deg[it] = countOutArcs(_digraph, it);
2013 2118
      }      
2014 2119
    }
2015 2120

	
2016 2121
    virtual void clear() {
2017 2122
      for(typename Digraph::NodeIt it(_digraph); it != INVALID; ++it) {
2018 2123
	_deg[it] = 0;
2019 2124
      }
2020 2125
    }
2021 2126
  private:
2022 2127
    
2023 2128
    const Digraph& _digraph;
2024 2129
    AutoNodeMap _deg;
2025 2130
  };
2026 2131

	
2027 2132

	
2028 2133
  ///Dynamic arc look up between given endpoints.
2029 2134
  
2030 2135
  ///\ingroup gutils
2031 2136
  ///Using this class, you can find an arc in a digraph from a given
2032 2137
  ///source to a given target in amortized time <em>O(log d)</em>,
2033 2138
  ///where <em>d</em> is the out-degree of the source node.
2034 2139
  ///
2035 2140
  ///It is possible to find \e all parallel arcs between two nodes with
2036 2141
  ///the \c findFirst() and \c findNext() members.
2037 2142
  ///
2038 2143
  ///See the \ref ArcLookUp and \ref AllArcLookUp classes if your
2039 2144
  ///digraph is not changed so frequently.
2040 2145
  ///
2041 2146
  ///This class uses a self-adjusting binary search tree, Sleator's
2042 2147
  ///and Tarjan's Splay tree for guarantee the logarithmic amortized
2043 2148
  ///time bound for arc lookups. This class also guarantees the
2044 2149
  ///optimal time bound in a constant factor for any distribution of
2045 2150
  ///queries.
2046 2151
  ///
2047 2152
  ///\param G The type of the underlying digraph.  
2048 2153
  ///
2049 2154
  ///\sa ArcLookUp  
2050 2155
  ///\sa AllArcLookUp  
2051 2156
  template<class G>
2052 2157
  class DynArcLookUp 
2053 2158
    : protected ItemSetTraits<G, typename G::Arc>::ItemNotifier::ObserverBase
2054 2159
  {
2055 2160
  public:
2056 2161
    typedef typename ItemSetTraits<G, typename G::Arc>
2057 2162
    ::ItemNotifier::ObserverBase Parent;
2058 2163

	
2059
    DIGRAPH_TYPEDEFS(typename G);
2164
    DIGRAPH_TYPEDEFS(G);
2060 2165
    typedef G Digraph;
2061 2166

	
2062 2167
  protected:
2063 2168

	
2064 2169
    class AutoNodeMap : public DefaultMap<G, Node, Arc> {
2065 2170
    public:
2066 2171

	
2067 2172
      typedef DefaultMap<G, Node, Arc> Parent;
2068 2173

	
2069 2174
      AutoNodeMap(const G& digraph) : Parent(digraph, INVALID) {}
2070 2175
      
2071 2176
      virtual void add(const Node& node) {
2072 2177
	Parent::add(node);
2073 2178
	Parent::set(node, INVALID);
2074 2179
      }
2075 2180

	
2076 2181
      virtual void add(const std::vector<Node>& nodes) {
2077 2182
	Parent::add(nodes);
2078 2183
	for (int i = 0; i < int(nodes.size()); ++i) {
2079 2184
	  Parent::set(nodes[i], INVALID);
2080 2185
	}
2081 2186
      }
2082 2187

	
2083 2188
      virtual void build() {
2084 2189
	Parent::build();
2085 2190
	Node it;
2086 2191
	typename Parent::Notifier* nf = Parent::notifier();
2087 2192
	for (nf->first(it); it != INVALID; nf->next(it)) {
2088 2193
	  Parent::set(it, INVALID);
2089 2194
	}
2090 2195
      }
2091 2196
    };
2092 2197

	
2093 2198
    const Digraph &_g;
2094 2199
    AutoNodeMap _head;
2095 2200
    typename Digraph::template ArcMap<Arc> _parent;
2096 2201
    typename Digraph::template ArcMap<Arc> _left;
2097 2202
    typename Digraph::template ArcMap<Arc> _right;
2098 2203
    
2099 2204
    class ArcLess {
2100 2205
      const Digraph &g;
2101 2206
    public:
2102 2207
      ArcLess(const Digraph &_g) : g(_g) {}
2103 2208
      bool operator()(Arc a,Arc b) const 
2104 2209
      {
2105 2210
	return g.target(a)<g.target(b);
2106 2211
      }
2107 2212
    };
... ...
@@ -2448,97 +2553,97 @@
2448 2553
#endif
2449 2554
    {
2450 2555
      if (_right[a] != INVALID) {
2451 2556
	a = _right[a];
2452 2557
	while (_left[a] != INVALID) {
2453 2558
	  a = _left[a];
2454 2559
	}
2455 2560
	const_cast<DynArcLookUp&>(*this).splay(a);
2456 2561
      } else {
2457 2562
	while (_parent[a] != INVALID && _right[_parent[a]] ==  a) {
2458 2563
	  a = _parent[a];
2459 2564
	}
2460 2565
	if (_parent[a] == INVALID) {
2461 2566
	  return INVALID;
2462 2567
	} else {
2463 2568
	  a = _parent[a];
2464 2569
	  const_cast<DynArcLookUp&>(*this).splay(a);
2465 2570
	}
2466 2571
      }
2467 2572
      if (_g.target(a) == t) return a;
2468 2573
      else return INVALID;    
2469 2574
    }
2470 2575

	
2471 2576
  };
2472 2577

	
2473 2578
  ///Fast arc look up between given endpoints.
2474 2579
  
2475 2580
  ///\ingroup gutils
2476 2581
  ///Using this class, you can find an arc in a digraph from a given
2477 2582
  ///source to a given target in time <em>O(log d)</em>,
2478 2583
  ///where <em>d</em> is the out-degree of the source node.
2479 2584
  ///
2480 2585
  ///It is not possible to find \e all parallel arcs between two nodes.
2481 2586
  ///Use \ref AllArcLookUp for this purpose.
2482 2587
  ///
2483 2588
  ///\warning This class is static, so you should refresh() (or at least
2484 2589
  ///refresh(Node)) this data structure
2485 2590
  ///whenever the digraph changes. This is a time consuming (superlinearly
2486 2591
  ///proportional (<em>O(m</em>log<em>m)</em>) to the number of arcs).
2487 2592
  ///
2488 2593
  ///\param G The type of the underlying digraph.
2489 2594
  ///
2490 2595
  ///\sa DynArcLookUp
2491 2596
  ///\sa AllArcLookUp  
2492 2597
  template<class G>
2493 2598
  class ArcLookUp 
2494 2599
  {
2495 2600
  public:
2496
    DIGRAPH_TYPEDEFS(typename G);
2601
    DIGRAPH_TYPEDEFS(G);
2497 2602
    typedef G Digraph;
2498 2603

	
2499 2604
  protected:
2500 2605
    const Digraph &_g;
2501 2606
    typename Digraph::template NodeMap<Arc> _head;
2502 2607
    typename Digraph::template ArcMap<Arc> _left;
2503 2608
    typename Digraph::template ArcMap<Arc> _right;
2504 2609
    
2505 2610
    class ArcLess {
2506 2611
      const Digraph &g;
2507 2612
    public:
2508 2613
      ArcLess(const Digraph &_g) : g(_g) {}
2509 2614
      bool operator()(Arc a,Arc b) const 
2510 2615
      {
2511 2616
	return g.target(a)<g.target(b);
2512 2617
      }
2513 2618
    };
2514 2619
    
2515 2620
  public:
2516 2621
    
2517 2622
    ///Constructor
2518 2623

	
2519 2624
    ///Constructor.
2520 2625
    ///
2521 2626
    ///It builds up the search database, which remains valid until the digraph
2522 2627
    ///changes.
2523 2628
    ArcLookUp(const Digraph &g) :_g(g),_head(g),_left(g),_right(g) {refresh();}
2524 2629
    
2525 2630
  private:
2526 2631
    Arc refreshRec(std::vector<Arc> &v,int a,int b) 
2527 2632
    {
2528 2633
      int m=(a+b)/2;
2529 2634
      Arc me=v[m];
2530 2635
      _left[me] = a<m?refreshRec(v,a,m-1):INVALID;
2531 2636
      _right[me] = m<b?refreshRec(v,m+1,b):INVALID;
2532 2637
      return me;
2533 2638
    }
2534 2639
  public:
2535 2640
    ///Refresh the data structure at a node.
2536 2641

	
2537 2642
    ///Build up the search database of node \c n.
2538 2643
    ///
2539 2644
    ///It runs in time <em>O(d</em>log<em>d)</em>, where <em>d</em> is
2540 2645
    ///the number of the outgoing arcs of \c n.
2541 2646
    void refresh(Node n) 
2542 2647
    {
2543 2648
      std::vector<Arc> v;
2544 2649
      for(OutArcIt e(_g,n);e!=INVALID;++e) v.push_back(e);
... ...
@@ -2565,97 +2670,97 @@
2565 2670
    ///Find an arc between two nodes.
2566 2671
    
2567 2672
    ///Find an arc between two nodes in time <em>O(</em>log<em>d)</em>, where
2568 2673
    /// <em>d</em> is the number of outgoing arcs of \c s.
2569 2674
    ///\param s The source node
2570 2675
    ///\param t The target node
2571 2676
    ///\return An arc from \c s to \c t if there exists,
2572 2677
    ///\ref INVALID otherwise.
2573 2678
    ///
2574 2679
    ///\warning If you change the digraph, refresh() must be called before using
2575 2680
    ///this operator. If you change the outgoing arcs of
2576 2681
    ///a single node \c n, then
2577 2682
    ///\ref refresh(Node) "refresh(n)" is enough.
2578 2683
    ///
2579 2684
    Arc operator()(Node s, Node t) const
2580 2685
    {
2581 2686
      Arc e;
2582 2687
      for(e=_head[s];
2583 2688
	  e!=INVALID&&_g.target(e)!=t;
2584 2689
	  e = t < _g.target(e)?_left[e]:_right[e]) ;
2585 2690
      return e;
2586 2691
    }
2587 2692

	
2588 2693
  };
2589 2694

	
2590 2695
  ///Fast look up of all arcs between given endpoints.
2591 2696
  
2592 2697
  ///\ingroup gutils
2593 2698
  ///This class is the same as \ref ArcLookUp, with the addition
2594 2699
  ///that it makes it possible to find all arcs between given endpoints.
2595 2700
  ///
2596 2701
  ///\warning This class is static, so you should refresh() (or at least
2597 2702
  ///refresh(Node)) this data structure
2598 2703
  ///whenever the digraph changes. This is a time consuming (superlinearly
2599 2704
  ///proportional (<em>O(m</em>log<em>m)</em>) to the number of arcs).
2600 2705
  ///
2601 2706
  ///\param G The type of the underlying digraph.
2602 2707
  ///
2603 2708
  ///\sa DynArcLookUp
2604 2709
  ///\sa ArcLookUp  
2605 2710
  template<class G>
2606 2711
  class AllArcLookUp : public ArcLookUp<G>
2607 2712
  {
2608 2713
    using ArcLookUp<G>::_g;
2609 2714
    using ArcLookUp<G>::_right;
2610 2715
    using ArcLookUp<G>::_left;
2611 2716
    using ArcLookUp<G>::_head;
2612 2717

	
2613
    DIGRAPH_TYPEDEFS(typename G);
2718
    DIGRAPH_TYPEDEFS(G);
2614 2719
    typedef G Digraph;
2615 2720
    
2616 2721
    typename Digraph::template ArcMap<Arc> _next;
2617 2722
    
2618 2723
    Arc refreshNext(Arc head,Arc next=INVALID)
2619 2724
    {
2620 2725
      if(head==INVALID) return next;
2621 2726
      else {
2622 2727
	next=refreshNext(_right[head],next);
2623 2728
// 	_next[head]=next;
2624 2729
	_next[head]=( next!=INVALID && _g.target(next)==_g.target(head))
2625 2730
	  ? next : INVALID;
2626 2731
	return refreshNext(_left[head],head);
2627 2732
      }
2628 2733
    }
2629 2734
    
2630 2735
    void refreshNext()
2631 2736
    {
2632 2737
      for(NodeIt n(_g);n!=INVALID;++n) refreshNext(_head[n]);
2633 2738
    }
2634 2739
    
2635 2740
  public:
2636 2741
    ///Constructor
2637 2742

	
2638 2743
    ///Constructor.
2639 2744
    ///
2640 2745
    ///It builds up the search database, which remains valid until the digraph
2641 2746
    ///changes.
2642 2747
    AllArcLookUp(const Digraph &g) : ArcLookUp<G>(g), _next(g) {refreshNext();}
2643 2748

	
2644 2749
    ///Refresh the data structure at a node.
2645 2750

	
2646 2751
    ///Build up the search database of node \c n.
2647 2752
    ///
2648 2753
    ///It runs in time <em>O(d</em>log<em>d)</em>, where <em>d</em> is
2649 2754
    ///the number of the outgoing arcs of \c n.
2650 2755
    
2651 2756
    void refresh(Node n) 
2652 2757
    {
2653 2758
      ArcLookUp<G>::refresh(n);
2654 2759
      refreshNext(_head[n]);
2655 2760
    }
2656 2761
    
2657 2762
    ///Refresh the full data structure.
2658 2763

	
2659 2764
    ///Build up the full search database. In fact, it simply calls
2660 2765
    ///\ref refresh(Node) "refresh(n)" for each node \c n.
2661 2766
    ///
Ignore white space 96 line context
... ...
@@ -257,97 +257,97 @@
257 257
	while (is.get(c) && !isWhiteSpace(c)) {
258 258
	  if (c == '\\') 
259 259
	    c = readEscape(is);
260 260
	  os << c;
261 261
	}
262 262
	if (!is) {
263 263
	  is.clear();
264 264
	} else {
265 265
	  is.putback(c);
266 266
	}
267 267
      }
268 268
      str = os.str();
269 269
      return is;
270 270
    }
271 271

	
272 272
    std::istream& readIdentifier(std::istream& is, std::string& str) {
273 273
      std::ostringstream os;
274 274

	
275 275
      char c;
276 276
      is >> std::ws;
277 277
      
278 278
      if (!is.get(c))
279 279
	return is;
280 280

	
281 281
      if (!isIdentifierFirstChar(c))
282 282
	throw DataFormatError("Wrong char in identifier");
283 283
      
284 284
      os << c;
285 285
      
286 286
      while (is.get(c) && !isWhiteSpace(c)) {
287 287
	if (!isIdentifierChar(c)) 
288 288
	  throw DataFormatError("Wrong char in identifier");	  
289 289
	os << c;
290 290
      }
291 291
      if (!is) is.clear();
292 292
     
293 293
      str = os.str();
294 294
      return is;
295 295
    }
296 296
    
297 297
  }
298 298
  
299 299
  /// \e
300 300
  template <typename _Digraph>
301 301
  class DigraphReader {
302 302
  public:
303 303

	
304 304
    typedef _Digraph Digraph;
305
    DIGRAPH_TYPEDEFS(typename Digraph);
305
    DIGRAPH_TYPEDEFS(Digraph);
306 306
    
307 307
  private:
308 308

	
309 309

	
310 310
    std::istream* _is;
311 311
    bool local_is;
312 312

	
313 313
    Digraph& _digraph;
314 314

	
315 315
    std::string _nodes_caption;
316 316
    std::string _arcs_caption;
317 317
    std::string _attributes_caption;
318 318

	
319 319
    typedef std::map<std::string, Node> NodeIndex;
320 320
    NodeIndex _node_index;
321 321
    typedef std::map<std::string, Arc> ArcIndex;
322 322
    ArcIndex _arc_index;
323 323
    
324 324
    typedef std::vector<std::pair<std::string, 
325 325
      _reader_bits::MapStorageBase<Node>*> > NodeMaps;    
326 326
    NodeMaps _node_maps; 
327 327

	
328 328
    typedef std::vector<std::pair<std::string,
329 329
      _reader_bits::MapStorageBase<Arc>*> >ArcMaps;
330 330
    ArcMaps _arc_maps;
331 331

	
332 332
    typedef std::multimap<std::string, _reader_bits::ValueStorageBase*> 
333 333
      Attributes;
334 334
    Attributes _attributes;
335 335

	
336 336
    bool _use_nodes;
337 337
    bool _use_arcs;
338 338

	
339 339
    int line_num;
340 340
    std::istringstream line;
341 341

	
342 342
  public:
343 343

	
344 344
    /// \e
345 345
    DigraphReader(std::istream& is, Digraph& digraph) 
346 346
      : _is(&is), local_is(false), _digraph(digraph),
347 347
	_use_nodes(false), _use_arcs(false) {}
348 348

	
349 349
    /// \e
350 350
    DigraphReader(const std::string& fn, Digraph& digraph) 
351 351
      : _is(new std::ifstream(fn.c_str())), local_is(true), _digraph(digraph),
352 352
    	_use_nodes(false), _use_arcs(false) {}
353 353

	
Ignore white space 96 line context
... ...
@@ -192,97 +192,97 @@
192 192
	return;
193 193
      case '\v':
194 194
	os << "\\v";
195 195
	return;
196 196
      default:
197 197
	if (c < 0x20) {
198 198
	  os << '\\' << std::oct << static_cast<int>(c);
199 199
	} else {
200 200
	  os << c;
201 201
	}
202 202
	return;
203 203
      }     
204 204
    }
205 205

	
206 206
    bool requireEscape(const std::string& str) {
207 207
      std::istringstream is(str);
208 208
      char c;
209 209
      while (is.get(c)) {
210 210
	if (isWhiteSpace(c) || isEscaped(c)) {
211 211
	  return true;
212 212
	}
213 213
      }
214 214
      return false;
215 215
    }
216 216
    
217 217
    std::ostream& writeToken(std::ostream& os, const std::string& str) {
218 218

	
219 219
      if (requireEscape(str)) {
220 220
	os << '\"';
221 221
	for (std::string::const_iterator it = str.begin(); 
222 222
	     it != str.end(); ++it) {
223 223
	  writeEscape(os, *it);
224 224
	}	
225 225
	os << '\"';
226 226
      } else {
227 227
	os << str;
228 228
      }
229 229
      return os;
230 230
    }
231 231

	
232 232
  }
233 233
  
234 234
  /// \e
235 235
  template <typename _Digraph>
236 236
  class DigraphWriter {
237 237
  public:
238 238

	
239 239
    typedef _Digraph Digraph;
240
    DIGRAPH_TYPEDEFS(typename Digraph);
240
    DIGRAPH_TYPEDEFS(Digraph);
241 241
    
242 242
  private:
243 243

	
244 244

	
245 245
    std::ostream* _os;
246 246
    bool local_os;
247 247

	
248 248
    Digraph& _digraph;
249 249

	
250 250
    std::string _nodes_caption;
251 251
    std::string _arcs_caption;
252 252
    std::string _attributes_caption;
253 253
    
254 254
    typedef std::map<Node, std::string> NodeIndex;
255 255
    NodeIndex _node_index;
256 256
    typedef std::map<Arc, std::string> ArcIndex;
257 257
    ArcIndex _arc_index;
258 258

	
259 259
    typedef std::vector<std::pair<std::string, 
260 260
      _writer_bits::MapStorageBase<Node>* > > NodeMaps;    
261 261
    NodeMaps _node_maps; 
262 262

	
263 263
    typedef std::vector<std::pair<std::string, 
264 264
      _writer_bits::MapStorageBase<Arc>* > >ArcMaps;
265 265
    ArcMaps _arc_maps;
266 266

	
267 267
    typedef std::vector<std::pair<std::string, 
268 268
      _writer_bits::ValueStorageBase*> > Attributes;
269 269
    Attributes _attributes;
270 270

	
271 271
    bool _skip_nodes;
272 272
    bool _skip_arcs;
273 273

	
274 274
  public:
275 275

	
276 276
    /// \e
277 277
    DigraphWriter(std::ostream& is, Digraph& digraph) 
278 278
      : _os(&is), local_os(false), _digraph(digraph),
279 279
	_skip_nodes(false), _skip_arcs(false) {}
280 280

	
281 281
    /// \e
282 282
    DigraphWriter(const std::string& fn, Digraph& digraph) 
283 283
      : _os(new std::ofstream(fn.c_str())), local_os(true), _digraph(digraph),
284 284
	_skip_nodes(false), _skip_arcs(false) {}
285 285

	
286 286
    /// \e
287 287
    DigraphWriter(const char* fn, Digraph& digraph) 
288 288
      : _os(new std::ofstream(fn)), local_os(true), _digraph(digraph),
0 comments (0 inline)