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