/* -*- C++ -*- * src/lemon/maps.h - Part of LEMON, a generic C++ optimization library * * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport * (Egervary Combinatorial Optimization Research Group, EGRES). * * Permission to use, modify and distribute this software is granted * provided that this copyright notice appears in all copies. For * precise terms see the accompanying LICENSE file. * * This software is provided "AS IS" with no warranty of any kind, * express or implied, and with no claim as to its suitability for any * purpose. * */ #ifndef LEMON_MAPS_H #define LEMON_MAPS_H ///\file ///\brief Miscellaneous property maps /// ///\todo This file has the same name as the concept file in concept/, /// and this is not easily detectable in docs... #include namespace lemon { /// Base class of maps. /// Base class of maps. /// It provides the necessary typedefs required by the map concept. template class MapBase { public: ///\e typedef K Key; ///\e typedef T Value; }; /// Null map. (a.k.a. DoNothingMap) /// If you have to provide a map only for its type definitions, /// or if you have to provide a writable map, but /// data written to it will sent to /dev/null... template class NullMap : public MapBase { public: /// Gives back a default constructed element. T operator[](const K&) const { return T(); } /// Absorbs the value. void set(const K&, const T&) {} }; /// Constant map. /// This is a readable map which assigns a specified value to each key. /// In other aspects it is equivalent to the \ref NullMap. /// \todo set could be used to set the value. template class ConstMap : public MapBase { T v; public: /// Default constructor /// The value of the map will be uninitialized. /// (More exactly it will be default constructed.) ConstMap() {} ///\e /// \param _v The initial value of the map. /// ConstMap(const T &_v) : v(_v) {} T operator[](const K&) const { return v; } void set(const K&, const T&) {} template struct rebind { typedef ConstMap other; }; template ConstMap(const ConstMap &, const T &_v) : v(_v) {} }; //to document later template struct Const { }; //to document later template class ConstMap > : public MapBase { public: ConstMap() { } V operator[](const K&) const { return v; } void set(const K&, const V&) { } }; /// \c std::map wrapper /// This is essentially a wrapper for \c std::map. With addition that /// you can specify a default value different from \c Value() . /// /// \todo Provide allocator parameter... template > class StdMap : public std::map { typedef std::map parent; T v; typedef typename parent::value_type PairType; public: typedef K Key; typedef T Value; typedef T& Reference; typedef const T& ConstReference; StdMap() : v() {} /// Constructor with specified default value StdMap(const T& _v) : v(_v) {} /// \brief Constructs the map from an appropriate std::map. /// /// \warning Inefficient: copies the content of \c m ! StdMap(const parent &m) : parent(m) {} /// \brief Constructs the map from an appropriate std::map, and explicitly /// specifies a default value. /// /// \warning Inefficient: copies the content of \c m ! StdMap(const parent &m, const T& _v) : parent(m), v(_v) {} template StdMap(const StdMap &m, const T &_v) { //FIXME; } Reference operator[](const Key &k) { return insert(PairType(k,v)).first -> second; } ConstReference operator[](const Key &k) const { typename parent::iterator i = lower_bound(k); if (i == parent::end() || parent::key_comp()(k, (*i).first)) return v; return (*i).second; } void set(const Key &k, const T &t) { parent::operator[](k) = t; } /// Changes the default value of the map. /// \return Returns the previous default value. /// /// \warning The value of some keys (which has already been queried, but /// the value has been unchanged from the default) may change! T setDefault(const T &_v) { T old=v; v=_v; return old; } template struct rebind { typedef StdMap other; }; }; } #endif // LEMON_MAPS_H