1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/lemon/default_map.h Wed Sep 29 15:30:04 2004 +0000
1.3 @@ -0,0 +1,144 @@
1.4 +/* -*- C++ -*-
1.5 + * src/lemon/default_map.h - Part of LEMON, a generic C++ optimization library
1.6 + *
1.7 + * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
1.8 + * (Egervary Combinatorial Optimization Research Group, EGRES).
1.9 + *
1.10 + * Permission to use, modify and distribute this software is granted
1.11 + * provided that this copyright notice appears in all copies. For
1.12 + * precise terms see the accompanying LICENSE file.
1.13 + *
1.14 + * This software is provided "AS IS" with no warranty of any kind,
1.15 + * express or implied, and with no claim as to its suitability for any
1.16 + * purpose.
1.17 + *
1.18 + */
1.19 +
1.20 +#ifndef LEMON_DEFAULT_MAP_H
1.21 +#define LEMON_DEFAULT_MAP_H
1.22 +
1.23 +
1.24 +#include <lemon/array_map.h>
1.25 +#include <lemon/vector_map.h>
1.26 +
1.27 +///\ingroup graphmaps
1.28 +///\file
1.29 +///\brief Graph maps that construates and destruates
1.30 +///their elements dynamically.
1.31 +
1.32 +namespace lemon {
1.33 +
1.34 +/// \addtogroup graphmaps
1.35 +/// @{
1.36 +
1.37 + /** The ArrayMap template class is graph map structure what
1.38 + * automatically updates the map when a key is added to or erased from
1.39 + * the map. This map uses the VectorMap if the ValueType is a primitive
1.40 + * type and the ArrayMap for the other cases.
1.41 + *
1.42 + * The template parameter is the MapRegistry that the maps
1.43 + * will belong to and the ValueType.
1.44 + */
1.45 +
1.46 +
1.47 + /** Macro to implement the DefaultMap.
1.48 + */
1.49 +#define DEFAULT_MAP_BODY(DynMap, Value) \
1.50 +{ \
1.51 +\
1.52 +public: \
1.53 +\
1.54 +typedef DynMap<MapRegistry, Value> Parent; \
1.55 +\
1.56 +typedef typename MapRegistry::Graph Graph; \
1.57 +\
1.58 +DefaultMap(const Graph& g, MapRegistry& r) : Parent(g, r) {} \
1.59 +DefaultMap(const Graph& g, MapRegistry& r, const Value& v) \
1.60 + : Parent(g, r, v) {} \
1.61 +DefaultMap(const DefaultMap& copy) \
1.62 + : Parent(static_cast<const Parent&>(copy)) {} \
1.63 +template <typename TT> \
1.64 +DefaultMap(const DefaultMap<MapRegistry, TT>& copy) \
1.65 + : { \
1.66 + Parent::MapBase::operator= \
1.67 + (static_cast<const typename Parent::MapBase&>(copy)); \
1.68 + if (Parent::getGraph()) { \
1.69 + for (typename Parent::KeyIt it(*Parent::getGraph()); it!=INVALID; ++it) {\
1.70 + Parent::add(it); \
1.71 + Parent::operator[](it) = copy[it]; \
1.72 + } \
1.73 + } \
1.74 +} \
1.75 +DefaultMap& operator=(const DefaultMap& copy) { \
1.76 + Parent::operator=(static_cast<const Parent&>(copy)); \
1.77 + return *this; \
1.78 +} \
1.79 +template <typename TT> \
1.80 +DefaultMap& operator=(const DefaultMap<MapRegistry, TT>& copy) { \
1.81 + if (Parent::getGraph() != copy.getGraph()) { \
1.82 + Parent::clear(); \
1.83 + Parent::MapBase::operator=(copy); \
1.84 + Parent::construct(); \
1.85 + } \
1.86 + if (Parent::getGraph()) { \
1.87 + for (typename Parent::KeyIt it(*Parent::getGraph()); it!=INVALID; ++it) {\
1.88 + Parent::operator[](it) = copy[it]; \
1.89 + } \
1.90 + } \
1.91 + return *this; \
1.92 +} \
1.93 +};
1.94 +
1.95 +
1.96 + template <typename MapRegistry, typename Type>
1.97 + class DefaultMap : public ArrayMap<MapRegistry, Type>
1.98 + DEFAULT_MAP_BODY(ArrayMap, Type);
1.99 +
1.100 + template <typename MapRegistry>
1.101 + class DefaultMap<MapRegistry, bool>
1.102 + : public VectorMap<MapRegistry, bool>
1.103 + DEFAULT_MAP_BODY(VectorMap, bool);
1.104 +
1.105 + template <typename MapRegistry>
1.106 + class DefaultMap<MapRegistry, char>
1.107 + : public VectorMap<MapRegistry, char>
1.108 + DEFAULT_MAP_BODY(VectorMap, char);
1.109 +
1.110 + template <typename MapRegistry>
1.111 + class DefaultMap<MapRegistry, int>
1.112 + : public VectorMap<MapRegistry, int>
1.113 + DEFAULT_MAP_BODY(VectorMap, int);
1.114 +
1.115 + template <typename MapRegistry>
1.116 + class DefaultMap<MapRegistry, short>
1.117 + : public VectorMap<MapRegistry, short>
1.118 + DEFAULT_MAP_BODY(VectorMap, short);
1.119 +
1.120 + template <typename MapRegistry>
1.121 + class DefaultMap<MapRegistry, long>
1.122 + : public VectorMap<MapRegistry, long>
1.123 + DEFAULT_MAP_BODY(VectorMap, long);
1.124 +
1.125 + template <typename MapRegistry>
1.126 + class DefaultMap<MapRegistry, float>
1.127 + : public VectorMap<MapRegistry, float>
1.128 + DEFAULT_MAP_BODY(VectorMap, float);
1.129 +
1.130 + template <typename MapRegistry>
1.131 + class DefaultMap<MapRegistry, double>
1.132 + : public VectorMap<MapRegistry, double>
1.133 + DEFAULT_MAP_BODY(VectorMap, double);
1.134 +
1.135 + template <typename MapRegistry>
1.136 + class DefaultMap<MapRegistry, long double>
1.137 + : public VectorMap<MapRegistry, long double>
1.138 + DEFAULT_MAP_BODY(VectorMap, long double);
1.139 +
1.140 + template <typename MapRegistry, typename Type>
1.141 + class DefaultMap<MapRegistry, Type*>
1.142 + : public VectorMap<MapRegistry, Type*>
1.143 + DEFAULT_MAP_BODY(VectorMap, Type*);
1.144 +
1.145 +}
1.146 +
1.147 +#endif