/* -*- 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 Key; /// Map's value type. (The type of objects associated with the keys). typedef T Value; /// Returns the value associated with a key. Value operator[](const Key &k) const {return Value();} ///Default constructor ReadMap() {} }; /// Writable map concept template class WriteMap { public: /// Map's key type. typedef K Key; /// Map's value type. (The type of objects associated with the keys). typedef T Value; /// Sets the value associated with a key. void set(const Key &k,const Value &t) {} ///Default constructor WriteMap() {} }; ///Read/Writable map concept template class ReadWriteMap : public ReadMap, public WriteMap { public: /// Map's key type. typedef K Key; /// Map's value type. (The type of objects associated with the keys). typedef T Value; /// Returns the value associated with a key. Value operator[](const Key &k) const {return Value();} /// Sets the value associated with a key. void set(const Key &k,const Value &t) {} ///Default constructor ReadWriteMap() {} }; ///Dereferable map concept template class ReferenceMap : public ReadWriteMap { public: /// Map's key type. typedef K Key; /// Map's value type. (The type of objects associated with the keys). typedef T Value; protected: Value tmp; public: typedef Value& Reference; /// Map's const reference type. typedef const Value& ConstReference; ///Returns a reference to the value associated to a key. Reference operator[](const Key &i) { return tmp; } ///Returns a const reference to the value associated to a key. ConstReference operator[](const Key &i) const { return tmp; } /// Sets the value associated with a key. void set(const Key &k,const Value &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::Key Key; typedef typename ReadMap::Value Value; void constraints() { // No constraints for constructor. // What are the requirement for the Value? // CopyConstructible? Assignable? None of these? Value v = m[k]; v = m[k]; // FIXME: ignore_unused_variable_warning(v); } ReadMap m; Key k; }; template struct WriteMapConcept { typedef typename WriteMap::Key Key; typedef typename WriteMap::Value Value; void constraints() { // No constraints for constructor. m.set(k, v); } WriteMap m; Key k; Value v; }; template struct ReadWriteMapConcept { void constraints() { function_requires< ReadMapConcept >(); function_requires< WriteMapConcept >(); } }; template struct ReferenceMapConcept { typedef typename ReferenceMap::Key Key; typedef typename ReferenceMap::Value Value; typedef typename ReferenceMap::Reference Reference; // What for is this? typedef typename ReferenceMap::ConstReference ConstReference; void constraints() { function_requires< ReadWriteMapConcept >(); m[k] = v; // Or should we require real reference? // Like this: // Value &vv = m[k]; // ignore_unused_variable_warning(vv); } ReferenceMap m; Key k; Value 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::Value &t; }; // @} } //namespace concept } //namespace lemon #endif // LEMON_CONCEPT_MAPS_H