alpar@906: /* -*- C++ -*-
ladanyi@1435:  * lemon/bits/array_map.h - Part of LEMON, a generic C++ optimization library
alpar@906:  *
alpar@1164:  * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@1359:  * (Egervary Research Group on Combinatorial Optimization, 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@1307: #include <lemon/bits/map_iterator.h>
deba@1669: #include <lemon/concept_check.h>
deba@1669: #include <lemon/concept/maps.h>
deba@822: 
deba@1669: /// \ingroup graphmapfactory
deba@1669: /// \file
deba@1669: /// \brief Graph maps that construct and destruct
deba@1669: /// their elements dynamically.
deba@822: 
alpar@921: namespace lemon {
deba@822: 
deba@1669:   /// \ingroup graphmapfactory
deba@1669:   ///
deba@1669:   /// \brief Graph map based on the array storage.
deba@1669:   ///
deba@1414:   /// The ArrayMap template class is graph map structure what
deba@1414:   /// automatically updates the map when a key is added to or erased from
deba@1669:   /// the map. This map uses the allocators to implement 
deba@1414:   /// the container functionality.
deba@1414:   ///
deba@1414:   /// The template parameter is the AlterationNotifier that the maps
deba@1414:   /// will belong to and the Value.
deba@822: 
klao@946:   template <typename _Graph, 
klao@946: 	    typename _Item,
klao@946: 	    typename _Value>
deba@1039:   class ArrayMap : public AlterationNotifier<_Item>::ObserverBase {
deba@897: 
deba@1267:     typedef _Item Item;
deba@822:   public:
deba@822: 		
deba@822:     /// The graph type of the maps. 
klao@946:     typedef _Graph Graph;
deba@822:     /// The key type of the maps.
alpar@987:     typedef _Item Key;
klao@946: 
deba@1039:     typedef AlterationNotifier<_Item> Registry;
klao@946: 
deba@822:     /// The MapBase of the Map which imlements the core regisitry function.
klao@946:     typedef typename Registry::ObserverBase Parent;
deba@822: 		
deba@822:     /// The value type of the map.
alpar@987:     typedef _Value Value;
deba@822: 
deba@822: 
klao@946:   private:
deba@822:     typedef std::allocator<Value> Allocator;
deba@822: 
klao@946: 
klao@946:   public:
klao@946: 
deba@1414:     /// Graph and Registry initialized map constructor.
deba@980:     ArrayMap(const Graph& _g) : graph(&_g) {
deba@1267:       Item it;
deba@1267:       attach(_g.getNotifier(Item()));
deba@822:       allocate_memory();
deba@1267:       for (graph->first(it); it != INVALID; graph->next(it)) {
deba@980: 	int id = graph->id(it);;
deba@822: 	allocator.construct(&(values[id]), Value());
deba@822:       }								
deba@822:     }
deba@822: 
klao@946:     /// Constructor to use default value to initialize the map. 
klao@946: 
klao@946:     /// It constrates a map and initialize all of the the map. 
klao@946: 
deba@980:     ArrayMap(const Graph& _g, const Value& _v) : graph(&_g) {
deba@1267:       Item it;
deba@1039:       attach(_g.getNotifier(_Item()));
deba@822:       allocate_memory();
deba@1267:       for (graph->first(it); it != INVALID; graph->next(it)) {
deba@980: 	int id = graph->id(it);;
klao@946: 	allocator.construct(&(values[id]), _v);
deba@822:       }								
deba@822:     }
deba@822: 
deba@1414:     /// Constructor to copy a map of the same map type.
deba@1414:      
alpar@1613:     ArrayMap(const ArrayMap& copy) : Parent(), graph(copy.graph) {
klao@946:       if (copy.attached()) {
klao@946: 	attach(*copy.getRegistry());
klao@946:       }
deba@822:       capacity = copy.capacity;
deba@822:       if (capacity == 0) return;
deba@822:       values = allocator.allocate(capacity);
deba@1267:       Item it;
deba@1267:       for (graph->first(it); it != INVALID; graph->next(it)) {
deba@980: 	int id = graph->id(it);;
deba@891: 	allocator.construct(&(values[id]), copy.values[id]);
deba@822:       }
deba@822:     }
deba@822: 
deba@1669:     /// \brief The destructor of the map.
deba@1669:     ///     
deba@1414:     /// The destructor of the map.
klao@946:     virtual ~ArrayMap() {      
klao@946:       if (attached()) {
klao@946: 	clear();
klao@946: 	detach();
deba@822:       }
deba@822:     }
deba@1669: 		
deba@1669:   private:
deba@1669: 
deba@1669:     ArrayMap& operator=(const ArrayMap&);
deba@1669: 
deba@1669:   protected:
deba@1669: 
deba@1669:     using Parent::attach;
deba@1669:     using Parent::detach;
deba@1669:     using Parent::attached;
deba@1669: 
deba@1669:     const Graph* getGraph() {
deba@1669:       return graph;
deba@1669:     }
deba@1669: 
deba@1669: 
deba@1669:   public:
deba@1669: 
deba@1414:     ///The subscript operator. The map can be subscripted by the
deba@1414:     ///actual keys of the graph. 
deba@1414:      
deba@1267:     Value& operator[](const Key& key) {
deba@980:       int id = graph->id(key);
deba@822:       return values[id];
deba@822:     } 
deba@822: 		
deba@1414: 
deba@1414:     ///The const subscript operator. The map can be subscripted by the
deba@1414:     ///actual keys of the graph. 
deba@1414:      
deba@1267:     const Value& operator[](const Key& key) const {
deba@980:       int id = graph->id(key);
deba@822:       return values[id];
deba@822:     }
deba@822: 	
deba@1414:     /// Setter function of the map. Equivalent with map[key] = val.
deba@1414:     /// This is a compatibility feature with the not dereferable maps.
deba@1414:      
alpar@987:     void set(const Key& key, const Value& val) {
klao@946:       (*this)[key] = val;
deba@822:     }
deba@1669: 
deba@1669:   protected:
deba@1669:     
deba@1414:     /// Add a new key to the map. It called by the map registry.
deba@1414:      
alpar@987:     void add(const Key& key) {
deba@980:       int id = graph->id(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: 	}
klao@946: 	Value* new_values = allocator.allocate(new_capacity);
deba@1267: 	Item it;
deba@1267: 	for (graph->first(it); it != INVALID; graph->next(it)) {
deba@980: 	  int jd = graph->id(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@1414: 
deba@1414:     void add(const std::vector<Key>& keys) {
deba@1414:       int max_id = -1;
deba@1414:       for (int i = 0; i < (int)keys.size(); ++i) {
deba@1414: 	int id = graph->id(keys[i]);
deba@1414: 	if (id > max_id) {
deba@1414: 	  max_id = id;
deba@1414: 	}
deba@1414:       }
deba@1414:       if (max_id >= capacity) {
deba@1414: 	int new_capacity = (capacity == 0 ? 1 : capacity);
deba@1414: 	while (new_capacity <= max_id) {
deba@1414: 	  new_capacity <<= 1;
deba@1414: 	}
deba@1414: 	Value* new_values = allocator.allocate(new_capacity);
deba@1414: 	Item it;
deba@1414: 	for (graph->first(it); it != INVALID; graph->next(it)) {
deba@1414: 	  int id = graph->id(it);
deba@1414: 	  bool found = false;
deba@1414: 	  for (int i = 0; i < (int)keys.size(); ++i) {
deba@1414: 	    int jd = graph->id(keys[i]);
deba@1414: 	    if (id == jd) {
deba@1414: 	      found = true;
deba@1414: 	      break;
deba@1414: 	    }
deba@1414: 	  }
deba@1414: 	  if (found) continue;
deba@1414: 	  allocator.construct(&(new_values[id]), values[id]);
deba@1414: 	  allocator.destroy(&(values[id]));
deba@1414: 	}
deba@1414: 	if (capacity != 0) allocator.deallocate(values, capacity);
deba@1414: 	values = new_values;
deba@1414: 	capacity = new_capacity;
deba@1414:       }
deba@1414:       for (int i = 0; i < (int)keys.size(); ++i) {
deba@1414: 	int id = graph->id(keys[i]);
deba@1414: 	allocator.construct(&(values[id]), Value());
deba@1414:       }
deba@1414:     }
deba@822: 		
deba@1414:     /// Erase a key from the map. It called by the map registry.
deba@1414:      
alpar@987:     void erase(const Key& key) {
deba@980:       int id = graph->id(key);
deba@822:       allocator.destroy(&(values[id]));
deba@822:     }
deba@822: 
deba@1414:     void erase(const std::vector<Key>& keys) {
deba@1414:       for (int i = 0; i < (int)keys.size(); ++i) {
deba@1414: 	int id = graph->id(keys[i]);
deba@1414: 	allocator.destroy(&(values[id]));
deba@1414:       }
deba@1414:     }
deba@1414: 
klao@946:     void build() {
klao@946:       allocate_memory();
deba@1267:       Item it;
deba@1267:       for (graph->first(it); it != INVALID; graph->next(it)) {
deba@980: 	int id = graph->id(it);;
klao@946: 	allocator.construct(&(values[id]), Value());
klao@946:       }								
klao@946:     }
klao@946: 
deba@822:     void clear() {	
deba@822:       if (capacity != 0) {
deba@1267: 	Item it;
deba@1267: 	for (graph->first(it); it != INVALID; graph->next(it)) {
deba@1414: 	  int id = graph->id(it);
klao@946: 	  allocator.destroy(&(values[id]));
klao@946: 	}								
deba@822: 	allocator.deallocate(values, capacity);
deba@822: 	capacity = 0;
deba@822:       }
deba@822:     }
deba@822: 
deba@822:   private:
deba@822:       
deba@822:     void allocate_memory() {
deba@980:       int max_id = graph->maxId(_Item());
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: 
klao@946:     const Graph* graph;
deba@822:     int capacity;
deba@822:     Value* values;
deba@822:     Allocator allocator;
deba@844: 
deba@822:   };		
deba@822: 
deba@822: }
deba@822: 
alpar@921: #endif //LEMON_ARRAY_MAP_H