COIN-OR::LEMON - Graph Library

source: lemon-0.x/lemon/bits/default_map.h @ 1966:65765fb5eb2f

Last change on this file since 1966:65765fb5eb2f was 1966:65765fb5eb2f, checked in by Balazs Dezso, 18 years ago

Easier checking in DEBUG mode

I hope we should not test ArrayMap? longer

The vector map checks its limits in debug mode what
helps us to find the bad memory accesses in the maps

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