1 | /* -*- C++ -*- |
---|
2 | * src/lemon/default_map.h - Part of LEMON, a generic C++ optimization library |
---|
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 | |
---|
17 | #ifndef LEMON_DEFAULT_MAP_H |
---|
18 | #define LEMON_DEFAULT_MAP_H |
---|
19 | |
---|
20 | |
---|
21 | #include <lemon/array_map.h> |
---|
22 | #include <lemon/vector_map.h> |
---|
23 | |
---|
24 | ///\ingroup graphmaps |
---|
25 | ///\file |
---|
26 | ///\brief Graph maps that construates and destruates |
---|
27 | ///their elements dynamically. |
---|
28 | |
---|
29 | namespace lemon { |
---|
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) \ |
---|
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> \ |
---|
61 | DefaultMap(const DefaultMap<MapRegistry, TT>& copy) \ |
---|
62 | : { \ |
---|
63 | Parent::MapBase::operator= \ |
---|
64 | (static_cast<const typename Parent::MapBase&>(copy)); \ |
---|
65 | if (Parent::getGraph()) { \ |
---|
66 | for (typename Parent::KeyIt it(*Parent::getGraph()); it!=INVALID; ++it) {\ |
---|
67 | Parent::add(it); \ |
---|
68 | Parent::operator[](it) = copy[it]; \ |
---|
69 | } \ |
---|
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) { \ |
---|
78 | if (Parent::getGraph() != copy.getGraph()) { \ |
---|
79 | Parent::clear(); \ |
---|
80 | Parent::MapBase::operator=(copy); \ |
---|
81 | Parent::construct(); \ |
---|
82 | } \ |
---|
83 | if (Parent::getGraph()) { \ |
---|
84 | for (typename Parent::KeyIt it(*Parent::getGraph()); it!=INVALID; ++it) {\ |
---|
85 | Parent::operator[](it) = copy[it]; \ |
---|
86 | } \ |
---|
87 | } \ |
---|
88 | return *this; \ |
---|
89 | } \ |
---|
90 | }; |
---|
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 |
---|