# source:lemon-0.x/doc/maps.dox@1801:049f42e44dee

Last change on this file since 1801:049f42e44dee was 1788:614ce2dd3cba, checked in by Balazs Dezso, 16 years ago

Some documentation modifications

File size: 3.8 KB
Line
1namespace lemon{
2/*!
3
4\page maps-page Maps
5
6Maps play a central role in LEMON. As their name suggests, they map a
7certain range of \e keys to certain \e values. Each map has two
8<tt>typedef</tt>'s to determine the types of keys and values, like this:
9
10\code
11  typedef Edge Key;
12  typedef double Value;
13\endcode
14
15A map can be
17\e writable (\ref lemon::concept::WriteMap "WriteMap") or both
19There also exists a special type of
20ReadWrite map called \ref lemon::concept::ReferenceMap "reference map".
22read and write the values of a key, a reference map
23can also give you a reference to the
25where it is stored.
26
27Each graph structure in LEMON provides two standard map templates called
28\c EdgeMap and \c NodeMap. Both are reference maps and you can easily
29assign data to the nodes and to the edges of the graph. For example if you
30have a graph \c g defined as
31\code
32  ListGraph g;
33\endcode
34and you want to assign a floating point value to each edge, you can do
35it like this.
36\code
37  ListGraph::EdgeMap<double> length(g);
38\endcode
39Note that you must give the underlying graph to the constructor.
40
41The value of a readable map can be obtained by <tt>operator[]</tt>.
42\code
43  d=length[e];
44\endcode
45where \c e is an instance of \c ListGraph::Edge.
46(Or anything else
47that converts to \c ListGraph::Edge, like  \c ListGraph::EdgeIt or
48\c ListGraph::OutEdgeIt etc.)
49
50There are two ways to assign a new value to a key
51
52- In case of a <em>reference map</em> <tt>operator[]</tt>
53gives you a reference to the
54value, thus you can use this.
55\code
56  length[e]=3.5;
57\endcode
58- <em>Writable maps</em> have
59a member function \c set(Key,const Value &)
60for this purpose.
61\code
62  length.set(e,3.5);
63\endcode
64
65The first case is more comfortable and if you store complex structures in your
66map, it might be more efficient. However, there are writable but
67not reference maps, so if you want to write a generic algorithm, you should
68insist on the second way.
69
70\section how-to-write-your-own-map How to Write Your Own Maps
71
73
74Readable maps are very frequently used as the input of an
75algorithm.  For this purpose the most straightforward way is the use of the
76default maps provided by LEMON's graph structures.
77Very often however, it is more
79
80You can find some examples below. In these examples \c Graph is the
81type of the particular graph structure you use.
82
83
84This simple map assigns \f$\pi\f$ to each edge.
85
86\code
87struct MyMap
88{
89  typedef double Value;
90  typedef Graph::Edge Key;
91  double operator[](Key e) const { return M_PI;}
92};
93\endcode
94
95An alternative way to define maps is to use \c MapBase
96
97\code
98struct MyMap : public MapBase<Graph::Edge,double>
99{
100  Value operator[](Key e) const { return M_PI;}
101};
102\endcode
103
104Here is a bit more complex example.
105It provides a length function obtained
106from a base length function shifted by a potential difference.
107
108\code
109class ReducedLengthMap  : public MapBase<Graph::Edge,double>
110{
111  const Graph &g;
112  const Graph::EdgeMap<double> &orig_len;
113  const Graph::NodeMap<double> &pot;
114
115public:
116  Value operator[](Key e) const {
117    return orig_len[e]-(pot[g.target(e)]-pot[g.source(e)]);
118  }
119
120  ReducedLengthMap(const Graph &_g,
121                   const Graph::EdgeMap &_o,
122                   const Graph::NodeMap &_p)
123    : g(_g), orig_len(_o), pot(_p) {};
124};
125\endcode
126
127Then, you can call e.g. Dijkstra algoritm on this map like this:
128\code
129  ...
130  ReducedLengthMap rm(g,len,pot);
131  Dijkstra<Graph,ReducedLengthMap> dij(g,rm);
132  dij.run(s);
133  ...
134\endcode
135
136
137\subsection write-maps Writable Maps
138
139To be written...
140
141\subsection side-effect-maps Maps with Side Effect
142
143To be written...
144
145*/
146}
Note: See TracBrowser for help on using the repository browser.