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