lemon/concept/maps.h
author alpar
Sat, 27 Aug 2005 11:37:05 +0000
changeset 1660 93792a112fd5
parent 1375 ebdce4f68ac4
child 1719 674182524bd9
permissions -rw-r--r--
- Doc images partly turned on again.
     1 /* -*- C++ -*-
     2  * lemon/concept/maps.h - Part of LEMON, a generic C++ optimization library
     3  *
     4  * Copyright (C) 2005 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/concept_check.h>
    21 
    22 ///\ingroup concept
    23 ///\file
    24 ///\brief Map concepts checking classes for testing and documenting.
    25 
    26 namespace lemon {
    27 
    28   namespace concept {
    29   
    30     /// \addtogroup concept
    31     /// @{
    32 
    33     /// Readable map concept
    34     template<typename K, typename T>
    35     class ReadMap
    36     {
    37     public:
    38       /// Map's key type.
    39       typedef K Key;    
    40       /// Map's value type. (The type of objects associated with the keys).
    41       typedef T Value;
    42 
    43       // \bug Value don't need to be default constructible.
    44       /// Returns the value associated with a key.
    45       Value operator[](const Key &) const {return Value();}
    46 
    47       template<typename _ReadMap>
    48       struct Constraints {
    49 
    50 	void constraints() {
    51 	  Value val = m[key];
    52 	  val = m[key];
    53 	  typename _ReadMap::Value own_val = m[own_key]; 
    54 	  own_val = m[own_key]; 
    55 
    56 	  ignore_unused_variable_warning(val);
    57 	  ignore_unused_variable_warning(own_val);
    58 	  ignore_unused_variable_warning(key);
    59 	}
    60 	Key& key;
    61 	typename _ReadMap::Key& own_key;
    62 	_ReadMap& m;
    63       };
    64       
    65     };
    66 
    67 
    68     /// Writable map concept
    69     template<typename K, typename T>
    70     class WriteMap
    71     {
    72     public:
    73       /// Map's key type.
    74       typedef K Key;    
    75       /// Map's value type. (The type of objects associated with the keys).
    76       typedef T Value;
    77 
    78       /// Sets the value associated with a key.
    79       void set(const Key &,const Value &) {}
    80 
    81       ///Default constructor
    82       WriteMap() {}
    83 
    84       template <typename _WriteMap>
    85       struct Constraints {
    86 	void constraints() {
    87 	  // No constraints for constructor.
    88 	  m.set(key, val);
    89 	  m.set(own_key, own_val);
    90 	  ignore_unused_variable_warning(key);
    91 	  ignore_unused_variable_warning(val);
    92 	  ignore_unused_variable_warning(own_key);
    93 	  ignore_unused_variable_warning(own_val);
    94 	}
    95 
    96 	Value& val;
    97 	typename _WriteMap::Value own_val;
    98 	Key& key;
    99 	typename _WriteMap::Key& own_key;
   100 	WriteMap& m;
   101 
   102       };
   103     };
   104 
   105     ///Read/Writable map concept
   106     template<typename K, typename T>
   107     class ReadWriteMap : public ReadMap<K,T>,
   108 			    public WriteMap<K,T>
   109     {
   110     public:
   111       /// Map's key type.
   112       typedef K Key;    
   113       /// Map's value type. (The type of objects associated with the keys).
   114       typedef T Value;
   115 
   116       /// Returns the value associated with a key.
   117       Value operator[](const Key &) const {return Value();}
   118       /// Sets the value associated with a key.
   119       void set(const Key & ,const Value &) {}
   120 
   121       template<typename _ReadWriteMap>
   122       struct Constraints {
   123 	void constraints() {
   124 	  checkConcept<ReadMap<K, T>, _ReadWriteMap >();
   125 	  checkConcept<ReadMap<K, T>, _ReadWriteMap >();
   126 	}
   127       };
   128     };
   129   
   130   
   131     ///Dereferable map concept
   132     template<typename K, typename T, typename R, typename CR>
   133     class ReferenceMap : public ReadWriteMap<K,T>
   134     {
   135     public:
   136       /// Map's key type.
   137       typedef K Key;    
   138       /// Map's value type. (The type of objects associated with the keys).
   139       typedef T Value;
   140       /// Map's reference type.
   141       typedef R Reference;
   142       /// Map's const reference type.
   143       typedef CR ConstReference;
   144 
   145     protected:
   146       Value tmp;
   147     public:
   148 
   149       ///Returns a reference to the value associated to a key.
   150       Reference operator[](const Key &) { return tmp; }
   151       ///Returns a const reference to the value associated to a key.
   152       ConstReference operator[](const Key &) const
   153       { return tmp; }
   154       /// Sets the value associated with a key.
   155       void set(const Key &k,const Value &t) { operator[](k)=t; }
   156 
   157       // \todo rethink this concept
   158       template<typename _ReferenceMap>
   159       struct ReferenceMapConcept {
   160 
   161 	void constraints() {
   162 	  checkConcept<ReadWriteMap, _ReferenceMap >();
   163 	  m[key] = val;
   164 	  val  = m[key];
   165 	  m[key] = ref;
   166 	  ref = m[key];
   167 	  m[own_key] = own_val;
   168 	  own_val  = m[own_key];
   169 	  m[own_key] = own_ref;
   170 	  own_ref = m[own_key];	  	  
   171 	}
   172 
   173 	typename _ReferenceMap::Key& own_key;
   174 	typename _ReferenceMap::Value& own_val;
   175 	typename _ReferenceMap::Reference& own_ref;
   176 	Key& key;
   177 	Value& val;
   178 	Reference& ref;
   179 	ReferenceMap& m;
   180       };
   181     };
   182 
   183     // @}
   184 
   185   } //namespace concept
   186 } //namespace lemon
   187 #endif // LEMON_CONCEPT_MAPS_H