lemon/concepts/maps.h
author Peter Kovacs <kpeter@inf.elte.hu>
Fri, 04 Jan 2008 08:16:15 +0100
changeset 30 72364ba3466d
child 28 e337bdf46777
permissions -rw-r--r--
Bug fix in maps.h.
     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       /// Returns the value associated with a key.
    47 
    48       /// \bug Value should n't need to be default constructible.
    49       ///
    50       Value operator[](const Key &) const {return Value();}
    51 
    52       template<typename _ReadMap>
    53       struct Constraints {
    54 
    55 	void constraints() {
    56 	  Value val = m[key];
    57 	  val = m[key];
    58 	  typename _ReadMap::Value own_val = m[own_key]; 
    59 	  own_val = m[own_key]; 
    60 
    61 	  ignore_unused_variable_warning(val);
    62 	  ignore_unused_variable_warning(own_val);
    63 	  ignore_unused_variable_warning(key);
    64 	}
    65 	Key& key;
    66 	typename _ReadMap::Key& own_key;
    67 	_ReadMap& m;
    68       };
    69       
    70     };
    71 
    72 
    73     /// Writable map concept
    74     template<typename K, typename T>
    75     class WriteMap
    76     {
    77     public:
    78       /// Map's key type.
    79       typedef K Key;    
    80       /// Map's value type. (The type of objects associated with the keys).
    81       typedef T Value;
    82 
    83       /// Sets the value associated with a key.
    84       void set(const Key &,const Value &) {}
    85 
    86       ///Default constructor
    87       WriteMap() {}
    88 
    89       template <typename _WriteMap>
    90       struct Constraints {
    91 	void constraints() {
    92 	  // No constraints for constructor.
    93 	  m.set(key, val);
    94 	  m.set(own_key, own_val);
    95 	  ignore_unused_variable_warning(key);
    96 	  ignore_unused_variable_warning(val);
    97 	  ignore_unused_variable_warning(own_key);
    98 	  ignore_unused_variable_warning(own_val);
    99 	}
   100 
   101 	Value& val;
   102 	typename _WriteMap::Value own_val;
   103 	Key& key;
   104 	typename _WriteMap::Key& own_key;
   105 	_WriteMap& m;
   106 
   107       };
   108     };
   109 
   110     ///Read/Writable map concept
   111     template<typename K, typename T>
   112     class ReadWriteMap : public ReadMap<K,T>,
   113 			    public WriteMap<K,T>
   114     {
   115     public:
   116       /// Map's key type.
   117       typedef K Key;    
   118       /// Map's value type. (The type of objects associated with the keys).
   119       typedef T Value;
   120 
   121       /// Returns the value associated with a key.
   122       Value operator[](const Key &) const {return Value();}
   123       /// Sets the value associated with a key.
   124       void set(const Key & ,const Value &) {}
   125 
   126       template<typename _ReadWriteMap>
   127       struct Constraints {
   128 	void constraints() {
   129 	  checkConcept<ReadMap<K, T>, _ReadWriteMap >();
   130 	  checkConcept<WriteMap<K, T>, _ReadWriteMap >();
   131 	}
   132       };
   133     };
   134   
   135   
   136     ///Dereferable map concept
   137     template<typename K, typename T, typename R, typename CR>
   138     class ReferenceMap : public ReadWriteMap<K,T>
   139     {
   140     public:
   141       /// Tag for reference maps.
   142       typedef True ReferenceMapTag;
   143       /// Map's key type.
   144       typedef K Key;    
   145       /// Map's value type. (The type of objects associated with the keys).
   146       typedef T Value;
   147       /// Map's reference type.
   148       typedef R Reference;
   149       /// Map's const reference type.
   150       typedef CR ConstReference;
   151 
   152     protected:
   153       Value tmp;
   154     public:
   155 
   156       ///Returns a reference to the value associated to a key.
   157       Reference operator[](const Key &) { return tmp; }
   158       ///Returns a const reference to the value associated to a key.
   159       ConstReference operator[](const Key &) const
   160       { return tmp; }
   161       /// Sets the value associated with a key.
   162       void set(const Key &k,const Value &t) { operator[](k)=t; }
   163 
   164       // \todo rethink this concept
   165       template<typename _ReferenceMap>
   166       struct ReferenceMapConcept {
   167 
   168 	void constraints() {
   169 	  checkConcept<ReadWriteMap, _ReferenceMap >();
   170 	  m[key] = val;
   171 	  val  = m[key];
   172 	  m[key] = ref;
   173 	  ref = m[key];
   174 	  m[own_key] = own_val;
   175 	  own_val  = m[own_key];
   176 	  m[own_key] = own_ref;
   177 	  own_ref = m[own_key];	  	  
   178 	}
   179 
   180 	typename _ReferenceMap::Key& own_key;
   181 	typename _ReferenceMap::Value& own_val;
   182 	typename _ReferenceMap::Reference& own_ref;
   183 	Key& key;
   184 	Value& val;
   185 	Reference& ref;
   186 	_ReferenceMap& m;
   187       };
   188     };
   189 
   190     // @}
   191 
   192   } //namespace concepts
   193 } //namespace lemon
   194 #endif // LEMON_CONCEPT_MAPS_H