alpar@209: /* -*- mode: C++; indent-tabs-mode: nil; -*-
alpar@25:  *
alpar@209:  * This file is a part of LEMON, a generic C++ optimization library.
alpar@25:  *
alpar@440:  * Copyright (C) 2003-2009
alpar@25:  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@25:  * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@25:  *
alpar@25:  * Permission to use, modify and distribute this software is granted
alpar@25:  * provided that this copyright notice appears in all copies. For
alpar@25:  * precise terms see the accompanying LICENSE file.
alpar@25:  *
alpar@25:  * This software is provided "AS IS" with no warranty of any kind,
alpar@25:  * express or implied, and with no claim as to its suitability for any
alpar@25:  * purpose.
alpar@25:  *
alpar@25:  */
alpar@25: 
deba@529: #ifndef LEMON_CONCEPTS_MAPS_H
deba@529: #define LEMON_CONCEPTS_MAPS_H
alpar@25: 
deba@220: #include <lemon/core.h>
alpar@25: #include <lemon/concept_check.h>
alpar@25: 
kpeter@314: ///\ingroup map_concepts
alpar@25: ///\file
kpeter@114: ///\brief The concept of maps.
alpar@25: 
alpar@25: namespace lemon {
alpar@25: 
alpar@25:   namespace concepts {
kpeter@79: 
kpeter@314:     /// \addtogroup map_concepts
alpar@25:     /// @{
alpar@25: 
alpar@25:     /// Readable map concept
kpeter@28: 
kpeter@28:     /// Readable map concept.
kpeter@28:     ///
alpar@25:     template<typename K, typename T>
alpar@25:     class ReadMap
alpar@25:     {
alpar@25:     public:
kpeter@35:       /// The key type of the map.
kpeter@79:       typedef K Key;
alpar@210:       /// \brief The value type of the map.
alpar@210:       /// (The type of objects associated with the keys).
alpar@25:       typedef T Value;
alpar@25: 
kpeter@79:       /// Returns the value associated with the given key.
alpar@209:       Value operator[](const Key &) const {
kpeter@94:         return *static_cast<Value *>(0);
kpeter@94:       }
alpar@25: 
alpar@25:       template<typename _ReadMap>
alpar@25:       struct Constraints {
alpar@209:         void constraints() {
alpar@209:           Value val = m[key];
alpar@209:           val = m[key];
alpar@209:           typename _ReadMap::Value own_val = m[own_key];
alpar@209:           own_val = m[own_key];
alpar@25: 
alpar@209:           ignore_unused_variable_warning(key);
alpar@209:           ignore_unused_variable_warning(val);
alpar@209:           ignore_unused_variable_warning(own_key);
alpar@209:           ignore_unused_variable_warning(own_val);
alpar@209:         }
alpar@209:         const Key& key;
alpar@209:         const typename _ReadMap::Key& own_key;
alpar@209:         const _ReadMap& m;
alpar@25:       };
kpeter@79: 
alpar@25:     };
alpar@25: 
alpar@25: 
alpar@25:     /// Writable map concept
kpeter@79: 
kpeter@28:     /// Writable map concept.
kpeter@28:     ///
alpar@25:     template<typename K, typename T>
alpar@25:     class WriteMap
alpar@25:     {
alpar@25:     public:
kpeter@35:       /// The key type of the map.
kpeter@79:       typedef K Key;
alpar@210:       /// \brief The value type of the map.
alpar@210:       /// (The type of objects associated with the keys).
alpar@25:       typedef T Value;
alpar@25: 
kpeter@79:       /// Sets the value associated with the given key.
kpeter@79:       void set(const Key &, const Value &) {}
alpar@25: 
kpeter@79:       /// Default constructor.
alpar@25:       WriteMap() {}
alpar@25: 
alpar@25:       template <typename _WriteMap>
alpar@25:       struct Constraints {
alpar@209:         void constraints() {
alpar@209:           m.set(key, val);
alpar@209:           m.set(own_key, own_val);
kpeter@79: 
alpar@209:           ignore_unused_variable_warning(key);
alpar@209:           ignore_unused_variable_warning(val);
alpar@209:           ignore_unused_variable_warning(own_key);
alpar@209:           ignore_unused_variable_warning(own_val);
alpar@209:         }
alpar@209:         const Key& key;
alpar@209:         const Value& val;
alpar@209:         const typename _WriteMap::Key& own_key;
alpar@209:         const typename _WriteMap::Value& own_val;
alpar@209:         _WriteMap& m;
alpar@25:       };
alpar@25:     };
alpar@25: 
kpeter@48:     /// Read/writable map concept
kpeter@79: 
kpeter@28:     /// Read/writable map concept.
kpeter@28:     ///
alpar@25:     template<typename K, typename T>
alpar@25:     class ReadWriteMap : public ReadMap<K,T>,
alpar@209:                          public WriteMap<K,T>
alpar@25:     {
alpar@25:     public:
kpeter@35:       /// The key type of the map.
kpeter@79:       typedef K Key;
alpar@210:       /// \brief The value type of the map.
alpar@210:       /// (The type of objects associated with the keys).
alpar@25:       typedef T Value;
alpar@25: 
kpeter@79:       /// Returns the value associated with the given key.
alpar@209:       Value operator[](const Key &) const {
kpeter@94:         return *static_cast<Value *>(0);
kpeter@94:       }
kpeter@79: 
kpeter@79:       /// Sets the value associated with the given key.
kpeter@79:       void set(const Key &, const Value &) {}
alpar@25: 
alpar@25:       template<typename _ReadWriteMap>
alpar@25:       struct Constraints {
alpar@209:         void constraints() {
alpar@209:           checkConcept<ReadMap<K, T>, _ReadWriteMap >();
alpar@209:           checkConcept<WriteMap<K, T>, _ReadWriteMap >();
alpar@209:         }
alpar@25:       };
alpar@25:     };
kpeter@79: 
kpeter@79: 
kpeter@28:     /// Dereferable map concept
kpeter@79: 
kpeter@28:     /// Dereferable map concept.
kpeter@28:     ///
alpar@25:     template<typename K, typename T, typename R, typename CR>
alpar@25:     class ReferenceMap : public ReadWriteMap<K,T>
alpar@25:     {
alpar@25:     public:
alpar@25:       /// Tag for reference maps.
alpar@25:       typedef True ReferenceMapTag;
kpeter@35:       /// The key type of the map.
kpeter@79:       typedef K Key;
alpar@210:       /// \brief The value type of the map.
alpar@210:       /// (The type of objects associated with the keys).
alpar@25:       typedef T Value;
kpeter@35:       /// The reference type of the map.
alpar@25:       typedef R Reference;
kpeter@35:       /// The const reference type of the map.
alpar@25:       typedef CR ConstReference;
alpar@25: 
alpar@25:     public:
alpar@25: 
kpeter@79:       /// Returns a reference to the value associated with the given key.
alpar@209:       Reference operator[](const Key &) {
kpeter@94:         return *static_cast<Value *>(0);
kpeter@94:       }
kpeter@79: 
kpeter@79:       /// Returns a const reference to the value associated with the given key.
kpeter@94:       ConstReference operator[](const Key &) const {
kpeter@94:         return *static_cast<Value *>(0);
kpeter@94:       }
kpeter@79: 
kpeter@79:       /// Sets the value associated with the given key.
alpar@25:       void set(const Key &k,const Value &t) { operator[](k)=t; }
alpar@25: 
alpar@25:       template<typename _ReferenceMap>
kpeter@74:       struct Constraints {
kpeter@718:         typename enable_if<typename _ReferenceMap::ReferenceMapTag, void>::type
kpeter@718:         constraints() {
alpar@209:           checkConcept<ReadWriteMap<K, T>, _ReferenceMap >();
alpar@209:           ref = m[key];
alpar@209:           m[key] = val;
alpar@209:           m[key] = ref;
alpar@209:           m[key] = cref;
alpar@209:           own_ref = m[own_key];
alpar@209:           m[own_key] = own_val;
alpar@209:           m[own_key] = own_ref;
alpar@209:           m[own_key] = own_cref;
alpar@209:           m[key] = m[own_key];
alpar@209:           m[own_key] = m[key];
alpar@209:         }
alpar@209:         const Key& key;
alpar@209:         Value& val;
alpar@209:         Reference ref;
alpar@209:         ConstReference cref;
alpar@209:         const typename _ReferenceMap::Key& own_key;
alpar@209:         typename _ReferenceMap::Value& own_val;
alpar@209:         typename _ReferenceMap::Reference own_ref;
alpar@209:         typename _ReferenceMap::ConstReference own_cref;
alpar@209:         _ReferenceMap& m;
alpar@25:       };
alpar@25:     };
alpar@25: 
alpar@25:     // @}
alpar@25: 
alpar@25:   } //namespace concepts
kpeter@28: 
alpar@25: } //namespace lemon
kpeter@28: 
deba@529: #endif