alpar@209: /* -*- mode: C++; indent-tabs-mode: nil; -*-
deba@57:  *
alpar@209:  * This file is a part of LEMON, a generic C++ optimization library.
deba@57:  *
alpar@107:  * Copyright (C) 2003-2008
deba@57:  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
deba@57:  * (Egervary Research Group on Combinatorial Optimization, EGRES).
deba@57:  *
deba@57:  * Permission to use, modify and distribute this software is granted
deba@57:  * provided that this copyright notice appears in all copies. For
deba@57:  * precise terms see the accompanying LICENSE file.
deba@57:  *
deba@57:  * This software is provided "AS IS" with no warranty of any kind,
deba@57:  * express or implied, and with no claim as to its suitability for any
deba@57:  * purpose.
deba@57:  *
deba@57:  */
deba@57: 
deba@57: #ifndef LEMON_BITS_VECTOR_MAP_H
deba@57: #define LEMON_BITS_VECTOR_MAP_H
deba@57: 
deba@57: #include <vector>
deba@57: #include <algorithm>
deba@57: 
deba@220: #include <lemon/core.h>
deba@57: #include <lemon/bits/alteration_notifier.h>
deba@57: 
deba@57: #include <lemon/concept_check.h>
deba@57: #include <lemon/concepts/maps.h>
deba@57: 
kpeter@314: //\ingroup graphbits
kpeter@314: //
kpeter@314: //\file
kpeter@314: //\brief Vector based graph maps.
deba@57: namespace lemon {
deba@57: 
kpeter@314:   // \ingroup graphbits
kpeter@314:   //
kpeter@314:   // \brief Graph map based on the std::vector storage.
kpeter@314:   //
kpeter@314:   // The VectorMap template class is graph map structure what
kpeter@314:   // automatically updates the map when a key is added to or erased from
kpeter@314:   // the map. This map type uses the std::vector to store the values.
kpeter@314:   //
kpeter@314:   // \tparam _Graph The graph this map is attached to.
kpeter@314:   // \tparam _Item The item type of the graph items.
kpeter@314:   // \tparam _Value The value type of the map.
deba@57:   template <typename _Graph, typename _Item, typename _Value>
alpar@209:   class VectorMap
deba@57:     : public ItemSetTraits<_Graph, _Item>::ItemNotifier::ObserverBase {
deba@57:   private:
alpar@209: 
kpeter@314:     // The container type of the map.
alpar@209:     typedef std::vector<_Value> Container;
deba@57: 
deba@57:   public:
deba@57: 
kpeter@314:     // The graph type of the map.
deba@57:     typedef _Graph Graph;
kpeter@314:     // The item type of the map.
deba@57:     typedef _Item Item;
kpeter@314:     // The reference map tag.
deba@57:     typedef True ReferenceMapTag;
deba@57: 
kpeter@314:     // The key type of the map.
deba@57:     typedef _Item Key;
kpeter@314:     // The value type of the map.
deba@57:     typedef _Value Value;
deba@57: 
kpeter@314:     // The notifier type.
deba@57:     typedef typename ItemSetTraits<_Graph, _Item>::ItemNotifier Notifier;
deba@57: 
kpeter@314:     // The map type.
deba@57:     typedef VectorMap Map;
kpeter@314:     // The base class of the map.
deba@57:     typedef typename Notifier::ObserverBase Parent;
deba@57: 
kpeter@314:     // The reference type of the map;
deba@57:     typedef typename Container::reference Reference;
kpeter@314:     // The const reference type of the map;
deba@57:     typedef typename Container::const_reference ConstReference;
deba@57: 
deba@57: 
kpeter@314:     // \brief Constructor to attach the new map into the notifier.
kpeter@314:     //
kpeter@314:     // It constructs a map and attachs it into the notifier.
kpeter@314:     // It adds all the items of the graph to the map.
deba@57:     VectorMap(const Graph& graph) {
deba@57:       Parent::attach(graph.notifier(Item()));
deba@57:       container.resize(Parent::notifier()->maxId() + 1);
deba@57:     }
deba@57: 
kpeter@314:     // \brief Constructor uses given value to initialize the map.
kpeter@314:     //
kpeter@314:     // It constructs a map uses a given value to initialize the map.
kpeter@314:     // It adds all the items of the graph to the map.
deba@57:     VectorMap(const Graph& graph, const Value& value) {
deba@57:       Parent::attach(graph.notifier(Item()));
deba@57:       container.resize(Parent::notifier()->maxId() + 1, value);
deba@57:     }
deba@57: 
kpeter@263:   private:
kpeter@314:     // \brief Copy constructor
kpeter@314:     //
kpeter@314:     // Copy constructor.
deba@57:     VectorMap(const VectorMap& _copy) : Parent() {
deba@57:       if (_copy.attached()) {
alpar@209:         Parent::attach(*_copy.notifier());
alpar@209:         container = _copy.container;
deba@57:       }
deba@57:     }
deba@57: 
kpeter@314:     // \brief Assign operator.
kpeter@314:     //
kpeter@314:     // This operator assigns for each item in the map the
kpeter@314:     // value mapped to the same item in the copied map.
kpeter@314:     // The parameter map should be indiced with the same
kpeter@314:     // itemset because this assign operator does not change
kpeter@314:     // the container of the map.
deba@57:     VectorMap& operator=(const VectorMap& cmap) {
deba@57:       return operator=<VectorMap>(cmap);
deba@57:     }
deba@57: 
deba@57: 
kpeter@314:     // \brief Template assign operator.
kpeter@314:     //
kpeter@314:     // The given parameter should be conform to the ReadMap
kpeter@314:     // concecpt and could be indiced by the current item set of
kpeter@314:     // the NodeMap. In this case the value for each item
kpeter@314:     // is assigned by the value of the given ReadMap.
deba@57:     template <typename CMap>
deba@57:     VectorMap& operator=(const CMap& cmap) {
deba@57:       checkConcept<concepts::ReadMap<Key, _Value>, CMap>();
deba@57:       const typename Parent::Notifier* nf = Parent::notifier();
deba@57:       Item it;
deba@57:       for (nf->first(it); it != INVALID; nf->next(it)) {
deba@57:         set(it, cmap[it]);
deba@57:       }
deba@57:       return *this;
deba@57:     }
alpar@209: 
deba@57:   public:
deba@57: 
kpeter@314:     // \brief The subcript operator.
kpeter@314:     //
kpeter@314:     // The subscript operator. The map can be subscripted by the
kpeter@314:     // actual items of the graph.
deba@57:     Reference operator[](const Key& key) {
deba@57:       return container[Parent::notifier()->id(key)];
alpar@209:     }
alpar@209: 
kpeter@314:     // \brief The const subcript operator.
kpeter@314:     //
kpeter@314:     // The const subscript operator. The map can be subscripted by the
kpeter@314:     // actual items of the graph.
deba@57:     ConstReference operator[](const Key& key) const {
deba@57:       return container[Parent::notifier()->id(key)];
deba@57:     }
deba@57: 
deba@57: 
kpeter@314:     // \brief The setter function of the map.
kpeter@314:     //
kpeter@314:     // It the same as operator[](key) = value expression.
deba@57:     void set(const Key& key, const Value& value) {
deba@57:       (*this)[key] = value;
deba@57:     }
deba@57: 
deba@57:   protected:
deba@57: 
kpeter@314:     // \brief Adds a new key to the map.
kpeter@314:     //
kpeter@314:     // It adds a new key to the map. It called by the observer notifier
kpeter@314:     // and it overrides the add() member function of the observer base.
deba@57:     virtual void add(const Key& key) {
deba@57:       int id = Parent::notifier()->id(key);
deba@57:       if (id >= int(container.size())) {
alpar@209:         container.resize(id + 1);
deba@57:       }
deba@57:     }
deba@57: 
kpeter@314:     // \brief Adds more new keys to the map.
kpeter@314:     //
kpeter@314:     // It adds more new keys to the map. It called by the observer notifier
kpeter@314:     // and it overrides the add() member function of the observer base.
deba@57:     virtual void add(const std::vector<Key>& keys) {
deba@57:       int max = container.size() - 1;
deba@57:       for (int i = 0; i < int(keys.size()); ++i) {
deba@57:         int id = Parent::notifier()->id(keys[i]);
deba@57:         if (id >= max) {
deba@57:           max = id;
deba@57:         }
deba@57:       }
deba@57:       container.resize(max + 1);
deba@57:     }
deba@57: 
kpeter@314:     // \brief Erase a key from the map.
kpeter@314:     //
kpeter@314:     // Erase a key from the map. It called by the observer notifier
kpeter@314:     // and it overrides the erase() member function of the observer base.
deba@57:     virtual void erase(const Key& key) {
deba@57:       container[Parent::notifier()->id(key)] = Value();
deba@57:     }
deba@57: 
kpeter@314:     // \brief Erase more keys from the map.
kpeter@314:     //
kpeter@314:     // Erase more keys from the map. It called by the observer notifier
kpeter@314:     // and it overrides the erase() member function of the observer base.
deba@57:     virtual void erase(const std::vector<Key>& keys) {
deba@57:       for (int i = 0; i < int(keys.size()); ++i) {
alpar@209:         container[Parent::notifier()->id(keys[i])] = Value();
deba@57:       }
deba@57:     }
alpar@209: 
kpeter@314:     // \brief Buildes the map.
kpeter@314:     //
kpeter@314:     // It buildes the map. It called by the observer notifier
kpeter@314:     // and it overrides the build() member function of the observer base.
alpar@209:     virtual void build() {
deba@57:       int size = Parent::notifier()->maxId() + 1;
deba@57:       container.reserve(size);
deba@57:       container.resize(size);
deba@57:     }
deba@57: 
kpeter@314:     // \brief Clear the map.
kpeter@314:     //
kpeter@314:     // It erase all items from the map. It called by the observer notifier
kpeter@314:     // and it overrides the clear() member function of the observer base.
alpar@209:     virtual void clear() {
deba@57:       container.clear();
deba@57:     }
alpar@209: 
deba@57:   private:
alpar@209: 
deba@57:     Container container;
deba@57: 
deba@57:   };
deba@57: 
deba@57: }
deba@57: 
deba@57: #endif