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