COIN-OR::LEMON - Graph Library

Changeset 79:d73c2e8b25cb in lemon for lemon


Ignore:
Timestamp:
03/15/08 20:21:21 (16 years ago)
Author:
Peter Kovacs <kpeter@…>
Branch:
default
Phase:
public
Message:

More exact concept checking for map concepts.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • lemon/concepts/maps.h

    r74 r79  
    3030
    3131  namespace concepts {
    32  
     32
    3333    /// \addtogroup concept
    3434    /// @{
     
    4343    public:
    4444      /// The key type of the map.
    45       typedef K Key;   
    46       /// The value type of the map. (The type of objects associated with the keys).
    47       typedef T Value;
    48 
    49       /// Returns the value associated with a key.
    50 
    51       /// Returns the value associated with a key.
    52       /// \bug Value shouldn't need to be default constructible.
    53       ///
    54       Value operator[](const Key &) const {return Value();}
     45      typedef K Key;
     46      /// The value type of the map. (The type of objects associated with the keys).
     47      typedef T Value;
     48
     49      /// Returns the value associated with the given key.
     50
     51      /// Returns the value associated with the given key.
     52      /// \bug Value shouldn't need to be default constructible.
     53      Value operator[](const Key &) const { return Value(); }
    5554
    5655      template<typename _ReadMap>
     
    5958          Value val = m[key];
    6059          val = m[key];
    61           typename _ReadMap::Value own_val = m[own_key];
    62           own_val = m[own_key];
    63 
    64           ignore_unused_variable_warning(val);
    65           ignore_unused_variable_warning(own_val);
    66           ignore_unused_variable_warning(key);
    67         }
    68         Key& key;
    69         typename _ReadMap::Key& own_key;
    70         _ReadMap& m;
    71       };
    72      
    73     };
    74 
    75 
    76     /// Writable map concept
    77    
    78     /// Writable map concept.
    79     ///
    80     template<typename K, typename T>
    81     class WriteMap
    82     {
    83     public:
    84       /// The key type of the map.
    85       typedef K Key;   
    86       /// The value type of the map. (The type of objects associated with the keys).
    87       typedef T Value;
    88 
    89       /// Sets the value associated with a key.
    90       void set(const Key &,const Value &) {}
    91 
    92       ///Default constructor
    93       WriteMap() {}
    94 
    95       template <typename _WriteMap>
    96       struct Constraints {
    97         void constraints() {
    98           // No constraints for constructor.
    99           m.set(key, val);
    100           m.set(own_key, own_val);
     60          typename _ReadMap::Value own_val = m[own_key];
     61          own_val = m[own_key];
     62
    10163          ignore_unused_variable_warning(key);
    10264          ignore_unused_variable_warning(val);
     
    10466          ignore_unused_variable_warning(own_val);
    10567        }
    106 
    107         Value& val;
    108         typename _WriteMap::Value own_val;
    109         Key& key;
    110         typename _WriteMap::Key& own_key;
     68        const Key& key;
     69        const typename _ReadMap::Key& own_key;
     70        const _ReadMap& m;
     71      };
     72
     73    };
     74
     75
     76    /// Writable map concept
     77
     78    /// Writable map concept.
     79    ///
     80    template<typename K, typename T>
     81    class WriteMap
     82    {
     83    public:
     84      /// The key type of the map.
     85      typedef K Key;
     86      /// The value type of the map. (The type of objects associated with the keys).
     87      typedef T Value;
     88
     89      /// Sets the value associated with the given key.
     90      void set(const Key &, const Value &) {}
     91
     92      /// Default constructor.
     93      WriteMap() {}
     94
     95      template <typename _WriteMap>
     96      struct Constraints {
     97        void constraints() {
     98          m.set(key, val);
     99          m.set(own_key, own_val);
     100
     101          ignore_unused_variable_warning(key);
     102          ignore_unused_variable_warning(val);
     103          ignore_unused_variable_warning(own_key);
     104          ignore_unused_variable_warning(own_val);
     105        }
     106        const Key& key;
     107        const Value& val;
     108        const typename _WriteMap::Key& own_key;
     109        const typename _WriteMap::Value own_val;
    111110        _WriteMap& m;
    112 
    113111      };
    114112    };
    115113
    116114    /// Read/writable map concept
    117    
     115
    118116    /// Read/writable map concept.
    119117    ///
     
    124122    public:
    125123      /// The key type of the map.
    126       typedef K Key;   
    127       /// The value type of the map. (The type of objects associated with the keys).
    128       typedef T Value;
    129 
    130       /// Returns the value associated with a key.
    131       Value operator[](const Key &) const {return Value();}
    132       /// Sets the value associated with a key.
    133       void set(const Key & ,const Value &) {}
     124      typedef K Key;
     125      /// The value type of the map. (The type of objects associated with the keys).
     126      typedef T Value;
     127
     128      /// Returns the value associated with the given key.
     129      Value operator[](const Key &) const { return Value(); }
     130
     131      /// Sets the value associated with the given key.
     132      void set(const Key &, const Value &) {}
    134133
    135134      template<typename _ReadWriteMap>
     
    141140      };
    142141    };
    143  
    144  
     142
     143
    145144    /// Dereferable map concept
    146    
     145
    147146    /// Dereferable map concept.
    148147    ///
    149     /// \todo Rethink this concept.
    150148    template<typename K, typename T, typename R, typename CR>
    151149    class ReferenceMap : public ReadWriteMap<K,T>
     
    155153      typedef True ReferenceMapTag;
    156154      /// The key type of the map.
    157       typedef K Key;   
     155      typedef K Key;
    158156      /// The value type of the map. (The type of objects associated with the keys).
    159157      typedef T Value;
     
    167165    public:
    168166
    169       ///Returns a reference to the value associated with a key.
     167      /// Returns a reference to the value associated with the given key.
    170168      Reference operator[](const Key &) { return tmp; }
    171       ///Returns a const reference to the value associated with a key.
     169
     170      /// Returns a const reference to the value associated with the given key.
    172171      ConstReference operator[](const Key &) const { return tmp; }
    173       /// Sets the value associated with a key.
     172
     173      /// Sets the value associated with the given key.
    174174      void set(const Key &k,const Value &t) { operator[](k)=t; }
    175175
     
    178178        void constraints() {
    179179          checkConcept<ReadWriteMap<K, T>, _ReferenceMap >();
     180          ref = m[key];
    180181          m[key] = val;
    181           val  = m[key];
    182182          m[key] = ref;
    183           ref = m[key];
     183          m[key] = cref;
     184          own_ref = m[own_key];
    184185          m[own_key] = own_val;
    185           own_val  = m[own_key];
    186186          m[own_key] = own_ref;
    187           own_ref = m[own_key];           
    188         }
    189 
    190         typename _ReferenceMap::Key& own_key;
     187          m[own_key] = own_cref;
     188          m[key] = m[own_key];
     189          m[own_key] = m[key];
     190        }
     191        const Key& key;
     192        Value& val;
     193        Reference ref;
     194        ConstReference cref;
     195        const typename _ReferenceMap::Key& own_key;
    191196        typename _ReferenceMap::Value& own_val;
    192197        typename _ReferenceMap::Reference own_ref;
    193         Key& key;
    194         Value& val;
    195         Reference ref;
     198        typename _ReferenceMap::ConstReference own_cref;
    196199        _ReferenceMap& m;
    197200      };
Note: See TracChangeset for help on using the changeset viewer.