lemon/concepts/maps.h
author Alpar Juttner <alpar@cs.elte.hu>
Fri, 05 Aug 2011 00:22:15 +0200
branch1.0
changeset 428 a1d8d1736c68
parent 220 a5d8c039f218
permissions -rw-r--r--
LEMON 1.0.7 released (c59bdcc8e33e tagged as r1.0.7)
     1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
     2  *
     3  * This file is a part of LEMON, a generic C++ optimization library.
     4  *
     5  * Copyright (C) 2003-2008
     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/core.h>
    23 #include <lemon/concept_check.h>
    24 
    25 ///\ingroup map_concepts
    26 ///\file
    27 ///\brief The concept of maps.
    28 
    29 namespace lemon {
    30 
    31   namespace concepts {
    32 
    33     /// \addtogroup map_concepts
    34     /// @{
    35 
    36     /// Readable map concept
    37 
    38     /// Readable map concept.
    39     ///
    40     template<typename K, typename T>
    41     class ReadMap
    42     {
    43     public:
    44       /// The key type of the map.
    45       typedef K Key;
    46       /// \brief The value type of the map.
    47       /// (The type of objects associated with the keys).
    48       typedef T Value;
    49 
    50       /// Returns the value associated with the given key.
    51       Value operator[](const Key &) const {
    52         return *static_cast<Value *>(0);
    53       }
    54 
    55       template<typename _ReadMap>
    56       struct Constraints {
    57         void constraints() {
    58           Value val = m[key];
    59           val = m[key];
    60           typename _ReadMap::Value own_val = m[own_key];
    61           own_val = m[own_key];
    62 
    63           ignore_unused_variable_warning(key);
    64           ignore_unused_variable_warning(val);
    65           ignore_unused_variable_warning(own_key);
    66           ignore_unused_variable_warning(own_val);
    67         }
    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       /// \brief The value type of the map.
    87       /// (The type of objects associated with the keys).
    88       typedef T Value;
    89 
    90       /// Sets the value associated with the given key.
    91       void set(const Key &, const Value &) {}
    92 
    93       /// Default constructor.
    94       WriteMap() {}
    95 
    96       template <typename _WriteMap>
    97       struct Constraints {
    98         void constraints() {
    99           m.set(key, val);
   100           m.set(own_key, own_val);
   101 
   102           ignore_unused_variable_warning(key);
   103           ignore_unused_variable_warning(val);
   104           ignore_unused_variable_warning(own_key);
   105           ignore_unused_variable_warning(own_val);
   106         }
   107         const Key& key;
   108         const Value& val;
   109         const typename _WriteMap::Key& own_key;
   110         const typename _WriteMap::Value& own_val;
   111         _WriteMap& m;
   112       };
   113     };
   114 
   115     /// Read/writable map concept
   116 
   117     /// Read/writable map concept.
   118     ///
   119     template<typename K, typename T>
   120     class ReadWriteMap : public ReadMap<K,T>,
   121                          public WriteMap<K,T>
   122     {
   123     public:
   124       /// The key type of the map.
   125       typedef K Key;
   126       /// \brief The value type of the map.
   127       /// (The type of objects associated with the keys).
   128       typedef T Value;
   129 
   130       /// Returns the value associated with the given key.
   131       Value operator[](const Key &) const {
   132         return *static_cast<Value *>(0);
   133       }
   134 
   135       /// Sets the value associated with the given key.
   136       void set(const Key &, const Value &) {}
   137 
   138       template<typename _ReadWriteMap>
   139       struct Constraints {
   140         void constraints() {
   141           checkConcept<ReadMap<K, T>, _ReadWriteMap >();
   142           checkConcept<WriteMap<K, T>, _ReadWriteMap >();
   143         }
   144       };
   145     };
   146 
   147 
   148     /// Dereferable map concept
   149 
   150     /// Dereferable map concept.
   151     ///
   152     template<typename K, typename T, typename R, typename CR>
   153     class ReferenceMap : public ReadWriteMap<K,T>
   154     {
   155     public:
   156       /// Tag for reference maps.
   157       typedef True ReferenceMapTag;
   158       /// The key type of the map.
   159       typedef K Key;
   160       /// \brief The value type of the map.
   161       /// (The type of objects associated with the keys).
   162       typedef T Value;
   163       /// The reference type of the map.
   164       typedef R Reference;
   165       /// The const reference type of the map.
   166       typedef CR ConstReference;
   167 
   168     public:
   169 
   170       /// Returns a reference to the value associated with the given key.
   171       Reference operator[](const Key &) {
   172         return *static_cast<Value *>(0);
   173       }
   174 
   175       /// Returns a const reference to the value associated with the given key.
   176       ConstReference operator[](const Key &) const {
   177         return *static_cast<Value *>(0);
   178       }
   179 
   180       /// Sets the value associated with the given key.
   181       void set(const Key &k,const Value &t) { operator[](k)=t; }
   182 
   183       template<typename _ReferenceMap>
   184       struct Constraints {
   185         void constraints() {
   186           checkConcept<ReadWriteMap<K, T>, _ReferenceMap >();
   187           ref = m[key];
   188           m[key] = val;
   189           m[key] = ref;
   190           m[key] = cref;
   191           own_ref = m[own_key];
   192           m[own_key] = own_val;
   193           m[own_key] = own_ref;
   194           m[own_key] = own_cref;
   195           m[key] = m[own_key];
   196           m[own_key] = m[key];
   197         }
   198         const Key& key;
   199         Value& val;
   200         Reference ref;
   201         ConstReference cref;
   202         const typename _ReferenceMap::Key& own_key;
   203         typename _ReferenceMap::Value& own_val;
   204         typename _ReferenceMap::Reference own_ref;
   205         typename _ReferenceMap::ConstReference own_cref;
   206         _ReferenceMap& m;
   207       };
   208     };
   209 
   210     // @}
   211 
   212   } //namespace concepts
   213 
   214 } //namespace lemon
   215 
   216 #endif // LEMON_CONCEPT_MAPS_H