COIN-OR::LEMON - Graph Library

source: lemon-0.x/lemon/concept/maps.h @ 1719:674182524bd9

Last change on this file since 1719:674182524bd9 was 1719:674182524bd9, checked in by Balazs Dezso, 19 years ago

Traits moved to own file
Tag for reference maps
Possibility to handle proper the return type
of the operator[]() const -- value or reference

File size: 4.8 KB
RevLine 
[959]1/* -*- C++ -*-
[1435]2 * lemon/concept/maps.h - Part of LEMON, a generic C++ optimization library
[959]3 *
[1164]4 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
[1359]5 * (Egervary Research Group on Combinatorial Optimization, EGRES).
[959]6 *
7 * Permission to use, modify and distribute this software is granted
8 * provided that this copyright notice appears in all copies. For
9 * precise terms see the accompanying LICENSE file.
10 *
11 * This software is provided "AS IS" with no warranty of any kind,
12 * express or implied, and with no claim as to its suitability for any
13 * purpose.
14 *
15 */
16
17#ifndef LEMON_CONCEPT_MAPS_H
18#define LEMON_CONCEPT_MAPS_H
19
[1719]20#include <lemon/utility.h>
[959]21#include <lemon/concept_check.h>
22
23///\ingroup concept
24///\file
25///\brief Map concepts checking classes for testing and documenting.
26
27namespace lemon {
28
29  namespace concept {
30 
31    /// \addtogroup concept
32    /// @{
33
34    /// Readable map concept
35    template<typename K, typename T>
36    class ReadMap
37    {
38    public:
39      /// Map's key type.
[987]40      typedef K Key;   
[959]41      /// Map's value type. (The type of objects associated with the keys).
[987]42      typedef T Value;
[959]43
[989]44      // \bug Value don't need to be default constructible.
[959]45      /// Returns the value associated with a key.
[989]46      Value operator[](const Key &) const {return Value();}
[959]47
[989]48      template<typename _ReadMap>
49      struct Constraints {
50
51        void constraints() {
52          Value val = m[key];
53          val = m[key];
54          typename _ReadMap::Value own_val = m[own_key];
55          own_val = m[own_key];
56
57          ignore_unused_variable_warning(val);
58          ignore_unused_variable_warning(own_val);
59          ignore_unused_variable_warning(key);
60        }
61        Key& key;
62        typename _ReadMap::Key& own_key;
63        _ReadMap& m;
64      };
65     
[959]66    };
67
68
69    /// Writable map concept
70    template<typename K, typename T>
71    class WriteMap
72    {
73    public:
74      /// Map's key type.
[987]75      typedef K Key;   
[959]76      /// Map's value type. (The type of objects associated with the keys).
[987]77      typedef T Value;
[959]78
79      /// Sets the value associated with a key.
[1367]80      void set(const Key &,const Value &) {}
[959]81
82      ///Default constructor
83      WriteMap() {}
[989]84
85      template <typename _WriteMap>
86      struct Constraints {
87        void constraints() {
88          // No constraints for constructor.
89          m.set(key, val);
90          m.set(own_key, own_val);
[1042]91          ignore_unused_variable_warning(key);
92          ignore_unused_variable_warning(val);
93          ignore_unused_variable_warning(own_key);
94          ignore_unused_variable_warning(own_val);
[989]95        }
96
97        Value& val;
98        typename _WriteMap::Value own_val;
99        Key& key;
100        typename _WriteMap::Key& own_key;
[1719]101        _WriteMap& m;
[989]102
103      };
[959]104    };
105
106    ///Read/Writable map concept
107    template<typename K, typename T>
108    class ReadWriteMap : public ReadMap<K,T>,
109                            public WriteMap<K,T>
110    {
111    public:
112      /// Map's key type.
[987]113      typedef K Key;   
[959]114      /// Map's value type. (The type of objects associated with the keys).
[987]115      typedef T Value;
[959]116
117      /// Returns the value associated with a key.
[1367]118      Value operator[](const Key &) const {return Value();}
[959]119      /// Sets the value associated with a key.
[1367]120      void set(const Key & ,const Value &) {}
[959]121
[989]122      template<typename _ReadWriteMap>
123      struct Constraints {
124        void constraints() {
125          checkConcept<ReadMap<K, T>, _ReadWriteMap >();
[1719]126          checkConcept<WriteMap<K, T>, _ReadWriteMap >();
[989]127        }
128      };
[959]129    };
130 
131 
132    ///Dereferable map concept
[989]133    template<typename K, typename T, typename R, typename CR>
[959]134    class ReferenceMap : public ReadWriteMap<K,T>
135    {
136    public:
[1719]137      /// Tag for reference maps.
138      typedef True ReferenceMapTag;
[959]139      /// Map's key type.
[987]140      typedef K Key;   
[959]141      /// Map's value type. (The type of objects associated with the keys).
[987]142      typedef T Value;
[989]143      /// Map's reference type.
144      typedef R Reference;
145      /// Map's const reference type.
146      typedef CR ConstReference;
[959]147
148    protected:
[987]149      Value tmp;
[959]150    public:
151
152      ///Returns a reference to the value associated to a key.
[1375]153      Reference operator[](const Key &) { return tmp; }
[959]154      ///Returns a const reference to the value associated to a key.
[1375]155      ConstReference operator[](const Key &) const
[959]156      { return tmp; }
157      /// Sets the value associated with a key.
[987]158      void set(const Key &k,const Value &t) { operator[](k)=t; }
[959]159
[989]160      // \todo rethink this concept
161      template<typename _ReferenceMap>
162      struct ReferenceMapConcept {
163
164        void constraints() {
165          checkConcept<ReadWriteMap, _ReferenceMap >();
166          m[key] = val;
167          val  = m[key];
168          m[key] = ref;
169          ref = m[key];
170          m[own_key] = own_val;
171          own_val  = m[own_key];
172          m[own_key] = own_ref;
173          own_ref = m[own_key];           
174        }
175
176        typename _ReferenceMap::Key& own_key;
177        typename _ReferenceMap::Value& own_val;
178        typename _ReferenceMap::Reference& own_ref;
179        Key& key;
180        Value& val;
181        Reference& ref;
[1719]182        _ReferenceMap& m;
[989]183      };
[959]184    };
185
186    // @}
187
188  } //namespace concept
189} //namespace lemon
190#endif // LEMON_CONCEPT_MAPS_H
Note: See TracBrowser for help on using the repository browser.