lemon/concept/maps.h
author deba
Wed, 25 Jan 2006 14:40:26 +0000
changeset 1903 f3d24016dad5
parent 1719 674182524bd9
child 1956 a055123339d5
permissions -rw-r--r--
Bug fix
     1 /* -*- C++ -*-
     2  * lemon/concept/maps.h - Part of LEMON, a generic C++ optimization library
     3  *
     4  * Copyright (C) 2006 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     5  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     6  *
     7  * Permission to use, modify and distribute this software is granted
     8  * provided that this copyright notice appears in all copies. For
     9  * precise terms see the accompanying LICENSE file.
    10  *
    11  * This software is provided "AS IS" with no warranty of any kind,
    12  * express or implied, and with no claim as to its suitability for any
    13  * purpose.
    14  *
    15  */
    16 
    17 #ifndef LEMON_CONCEPT_MAPS_H
    18 #define LEMON_CONCEPT_MAPS_H
    19 
    20 #include <lemon/utility.h>
    21 #include <lemon/concept_check.h>
    22 
    23 ///\ingroup concept
    24 ///\file
    25 ///\brief Map concepts checking classes for testing and documenting.
    26 
    27 namespace lemon {
    28 
    29   namespace concept {
    30   
    31     /// \addtogroup concept
    32     /// @{
    33 
    34     /// Readable map concept
    35     template<typename K, typename T>
    36     class ReadMap
    37     {
    38     public:
    39       /// Map's key type.
    40       typedef K Key;    
    41       /// Map's value type. (The type of objects associated with the keys).
    42       typedef T Value;
    43 
    44       // \bug Value don't need to be default constructible.
    45       /// Returns the value associated with a key.
    46       Value operator[](const Key &) const {return Value();}
    47 
    48       template<typename _ReadMap>
    49       struct Constraints {
    50 
    51 	void constraints() {
    52 	  Value val = m[key];
    53 	  val = m[key];
    54 	  typename _ReadMap::Value own_val = m[own_key]; 
    55 	  own_val = m[own_key]; 
    56 
    57 	  ignore_unused_variable_warning(val);
    58 	  ignore_unused_variable_warning(own_val);
    59 	  ignore_unused_variable_warning(key);
    60 	}
    61 	Key& key;
    62 	typename _ReadMap::Key& own_key;
    63 	_ReadMap& m;
    64       };
    65       
    66     };
    67 
    68 
    69     /// Writable map concept
    70     template<typename K, typename T>
    71     class WriteMap
    72     {
    73     public:
    74       /// Map's key type.
    75       typedef K Key;    
    76       /// Map's value type. (The type of objects associated with the keys).
    77       typedef T Value;
    78 
    79       /// Sets the value associated with a key.
    80       void set(const Key &,const Value &) {}
    81 
    82       ///Default constructor
    83       WriteMap() {}
    84 
    85       template <typename _WriteMap>
    86       struct Constraints {
    87 	void constraints() {
    88 	  // No constraints for constructor.
    89 	  m.set(key, val);
    90 	  m.set(own_key, own_val);
    91 	  ignore_unused_variable_warning(key);
    92 	  ignore_unused_variable_warning(val);
    93 	  ignore_unused_variable_warning(own_key);
    94 	  ignore_unused_variable_warning(own_val);
    95 	}
    96 
    97 	Value& val;
    98 	typename _WriteMap::Value own_val;
    99 	Key& key;
   100 	typename _WriteMap::Key& own_key;
   101 	_WriteMap& m;
   102 
   103       };
   104     };
   105 
   106     ///Read/Writable map concept
   107     template<typename K, typename T>
   108     class ReadWriteMap : public ReadMap<K,T>,
   109 			    public WriteMap<K,T>
   110     {
   111     public:
   112       /// Map's key type.
   113       typedef K Key;    
   114       /// Map's value type. (The type of objects associated with the keys).
   115       typedef T Value;
   116 
   117       /// Returns the value associated with a key.
   118       Value operator[](const Key &) const {return Value();}
   119       /// Sets the value associated with a key.
   120       void set(const Key & ,const Value &) {}
   121 
   122       template<typename _ReadWriteMap>
   123       struct Constraints {
   124 	void constraints() {
   125 	  checkConcept<ReadMap<K, T>, _ReadWriteMap >();
   126 	  checkConcept<WriteMap<K, T>, _ReadWriteMap >();
   127 	}
   128       };
   129     };
   130   
   131   
   132     ///Dereferable map concept
   133     template<typename K, typename T, typename R, typename CR>
   134     class ReferenceMap : public ReadWriteMap<K,T>
   135     {
   136     public:
   137       /// Tag for reference maps.
   138       typedef True ReferenceMapTag;
   139       /// Map's key type.
   140       typedef K Key;    
   141       /// Map's value type. (The type of objects associated with the keys).
   142       typedef T Value;
   143       /// Map's reference type.
   144       typedef R Reference;
   145       /// Map's const reference type.
   146       typedef CR ConstReference;
   147 
   148     protected:
   149       Value tmp;
   150     public:
   151 
   152       ///Returns a reference to the value associated to a key.
   153       Reference operator[](const Key &) { return tmp; }
   154       ///Returns a const reference to the value associated to a key.
   155       ConstReference operator[](const Key &) const
   156       { return tmp; }
   157       /// Sets the value associated with a key.
   158       void set(const Key &k,const Value &t) { operator[](k)=t; }
   159 
   160       // \todo rethink this concept
   161       template<typename _ReferenceMap>
   162       struct ReferenceMapConcept {
   163 
   164 	void constraints() {
   165 	  checkConcept<ReadWriteMap, _ReferenceMap >();
   166 	  m[key] = val;
   167 	  val  = m[key];
   168 	  m[key] = ref;
   169 	  ref = m[key];
   170 	  m[own_key] = own_val;
   171 	  own_val  = m[own_key];
   172 	  m[own_key] = own_ref;
   173 	  own_ref = m[own_key];	  	  
   174 	}
   175 
   176 	typename _ReferenceMap::Key& own_key;
   177 	typename _ReferenceMap::Value& own_val;
   178 	typename _ReferenceMap::Reference& own_ref;
   179 	Key& key;
   180 	Value& val;
   181 	Reference& ref;
   182 	_ReferenceMap& m;
   183       };
   184     };
   185 
   186     // @}
   187 
   188   } //namespace concept
   189 } //namespace lemon
   190 #endif // LEMON_CONCEPT_MAPS_H