klao@959: /* -*- C++ -*-
klao@959:  * src/lemon/concept/maps.h - Part of LEMON, a generic C++ optimization library
klao@959:  *
klao@959:  * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
klao@959:  * (Egervary Combinatorial Optimization Research Group, 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: 
klao@959: #include <lemon/concept_check.h>
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<typename K, typename T>
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<typename _ReadMap>
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<typename K, typename T>
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@987:       void set(const Key &k,const Value &t) {}
klao@959: 
klao@959:       ///Default constructor
klao@959:       WriteMap() {}
deba@989: 
deba@989:       template <typename _WriteMap>
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);
deba@989: 	  ignore_unused_variable(key);
deba@989: 	  ignore_unused_variable(val);
deba@989: 	  ignore_unused_variable(own_key);
deba@989: 	  ignore_unused_variable(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@989: 	WriteMap& m;
deba@989: 
deba@989:       };
klao@959:     };
klao@959: 
klao@959:     ///Read/Writable map concept
klao@959:     template<typename K, typename T>
klao@959:     class ReadWriteMap : public ReadMap<K,T>,
klao@959: 			    public WriteMap<K,T>
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@987:       Value operator[](const Key &k) const {return Value();}
klao@959:       /// Sets the value associated with a key.
alpar@987:       void set(const Key &k,const Value &t) {}
klao@959: 
deba@989:       template<typename _ReadWriteMap>
deba@989:       struct Constraints {
deba@989: 	void constraints() {
deba@989: 	  checkConcept<ReadMap<K, T>, _ReadWriteMap >();
deba@989: 	  checkConcept<ReadMap<K, T>, _ReadWriteMap >();
deba@989: 	}
deba@989:       };
klao@959:     };
klao@959:   
klao@959:   
klao@959:     ///Dereferable map concept
deba@989:     template<typename K, typename T, typename R, typename CR>
klao@959:     class ReferenceMap : public ReadWriteMap<K,T>
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;
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@987:       Reference operator[](const Key &i) { return tmp; }
klao@959:       ///Returns a const reference to the value associated to a key.
alpar@987:       ConstReference operator[](const Key &i) 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<typename _ReferenceMap>
deba@989:       struct ReferenceMapConcept {
deba@989: 
deba@989: 	void constraints() {
deba@989: 	  checkConcept<ReadWriteMap, _ReferenceMap >();
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@989: 	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