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