alpar@25: /* -*- C++ -*- alpar@25: * alpar@25: * This file is a part of LEMON, a generic C++ optimization library alpar@25: * alpar@25: * Copyright (C) 2003-2007 alpar@25: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport alpar@25: * (Egervary Research Group on Combinatorial Optimization, EGRES). alpar@25: * alpar@25: * Permission to use, modify and distribute this software is granted alpar@25: * provided that this copyright notice appears in all copies. For alpar@25: * precise terms see the accompanying LICENSE file. alpar@25: * alpar@25: * This software is provided "AS IS" with no warranty of any kind, alpar@25: * express or implied, and with no claim as to its suitability for any alpar@25: * purpose. alpar@25: * alpar@25: */ alpar@25: alpar@25: #ifndef LEMON_CONCEPT_MAPS_H alpar@25: #define LEMON_CONCEPT_MAPS_H alpar@25: alpar@25: #include alpar@25: #include alpar@25: alpar@25: ///\ingroup concept alpar@25: ///\file alpar@25: ///\brief Map concepts checking classes for testing and documenting. alpar@25: alpar@25: namespace lemon { alpar@25: alpar@25: namespace concepts { alpar@25: alpar@25: /// \addtogroup concept alpar@25: /// @{ alpar@25: alpar@25: /// Readable map concept kpeter@28: kpeter@28: /// Readable map concept. kpeter@28: /// alpar@25: template alpar@25: class ReadMap alpar@25: { alpar@25: public: kpeter@35: /// The key type of the map. alpar@25: typedef K Key; kpeter@35: /// The value type of the map. (The type of objects associated with the keys). alpar@25: typedef T Value; alpar@25: alpar@25: /// Returns the value associated with a key. alpar@25: kpeter@28: /// \bug Value shouldn't need to be default constructible. alpar@25: /// alpar@25: Value operator[](const Key &) const {return Value();} alpar@25: alpar@25: template alpar@25: struct Constraints { alpar@25: alpar@25: void constraints() { alpar@25: Value val = m[key]; alpar@25: val = m[key]; alpar@25: typename _ReadMap::Value own_val = m[own_key]; alpar@25: own_val = m[own_key]; alpar@25: alpar@25: ignore_unused_variable_warning(val); alpar@25: ignore_unused_variable_warning(own_val); alpar@25: ignore_unused_variable_warning(key); alpar@25: } alpar@25: Key& key; alpar@25: typename _ReadMap::Key& own_key; alpar@25: _ReadMap& m; alpar@25: }; alpar@25: alpar@25: }; alpar@25: alpar@25: alpar@25: /// Writable map concept kpeter@28: kpeter@28: /// Writable map concept. kpeter@28: /// alpar@25: template alpar@25: class WriteMap alpar@25: { alpar@25: public: kpeter@35: /// The key type of the map. alpar@25: typedef K Key; kpeter@35: /// The value type of the map. (The type of objects associated with the keys). alpar@25: typedef T Value; alpar@25: alpar@25: /// Sets the value associated with a key. alpar@25: void set(const Key &,const Value &) {} alpar@25: alpar@25: ///Default constructor alpar@25: WriteMap() {} alpar@25: alpar@25: template alpar@25: struct Constraints { alpar@25: void constraints() { alpar@25: // No constraints for constructor. alpar@25: m.set(key, val); alpar@25: m.set(own_key, own_val); alpar@25: ignore_unused_variable_warning(key); alpar@25: ignore_unused_variable_warning(val); alpar@25: ignore_unused_variable_warning(own_key); alpar@25: ignore_unused_variable_warning(own_val); alpar@25: } alpar@25: alpar@25: Value& val; alpar@25: typename _WriteMap::Value own_val; alpar@25: Key& key; alpar@25: typename _WriteMap::Key& own_key; alpar@25: _WriteMap& m; alpar@25: alpar@25: }; alpar@25: }; alpar@25: kpeter@28: /// Read/Writable map concept kpeter@28: kpeter@28: /// Read/writable map concept. kpeter@28: /// alpar@25: template alpar@25: class ReadWriteMap : public ReadMap, kpeter@35: public WriteMap alpar@25: { alpar@25: public: kpeter@35: /// The key type of the map. alpar@25: typedef K Key; kpeter@35: /// The value type of the map. (The type of objects associated with the keys). alpar@25: typedef T Value; alpar@25: alpar@25: /// Returns the value associated with a key. alpar@25: Value operator[](const Key &) const {return Value();} alpar@25: /// Sets the value associated with a key. alpar@25: void set(const Key & ,const Value &) {} alpar@25: alpar@25: template alpar@25: struct Constraints { alpar@25: void constraints() { alpar@25: checkConcept, _ReadWriteMap >(); alpar@25: checkConcept, _ReadWriteMap >(); alpar@25: } alpar@25: }; alpar@25: }; alpar@25: alpar@25: kpeter@28: /// Dereferable map concept kpeter@28: kpeter@28: /// Dereferable map concept. kpeter@28: /// alpar@25: template alpar@25: class ReferenceMap : public ReadWriteMap alpar@25: { alpar@25: public: alpar@25: /// Tag for reference maps. alpar@25: typedef True ReferenceMapTag; kpeter@35: /// The key type of the map. alpar@25: typedef K Key; kpeter@35: /// The value type of the map. (The type of objects associated with the keys). alpar@25: typedef T Value; kpeter@35: /// The reference type of the map. alpar@25: typedef R Reference; kpeter@35: /// The const reference type of the map. alpar@25: typedef CR ConstReference; alpar@25: alpar@25: protected: alpar@25: Value tmp; alpar@25: public: alpar@25: alpar@25: ///Returns a reference to the value associated to a key. alpar@25: Reference operator[](const Key &) { return tmp; } alpar@25: ///Returns a const reference to the value associated to a key. kpeter@28: ConstReference operator[](const Key &) const { return tmp; } alpar@25: /// Sets the value associated with a key. alpar@25: void set(const Key &k,const Value &t) { operator[](k)=t; } alpar@25: kpeter@28: /// \todo Rethink this concept. alpar@25: template alpar@25: struct ReferenceMapConcept { alpar@25: alpar@25: void constraints() { alpar@25: checkConcept(); alpar@25: m[key] = val; alpar@25: val = m[key]; alpar@25: m[key] = ref; alpar@25: ref = m[key]; alpar@25: m[own_key] = own_val; alpar@25: own_val = m[own_key]; alpar@25: m[own_key] = own_ref; alpar@25: own_ref = m[own_key]; alpar@25: } alpar@25: alpar@25: typename _ReferenceMap::Key& own_key; alpar@25: typename _ReferenceMap::Value& own_val; alpar@25: typename _ReferenceMap::Reference& own_ref; alpar@25: Key& key; alpar@25: Value& val; alpar@25: Reference& ref; alpar@25: _ReferenceMap& m; alpar@25: }; alpar@25: }; alpar@25: alpar@25: // @} alpar@25: alpar@25: } //namespace concepts kpeter@28: alpar@25: } //namespace lemon kpeter@28: alpar@25: #endif // LEMON_CONCEPT_MAPS_H