COIN-OR::LEMON - Graph Library

Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • lemon/concepts/maps.h

    r74 r94  
    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      Value operator[](const Key &) const {
     51        return *static_cast<Value *>(0);
     52      }
    5553
    5654      template<typename _ReadMap>
     
    5957          Value val = m[key];
    6058          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);
     59          typename _ReadMap::Value own_val = m[own_key];
     60          own_val = m[own_key];
     61
    10162          ignore_unused_variable_warning(key);
    10263          ignore_unused_variable_warning(val);
     
    10465          ignore_unused_variable_warning(own_val);
    10566        }
    106 
    107         Value& val;
    108         typename _WriteMap::Value own_val;
    109         Key& key;
    110         typename _WriteMap::Key& own_key;
     67        const Key& key;
     68        const typename _ReadMap::Key& own_key;
     69        const _ReadMap& m;
     70      };
     71
     72    };
     73
     74
     75    /// Writable map concept
     76
     77    /// Writable map concept.
     78    ///
     79    template<typename K, typename T>
     80    class WriteMap
     81    {
     82    public:
     83      /// The key type of the map.
     84      typedef K Key;
     85      /// The value type of the map. (The type of objects associated with the keys).
     86      typedef T Value;
     87
     88      /// Sets the value associated with the given key.
     89      void set(const Key &, const Value &) {}
     90
     91      /// Default constructor.
     92      WriteMap() {}
     93
     94      template <typename _WriteMap>
     95      struct Constraints {
     96        void constraints() {
     97          m.set(key, val);
     98          m.set(own_key, own_val);
     99
     100          ignore_unused_variable_warning(key);
     101          ignore_unused_variable_warning(val);
     102          ignore_unused_variable_warning(own_key);
     103          ignore_unused_variable_warning(own_val);
     104        }
     105        const Key& key;
     106        const Value& val;
     107        const typename _WriteMap::Key& own_key;
     108        const typename _WriteMap::Value own_val;
    111109        _WriteMap& m;
    112 
    113110      };
    114111    };
    115112
    116113    /// Read/writable map concept
    117    
     114
    118115    /// Read/writable map concept.
    119116    ///
     
    124121    public:
    125122      /// 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 &) {}
     123      typedef K Key;
     124      /// The value type of the map. (The type of objects associated with the keys).
     125      typedef T Value;
     126
     127      /// Returns the value associated with the given key.
     128      Value operator[](const Key &) const {
     129        return *static_cast<Value *>(0);
     130      }
     131
     132      /// Sets the value associated with the given key.
     133      void set(const Key &, const Value &) {}
    134134
    135135      template<typename _ReadWriteMap>
     
    141141      };
    142142    };
    143  
    144  
     143
     144
    145145    /// Dereferable map concept
    146    
     146
    147147    /// Dereferable map concept.
    148148    ///
    149     /// \todo Rethink this concept.
    150149    template<typename K, typename T, typename R, typename CR>
    151150    class ReferenceMap : public ReadWriteMap<K,T>
     
    155154      typedef True ReferenceMapTag;
    156155      /// The key type of the map.
    157       typedef K Key;   
     156      typedef K Key;
    158157      /// The value type of the map. (The type of objects associated with the keys).
    159158      typedef T Value;
     
    163162      typedef CR ConstReference;
    164163
    165     protected:
    166       Value tmp;
    167     public:
    168 
    169       ///Returns a reference to the value associated with a key.
    170       Reference operator[](const Key &) { return tmp; }
    171       ///Returns a const reference to the value associated with a key.
    172       ConstReference operator[](const Key &) const { return tmp; }
    173       /// Sets the value associated with a key.
     164    public:
     165
     166      /// Returns a reference to the value associated with the given key.
     167      Reference operator[](const Key &) {
     168        return *static_cast<Value *>(0);
     169      }
     170
     171      /// Returns a const reference to the value associated with the given key.
     172      ConstReference operator[](const Key &) const {
     173        return *static_cast<Value *>(0);
     174      }
     175
     176      /// Sets the value associated with the given key.
    174177      void set(const Key &k,const Value &t) { operator[](k)=t; }
    175178
     
    178181        void constraints() {
    179182          checkConcept<ReadWriteMap<K, T>, _ReferenceMap >();
     183          ref = m[key];
    180184          m[key] = val;
    181           val  = m[key];
    182185          m[key] = ref;
    183           ref = m[key];
     186          m[key] = cref;
     187          own_ref = m[own_key];
    184188          m[own_key] = own_val;
    185           own_val  = m[own_key];
    186189          m[own_key] = own_ref;
    187           own_ref = m[own_key];           
    188         }
    189 
    190         typename _ReferenceMap::Key& own_key;
     190          m[own_key] = own_cref;
     191          m[key] = m[own_key];
     192          m[own_key] = m[key];
     193        }
     194        const Key& key;
     195        Value& val;
     196        Reference ref;
     197        ConstReference cref;
     198        const typename _ReferenceMap::Key& own_key;
    191199        typename _ReferenceMap::Value& own_val;
    192200        typename _ReferenceMap::Reference own_ref;
    193         Key& key;
    194         Value& val;
    195         Reference ref;
     201        typename _ReferenceMap::ConstReference own_cref;
    196202        _ReferenceMap& m;
    197203      };
Note: See TracChangeset for help on using the changeset viewer.