lemon/concepts/maps.h
author Alpar Juttner <alpar@cs.elte.hu>
Mon, 30 Jan 2012 10:54:49 +0100
changeset 981 cdd2c7dad989
parent 718 703ebf476a1d
child 997 761fe0846f49
permissions -rw-r--r--
Remove autotool related files (#434)
     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-2009
     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_CONCEPTS_MAPS_H
    20 #define LEMON_CONCEPTS_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         Constraints() {}
    72       };
    73 
    74     };
    75 
    76 
    77     /// Writable map concept
    78 
    79     /// Writable map concept.
    80     ///
    81     template<typename K, typename T>
    82     class WriteMap
    83     {
    84     public:
    85       /// The key type of the map.
    86       typedef K Key;
    87       /// \brief The value type of the map.
    88       /// (The type of objects associated with the keys).
    89       typedef T Value;
    90 
    91       /// Sets the value associated with the given key.
    92       void set(const Key &, const Value &) {}
    93 
    94       /// Default constructor.
    95       WriteMap() {}
    96 
    97       template <typename _WriteMap>
    98       struct Constraints {
    99         void constraints() {
   100           m.set(key, val);
   101           m.set(own_key, own_val);
   102 
   103           ignore_unused_variable_warning(key);
   104           ignore_unused_variable_warning(val);
   105           ignore_unused_variable_warning(own_key);
   106           ignore_unused_variable_warning(own_val);
   107         }
   108         const Key& key;
   109         const Value& val;
   110         const typename _WriteMap::Key& own_key;
   111         const typename _WriteMap::Value& own_val;
   112         _WriteMap& m;
   113         Constraints() {}
   114       };
   115     };
   116 
   117     /// Read/writable map concept
   118 
   119     /// Read/writable map concept.
   120     ///
   121     template<typename K, typename T>
   122     class ReadWriteMap : public ReadMap<K,T>,
   123                          public WriteMap<K,T>
   124     {
   125     public:
   126       /// The key type of the map.
   127       typedef K Key;
   128       /// \brief The value type of the map.
   129       /// (The type of objects associated with the keys).
   130       typedef T Value;
   131 
   132       /// Returns the value associated with the given key.
   133       Value operator[](const Key &) const {
   134         Value *r = 0;
   135         return *r;
   136       }
   137 
   138       /// Sets the value associated with the given key.
   139       void set(const Key &, const Value &) {}
   140 
   141       template<typename _ReadWriteMap>
   142       struct Constraints {
   143         void constraints() {
   144           checkConcept<ReadMap<K, T>, _ReadWriteMap >();
   145           checkConcept<WriteMap<K, T>, _ReadWriteMap >();
   146         }
   147       };
   148     };
   149 
   150 
   151     /// Dereferable map concept
   152 
   153     /// Dereferable map concept.
   154     ///
   155     template<typename K, typename T, typename R, typename CR>
   156     class ReferenceMap : public ReadWriteMap<K,T>
   157     {
   158     public:
   159       /// Tag for reference maps.
   160       typedef True ReferenceMapTag;
   161       /// The key type of the map.
   162       typedef K Key;
   163       /// \brief The value type of the map.
   164       /// (The type of objects associated with the keys).
   165       typedef T Value;
   166       /// The reference type of the map.
   167       typedef R Reference;
   168       /// The const reference type of the map.
   169       typedef CR ConstReference;
   170 
   171     public:
   172 
   173       /// Returns a reference to the value associated with the given key.
   174       Reference operator[](const Key &) {
   175         Value *r = 0;
   176         return *r;
   177       }
   178 
   179       /// Returns a const reference to the value associated with the given key.
   180       ConstReference operator[](const Key &) const {
   181         Value *r = 0;
   182         return *r;
   183       }
   184 
   185       /// Sets the value associated with the given key.
   186       void set(const Key &k,const Value &t) { operator[](k)=t; }
   187 
   188       template<typename _ReferenceMap>
   189       struct Constraints {
   190         typename enable_if<typename _ReferenceMap::ReferenceMapTag, void>::type
   191         constraints() {
   192           checkConcept<ReadWriteMap<K, T>, _ReferenceMap >();
   193           ref = m[key];
   194           m[key] = val;
   195           m[key] = ref;
   196           m[key] = cref;
   197           own_ref = m[own_key];
   198           m[own_key] = own_val;
   199           m[own_key] = own_ref;
   200           m[own_key] = own_cref;
   201           m[key] = m[own_key];
   202           m[own_key] = m[key];
   203         }
   204         const Key& key;
   205         Value& val;
   206         Reference ref;
   207         ConstReference cref;
   208         const typename _ReferenceMap::Key& own_key;
   209         typename _ReferenceMap::Value& own_val;
   210         typename _ReferenceMap::Reference own_ref;
   211         typename _ReferenceMap::ConstReference own_cref;
   212         _ReferenceMap& m;
   213         Constraints() {}
   214       };
   215     };
   216 
   217     // @}
   218 
   219   } //namespace concepts
   220 
   221 } //namespace lemon
   222 
   223 #endif