alpar@906: /* -*- C++ -*-
alpar@921:  * src/lemon/array_map.h - Part of LEMON, a generic C++ optimization library
alpar@906:  *
alpar@906:  * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@906:  * (Egervary Combinatorial Optimization Research Group, EGRES).
alpar@906:  *
alpar@906:  * Permission to use, modify and distribute this software is granted
alpar@906:  * provided that this copyright notice appears in all copies. For
alpar@906:  * precise terms see the accompanying LICENSE file.
alpar@906:  *
alpar@906:  * This software is provided "AS IS" with no warranty of any kind,
alpar@906:  * express or implied, and with no claim as to its suitability for any
alpar@906:  * purpose.
alpar@906:  *
alpar@906:  */
alpar@906: 
alpar@921: #ifndef LEMON_ARRAY_MAP_H
alpar@921: #define LEMON_ARRAY_MAP_H
deba@822: 
deba@822: #include <memory>
deba@822: 
alpar@921: #include <lemon/map_iterator.h>
alpar@921: #include <lemon/map_bits.h>
deba@822: 
deba@822: ///\ingroup graphmaps
deba@822: ///\file
deba@822: ///\brief Graph maps that construates and destruates
deba@822: ///their elements dynamically.
deba@822: 
alpar@921: namespace lemon {
deba@822: 
deba@822: 
deba@822:   /// \addtogroup graphmaps
deba@822:   /// @{
deba@822: 	
deba@822:   /** The ArrayMap template class is graph map structure what
deba@822:    *  automatically updates the map when a key is added to or erased from
deba@822:    *  the map. This map factory uses the allocators to implement 
deba@822:    *  the container functionality.
deba@822:    *
deba@822:    *  The template parameter is the MapRegistry that the maps
deba@822:    *  will belong to and the ValueType.
deba@822:    */
deba@822: 
deba@822:   template <typename MapRegistry, typename Value> 
deba@822:   class ArrayMap : public MapRegistry::MapBase {
deba@897: 
deba@897:     template <typename MR, typename V> friend class ArrayMap;
deba@822: 		
deba@822:   public:
deba@822: 		
deba@822:     /// The graph type of the maps. 
deba@822:     typedef typename MapRegistry::Graph Graph;
deba@822:     /// The key type of the maps.
deba@822:     typedef typename MapRegistry::KeyType KeyType;
deba@822:     /// The iterator to iterate on the keys.
deba@822:     typedef typename MapRegistry::KeyIt KeyIt;
deba@822: 
deba@822:     /// The MapBase of the Map which imlements the core regisitry function.
deba@822:     typedef typename MapRegistry::MapBase MapBase;
deba@822: 		
deba@822:     
deba@822:   public:
deba@822: 
deba@822:     /// The value type of the map.
deba@822:     typedef Value ValueType;
deba@822:     /// The reference type of the map;
deba@822:     typedef Value& ReferenceType;
deba@822:     /// The pointer type of the map;
deba@822:     typedef Value* PointerType;
deba@822: 
deba@822:     /// The const value type of the map.
deba@822:     typedef const Value ConstValueType;
deba@822:     /// The const reference type of the map;
deba@822:     typedef const Value& ConstReferenceType;
deba@822:     /// The pointer type of the map;
deba@822:     typedef const Value* ConstPointerType;
deba@822: 
deba@822: 
deba@822:     typedef std::allocator<Value> Allocator;
deba@822: 
deba@822: 	
deba@822:     /** Graph and Registry initialized map constructor.
deba@822:      */
deba@822:     ArrayMap(const Graph& g, MapRegistry& r) : MapBase(g, r) {
deba@822:       allocate_memory();
deba@822:       for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) {
deba@844: 	int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), it);
deba@822: 	allocator.construct(&(values[id]), Value());
deba@822:       }								
deba@822:     }
deba@822: 
deba@822:     /** Constructor to use default value to initialize the map. 
deba@822:      */
deba@822:     ArrayMap(const Graph& g, MapRegistry& r, const Value& v) 
deba@822:       : MapBase(g, r) {
deba@822:       allocate_memory();
deba@822:       for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) {
deba@844: 	int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), it);
deba@822: 	allocator.construct(&(values[id]), v);
deba@822:       }								
deba@822:     }
deba@822: 
deba@822:     /** Constructor to copy a map of the same map type.
deba@822:      */
deba@891:     ArrayMap(const ArrayMap& copy) : MapBase(copy) {
deba@822:       capacity = copy.capacity;
deba@822:       if (capacity == 0) return;
deba@822:       values = allocator.allocate(capacity);
deba@822:       for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) {
deba@844: 	int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), it);
deba@822: 	allocator.construct(&(values[id]), copy.values[id]);
deba@822:       }
deba@822:     }
deba@822: 
deba@822:     /** Constructor to copy a map of an other map type.
deba@822:      */
deba@891:     template <typename TT>
deba@891:     ArrayMap(const ArrayMap<MapRegistry, TT>& copy) 
deba@891:       : MapBase(copy) {
deba@891:       capacity = copy.capacity;
deba@891:       if (capacity == 0) return;
deba@891:       values = allocator.allocate(capacity);
deba@891:       for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) {
deba@891: 	int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), it);
deba@891: 	allocator.construct(&(values[id]), copy.values[id]);
deba@822:       }
deba@822:     }
deba@822: 
deba@822:     /** Assign operator to copy a map of the same map type.
deba@822:      */
deba@822:     ArrayMap& operator=(const ArrayMap& copy) {
deba@822:       if (&copy == this) return *this;
deba@897:       
deba@897:       if (MapBase::getGraph() != copy.getGraph()) {
deba@897: 	if (capacity != 0) {
deba@897: 	  MapBase::destroy();
deba@897: 	  allocator.deallocate(values, capacity);
deba@897: 	}
deba@891: 
deba@897: 	MapBase::operator=(copy);
deba@897: 	capacity = copy.capacity;
deba@897: 	if (capacity == 0) return *this;
deba@897: 	values = allocator.allocate(capacity);      
deba@822:       }
deba@891: 
deba@822:       for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) {
deba@844: 	int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), it);
deba@822: 	allocator.construct(&(values[id]), copy.values[id]);
deba@822:       }
deba@891: 
deba@822:       return *this;
deba@822:     }
deba@822: 
deba@891:     /** Assign operator to copy a map of an other map type.
deba@822:      */
deba@891:     template <typename TT>
deba@891:     ArrayMap& operator=(const ArrayMap<MapRegistry, TT>& copy) {
deba@897: 
deba@897:       if (MapBase::getGraph() != copy.getGraph()) {
deba@897: 	if (capacity != 0) {
deba@897: 	  MapBase::destroy();
deba@897: 	  allocator.deallocate(values, capacity);
deba@897: 	}
deba@897: 
deba@897: 	MapBase::operator=(copy);
deba@897: 
deba@897: 	capacity = copy.capacity;
deba@897: 	if (capacity == 0) return *this;
deba@897: 	values = allocator.allocate(capacity);
deba@844:       }
deba@891: 
deba@891:       for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) {
deba@891: 	int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), it);
deba@891: 	allocator.construct(&(values[id]), copy.values[id]);
deba@822:       }
deba@891: 
deba@822:       return *this;
deba@822:     }
deba@822: 				
deba@822:     /** The destructor of the map.
deba@822:      */
deba@822:     virtual ~ArrayMap() {
deba@822:       if (capacity != 0) {
deba@822: 	MapBase::destroy();
deba@822: 	allocator.deallocate(values, capacity);
deba@822:       }
deba@822:     }
deba@822: 	
deba@822: 	
deba@822:     /**
deba@822:      * The subscript operator. The map can be subscripted by the
deba@822:      * actual keys of the graph. 
deba@822:      */
deba@822:     ReferenceType operator[](const KeyType& key) {
deba@844:       int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), key);
deba@822:       return values[id];
deba@822:     } 
deba@822: 		
deba@822:     /**
deba@822:      * The const subscript operator. The map can be subscripted by the
deba@822:      * actual keys of the graph. 
deba@822:      */
deba@822:     ConstReferenceType operator[](const KeyType& key) const {
deba@844:       int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), key);
deba@822:       return values[id];
deba@822:     }
deba@822: 	
deba@822:     /** Setter function of the map. Equivalent with map[key] = val.
deba@822:      *  This is a compatibility feature with the not dereferable maps.
deba@822:      */
deba@822:     void set(const KeyType& key, const ValueType& val) {
deba@844:       int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), key);
deba@822:       values[id] = val;
deba@822:     }
deba@822: 		
deba@822:     /** Add a new key to the map. It called by the map registry.
deba@822:      */
deba@822:     void add(const KeyType& key) {
deba@844:       int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), key);
deba@822:       if (id >= capacity) {
deba@822: 	int new_capacity = (capacity == 0 ? 1 : capacity);
deba@822: 	while (new_capacity <= id) {
deba@822: 	  new_capacity <<= 1;
deba@822: 	}
deba@822: 	Value* new_values = allocator.allocate(new_capacity);;
deba@822: 	for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) {
deba@844: 	  int jd = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), it);
deba@822: 	  if (id != jd) {
deba@822: 	    allocator.construct(&(new_values[jd]), values[jd]);
deba@822: 	    allocator.destroy(&(values[jd]));
deba@822: 	  }
deba@822: 	}
deba@822: 	if (capacity != 0) allocator.deallocate(values, capacity);
deba@822: 	values = new_values;
deba@822: 	capacity = new_capacity;
deba@822:       }
deba@822:       allocator.construct(&(values[id]), Value());
deba@822:     }
deba@822: 		
deba@822:     /** Erase a key from the map. It called by the map registry.
deba@822:      */
deba@822:     void erase(const KeyType& key) {
deba@844:       int id = KeyInfo<Graph, KeyIt>::id(*MapBase::getGraph(), key);
deba@822:       allocator.destroy(&(values[id]));
deba@822:     }
deba@822: 
deba@822:     /** Clear the data structure.
deba@822:      */
deba@822:     void clear() {	
deba@822:       if (capacity != 0) {
deba@822: 	MapBase::destroy();
deba@822: 	allocator.deallocate(values, capacity);
deba@822: 	capacity = 0;
deba@822:       }
deba@822:     }
deba@822: 
deba@822:     /// The stl compatible pair iterator of the map.
deba@822:     typedef MapIterator<ArrayMap> Iterator;
deba@822:     /// The stl compatible const pair iterator of the map.
deba@822:     typedef MapConstIterator<ArrayMap> ConstIterator;
deba@822: 
deba@822:     /** Returns the begin iterator of the map.
deba@822:      */
deba@822:     Iterator begin() {
deba@822:       return Iterator(*this, KeyIt(*MapBase::getGraph()));
deba@822:     }
deba@822: 
deba@822:     /** Returns the end iterator of the map.
deba@822:      */
deba@822:     Iterator end() {
deba@822:       return Iterator(*this, INVALID);
deba@822:     }
deba@822: 
deba@822:     /** Returns the begin ConstIterator of the map.
deba@822:      */
deba@822:     ConstIterator begin() const {
deba@822:       return ConstIterator(*this, KeyIt(*MapBase::getGraph()));
deba@822:     }
deba@822: 
deba@822:     /** Returns the end const_iterator of the map.
deba@822:      */
deba@822:     ConstIterator end() const {
deba@822:       return ConstIterator(*this, INVALID);
deba@822:     }
deba@822: 
deba@830:     /// The KeySet of the Map.
deba@830:     typedef MapConstKeySet<ArrayMap> ConstKeySet;
deba@830: 
deba@830:     /// KeySet getter function.
deba@830:     ConstKeySet keySet() const {
deba@830:       return ConstKeySet(*this); 
deba@830:     }
deba@830: 
deba@830:     /// The ConstValueSet of the Map.
deba@830:     typedef MapConstValueSet<ArrayMap> ConstValueSet;
deba@830: 
deba@830:     /// ConstValueSet getter function.
deba@830:     ConstValueSet valueSet() const {
deba@830:       return ConstValueSet(*this);
deba@830:     }
deba@830: 
deba@830:     /// The ValueSet of the Map.
deba@830:     typedef MapValueSet<ArrayMap> ValueSet;
deba@830: 
deba@830:     /// ValueSet getter function.
deba@830:     ValueSet valueSet() {
deba@830:       return ValueSet(*this);
deba@830:     }
deba@830: 
deba@822:   private:
deba@822:       
deba@822:     void allocate_memory() {
deba@844:       int max_id = KeyInfo<Graph, KeyIt>::maxId(*MapBase::getGraph());
deba@822:       if (max_id == -1) {
deba@822: 	capacity = 0;
deba@822: 	values = 0;
deba@822: 	return;
deba@822:       }
deba@822:       capacity = 1;
deba@822:       while (capacity <= max_id) {
deba@822: 	capacity <<= 1;
deba@822:       }
deba@822:       values = allocator.allocate(capacity);	
deba@822:     }      
deba@822: 
deba@822:     int capacity;
deba@822:     Value* values;
deba@822:     Allocator allocator;
deba@844: 
deba@844:   public:
deba@844:     // STL  compatibility typedefs.
deba@844:     typedef Iterator iterator;
deba@844:     typedef ConstIterator const_iterator;
deba@844:     typedef typename Iterator::PairValueType value_type;
deba@844:     typedef typename Iterator::KeyType key_type;
deba@844:     typedef typename Iterator::ValueType data_type;
deba@844:     typedef typename Iterator::PairReferenceType reference;
deba@844:     typedef typename Iterator::PairPointerType pointer;
deba@844:     typedef typename ConstIterator::PairReferenceType const_reference;
deba@844:     typedef typename ConstIterator::PairPointerType const_pointer;
deba@844:     typedef int difference_type;		
deba@822:   };		
deba@822: 
deba@822: /// @}
deba@822: 
deba@822: }
deba@822: 
alpar@921: #endif //LEMON_ARRAY_MAP_H