/* -*- C++ -*- * src/lemon/concept/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_CONCEPT_MAPS_H #define LEMON_CONCEPT_MAPS_H #include ///\ingroup concept ///\file ///\brief Map concepts checking classes for testing and documenting. namespace lemon { namespace concept { /// \addtogroup concept /// @{ /// Readable map concept template class ReadMap { public: /// Map's key type. typedef K KeyType; /// Map's value type. (The type of objects associated with the keys). typedef T ValueType; /// Returns the value associated with a key. ValueType operator[](const KeyType &k) const {return ValueType();} ///Default constructor ReadMap() {} }; /// Writable map concept template class WriteMap { public: /// Map's key type. typedef K KeyType; /// Map's value type. (The type of objects associated with the keys). typedef T ValueType; /// Sets the value associated with a key. void set(const KeyType &k,const ValueType &t) {} ///Default constructor WriteMap() {} }; ///Read/Writable map concept template class ReadWriteMap : public ReadMap, public WriteMap { public: /// Map's key type. typedef K KeyType; /// Map's value type. (The type of objects associated with the keys). typedef T ValueType; /// Returns the value associated with a key. ValueType operator[](const KeyType &k) const {return ValueType();} /// Sets the value associated with a key. void set(const KeyType &k,const ValueType &t) {} ///Default constructor ReadWriteMap() {} }; ///Dereferable map concept template class ReferenceMap : public ReadWriteMap { public: /// Map's key type. typedef K KeyType; /// Map's value type. (The type of objects associated with the keys). typedef T ValueType; protected: ValueType tmp; public: typedef ValueType& ReferenceType; /// Map's const reference type. typedef const ValueType& ConstReferenceType; ///Returns a reference to the value associated to a key. ReferenceType operator[](const KeyType &i) { return tmp; } ///Returns a const reference to the value associated to a key. ConstReferenceType operator[](const KeyType &i) const { return tmp; } /// Sets the value associated with a key. void set(const KeyType &k,const ValueType &t) { operator[](k)=t; } ///Default constructor ReferenceMap() {} }; template class GraphMap : public ReadWriteMap { // I really, really don't like the idea that every graph should have // reference maps! --klao private: // We state explicitly that graph maps have no default constructor? GraphMap(); public: explicit GraphMap(Graph const&) {} // value for initializing GraphMap(Graph const&, T) {} // this probably should be required: GraphMap(GraphMap const&) {} GraphMap& operator=(GraphMap const&) { return *this; } // but this is a absolute no-op! We should provide a more generic // graph-map-copy operation. // // template // GraphMap(GraphMap const&); // // template // GraphMap& operator=(const GraphMap&); }; /**************** Concept-checking classes ****************/ template struct ReadMapConcept { typedef typename ReadMap::KeyType KeyType; typedef typename ReadMap::ValueType ValueType; void constraints() { // No constraints for constructor. // What are the requirement for the ValueType? // CopyConstructible? Assignable? None of these? ValueType v = m[k]; v = m[k]; // FIXME: ignore_unused_variable_warning(v); } ReadMap m; KeyType k; }; template struct WriteMapConcept { typedef typename WriteMap::KeyType KeyType; typedef typename WriteMap::ValueType ValueType; void constraints() { // No constraints for constructor. m.set(k, v); } WriteMap m; KeyType k; ValueType v; }; template struct ReadWriteMapConcept { void constraints() { function_requires< ReadMapConcept >(); function_requires< WriteMapConcept >(); } }; template struct ReferenceMapConcept { typedef typename ReferenceMap::KeyType KeyType; typedef typename ReferenceMap::ValueType ValueType; typedef typename ReferenceMap::ReferenceType ReferenceType; // What for is this? typedef typename ReferenceMap::ConstReferenceType ConstReferenceType; void constraints() { function_requires< ReadWriteMapConcept >(); m[k] = v; // Or should we require real reference? // Like this: // ValueType &vv = m[k]; // ignore_unused_variable_warning(vv); } ReferenceMap m; KeyType k; ValueType v; }; /// \todo GraphMapConceptCheck template struct GraphMapConcept { void constraints() { function_requires< ReadWriteMapConcept >(); // Construction with a graph parameter GraphMap a(g); // Ctor with a graph and a default value parameter GraphMap a2(g,t); // Copy ctor. Do we need it? GraphMap b=c; // Copy operator. Do we need it? a=b; ignore_unused_variable_warning(a2); } const GraphMap &c; const Graph &g; const typename GraphMap::ValueType &t; }; // @} } //namespace concept } //namespace lemon #endif // LEMON_CONCEPT_MAPS_H