src/hugo/array_map_factory.h
changeset 799 3393abe30678
parent 786 d7b3b13b9df6
child 817 3e30caeb9c00
equal deleted inserted replaced
3:d816b9dbb169 4:0da22869bbe6
    10 ///\file
    10 ///\file
    11 ///\brief Graph maps that construates and destruates
    11 ///\brief Graph maps that construates and destruates
    12 ///their elements dynamically.
    12 ///their elements dynamically.
    13 
    13 
    14 namespace hugo {
    14 namespace hugo {
    15 	
    15 
       
    16 
    16 /// \addtogroup graphmapfactory
    17 /// \addtogroup graphmapfactory
    17 /// @{
    18 /// @{
    18 
    19 	
    19   ///.
    20   /** The ArrayMapFactory template class is a factory class
    20   template <typename MapRegistry> class ArrayMapFactory {
    21    *  to create maps for the edge and nodes. This map factory
       
    22    *  uses the allocators to implement the container function.
       
    23    *
       
    24    *  The template parameter is the MapRegistry that the maps
       
    25    *  will belong to.
       
    26    */
       
    27 
       
    28   template <typename MapRegistry> 
       
    29   class ArrayMapFactory {
    21 		
    30 		
    22   public:
    31   public:
    23 		
    32 		
       
    33     /// The graph type of the maps. 
    24     typedef typename MapRegistry::Graph Graph;
    34     typedef typename MapRegistry::Graph Graph;
       
    35     /// The key type of the maps.
    25     typedef typename MapRegistry::KeyType KeyType;
    36     typedef typename MapRegistry::KeyType KeyType;
       
    37     /// The iterator to iterate on the keys.
    26     typedef typename MapRegistry::KeyIt KeyIt;
    38     typedef typename MapRegistry::KeyIt KeyIt;
    27 
    39 
       
    40     /// The MapBase of the Map which imlements the core regisitry function.
    28     typedef typename MapRegistry::MapBase MapBase;
    41     typedef typename MapRegistry::MapBase MapBase;
    29 		
    42 		
       
    43     /** The template Map type.
       
    44      */
    30     template <typename V, typename A = std::allocator<V> > 
    45     template <typename V, typename A = std::allocator<V> > 
    31     class Map : public MapBase {
    46     class Map : public MapBase {
    32     
    47     
    33       public:
    48       public:
    34 
    49 
       
    50       /// The value type of the map.
       
    51       typedef V ValueType;
       
    52 
       
    53       /// The value type of the map.
    35       typedef V Value;
    54       typedef V Value;
    36       typedef V ValueType;
    55       /// The reference type of the map;
       
    56       typedef Value& Reference;
       
    57       /// The pointer type of the map;
       
    58       typedef Value* Pointer;
       
    59 
       
    60       /// The const value type of the map.
       
    61       typedef const Value ConstValue;
       
    62       /// The const reference type of the map;
       
    63       typedef const Value& ConstReference;
       
    64       /// The pointer type of the map;
       
    65       typedef const Value* ConstPointer;
       
    66 
       
    67 
    37       typedef A Allocator;
    68       typedef A Allocator;
    38 
    69 
    39 	
    70 	
       
    71       /** Default constructor for the map.
       
    72        */
    40       Map() : values(0), capacity(0) {}
    73       Map() : values(0), capacity(0) {}
    41 			
    74 			
       
    75       /** Graph and Registry initialized map constructor.
       
    76        */
    42       Map(const Graph& g, MapRegistry& r) : MapBase(g, r) {
    77       Map(const Graph& g, MapRegistry& r) : MapBase(g, r) {
    43 	allocate_memory();
    78 	allocate_memory();
    44 	for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) {
    79 	for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) {
    45 	  int id = MapBase::getGraph()->id(it);
    80 	  int id = MapBase::getGraph()->id(it);
    46 	  allocator.construct(&(values[id]), Value());
    81 	  allocator.construct(&(values[id]), Value());
    47 	}								
    82 	}								
    48       }
    83       }
    49 
    84 
       
    85       /** Constructor to use default value to initialize the map. 
       
    86        */
    50       Map(const Graph& g, MapRegistry& r, const Value& v) : MapBase(g, r) {
    87       Map(const Graph& g, MapRegistry& r, const Value& v) : MapBase(g, r) {
    51 	allocate_memory();
    88 	allocate_memory();
    52 	for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) {
    89 	for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) {
    53 	  int id = MapBase::getGraph()->id(it);
    90 	  int id = MapBase::getGraph()->id(it);
    54 	  allocator.construct(&(values[id]), v);
    91 	  allocator.construct(&(values[id]), v);
    55 	}								
    92 	}								
    56       }
    93       }
    57 
    94 
       
    95       /** Constructor to copy a map of the same map type.
       
    96        */
    58       Map(const Map& copy) : MapBase(*copy.graph, *copy.registry) {
    97       Map(const Map& copy) : MapBase(*copy.graph, *copy.registry) {
    59 	capacity = copy.capacity;
    98 	capacity = copy.capacity;
    60 	if (capacity == 0) return;
    99 	if (capacity == 0) return;
    61 	values = allocator.allocate(capacity);
   100 	values = allocator.allocate(capacity);
    62 	for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) {
   101 	for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) {
    63 	  int id = MapBase::getGraph()->id(it);
   102 	  int id = MapBase::getGraph()->id(it);
    64 	  allocator.construct(&(values[id]), copy.values[id]);
   103 	  allocator.construct(&(values[id]), copy.values[id]);
    65 	}
   104 	}
    66       }
   105       }
    67 
   106 
       
   107       /** Constructor to copy a map of an other map type.
       
   108        */
    68       template <typename CMap> Map(const CMap& copy) 
   109       template <typename CMap> Map(const CMap& copy) 
    69 	: MapBase(copy), capacity(0), values(0) {
   110 	: MapBase(copy), capacity(0), values(0) {
    70 	if (MapBase::getGraph()) {
   111 	if (MapBase::getGraph()) {
    71 	  allocate_memory();
   112 	  allocate_memory();
    72 	  for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) {
   113 	  for (KeyIt it(*MapBase::getGraph()); it != INVALID; ++it) {
    73 	    set(it, copy[it]);
   114 	    set(it, copy[it]);
    74 	  }
   115 	  }
    75 	}
   116 	}
    76       }
   117       }
    77 
   118 
       
   119       /** Assign operator to copy a map of the same map type.
       
   120        */
    78       Map& operator=(const Map& copy) {
   121       Map& operator=(const Map& copy) {
    79 	if (&copy == this) return *this;
   122 	if (&copy == this) return *this;
    80 	if (capacity != 0) {
   123 	if (capacity != 0) {
    81 	  MapBase::destroy();
   124 	  MapBase::destroy();
    82 	  allocator.deallocate(values, capacity);
   125 	  allocator.deallocate(values, capacity);
    89 	  allocator.construct(&(values[id]), copy.values[id]);
   132 	  allocator.construct(&(values[id]), copy.values[id]);
    90 	}
   133 	}
    91 	return *this;
   134 	return *this;
    92       }
   135       }
    93 
   136 
       
   137       /** Assign operator to copy a map an other map type.
       
   138        */
    94       template <typename CMap> Map& operator=(const CMap& copy) {
   139       template <typename CMap> Map& operator=(const CMap& copy) {
    95 	if (MapBase::getGraph()) {
   140 	if (MapBase::getGraph()) {
    96 	  MapBase::destroy();
   141 	  MapBase::destroy();
    97 	} 
   142 	} 
    98 	MapBase::operator=(copy);
   143 	MapBase::operator=(copy);
   103 	  }
   148 	  }
   104 	}
   149 	}
   105 	return *this;
   150 	return *this;
   106       }
   151       }
   107 				
   152 				
       
   153       /** The destructor of the map.
       
   154        */
   108       virtual ~Map() {
   155       virtual ~Map() {
   109 	if (capacity != 0) {
   156 	if (capacity != 0) {
   110 	  MapBase::destroy();
   157 	  MapBase::destroy();
   111 	  allocator.deallocate(values, capacity);
   158 	  allocator.deallocate(values, capacity);
   112 	}
   159 	}
   113       }
   160       }
   114 	
   161 	
   115 	
   162 	
       
   163       /**
       
   164        * The subscript operator. The map can be subscripted by the
       
   165        * actual keys of the graph. 
       
   166        */
   116       Value& operator[](const KeyType& key) {
   167       Value& operator[](const KeyType& key) {
   117 	int id = MapBase::getGraph()->id(key);
   168 	int id = MapBase::getGraph()->id(key);
   118 	return values[id];
   169 	return values[id];
   119       } 
   170       } 
   120 		
   171 		
       
   172       /**
       
   173        * The const subscript operator. The map can be subscripted by the
       
   174        * actual keys of the graph. 
       
   175        */
   121       const Value& operator[](const KeyType& key) const {
   176       const Value& operator[](const KeyType& key) const {
   122 	int id = MapBase::getGraph()->id(key);
   177 	int id = MapBase::getGraph()->id(key);
   123 	return values[id];
   178 	return values[id];
   124       }
   179       }
   125 	
   180 	
   126       const Value& get(const KeyType& key) const {
   181       /** Setter function of the map. Equivalent with map[key] = val.
   127 	int id = MapBase::getGraph()->id(key);
   182        *  This is a compatibility feature with the not dereferable maps.
   128 	return values[id];
   183        */
   129       } 
       
   130 		
       
   131       void set(const KeyType& key, const Value& val) {
   184       void set(const KeyType& key, const Value& val) {
   132 	int id = MapBase::getGraph()->id(key);
   185 	int id = MapBase::getGraph()->id(key);
   133 	values[id] = val;
   186 	values[id] = val;
   134       }
   187       }
   135 		
   188 		
       
   189       /** Add a new key to the map. It called by the map registry.
       
   190        */
   136       void add(const KeyType& key) {
   191       void add(const KeyType& key) {
   137 	int id = MapBase::getGraph()->id(key);
   192 	int id = MapBase::getGraph()->id(key);
   138 	if (id >= capacity) {
   193 	if (id >= capacity) {
   139 	  int new_capacity = (capacity == 0 ? 1 : capacity);
   194 	  int new_capacity = (capacity == 0 ? 1 : capacity);
   140 	  while (new_capacity <= id) {
   195 	  while (new_capacity <= id) {
   153 	  capacity = new_capacity;
   208 	  capacity = new_capacity;
   154 	}
   209 	}
   155 	allocator.construct(&(values[id]), Value());
   210 	allocator.construct(&(values[id]), Value());
   156       }
   211       }
   157 		
   212 		
       
   213       /** Erase a key from the map. It called by the map registry.
       
   214        */
   158       void erase(const KeyType& key) {
   215       void erase(const KeyType& key) {
   159 	int id = MapBase::getGraph()->id(key);
   216 	int id = MapBase::getGraph()->id(key);
   160 	allocator.destroy(&(values[id]));
   217 	allocator.destroy(&(values[id]));
   161       }
   218       }
   162 
   219 
       
   220       /** Clear the data structure.
       
   221        */
   163       void clear() {	
   222       void clear() {	
   164 	if (capacity != 0) {
   223 	if (capacity != 0) {
   165 	  MapBase::destroy();
   224 	  MapBase::destroy();
   166 	  allocator.deallocate(values, capacity);
   225 	  allocator.deallocate(values, capacity);
   167 	  capacity = 0;
   226 	  capacity = 0;
   168 	}
   227 	}
   169       }
   228       }
   170 	
   229 	
       
   230       /** Compatible iterator with the stl maps' iterators.
       
   231        *  It iterates on pairs of a key and a value.
       
   232        */
   171       class iterator {
   233       class iterator {
   172 	friend class Map;
   234 	friend class Map;
   173 	friend class const_iterator;
   235 	friend class const_iterator;
   174       private:
   236       private:
   175 
   237 
   369       int capacity;
   431       int capacity;
   370       Value* values;
   432       Value* values;
   371       Allocator allocator;
   433       Allocator allocator;
   372     };		
   434     };		
   373   };
   435   };
   374   
   436 
   375 /// @}
   437 /// @}
   376   
       
   377 
   438 
   378 }
   439 }
   379 
   440 
   380 #endif
   441 #endif