... |
... |
@@ -33,49 +33,154 @@
|
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;
|
... |
... |
@@ -2047,25 +2152,25 @@
|
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) {
|
... |
... |
@@ -2484,25 +2589,25 @@
|
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) {}
|
... |
... |
@@ -2601,25 +2706,25 @@
|
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;
|