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_ARRAY_MAP_H
deba@57: #define LEMON_BITS_ARRAY_MAP_H
deba@57: 
deba@57: #include <memory>
deba@57: 
deba@57: #include <lemon/bits/traits.h>
deba@57: #include <lemon/bits/alteration_notifier.h>
deba@57: #include <lemon/concept_check.h>
deba@57: #include <lemon/concepts/maps.h>
deba@57: 
deba@57: /// \ingroup graphbits
deba@57: /// \file
deba@57: /// \brief Graph map based on the array storage.
deba@57: 
deba@57: namespace lemon {
deba@57: 
deba@57:   /// \ingroup graphbits
deba@57:   ///
deba@57:   /// \brief Graph map based on the array storage.
deba@57:   ///
deba@57:   /// The ArrayMap template class is graph map structure what
deba@57:   /// automatically updates the map when a key is added to or erased from
alpar@209:   /// the map. This map uses the allocators to implement
deba@57:   /// the container functionality.
deba@57:   ///
deba@57:   /// The template parameters are the Graph the current Item type and
deba@57:   /// the Value type of the map.
deba@57:   template <typename _Graph, typename _Item, typename _Value>
alpar@209:   class ArrayMap
deba@57:     : public ItemSetTraits<_Graph, _Item>::ItemNotifier::ObserverBase {
deba@57:   public:
alpar@209:     /// The graph type of the maps.
deba@57:     typedef _Graph Graph;
deba@57:     /// The item type of the map.
deba@57:     typedef _Item Item;
deba@57:     /// The reference map tag.
deba@57:     typedef True ReferenceMapTag;
deba@57: 
deba@57:     /// The key type of the maps.
deba@57:     typedef _Item Key;
deba@57:     /// The value type of the map.
deba@57:     typedef _Value Value;
deba@57: 
deba@57:     /// The const reference type of the map.
deba@57:     typedef const _Value& ConstReference;
deba@57:     /// The reference type of the map.
deba@57:     typedef _Value& Reference;
deba@57: 
deba@57:     /// The notifier type.
deba@57:     typedef typename ItemSetTraits<_Graph, _Item>::ItemNotifier Notifier;
deba@57: 
deba@57:     /// The MapBase of the Map which imlements the core regisitry function.
deba@57:     typedef typename Notifier::ObserverBase Parent;
alpar@209: 
deba@57:   private:
deba@57:     typedef std::allocator<Value> Allocator;
deba@57: 
deba@57:   public:
deba@57: 
deba@57:     /// \brief Graph initialized map constructor.
deba@57:     ///
deba@57:     /// Graph initialized map constructor.
deba@57:     explicit ArrayMap(const Graph& graph) {
deba@57:       Parent::attach(graph.notifier(Item()));
deba@57:       allocate_memory();
deba@57:       Notifier* nf = Parent::notifier();
deba@57:       Item it;
deba@57:       for (nf->first(it); it != INVALID; nf->next(it)) {
alpar@209:         int id = nf->id(it);;
alpar@209:         allocator.construct(&(values[id]), Value());
alpar@209:       }
deba@57:     }
deba@57: 
alpar@209:     /// \brief Constructor to use default value to initialize the map.
deba@57:     ///
alpar@209:     /// It constructs a map and initialize all of the the map.
deba@57:     ArrayMap(const Graph& graph, const Value& value) {
deba@57:       Parent::attach(graph.notifier(Item()));
deba@57:       allocate_memory();
deba@57:       Notifier* nf = Parent::notifier();
deba@57:       Item it;
deba@57:       for (nf->first(it); it != INVALID; nf->next(it)) {
alpar@209:         int id = nf->id(it);;
alpar@209:         allocator.construct(&(values[id]), value);
alpar@209:       }
deba@57:     }
deba@57: 
deba@57:     /// \brief Constructor to copy a map of the same map type.
deba@57:     ///
alpar@209:     /// Constructor to copy a map of the same map type.
deba@57:     ArrayMap(const ArrayMap& copy) : Parent() {
deba@57:       if (copy.attached()) {
alpar@209:         attach(*copy.notifier());
deba@57:       }
deba@57:       capacity = copy.capacity;
deba@57:       if (capacity == 0) return;
deba@57:       values = allocator.allocate(capacity);
deba@57:       Notifier* nf = Parent::notifier();
deba@57:       Item it;
deba@57:       for (nf->first(it); it != INVALID; nf->next(it)) {
alpar@209:         int id = nf->id(it);;
alpar@209:         allocator.construct(&(values[id]), copy.values[id]);
deba@57:       }
deba@57:     }
deba@57: 
deba@57:     /// \brief Assign operator.
deba@57:     ///
deba@57:     /// This operator assigns for each item in the map the
alpar@209:     /// value mapped to the same item in the copied map.
deba@57:     /// The parameter map should be indiced with the same
deba@57:     /// itemset because this assign operator does not change
alpar@209:     /// the container of the map.
deba@57:     ArrayMap& operator=(const ArrayMap& cmap) {
deba@57:       return operator=<ArrayMap>(cmap);
deba@57:     }
deba@57: 
deba@57: 
deba@57:     /// \brief Template assign operator.
deba@57:     ///
deba@57:     /// The given parameter should be conform to the ReadMap
deba@57:     /// concecpt and could be indiced by the current item set of
deba@57:     /// the NodeMap. In this case the value for each item
alpar@209:     /// is assigned by the value of the given ReadMap.
deba@57:     template <typename CMap>
deba@57:     ArrayMap& 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:     }
deba@57: 
deba@57:     /// \brief The destructor of the map.
alpar@209:     ///
deba@57:     /// The destructor of the map.
alpar@209:     virtual ~ArrayMap() {
deba@57:       if (attached()) {
alpar@209:         clear();
alpar@209:         detach();
deba@57:       }
deba@57:     }
alpar@209: 
deba@57:   protected:
deba@57: 
deba@57:     using Parent::attach;
deba@57:     using Parent::detach;
deba@57:     using Parent::attached;
deba@57: 
deba@57:   public:
deba@57: 
alpar@209:     /// \brief The subscript operator.
deba@57:     ///
deba@57:     /// The subscript operator. The map can be subscripted by the
alpar@209:     /// actual keys of the graph.
deba@57:     Value& operator[](const Key& key) {
deba@57:       int id = Parent::notifier()->id(key);
deba@57:       return values[id];
alpar@209:     }
alpar@209: 
deba@57:     /// \brief The const subscript operator.
deba@57:     ///
deba@57:     /// The const subscript operator. The map can be subscripted by the
alpar@209:     /// actual keys of the graph.
deba@57:     const Value& operator[](const Key& key) const {
deba@57:       int id = Parent::notifier()->id(key);
deba@57:       return values[id];
deba@57:     }
deba@57: 
deba@57:     /// \brief Setter function of the map.
alpar@209:     ///
deba@57:     /// Setter function of the map. Equivalent with map[key] = val.
deba@57:     /// This is a compatibility feature with the not dereferable maps.
deba@57:     void set(const Key& key, const Value& val) {
deba@57:       (*this)[key] = val;
deba@57:     }
deba@57: 
deba@57:   protected:
deba@57: 
deba@57:     /// \brief Adds a new key to the map.
alpar@209:     ///
deba@57:     /// It adds a new key to the map. It called by the observer notifier
alpar@209:     /// and it overrides the add() member function of the observer base.
deba@57:     virtual void add(const Key& key) {
deba@57:       Notifier* nf = Parent::notifier();
deba@57:       int id = nf->id(key);
deba@57:       if (id >= capacity) {
alpar@209:         int new_capacity = (capacity == 0 ? 1 : capacity);
alpar@209:         while (new_capacity <= id) {
alpar@209:           new_capacity <<= 1;
alpar@209:         }
alpar@209:         Value* new_values = allocator.allocate(new_capacity);
alpar@209:         Item it;
alpar@209:         for (nf->first(it); it != INVALID; nf->next(it)) {
alpar@209:           int jd = nf->id(it);;
alpar@209:           if (id != jd) {
alpar@209:             allocator.construct(&(new_values[jd]), values[jd]);
alpar@209:             allocator.destroy(&(values[jd]));
alpar@209:           }
alpar@209:         }
alpar@209:         if (capacity != 0) allocator.deallocate(values, capacity);
alpar@209:         values = new_values;
alpar@209:         capacity = new_capacity;
deba@57:       }
deba@57:       allocator.construct(&(values[id]), Value());
deba@57:     }
deba@57: 
deba@57:     /// \brief Adds more new keys to the map.
alpar@209:     ///
deba@57:     /// It adds more new keys to the map. It called by the observer notifier
alpar@209:     /// and it overrides the add() member function of the observer base.
deba@57:     virtual void add(const std::vector<Key>& keys) {
deba@57:       Notifier* nf = Parent::notifier();
deba@57:       int max_id = -1;
deba@57:       for (int i = 0; i < int(keys.size()); ++i) {
alpar@209:         int id = nf->id(keys[i]);
alpar@209:         if (id > max_id) {
alpar@209:           max_id = id;
alpar@209:         }
deba@57:       }
deba@57:       if (max_id >= capacity) {
alpar@209:         int new_capacity = (capacity == 0 ? 1 : capacity);
alpar@209:         while (new_capacity <= max_id) {
alpar@209:           new_capacity <<= 1;
alpar@209:         }
alpar@209:         Value* new_values = allocator.allocate(new_capacity);
alpar@209:         Item it;
alpar@209:         for (nf->first(it); it != INVALID; nf->next(it)) {
alpar@209:           int id = nf->id(it);
alpar@209:           bool found = false;
alpar@209:           for (int i = 0; i < int(keys.size()); ++i) {
alpar@209:             int jd = nf->id(keys[i]);
alpar@209:             if (id == jd) {
alpar@209:               found = true;
alpar@209:               break;
alpar@209:             }
alpar@209:           }
alpar@209:           if (found) continue;
alpar@209:           allocator.construct(&(new_values[id]), values[id]);
alpar@209:           allocator.destroy(&(values[id]));
alpar@209:         }
alpar@209:         if (capacity != 0) allocator.deallocate(values, capacity);
alpar@209:         values = new_values;
alpar@209:         capacity = new_capacity;
deba@57:       }
deba@57:       for (int i = 0; i < int(keys.size()); ++i) {
alpar@209:         int id = nf->id(keys[i]);
alpar@209:         allocator.construct(&(values[id]), Value());
deba@57:       }
deba@57:     }
alpar@209: 
deba@57:     /// \brief Erase a key from the map.
deba@57:     ///
deba@57:     /// Erase a key from the map. It called by the observer notifier
alpar@209:     /// and it overrides the erase() member function of the observer base.
deba@57:     virtual void erase(const Key& key) {
deba@57:       int id = Parent::notifier()->id(key);
deba@57:       allocator.destroy(&(values[id]));
deba@57:     }
deba@57: 
deba@57:     /// \brief Erase more keys from the map.
deba@57:     ///
deba@57:     /// Erase more keys from the map. It called by the observer notifier
alpar@209:     /// 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:         int id = Parent::notifier()->id(keys[i]);
alpar@209:         allocator.destroy(&(values[id]));
deba@57:       }
deba@57:     }
deba@57: 
deba@57:     /// \brief Buildes the map.
alpar@209:     ///
deba@57:     /// It buildes the map. It called by the observer notifier
alpar@209:     /// and it overrides the build() member function of the observer base.
deba@57:     virtual void build() {
deba@57:       Notifier* nf = Parent::notifier();
deba@57:       allocate_memory();
deba@57:       Item it;
deba@57:       for (nf->first(it); it != INVALID; nf->next(it)) {
alpar@209:         int id = nf->id(it);;
alpar@209:         allocator.construct(&(values[id]), Value());
alpar@209:       }
deba@57:     }
deba@57: 
deba@57:     /// \brief Clear the map.
deba@57:     ///
deba@57:     /// It erase all items from the map. It called by the observer notifier
alpar@209:     /// and it overrides the clear() member function of the observer base.
alpar@209:     virtual void clear() {
deba@57:       Notifier* nf = Parent::notifier();
deba@57:       if (capacity != 0) {
alpar@209:         Item it;
alpar@209:         for (nf->first(it); it != INVALID; nf->next(it)) {
alpar@209:           int id = nf->id(it);
alpar@209:           allocator.destroy(&(values[id]));
alpar@209:         }
alpar@209:         allocator.deallocate(values, capacity);
alpar@209:         capacity = 0;
deba@57:       }
deba@57:     }
deba@57: 
deba@57:   private:
alpar@209: 
deba@57:     void allocate_memory() {
deba@57:       int max_id = Parent::notifier()->maxId();
deba@57:       if (max_id == -1) {
alpar@209:         capacity = 0;
alpar@209:         values = 0;
alpar@209:         return;
deba@57:       }
deba@57:       capacity = 1;
deba@57:       while (capacity <= max_id) {
alpar@209:         capacity <<= 1;
deba@57:       }
alpar@209:       values = allocator.allocate(capacity);
alpar@209:     }
deba@57: 
deba@57:     int capacity;
deba@57:     Value* values;
deba@57:     Allocator allocator;
deba@57: 
alpar@209:   };
deba@57: 
deba@57: }
deba@57: 
alpar@209: #endif