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