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