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 | : Parent(*copy.getGraph()) { \ |
---|
63 | if (Parent::getGraph()) { \ |
---|
64 | for (typename Parent::KeyIt it(*Parent::getGraph()); it!=INVALID; ++it) {\ |
---|
65 | Parent::operator[](it) = copy[it]; \ |
---|
66 | } \ |
---|
67 | } \ |
---|
68 | } \ |
---|
69 | DefaultMap& operator=(const DefaultMap& copy) { \ |
---|
70 | Parent::operator=(static_cast<const Parent&>(copy)); \ |
---|
71 | return *this; \ |
---|
72 | } \ |
---|
73 | template <typename TT> \ |
---|
74 | DefaultMap& operator=(const DefaultMap<MapRegistry, TT>& copy) { \ |
---|
75 | if (Parent::getGraph() != copy.getGraph()) { \ |
---|
76 | Parent::clear(); \ |
---|
77 | Parent::MapBase::operator=(copy); \ |
---|
78 | Parent::construct(); \ |
---|
79 | } \ |
---|
80 | if (Parent::getGraph()) { \ |
---|
81 | for (typename Parent::KeyIt it(*Parent::getGraph()); it!=INVALID; ++it) {\ |
---|
82 | Parent::operator[](it) = copy[it]; \ |
---|
83 | } \ |
---|
84 | } \ |
---|
85 | return *this; \ |
---|
86 | } \ |
---|
87 | }; |
---|
88 | |
---|
89 | |
---|
90 | template <typename MapRegistry, typename Type> |
---|
91 | class DefaultMap : public ArrayMap<MapRegistry, Type> |
---|
92 | DEFAULT_MAP_BODY(ArrayMap, Type); |
---|
93 | |
---|
94 | template <typename MapRegistry> |
---|
95 | class DefaultMap<MapRegistry, bool> |
---|
96 | : public VectorMap<MapRegistry, bool> |
---|
97 | DEFAULT_MAP_BODY(VectorMap, bool); |
---|
98 | |
---|
99 | template <typename MapRegistry> |
---|
100 | class DefaultMap<MapRegistry, char> |
---|
101 | : public VectorMap<MapRegistry, char> |
---|
102 | DEFAULT_MAP_BODY(VectorMap, char); |
---|
103 | |
---|
104 | template <typename MapRegistry> |
---|
105 | class DefaultMap<MapRegistry, int> |
---|
106 | : public VectorMap<MapRegistry, int> |
---|
107 | DEFAULT_MAP_BODY(VectorMap, int); |
---|
108 | |
---|
109 | template <typename MapRegistry> |
---|
110 | class DefaultMap<MapRegistry, short> |
---|
111 | : public VectorMap<MapRegistry, short> |
---|
112 | DEFAULT_MAP_BODY(VectorMap, short); |
---|
113 | |
---|
114 | template <typename MapRegistry> |
---|
115 | class DefaultMap<MapRegistry, long> |
---|
116 | : public VectorMap<MapRegistry, long> |
---|
117 | DEFAULT_MAP_BODY(VectorMap, long); |
---|
118 | |
---|
119 | template <typename MapRegistry> |
---|
120 | class DefaultMap<MapRegistry, float> |
---|
121 | : public VectorMap<MapRegistry, float> |
---|
122 | DEFAULT_MAP_BODY(VectorMap, float); |
---|
123 | |
---|
124 | template <typename MapRegistry> |
---|
125 | class DefaultMap<MapRegistry, double> |
---|
126 | : public VectorMap<MapRegistry, double> |
---|
127 | DEFAULT_MAP_BODY(VectorMap, double); |
---|
128 | |
---|
129 | template <typename MapRegistry> |
---|
130 | class DefaultMap<MapRegistry, long double> |
---|
131 | : public VectorMap<MapRegistry, long double> |
---|
132 | DEFAULT_MAP_BODY(VectorMap, long double); |
---|
133 | |
---|
134 | template <typename MapRegistry, typename Type> |
---|
135 | class DefaultMap<MapRegistry, Type*> |
---|
136 | : public VectorMap<MapRegistry, Type*> |
---|
137 | DEFAULT_MAP_BODY(VectorMap, Type*); |
---|
138 | |
---|
139 | } |
---|
140 | |
---|
141 | #endif |
---|