COIN-OR::LEMON - Graph Library

source: lemon-0.x/lemon/bits/default_map.h @ 1961:8e19ca944727

Last change on this file since 1961:8e19ca944727 was 1961:8e19ca944727, checked in by Balazs Dezso, 18 years ago

Bug fix

File size: 16.5 KB
RevLine 
[906]1/* -*- C++ -*-
2 *
[1956]3 * This file is a part of LEMON, a generic C++ optimization library
4 *
5 * Copyright (C) 2003-2006
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
[906]8 *
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
12 *
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
15 * purpose.
16 *
17 */
18
[921]19#ifndef LEMON_DEFAULT_MAP_H
20#define LEMON_DEFAULT_MAP_H
[822]21
22
[1307]23#include <lemon/bits/array_map.h>
24#include <lemon/bits/vector_map.h>
[822]25
[1946]26///\ingroup graphmapfactory
[822]27///\file
[946]28///\brief Graph maps that construct and destruct
[822]29///their elements dynamically.
30
[921]31namespace lemon {
[822]32
33
[1267]34  template <typename _Graph, typename _Item, typename _Value>
[946]35  struct DefaultMapSelector {
[1267]36    typedef ArrayMap<_Graph, _Item, _Value> Map;
[946]37  };
[822]38
[946]39  // bool
[1267]40  template <typename _Graph, typename _Item>
41  struct DefaultMapSelector<_Graph, _Item, bool> {
[980]42    typedef VectorMap<_Graph, _Item, bool> Map;
[946]43  };
[822]44
[946]45  // char
[1267]46  template <typename _Graph, typename _Item>
47  struct DefaultMapSelector<_Graph, _Item, char> {
[980]48    typedef VectorMap<_Graph, _Item, char> Map;
[946]49  };
[822]50
[1267]51  template <typename _Graph, typename _Item>
52  struct DefaultMapSelector<_Graph, _Item, signed char> {
[980]53    typedef VectorMap<_Graph, _Item, signed char> Map;
[946]54  };
[822]55
[1267]56  template <typename _Graph, typename _Item>
57  struct DefaultMapSelector<_Graph, _Item, unsigned char> {
[980]58    typedef VectorMap<_Graph, _Item, unsigned char> Map;
[946]59  };
[822]60
61
[946]62  // int
[1267]63  template <typename _Graph, typename _Item>
64  struct DefaultMapSelector<_Graph, _Item, signed int> {
[980]65    typedef VectorMap<_Graph, _Item, signed int> Map;
[946]66  };
[822]67
[1267]68  template <typename _Graph, typename _Item>
69  struct DefaultMapSelector<_Graph, _Item, unsigned int> {
[980]70    typedef VectorMap<_Graph, _Item, unsigned int> Map;
[946]71  };
[822]72
73
[946]74  // short
[1267]75  template <typename _Graph, typename _Item>
76  struct DefaultMapSelector<_Graph, _Item, signed short> {
[980]77    typedef VectorMap<_Graph, _Item, signed short> Map;
[946]78  };
[822]79
[1267]80  template <typename _Graph, typename _Item>
81  struct DefaultMapSelector<_Graph, _Item, unsigned short> {
[980]82    typedef VectorMap<_Graph, _Item, unsigned short> Map;
[946]83  };
84
85
86  // long
[1267]87  template <typename _Graph, typename _Item>
88  struct DefaultMapSelector<_Graph, _Item, signed long> {
[980]89    typedef VectorMap<_Graph, _Item, signed long> Map;
[946]90  };
91
[1267]92  template <typename _Graph, typename _Item>
93  struct DefaultMapSelector<_Graph, _Item, unsigned long> {
[980]94    typedef VectorMap<_Graph, _Item, unsigned long> Map;
[946]95  };
96
97  // \todo handling long long type
98
99
100  // float
[1267]101  template <typename _Graph, typename _Item>
102  struct DefaultMapSelector<_Graph, _Item, float> {
[980]103    typedef VectorMap<_Graph, _Item, float> Map;
[946]104  };
105
106
107  // double
[1267]108  template <typename _Graph, typename _Item>
109  struct DefaultMapSelector<_Graph, _Item, double> {
[980]110    typedef VectorMap<_Graph, _Item,  double> Map;
[946]111  };
112
113
114  // long double
[1267]115  template <typename _Graph, typename _Item>
116  struct DefaultMapSelector<_Graph, _Item, long double> {
[980]117    typedef VectorMap<_Graph, _Item, long double> Map;
[946]118  };
119
120
121  // pointer
[1267]122  template <typename _Graph, typename _Item, typename _Ptr>
123  struct DefaultMapSelector<_Graph, _Item, _Ptr*> {
[980]124    typedef VectorMap<_Graph, _Item, _Ptr*> Map;
[946]125  };
126
[1669]127  /// \e
[1267]128  template <
129    typename _Graph,
130    typename _Item,
131    typename _Value>
132  class DefaultMap
133    : public DefaultMapSelector<_Graph, _Item, _Value>::Map {
[946]134  public:
[1267]135    typedef typename DefaultMapSelector<_Graph, _Item, _Value>::Map Parent;
136    typedef DefaultMap<_Graph, _Item, _Value> Map;
[946]137   
138    typedef typename Parent::Graph Graph;
[987]139    typedef typename Parent::Value Value;
[946]140
[980]141    DefaultMap(const Graph& _g) : Parent(_g) {}
[987]142    DefaultMap(const Graph& _g, const Value& _v) : Parent(_g, _v) {}
[1669]143
[946]144  };
145
146
[1669]147  /// \e
[946]148  template <typename _Base>
[1669]149  class MappableGraphExtender : public _Base {
[946]150  public:
151
[1669]152    typedef MappableGraphExtender<_Base> Graph;
[946]153    typedef _Base Parent;
154
155    typedef typename Parent::Node Node;
156    typedef typename Parent::NodeIt NodeIt;
157
158    typedef typename Parent::Edge Edge;
159    typedef typename Parent::EdgeIt EdgeIt;
160
161   
162    template <typename _Value>
[1267]163    class NodeMap
164      : public IterableMapExtender<DefaultMap<Graph, Node, _Value> > {
[946]165    public:
[1669]166      typedef MappableGraphExtender Graph;
[1267]167      typedef IterableMapExtender<DefaultMap<Graph, Node, _Value> > Parent;
[946]168
[980]169      NodeMap(const Graph& _g)
170        : Parent(_g) {}
[1022]171      NodeMap(const Graph& _g, const _Value& _v)
[980]172        : Parent(_g, _v) {}
[1669]173
[1672]174      NodeMap& operator=(const NodeMap& cmap) {
175        return operator=<NodeMap>(cmap);
176      }
177
178
[1669]179      /// \brief Template assign operator.
180      ///
181      /// The given parameter should be conform to the ReadMap
182      /// concecpt and could be indiced by the current item set of
183      /// the NodeMap. In this case the value for each item
184      /// is assigned by the value of the given ReadMap.
185      template <typename CMap>
186      NodeMap& operator=(const CMap& cmap) {
187        checkConcept<concept::ReadMap<Node, _Value>, CMap>();
188        const typename Parent::Graph* graph = Parent::getGraph();
189        Node it;
190        for (graph->first(it); it != INVALID; graph->next(it)) {
191          Parent::set(it, cmap[it]);
192        }
193        return *this;
194      }
195
[946]196    };
197
198    template <typename _Value>
[1267]199    class EdgeMap
200      : public IterableMapExtender<DefaultMap<Graph, Edge, _Value> > {
[946]201    public:
[1669]202      typedef MappableGraphExtender Graph;
[1267]203      typedef IterableMapExtender<DefaultMap<Graph, Edge, _Value> > Parent;
[946]204
[980]205      EdgeMap(const Graph& _g)
206        : Parent(_g) {}
[1022]207      EdgeMap(const Graph& _g, const _Value& _v)
[980]208        : Parent(_g, _v) {}
[1669]209
[1672]210      EdgeMap& operator=(const EdgeMap& cmap) {
211        return operator=<EdgeMap>(cmap);
212      }
213
[1669]214      template <typename CMap>
215      EdgeMap& operator=(const CMap& cmap) {
216        checkConcept<concept::ReadMap<Edge, _Value>, CMap>();
217        const typename Parent::Graph* graph = Parent::getGraph();
218        Edge it;
219        for (graph->first(it); it != INVALID; graph->next(it)) {
220          Parent::set(it, cmap[it]);
221        }
222        return *this;
223      }
[946]224    };
225   
226  };
227
[1669]228  /// \e
[1022]229  template <typename _Base>
[1842]230  class MappableEdgeSetExtender : public _Base {
231  public:
232
233    typedef MappableEdgeSetExtender<_Base> Graph;
234    typedef _Base Parent;
235
236    typedef typename Parent::Edge Edge;
237    typedef typename Parent::EdgeIt EdgeIt;
238
239    template <typename _Value>
240    class EdgeMap
241      : public IterableMapExtender<DefaultMap<Graph, Edge, _Value> > {
242    public:
243      typedef MappableEdgeSetExtender Graph;
244      typedef IterableMapExtender<DefaultMap<Graph, Edge, _Value> > Parent;
245
246      EdgeMap(const Graph& _g)
247        : Parent(_g) {}
248      EdgeMap(const Graph& _g, const _Value& _v)
249        : Parent(_g, _v) {}
250
251      EdgeMap& operator=(const EdgeMap& cmap) {
252        return operator=<EdgeMap>(cmap);
253      }
254
255      template <typename CMap>
256      EdgeMap& operator=(const CMap& cmap) {
257        checkConcept<concept::ReadMap<Edge, _Value>, CMap>();
258        const typename Parent::Graph* graph = Parent::getGraph();
259        Edge it;
260        for (graph->first(it); it != INVALID; graph->next(it)) {
261          Parent::set(it, cmap[it]);
262        }
263        return *this;
264      }
265    };
266   
267  };
268
269  /// \e
270  template <typename _Base>
[1909]271  class MappableUGraphExtender :
[1669]272    public MappableGraphExtender<_Base> {
[1022]273  public:
274
[1909]275    typedef MappableUGraphExtender Graph;
[1669]276    typedef MappableGraphExtender<_Base> Parent;
[1022]277
[1909]278    typedef typename Parent::UEdge UEdge;
[1022]279
280    template <typename _Value>
[1909]281    class UEdgeMap
282      : public IterableMapExtender<DefaultMap<Graph, UEdge, _Value> > {
[1022]283    public:
[1909]284      typedef MappableUGraphExtender Graph;
[1267]285      typedef IterableMapExtender<
[1909]286        DefaultMap<Graph, UEdge, _Value> > Parent;
[1022]287
[1909]288      UEdgeMap(const Graph& _g)
[1022]289        : Parent(_g) {}
[1909]290      UEdgeMap(const Graph& _g, const _Value& _v)
[1022]291        : Parent(_g, _v) {}
[1669]292
[1909]293      UEdgeMap& operator=(const UEdgeMap& cmap) {
294        return operator=<UEdgeMap>(cmap);
[1672]295      }
296
[1669]297      template <typename CMap>
[1909]298      UEdgeMap& operator=(const CMap& cmap) {
299        checkConcept<concept::ReadMap<UEdge, _Value>, CMap>();
[1669]300        const typename Parent::Graph* graph = Parent::getGraph();
[1909]301        UEdge it;
[1669]302        for (graph->first(it); it != INVALID; graph->next(it)) {
303          Parent::set(it, cmap[it]);
304        }
305        return *this;
306      }
[1022]307    };
308
309
310  };
[822]311
[1842]312  /// \e
313  template <typename _Base>
[1909]314  class MappableUEdgeSetExtender :
[1842]315    public MappableEdgeSetExtender<_Base> {
316  public:
317
[1909]318    typedef MappableUEdgeSetExtender Graph;
[1842]319    typedef MappableEdgeSetExtender<_Base> Parent;
320
[1909]321    typedef typename Parent::UEdge UEdge;
[1842]322
323    template <typename _Value>
[1909]324    class UEdgeMap
325      : public IterableMapExtender<DefaultMap<Graph, UEdge, _Value> > {
[1842]326    public:
[1909]327      typedef MappableUEdgeSetExtender Graph;
[1842]328      typedef IterableMapExtender<
[1909]329        DefaultMap<Graph, UEdge, _Value> > Parent;
[1842]330
[1909]331      UEdgeMap(const Graph& _g)
[1842]332        : Parent(_g) {}
[1909]333      UEdgeMap(const Graph& _g, const _Value& _v)
[1842]334        : Parent(_g, _v) {}
335
[1909]336      UEdgeMap& operator=(const UEdgeMap& cmap) {
337        return operator=<UEdgeMap>(cmap);
[1842]338      }
339
340      template <typename CMap>
[1909]341      UEdgeMap& operator=(const CMap& cmap) {
342        checkConcept<concept::ReadMap<UEdge, _Value>, CMap>();
[1842]343        const typename Parent::Graph* graph = Parent::getGraph();
[1909]344        UEdge it;
[1842]345        for (graph->first(it); it != INVALID; graph->next(it)) {
346          Parent::set(it, cmap[it]);
347        }
348        return *this;
349      }
350    };
351
352
353  };
354
[1820]355
356  template <typename _Base>
[1910]357  class MappableBpUGraphExtender : public _Base {
[1820]358  public:
359
360    typedef _Base Parent;
[1910]361    typedef MappableBpUGraphExtender Graph;
[1820]362
363    typedef typename Parent::Node Node;
[1910]364    typedef typename Parent::ANode ANode;
365    typedef typename Parent::BNode BNode;
[1820]366    typedef typename Parent::Edge Edge;
[1909]367    typedef typename Parent::UEdge UEdge;
[1820]368   
369    template <typename _Value>
[1910]370    class ANodeMap
371      : public IterableMapExtender<DefaultMap<Graph, ANode, _Value> > {
[1820]372    public:
[1910]373      typedef MappableBpUGraphExtender Graph;
374      typedef IterableMapExtender<DefaultMap<Graph, ANode, _Value> >
[1820]375      Parent;
376   
[1910]377      ANodeMap(const Graph& _g)
[1820]378        : Parent(_g) {}
[1910]379      ANodeMap(const Graph& _g, const _Value& _v)
[1820]380        : Parent(_g, _v) {}
381   
[1910]382      ANodeMap& operator=(const ANodeMap& cmap) {
383        return operator=<ANodeMap>(cmap);
[1820]384      }
385   
386
387      /// \brief Template assign operator.
388      ///
389      /// The given parameter should be conform to the ReadMap
390      /// concept and could be indiced by the current item set of
[1910]391      /// the ANodeMap. In this case the value for each item
[1820]392      /// is assigned by the value of the given ReadMap.
393      template <typename CMap>
[1910]394      ANodeMap& operator=(const CMap& cmap) {
395        checkConcept<concept::ReadMap<ANode, _Value>, CMap>();
[1820]396        const typename Parent::Graph* graph = Parent::getGraph();
[1910]397        ANode it;
[1820]398        for (graph->first(it); it != INVALID; graph->next(it)) {
399          Parent::set(it, cmap[it]);
400        }
401        return *this;
402      }
403   
404    };
405
406    template <typename _Value>
[1910]407    class BNodeMap
408      : public IterableMapExtender<DefaultMap<Graph, BNode, _Value> > {
[1820]409    public:
[1910]410      typedef MappableBpUGraphExtender Graph;
411      typedef IterableMapExtender<DefaultMap<Graph, BNode, _Value> >
[1820]412      Parent;
413   
[1910]414      BNodeMap(const Graph& _g)
[1820]415        : Parent(_g) {}
[1910]416      BNodeMap(const Graph& _g, const _Value& _v)
[1820]417        : Parent(_g, _v) {}
418   
[1910]419      BNodeMap& operator=(const BNodeMap& cmap) {
420        return operator=<BNodeMap>(cmap);
[1820]421      }
422   
423
424      /// \brief Template assign operator.
425      ///
426      /// The given parameter should be conform to the ReadMap
427      /// concept and could be indiced by the current item set of
[1910]428      /// the BNodeMap. In this case the value for each item
[1820]429      /// is assigned by the value of the given ReadMap.
430      template <typename CMap>
[1910]431      BNodeMap& operator=(const CMap& cmap) {
432        checkConcept<concept::ReadMap<BNode, _Value>, CMap>();
[1820]433        const typename Parent::Graph* graph = Parent::getGraph();
[1910]434        BNode it;
[1820]435        for (graph->first(it); it != INVALID; graph->next(it)) {
436          Parent::set(it, cmap[it]);
437        }
438        return *this;
439      }
440   
441    };
442
443  protected:
444
445    template <typename _Value>
446    class NodeMapBase : public Parent::NodeNotifier::ObserverBase {
447    public:
[1910]448      typedef MappableBpUGraphExtender Graph;
[1820]449
450      typedef Node Key;
451      typedef _Value Value;
452
453      /// The reference type of the map;
[1910]454      typedef typename BNodeMap<_Value>::Reference Reference;
[1820]455      /// The pointer type of the map;
[1910]456      typedef typename BNodeMap<_Value>::Pointer Pointer;
[1820]457     
458      /// The const value type of the map.
459      typedef const Value ConstValue;
460      /// The const reference type of the map;
[1910]461      typedef typename BNodeMap<_Value>::ConstReference ConstReference;
[1820]462      /// The pointer type of the map;
[1910]463      typedef typename BNodeMap<_Value>::ConstPointer ConstPointer;
[1820]464
465      typedef True ReferenceMapTag;
466
467      NodeMapBase(const Graph& _g)
[1910]468        : graph(&_g), bNodeMap(_g), aNodeMap(_g) {
[1820]469        Parent::NodeNotifier::ObserverBase::attach(_g.getNotifier(Node()));
470      }
471      NodeMapBase(const Graph& _g, const _Value& _v)
[1910]472        : graph(&_g), bNodeMap(_g, _v),
473          aNodeMap(_g, _v) {
[1820]474        Parent::NodeNotifier::ObserverBase::attach(_g.getNotifier(Node()));
475      }
476
477      virtual ~NodeMapBase() {     
478        if (Parent::NodeNotifier::ObserverBase::attached()) {
479          Parent::NodeNotifier::ObserverBase::detach();
480        }
481      }
482   
483      ConstReference operator[](const Key& node) const {
[1910]484        if (Parent::aNode(node)) {
485          return aNodeMap[node];
[1820]486        } else {
[1910]487          return bNodeMap[node];
[1820]488        }
489      }
490
491      Reference operator[](const Key& node) {
[1910]492        if (Parent::aNode(node)) {
493          return aNodeMap[node];
[1820]494        } else {
[1910]495          return bNodeMap[node];
[1820]496        }
497      }
498
499      void set(const Key& node, const Value& value) {
[1910]500        if (Parent::aNode(node)) {
501          aNodeMap.set(node, value);
[1820]502        } else {
[1910]503          bNodeMap.set(node, value);
[1820]504        }
505      }
506
507    protected:
508     
509      virtual void add(const Node&) {}
[1961]510      virtual void add(const std::vector<Node>&) {}
[1820]511      virtual void erase(const Node&) {}
[1961]512      virtual void erase(const std::vector<Node>&) {}
[1820]513      virtual void clear() {}
514      virtual void build() {}
515
516      const Graph* getGraph() const { return graph; }
517     
518    private:
519      const Graph* graph;
[1910]520      BNodeMap<_Value> bNodeMap;
521      ANodeMap<_Value> aNodeMap;
[1820]522    };
523   
524  public:
525
526    template <typename _Value>
527    class NodeMap
528      : public IterableMapExtender<NodeMapBase<_Value> > {
529    public:
[1910]530      typedef MappableBpUGraphExtender Graph;
[1820]531      typedef IterableMapExtender< NodeMapBase<_Value> > Parent;
532   
533      NodeMap(const Graph& _g)
534        : Parent(_g) {}
535      NodeMap(const Graph& _g, const _Value& _v)
536        : Parent(_g, _v) {}
537   
538      NodeMap& operator=(const NodeMap& cmap) {
539        return operator=<NodeMap>(cmap);
540      }
541   
542
543      /// \brief Template assign operator.
544      ///
545      /// The given parameter should be conform to the ReadMap
546      /// concept and could be indiced by the current item set of
547      /// the NodeMap. In this case the value for each item
548      /// is assigned by the value of the given ReadMap.
549      template <typename CMap>
550      NodeMap& operator=(const CMap& cmap) {
551        checkConcept<concept::ReadMap<Node, _Value>, CMap>();
552        const typename Parent::Graph* graph = Parent::getGraph();
553        Node it;
554        for (graph->first(it); it != INVALID; graph->next(it)) {
555          Parent::set(it, cmap[it]);
556        }
557        return *this;
558      }
559   
560    };
561
562
563
564    template <typename _Value>
565    class EdgeMap
566      : public IterableMapExtender<DefaultMap<Graph, Edge, _Value> > {
567    public:
[1910]568      typedef MappableBpUGraphExtender Graph;
[1820]569      typedef IterableMapExtender<DefaultMap<Graph, Edge, _Value> > Parent;
570   
571      EdgeMap(const Graph& _g)
572        : Parent(_g) {}
573      EdgeMap(const Graph& _g, const _Value& _v)
574        : Parent(_g, _v) {}
575   
576      EdgeMap& operator=(const EdgeMap& cmap) {
577        return operator=<EdgeMap>(cmap);
578      }
579   
580      template <typename CMap>
581      EdgeMap& operator=(const CMap& cmap) {
582        checkConcept<concept::ReadMap<Edge, _Value>, CMap>();
583        const typename Parent::Graph* graph = Parent::getGraph();
584        Edge it;
585        for (graph->first(it); it != INVALID; graph->next(it)) {
586          Parent::set(it, cmap[it]);
587        }
588        return *this;
589      }
590    };
591
592    template <typename _Value>
[1909]593    class UEdgeMap
594      : public IterableMapExtender<DefaultMap<Graph, UEdge, _Value> > {
[1820]595    public:
[1910]596      typedef MappableBpUGraphExtender Graph;
[1909]597      typedef IterableMapExtender<DefaultMap<Graph, UEdge, _Value> >
[1820]598      Parent;
599   
[1909]600      UEdgeMap(const Graph& _g)
[1820]601        : Parent(_g) {}
[1909]602      UEdgeMap(const Graph& _g, const _Value& _v)
[1820]603        : Parent(_g, _v) {}
604   
[1909]605      UEdgeMap& operator=(const UEdgeMap& cmap) {
606        return operator=<UEdgeMap>(cmap);
[1820]607      }
608   
609      template <typename CMap>
[1909]610      UEdgeMap& operator=(const CMap& cmap) {
611        checkConcept<concept::ReadMap<UEdge, _Value>, CMap>();
[1820]612        const typename Parent::Graph* graph = Parent::getGraph();
[1909]613        UEdge it;
[1820]614        for (graph->first(it); it != INVALID; graph->next(it)) {
615          Parent::set(it, cmap[it]);
616        }
617        return *this;
618      }
619    };
620 
621  };
622
[822]623}
624
625#endif
Note: See TracBrowser for help on using the repository browser.