COIN-OR::LEMON - Graph Library

Changeset 989:ca95f8b5c931 in lemon-0.x for src/lemon/concept/maps.h


Ignore:
Timestamp:
11/13/04 22:37:54 (19 years ago)
Author:
Balazs Dezso
Branch:
default
Phase:
public
Convert:
svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@1379
Message:

XyzConcept? moved to Xyz::Constraints
use checkConcept in the next way:

checkConcept<ErasableGraph?, ListGraph?>();
checkConcept<ReadWriteMap?<Node, Node>, PredMap?>;

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/lemon/concept/maps.h

    r987 r989  
    4141      typedef T Value;
    4242
     43      // \bug Value don't need to be default constructible.
    4344      /// Returns the value associated with a key.
    44       Value operator[](const Key &k) const {return Value();}
     45      Value operator[](const Key &) const {return Value();}
    4546
    46       ///Default constructor
    47       ReadMap() {}
     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     
    4865    };
    4966
     
    6481      ///Default constructor
    6582      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(key);
     91          ignore_unused_variable(val);
     92          ignore_unused_variable(own_key);
     93          ignore_unused_variable(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      };
    66103    };
    67104
     
    82119      void set(const Key &k,const Value &t) {}
    83120
    84       ///Default constructor
    85       ReadWriteMap() {}
     121      template<typename _ReadWriteMap>
     122      struct Constraints {
     123        void constraints() {
     124          checkConcept<ReadMap<K, T>, _ReadWriteMap >();
     125          checkConcept<ReadMap<K, T>, _ReadWriteMap >();
     126        }
     127      };
    86128    };
    87129 
    88130 
    89131    ///Dereferable map concept
    90     template<typename K, typename T>
     132    template<typename K, typename T, typename R, typename CR>
    91133    class ReferenceMap : public ReadWriteMap<K,T>
    92134    {
     
    96138      /// Map's value type. (The type of objects associated with the keys).
    97139      typedef T Value;
     140      /// Map's reference type.
     141      typedef R Reference;
     142      /// Map's const reference type.
     143      typedef CR ConstReference;
    98144
    99145    protected:
    100146      Value tmp;
    101147    public:
    102       typedef Value& Reference;
    103       /// Map's const reference type.
    104       typedef const Value& ConstReference;
    105148
    106149      ///Returns a reference to the value associated to a key.
     
    112155      void set(const Key &k,const Value &t) { operator[](k)=t; }
    113156
    114       ///Default constructor
    115       ReferenceMap() {}
     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      };
    116181    };
    117 
    118 
    119     template<typename Item, typename T, typename Graph>
    120     class GraphMap : public ReadWriteMap<Item, T> {
    121       // I really, really don't like the idea that every graph should have
    122       // reference maps! --klao
    123 
    124     private:
    125       // We state explicitly that graph maps have no default constructor?
    126       GraphMap();
    127 
    128     public:
    129       explicit GraphMap(Graph const&) {}
    130       // value for initializing
    131       GraphMap(Graph const&, T) {}
    132 
    133       // this probably should be required:
    134       GraphMap(GraphMap const&) {}
    135       GraphMap& operator=(GraphMap const&) { return *this; }
    136 
    137       // but this is a absolute no-op! We should provide a more generic
    138       // graph-map-copy operation.
    139       //
    140       // template<typename TT>
    141       // GraphMap(GraphMap<TT> const&);
    142       //
    143       // template<typename TT>
    144       // GraphMap& operator=(const GraphMap<TT>&);
    145     };
    146 
    147 
    148     /****************  Concept-checking classes  ****************/
    149 
    150     template<typename ReadMap>
    151     struct ReadMapConcept {
    152       typedef typename ReadMap::Key Key;
    153       typedef typename ReadMap::Value Value;
    154 
    155       void constraints() {
    156         // No constraints for constructor.
    157 
    158         // What are the requirement for the Value?
    159         // CopyConstructible? Assignable? None of these?
    160         Value v = m[k];
    161         v = m[k];
    162 
    163         // FIXME:
    164         ignore_unused_variable_warning(v);
    165       }
    166 
    167       ReadMap m;
    168       Key k;
    169     };
    170 
    171     template<typename WriteMap>
    172     struct WriteMapConcept {
    173       typedef typename WriteMap::Key Key;
    174       typedef typename WriteMap::Value Value;
    175 
    176       void constraints() {
    177         // No constraints for constructor.
    178 
    179         m.set(k, v);
    180       }
    181 
    182       WriteMap m;
    183       Key k;
    184       Value v;
    185     };
    186 
    187     template<typename ReadWriteMap>
    188     struct ReadWriteMapConcept {
    189       void constraints() {
    190         function_requires< ReadMapConcept<ReadWriteMap> >();
    191         function_requires< WriteMapConcept<ReadWriteMap> >();
    192       }
    193     };
    194 
    195     template<typename ReferenceMap>
    196     struct ReferenceMapConcept {
    197       typedef typename ReferenceMap::Key Key;
    198       typedef typename ReferenceMap::Value Value;
    199       typedef typename ReferenceMap::Reference Reference;
    200 
    201       // What for is this?
    202       typedef typename ReferenceMap::ConstReference ConstReference;
    203 
    204       void constraints() {
    205         function_requires< ReadWriteMapConcept<ReferenceMap> >();
    206 
    207         m[k] = v;
    208         // Or should we require real reference?
    209         // Like this:
    210         // Value &vv = m[k];
    211         // ignore_unused_variable_warning(vv);
    212       }
    213 
    214       ReferenceMap m;
    215       Key k;
    216       Value v;
    217     };
    218 
    219     /// \todo GraphMapConceptCheck
    220 
    221     template<typename GraphMap, typename Graph>
    222     struct GraphMapConcept {
    223       void constraints() {
    224         function_requires< ReadWriteMapConcept<GraphMap> >();
    225         // Construction with a graph parameter
    226         GraphMap a(g);
    227         // Ctor with a graph and a default value parameter
    228         GraphMap a2(g,t);
    229         // Copy ctor. Do we need it?
    230         GraphMap b=c;
    231         // Copy operator. Do we need it?
    232         a=b;
    233 
    234         ignore_unused_variable_warning(a2);
    235       }
    236       const GraphMap &c;
    237       const Graph &g;
    238       const typename GraphMap::Value &t;
    239     };
    240    
    241182
    242183    // @}
Note: See TracChangeset for help on using the changeset viewer.