src/work/deba/vector_map_factory.h
changeset 915 751ed145bdae
parent 702 4207f82a1778
child 921 818510fa3d99
equal deleted inserted replaced
5:953fb109bdf3 6:0616a778c854
       
     1 // -*- c++ -*-
     1 #ifndef VECTOR_MAP_H
     2 #ifndef VECTOR_MAP_H
     2 #define VECTOR_MAP_H
     3 #define VECTOR_MAP_H
     3 
     4 
     4 #include <vector>
     5 #include <vector>
     5 #include <iostream>
       
     6 
     6 
     7 #include "extended_pair.h"
     7 #include "extended_pair.h"
     8 
     8 
     9 namespace hugo {
     9 namespace hugo {
    10 
    10 
    53       }
    53       }
    54 
    54 
    55       /** Constructor to use default value to initialize the map. 
    55       /** Constructor to use default value to initialize the map. 
    56        */
    56        */
    57       Map(const Graph& g, MapRegistry& r, const Value& v) : MapBase(g, r) {
    57       Map(const Graph& g, MapRegistry& r, const Value& v) : MapBase(g, r) {
    58 	init();
       
    59 	for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) {
    58 	for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) {
    60           set(it, v);
    59           int id = getGraph->id(it);
       
    60 	  if (id >= container.size) {
       
    61 	    container.resize(id + 1);
       
    62 	  }
       
    63 	  set(it, v);
    61         }
    64         }
    62       }
    65       }
    63 
    66 
    64       /** Constructor to copy a map of an other map type.
    67       /** Constructor to copy a map of an other map type.
    65        */
    68        */
    66       template <typename CMap> Map(const CMap& copy) : MapBase(copy) {
    69       template <typename CMap> Map(const CMap& copy) : MapBase(copy) {
    67 	if (getGraph()) {
    70 	if (getGraph()) {
    68 	  init();
       
    69 	  for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) {
    71 	  for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) {
       
    72 	    int id = getGraph->id(it);
       
    73 	    if (id >= container.size) {
       
    74 	      container.resize(id + 1);
       
    75 	    }
    70 	    set(it, copy[it]);
    76 	    set(it, copy[it]);
    71 	  }
    77 	  }
    72 	}
    78 	}
    73       }
    79       }
    74 
    80 
    78 	if (getGraph()) {
    84 	if (getGraph()) {
    79 	  destroy();
    85 	  destroy();
    80 	} 
    86 	} 
    81 	this->MapBase::operator=(copy);
    87 	this->MapBase::operator=(copy);
    82 	if (getGraph()) {
    88 	if (getGraph()) {
    83 	  init();
       
    84 	  for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) {
    89 	  for (KeyIt it(*getGraph()); getGraph()->valid(it); getGraph()->next(it)) {
       
    90 	    int id = getGraph->id(it);
       
    91 	    if (id >= container.size) {
       
    92 	      container.resize(id + 1);
       
    93 	    }
    85 	    set(it, copy[it]);
    94 	    set(it, copy[it]);
    86 	  }
    95 	  }
    87 	}
    96 	}
    88       }
    97       }
    89 
    98 
    90       /** The destructor of the map.
    99       /** The destructor of the map.
    91        */
   100        */
    92       virtual ~Map() {
   101       virtual ~Map() {
    93 	destroy();
       
    94       }
   102       }
    95 		
   103 		
    96       /**
   104       /**
    97        * The subscript operator. The map can be subscripted by the
   105        * The subscript operator. The map can be subscripted by the
    98        * actual keys of the graph. 
   106        * actual keys of the graph. 
   149 
   157 
   150 	/** Default constructor. 
   158 	/** Default constructor. 
   151 	 */
   159 	 */
   152 	iterator() {}
   160 	iterator() {}
   153 
   161 
       
   162 	typedef extended_pair<const Key&, const Key&, 
       
   163 			      Value&, Value&> Reference;
       
   164 
   154 	/** Dereference operator for map.
   165 	/** Dereference operator for map.
   155 	 */	 
   166 	 */	 
   156 	std::pair<const Key, Value> operator*() {
   167 	Reference operator*() {
   157 	  return std::pair<const Key, Value>(it, (*map)[it]);
   168 	  return Reference(it, (*map)[it]);
   158 	}
   169 	}
   159 
       
   160 
   170 
   161 	class Pointer {
   171 	class Pointer {
   162 	  friend class iterator;
   172 	  friend class iterator;
   163 	private:
   173 	private:
   164 	  typedef extended_pair<const Key&, const Key&, Value&, Value&> Pair;
   174 	  Reference data;
   165 	  Pair data;
       
   166 	  Pointer(const Key& key, Value& val) : data(key, val) {}
   175 	  Pointer(const Key& key, Value& val) : data(key, val) {}
   167 	public:
   176 	public:
   168 	  Pair* operator->() {return &data;}
   177 	  Reference* operator->() {return &data;}
   169 	};
   178 	};
   170 
   179 
   171 	/** Arrow operator for map.
   180 	/** Arrow operator for map.
   172 	 */	 
   181 	 */	 
   173 	Pointer operator->() {
   182 	Pointer operator->() {
   225       private:
   234       private:
   226 
   235 
   227 	/** Private constructor to initalize the the iterators returned
   236 	/** Private constructor to initalize the the iterators returned
   228 	 *  by the begin() and end().
   237 	 *  by the begin() and end().
   229 	 */
   238 	 */
   230 	const_iterator (Map& pmap, const KeyIt& pit) : map(&pmap), it(pit) {}
   239 	const_iterator (const Map& pmap, const KeyIt& pit) 
       
   240 	  : map(&pmap), it(pit) {}
   231 
   241 
   232       public:
   242       public:
   233 
   243 
   234 	/** Default constructor. 
   244 	/** Default constructor. 
   235 	 */
   245 	 */
   236 	const_iterator() {}
   246 	const_iterator() {}
   237 
   247 
   238 	/** Constructor to convert iterator to const_iterator.
   248 	/** Constructor to convert iterator to const_iterator.
   239 	 */
   249 	 */
   240 	const_iterator(iterator p_it) {
   250 	const_iterator(iterator p_it) : map(p_it.map), it(p_it.it) {}
   241 	  it = p_it.it;
       
   242 	}
       
   243       
   251       
       
   252 	typedef extended_pair<const Key&, const Key&, 
       
   253 	  const Value&, const Value&> Reference;
       
   254 
   244 	/** Dereference operator for map.
   255 	/** Dereference operator for map.
   245 	 */	 
   256 	 */	 
   246 	std::pair<const Key, const Value> operator*() const {
   257 	Reference operator*() {
   247 	  return std::pair<const Key, const Value>(it, (*map)[it]);
   258 	  return Reference(it, (*map)[it]);
   248 	}
   259 	}
       
   260 
   249 
   261 
   250 	class Pointer {
   262 	class Pointer {
   251 	  friend class const_iterator;
   263 	  friend class const_iterator;
   252 	private:
   264 	private:
   253 	  typedef extended_pair<const Key&, const Key&, const Value&, const Value&> Pair;
   265 	  Reference data;
   254 	  Pair data;
       
   255 	  Pointer(const Key& key, const Value& val) : data(key, val) {}
   266 	  Pointer(const Key& key, const Value& val) : data(key, val) {}
   256 	public:
   267 	public:
   257 	  Pair* operator->() {return &data;}
   268 	  Reference* operator->() {return &data;}
   258 	};
   269 	};
       
   270 
   259 	/** Arrow operator for map.
   271 	/** Arrow operator for map.
   260 	 */	 
   272 	 */	 
   261 	Pointer operator->() const {
   273 	Pointer operator->() {
   262 	  return Pointer(it, (*map)[it]);
   274 	  return Pointer(it, ((*map)[it])); 
   263 	}
   275 	}
   264 
   276 
   265 	/** The pre increment operator of the map.
   277 	/** The pre increment operator of the map.
   266 	 */
   278 	 */
   267 	const_iterator& operator++() { 
   279 	const_iterator& operator++() {