COIN-OR::LEMON - Graph Library

source: lemon-1.2/lemon/concepts/maps.h @ 113:18a7ee8fa56e

Last change on this file since 113:18a7ee8fa56e was 94:a4688e4138ec, checked in by Peter Kovacs <kpeter@…>, 16 years ago

Fixes in the map concepts

  • Now Value type needn't be default constructible.
  • Extend the test file to check this.
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.
[94]50      Value operator[](const Key &) const {
51        return *static_cast<Value *>(0);
52      }
[25]53
54      template<typename _ReadMap>
55      struct Constraints {
56        void constraints() {
57          Value val = m[key];
58          val = m[key];
[79]59          typename _ReadMap::Value own_val = m[own_key];
60          own_val = m[own_key];
[25]61
[79]62          ignore_unused_variable_warning(key);
[25]63          ignore_unused_variable_warning(val);
[79]64          ignore_unused_variable_warning(own_key);
[25]65          ignore_unused_variable_warning(own_val);
66        }
[79]67        const Key& key;
68        const typename _ReadMap::Key& own_key;
69        const _ReadMap& m;
[25]70      };
[79]71
[25]72    };
73
74
75    /// Writable map concept
[79]76
[28]77    /// Writable map concept.
78    ///
[25]79    template<typename K, typename T>
80    class WriteMap
81    {
82    public:
[35]83      /// The key type of the map.
[79]84      typedef K Key;
[35]85      /// The value type of the map. (The type of objects associated with the keys).
[25]86      typedef T Value;
87
[79]88      /// Sets the value associated with the given key.
89      void set(const Key &, const Value &) {}
[25]90
[79]91      /// Default constructor.
[25]92      WriteMap() {}
93
94      template <typename _WriteMap>
95      struct Constraints {
96        void constraints() {
97          m.set(key, val);
98          m.set(own_key, own_val);
[79]99
[25]100          ignore_unused_variable_warning(key);
101          ignore_unused_variable_warning(val);
102          ignore_unused_variable_warning(own_key);
103          ignore_unused_variable_warning(own_val);
104        }
[79]105        const Key& key;
106        const Value& val;
107        const typename _WriteMap::Key& own_key;
108        const typename _WriteMap::Value own_val;
[25]109        _WriteMap& m;
110      };
111    };
112
[48]113    /// Read/writable map concept
[79]114
[28]115    /// Read/writable map concept.
116    ///
[25]117    template<typename K, typename T>
118    class ReadWriteMap : public ReadMap<K,T>,
[35]119                         public WriteMap<K,T>
[25]120    {
121    public:
[35]122      /// The key type of the map.
[79]123      typedef K Key;
[35]124      /// The value type of the map. (The type of objects associated with the keys).
[25]125      typedef T Value;
126
[79]127      /// Returns the value associated with the given key.
[94]128      Value operator[](const Key &) const {
129        return *static_cast<Value *>(0);
130      }
[79]131
132      /// Sets the value associated with the given key.
133      void set(const Key &, const Value &) {}
[25]134
135      template<typename _ReadWriteMap>
136      struct Constraints {
137        void constraints() {
138          checkConcept<ReadMap<K, T>, _ReadWriteMap >();
139          checkConcept<WriteMap<K, T>, _ReadWriteMap >();
140        }
141      };
142    };
[79]143
144
[28]145    /// Dereferable map concept
[79]146
[28]147    /// Dereferable map concept.
148    ///
[25]149    template<typename K, typename T, typename R, typename CR>
150    class ReferenceMap : public ReadWriteMap<K,T>
151    {
152    public:
153      /// Tag for reference maps.
154      typedef True ReferenceMapTag;
[35]155      /// The key type of the map.
[79]156      typedef K Key;
[35]157      /// The value type of the map. (The type of objects associated with the keys).
[25]158      typedef T Value;
[35]159      /// The reference type of the map.
[25]160      typedef R Reference;
[35]161      /// The const reference type of the map.
[25]162      typedef CR ConstReference;
163
164    public:
165
[79]166      /// Returns a reference to the value associated with the given key.
[94]167      Reference operator[](const Key &) {
168        return *static_cast<Value *>(0);
169      }
[79]170
171      /// Returns a const reference to the value associated with the given key.
[94]172      ConstReference operator[](const Key &) const {
173        return *static_cast<Value *>(0);
174      }
[79]175
176      /// Sets the value associated with the given key.
[25]177      void set(const Key &k,const Value &t) { operator[](k)=t; }
178
179      template<typename _ReferenceMap>
[74]180      struct Constraints {
[25]181        void constraints() {
[74]182          checkConcept<ReadWriteMap<K, T>, _ReferenceMap >();
[79]183          ref = m[key];
[25]184          m[key] = val;
185          m[key] = ref;
[79]186          m[key] = cref;
187          own_ref = m[own_key];
[25]188          m[own_key] = own_val;
189          m[own_key] = own_ref;
[79]190          m[own_key] = own_cref;
191          m[key] = m[own_key];
192          m[own_key] = m[key];
[25]193        }
[79]194        const Key& key;
195        Value& val;
196        Reference ref;
197        ConstReference cref;
198        const typename _ReferenceMap::Key& own_key;
[25]199        typename _ReferenceMap::Value& own_val;
[74]200        typename _ReferenceMap::Reference own_ref;
[79]201        typename _ReferenceMap::ConstReference own_cref;
[25]202        _ReferenceMap& m;
203      };
204    };
205
206    // @}
207
208  } //namespace concepts
[28]209
[25]210} //namespace lemon
[28]211
[25]212#endif // LEMON_CONCEPT_MAPS_H
Note: See TracBrowser for help on using the repository browser.