[906] | 1 | /* -*- C++ -*- |
---|
[921] | 2 | * src/lemon/default_map.h - Part of LEMON, a generic C++ optimization library |
---|
[906] | 3 | * |
---|
| 4 | * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
| 5 | * (Egervary Combinatorial Optimization Research Group, EGRES). |
---|
| 6 | * |
---|
| 7 | * Permission to use, modify and distribute this software is granted |
---|
| 8 | * provided that this copyright notice appears in all copies. For |
---|
| 9 | * precise terms see the accompanying LICENSE file. |
---|
| 10 | * |
---|
| 11 | * This software is provided "AS IS" with no warranty of any kind, |
---|
| 12 | * express or implied, and with no claim as to its suitability for any |
---|
| 13 | * purpose. |
---|
| 14 | * |
---|
| 15 | */ |
---|
| 16 | |
---|
[921] | 17 | #ifndef LEMON_DEFAULT_MAP_H |
---|
| 18 | #define LEMON_DEFAULT_MAP_H |
---|
[822] | 19 | |
---|
| 20 | |
---|
[921] | 21 | #include <lemon/array_map.h> |
---|
| 22 | #include <lemon/vector_map.h> |
---|
[822] | 23 | |
---|
| 24 | ///\ingroup graphmaps |
---|
| 25 | ///\file |
---|
| 26 | ///\brief Graph maps that construates and destruates |
---|
| 27 | ///their elements dynamically. |
---|
| 28 | |
---|
[921] | 29 | namespace lemon { |
---|
[822] | 30 | |
---|
| 31 | /// \addtogroup graphmaps |
---|
| 32 | /// @{ |
---|
| 33 | |
---|
| 34 | /** The ArrayMap template class is graph map structure what |
---|
| 35 | * automatically updates the map when a key is added to or erased from |
---|
| 36 | * the map. This map uses the VectorMap if the ValueType is a primitive |
---|
| 37 | * type and the ArrayMap for the other cases. |
---|
| 38 | * |
---|
| 39 | * The template parameter is the MapRegistry that the maps |
---|
| 40 | * will belong to and the ValueType. |
---|
| 41 | */ |
---|
| 42 | |
---|
| 43 | |
---|
| 44 | /** Macro to implement the DefaultMap. |
---|
| 45 | */ |
---|
| 46 | #define DEFAULT_MAP_BODY(DynMap, Value) \ |
---|
[891] | 47 | { \ |
---|
| 48 | \ |
---|
| 49 | public: \ |
---|
| 50 | \ |
---|
| 51 | typedef DynMap<MapRegistry, Value> Parent; \ |
---|
| 52 | \ |
---|
| 53 | typedef typename MapRegistry::Graph Graph; \ |
---|
| 54 | \ |
---|
| 55 | DefaultMap(const Graph& g, MapRegistry& r) : Parent(g, r) {} \ |
---|
| 56 | DefaultMap(const Graph& g, MapRegistry& r, const Value& v) \ |
---|
| 57 | : Parent(g, r, v) {} \ |
---|
| 58 | DefaultMap(const DefaultMap& copy) \ |
---|
| 59 | : Parent(static_cast<const Parent&>(copy)) {} \ |
---|
| 60 | template <typename TT> \ |
---|
[897] | 61 | DefaultMap(const DefaultMap<MapRegistry, TT>& copy) \ |
---|
[919] | 62 | : { \ |
---|
| 63 | Parent::MapBase::operator= \ |
---|
| 64 | (static_cast<const typename Parent::MapBase&>(copy)); \ |
---|
[891] | 65 | if (Parent::getGraph()) { \ |
---|
| 66 | for (typename Parent::KeyIt it(*Parent::getGraph()); it!=INVALID; ++it) {\ |
---|
[919] | 67 | Parent::add(it); \ |
---|
[891] | 68 | Parent::operator[](it) = copy[it]; \ |
---|
[822] | 69 | } \ |
---|
[891] | 70 | } \ |
---|
| 71 | } \ |
---|
| 72 | DefaultMap& operator=(const DefaultMap& copy) { \ |
---|
| 73 | Parent::operator=(static_cast<const Parent&>(copy)); \ |
---|
| 74 | return *this; \ |
---|
| 75 | } \ |
---|
| 76 | template <typename TT> \ |
---|
| 77 | DefaultMap& operator=(const DefaultMap<MapRegistry, TT>& copy) { \ |
---|
[897] | 78 | if (Parent::getGraph() != copy.getGraph()) { \ |
---|
| 79 | Parent::clear(); \ |
---|
| 80 | Parent::MapBase::operator=(copy); \ |
---|
| 81 | Parent::construct(); \ |
---|
| 82 | } \ |
---|
[891] | 83 | if (Parent::getGraph()) { \ |
---|
| 84 | for (typename Parent::KeyIt it(*Parent::getGraph()); it!=INVALID; ++it) {\ |
---|
| 85 | Parent::operator[](it) = copy[it]; \ |
---|
[822] | 86 | } \ |
---|
[891] | 87 | } \ |
---|
| 88 | return *this; \ |
---|
| 89 | } \ |
---|
| 90 | }; |
---|
[822] | 91 | |
---|
| 92 | |
---|
| 93 | template <typename MapRegistry, typename Type> |
---|
| 94 | class DefaultMap : public ArrayMap<MapRegistry, Type> |
---|
| 95 | DEFAULT_MAP_BODY(ArrayMap, Type); |
---|
| 96 | |
---|
| 97 | template <typename MapRegistry> |
---|
| 98 | class DefaultMap<MapRegistry, bool> |
---|
| 99 | : public VectorMap<MapRegistry, bool> |
---|
| 100 | DEFAULT_MAP_BODY(VectorMap, bool); |
---|
| 101 | |
---|
| 102 | template <typename MapRegistry> |
---|
| 103 | class DefaultMap<MapRegistry, char> |
---|
| 104 | : public VectorMap<MapRegistry, char> |
---|
| 105 | DEFAULT_MAP_BODY(VectorMap, char); |
---|
| 106 | |
---|
| 107 | template <typename MapRegistry> |
---|
| 108 | class DefaultMap<MapRegistry, int> |
---|
| 109 | : public VectorMap<MapRegistry, int> |
---|
| 110 | DEFAULT_MAP_BODY(VectorMap, int); |
---|
| 111 | |
---|
| 112 | template <typename MapRegistry> |
---|
| 113 | class DefaultMap<MapRegistry, short> |
---|
| 114 | : public VectorMap<MapRegistry, short> |
---|
| 115 | DEFAULT_MAP_BODY(VectorMap, short); |
---|
| 116 | |
---|
| 117 | template <typename MapRegistry> |
---|
| 118 | class DefaultMap<MapRegistry, long> |
---|
| 119 | : public VectorMap<MapRegistry, long> |
---|
| 120 | DEFAULT_MAP_BODY(VectorMap, long); |
---|
| 121 | |
---|
| 122 | template <typename MapRegistry> |
---|
| 123 | class DefaultMap<MapRegistry, float> |
---|
| 124 | : public VectorMap<MapRegistry, float> |
---|
| 125 | DEFAULT_MAP_BODY(VectorMap, float); |
---|
| 126 | |
---|
| 127 | template <typename MapRegistry> |
---|
| 128 | class DefaultMap<MapRegistry, double> |
---|
| 129 | : public VectorMap<MapRegistry, double> |
---|
| 130 | DEFAULT_MAP_BODY(VectorMap, double); |
---|
| 131 | |
---|
| 132 | template <typename MapRegistry> |
---|
| 133 | class DefaultMap<MapRegistry, long double> |
---|
| 134 | : public VectorMap<MapRegistry, long double> |
---|
| 135 | DEFAULT_MAP_BODY(VectorMap, long double); |
---|
| 136 | |
---|
| 137 | template <typename MapRegistry, typename Type> |
---|
| 138 | class DefaultMap<MapRegistry, Type*> |
---|
| 139 | : public VectorMap<MapRegistry, Type*> |
---|
| 140 | DEFAULT_MAP_BODY(VectorMap, Type*); |
---|
| 141 | |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | #endif |
---|