COIN-OR::LEMON - Graph Library

source: lemon/lemon/concepts/maps.h @ 93:f857981306ea

Last change on this file since 93:f857981306ea was 79:d73c2e8b25cb, checked in by Peter Kovacs <kpeter@…>, 16 years ago

More exact concept checking for map concepts.

File size: 5.3 KB
RevLine 
[25]1/* -*- C++ -*-
2 *
3 * This file is a part of LEMON, a generic C++ optimization library
4 *
[39]5 * Copyright (C) 2003-2008
[25]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/bits/utility.h>
23#include <lemon/concept_check.h>
24
25///\ingroup concept
26///\file
27///\brief Map concepts checking classes for testing and documenting.
28
29namespace lemon {
30
31  namespace concepts {
[79]32
[25]33    /// \addtogroup concept
34    /// @{
35
36    /// Readable map concept
[28]37
38    /// Readable map concept.
39    ///
[25]40    template<typename K, typename T>
41    class ReadMap
42    {
43    public:
[35]44      /// The key type of the map.
[79]45      typedef K Key;
[35]46      /// The value type of the map. (The type of objects associated with the keys).
[25]47      typedef T Value;
48
[79]49      /// Returns the value associated with the given key.
[25]50
[79]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(); }
[25]54
55      template<typename _ReadMap>
56      struct Constraints {
57        void constraints() {
58          Value val = m[key];
59          val = m[key];
[79]60          typename _ReadMap::Value own_val = m[own_key];
61          own_val = m[own_key];
[25]62
[79]63          ignore_unused_variable_warning(key);
[25]64          ignore_unused_variable_warning(val);
[79]65          ignore_unused_variable_warning(own_key);
[25]66          ignore_unused_variable_warning(own_val);
67        }
[79]68        const Key& key;
69        const typename _ReadMap::Key& own_key;
70        const _ReadMap& m;
[25]71      };
[79]72
[25]73    };
74
75
76    /// Writable map concept
[79]77
[28]78    /// Writable map concept.
79    ///
[25]80    template<typename K, typename T>
81    class WriteMap
82    {
83    public:
[35]84      /// The key type of the map.
[79]85      typedef K Key;
[35]86      /// The value type of the map. (The type of objects associated with the keys).
[25]87      typedef T Value;
88
[79]89      /// Sets the value associated with the given key.
90      void set(const Key &, const Value &) {}
[25]91
[79]92      /// Default constructor.
[25]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);
[79]100
[25]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        }
[79]106        const Key& key;
107        const Value& val;
108        const typename _WriteMap::Key& own_key;
109        const typename _WriteMap::Value own_val;
[25]110        _WriteMap& m;
111      };
112    };
113
[48]114    /// Read/writable map concept
[79]115
[28]116    /// Read/writable map concept.
117    ///
[25]118    template<typename K, typename T>
119    class ReadWriteMap : public ReadMap<K,T>,
[35]120                         public WriteMap<K,T>
[25]121    {
122    public:
[35]123      /// The key type of the map.
[79]124      typedef K Key;
[35]125      /// The value type of the map. (The type of objects associated with the keys).
[25]126      typedef T Value;
127
[79]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 &) {}
[25]133
134      template<typename _ReadWriteMap>
135      struct Constraints {
136        void constraints() {
137          checkConcept<ReadMap<K, T>, _ReadWriteMap >();
138          checkConcept<WriteMap<K, T>, _ReadWriteMap >();
139        }
140      };
141    };
[79]142
143
[28]144    /// Dereferable map concept
[79]145
[28]146    /// Dereferable map concept.
147    ///
[25]148    template<typename K, typename T, typename R, typename CR>
149    class ReferenceMap : public ReadWriteMap<K,T>
150    {
151    public:
152      /// Tag for reference maps.
153      typedef True ReferenceMapTag;
[35]154      /// The key type of the map.
[79]155      typedef K Key;
[35]156      /// The value type of the map. (The type of objects associated with the keys).
[25]157      typedef T Value;
[35]158      /// The reference type of the map.
[25]159      typedef R Reference;
[35]160      /// The const reference type of the map.
[25]161      typedef CR ConstReference;
162
163    protected:
164      Value tmp;
165    public:
166
[79]167      /// Returns a reference to the value associated with the given key.
[25]168      Reference operator[](const Key &) { return tmp; }
[79]169
170      /// Returns a const reference to the value associated with the given key.
[28]171      ConstReference operator[](const Key &) const { return tmp; }
[79]172
173      /// Sets the value associated with the given key.
[25]174      void set(const Key &k,const Value &t) { operator[](k)=t; }
175
176      template<typename _ReferenceMap>
[74]177      struct Constraints {
[25]178        void constraints() {
[74]179          checkConcept<ReadWriteMap<K, T>, _ReferenceMap >();
[79]180          ref = m[key];
[25]181          m[key] = val;
182          m[key] = ref;
[79]183          m[key] = cref;
184          own_ref = m[own_key];
[25]185          m[own_key] = own_val;
186          m[own_key] = own_ref;
[79]187          m[own_key] = own_cref;
188          m[key] = m[own_key];
189          m[own_key] = m[key];
[25]190        }
[79]191        const Key& key;
192        Value& val;
193        Reference ref;
194        ConstReference cref;
195        const typename _ReferenceMap::Key& own_key;
[25]196        typename _ReferenceMap::Value& own_val;
[74]197        typename _ReferenceMap::Reference own_ref;
[79]198        typename _ReferenceMap::ConstReference own_cref;
[25]199        _ReferenceMap& m;
200      };
201    };
202
203    // @}
204
205  } //namespace concepts
[28]206
[25]207} //namespace lemon
[28]208
[25]209#endif // LEMON_CONCEPT_MAPS_H
Note: See TracBrowser for help on using the repository browser.