(none)
authordeba
Fri, 07 May 2004 08:18:30 +0000
changeset 5719632ea8be6ca
parent 570 eec0a62979c9
child 572 e9ed28955421
(none)
src/work/deba/main.cpp
src/work/deba/map_base.h
src/work/deba/map_registry.h
src/work/deba/test_graph.h
src/work/deba/vector_map.h
src/work/deba/vector_map_factory.h
     1.1 --- a/src/work/deba/main.cpp	Fri May 07 08:02:17 2004 +0000
     1.2 +++ b/src/work/deba/main.cpp	Fri May 07 08:18:30 2004 +0000
     1.3 @@ -7,7 +7,7 @@
     1.4  
     1.5  int main() {
     1.6  	ListGraph g;
     1.7 -	ListGraph::NodeMap<int> map(g);
     1.8 +	ListGraph::NodeMapFactory::VectorMap<int> map(g, g.node_maps);
     1.9  	ListGraph::Node node = g.addNode();
    1.10  	map[node] = 12;	
    1.11  	return 0;
     2.1 --- a/src/work/deba/map_base.h	Fri May 07 08:02:17 2004 +0000
     2.2 +++ b/src/work/deba/map_base.h	Fri May 07 08:18:30 2004 +0000
     2.3 @@ -39,17 +39,17 @@
     2.4  			Simple constructor to register into a graph registry.
     2.5  		*/
     2.6  	
     2.7 -		MapBase(Registry& r) : registry(0) {
     2.8 -			registry->add(*this);
     2.9 +		MapBase(Graph& g, Registry& r) : graph(&g), registry(0) {
    2.10 +			registry->attach(*this);
    2.11  		}
    2.12  
    2.13  		/** 
    2.14  			Copy constructor with registering into the map.
    2.15  		*/	
    2.16  	
    2.17 -		MapBase(const MapBase& copy) : registry(0) {
    2.18 -			if (registry) {
    2.19 -				registry->add(*this);
    2.20 +		MapBase(const MapBase& copy) : registry(0), graph(copy.graph) {
    2.21 +			if (copy.registry) {
    2.22 +				copy.registry->attach(*this);
    2.23  			}
    2.24  		} 
    2.25  	
    2.26 @@ -59,11 +59,11 @@
    2.27  
    2.28  		const MapBase& operator=(const MapBase& copy) {
    2.29  			if (registry) {
    2.30 -				registry->erase(*this);
    2.31 +				registry->detach(*this);
    2.32  			}
    2.33 -			registry = copy.registry;
    2.34 -			if (registry) {
    2.35 -				registry->add(*this);
    2.36 +			graph = copy.graph;
    2.37 +			if (copy.registry) {
    2.38 +				copy.registry->attach(*this);
    2.39  			}
    2.40  		}
    2.41  	
    2.42 @@ -74,13 +74,14 @@
    2.43  
    2.44  		virtual ~MapBase() {
    2.45  			if (registry) {
    2.46 -				registry->erase(*this);
    2.47 +				registry->detach(*this);
    2.48  			}
    2.49  		}
    2.50  	
    2.51  	protected:
    2.52  		
    2.53  		Registry* registry;
    2.54 +		Graph* graph;
    2.55  
    2.56  		int registry_index;
    2.57  	
    2.58 @@ -88,9 +89,9 @@
    2.59  			Helper function to implement the default constructor in the subclasses.
    2.60  		*/
    2.61  	
    2.62 -		virtual void init(Graph& g) {
    2.63 +		virtual void init() {
    2.64  
    2.65 -			for (KeyIt it(g); g.valid(it); g.next(it)) {
    2.66 +			for (KeyIt it(*graph); graph->valid(it); graph->next(it)) {
    2.67  				add(it);
    2.68  			}
    2.69  		}
    2.70 @@ -99,8 +100,8 @@
    2.71  			Helper function to implement the destructor in the subclasses.
    2.72  		*/
    2.73  	
    2.74 -		virtual void destroy(Graph& g) {
    2.75 -			for (KeyIt it(g); g.valid(it); g.next(it)) {
    2.76 +		virtual void destroy() {
    2.77 +			for (KeyIt it(*graph); graph->valid(it); graph->next(it)) {
    2.78  				erase(it);
    2.79  			}
    2.80  		}
     3.1 --- a/src/work/deba/map_registry.h	Fri May 07 08:02:17 2004 +0000
     3.2 +++ b/src/work/deba/map_registry.h	Fri May 07 08:18:30 2004 +0000
     3.3 @@ -28,43 +28,50 @@
     3.4  		typedef std::vector<Map*> Container; 
     3.5  	  Container container;
     3.6  
     3.7 -		Graph* graph;		
     3.8 -		
     3.9  		
    3.10  	public:
    3.11  	
    3.12 -		MapRegistry(Graph& g) : container(0), graph(&g) {}
    3.13 +		MapRegistry() {}
    3.14 +	
    3.15 +		MapRegistry(const MapRegistry&) {}
    3.16 +		
    3.17 +		MapRegistry& operator=(const MapRegistry&) {
    3.18 +			for (it = container.begin(); it != container.end(); ++it) {
    3.19 +				(*it)->destroy();
    3.20 +				(*it)->graph = 0;
    3.21 +				(*it)->registry = 0;
    3.22 +			}
    3.23 +		}
    3.24  				
    3.25  		~MapRegistry() {
    3.26  			typename Container::iterator it;
    3.27  			for (it = container.begin(); it != container.end(); ++it) {
    3.28 -				(*it)->destroy(*graph);
    3.29 +				(*it)->destroy();
    3.30  				(*it)->registry = 0;
    3.31 +				(*it)->graph = 0;
    3.32  			}
    3.33  		}
    3.34  	
    3.35 -	private:
    3.36 -		MapRegistry(const MapRegistry& ) {}
    3.37 -		MapRegistry& operator=(const MapRegistry& ) {}
    3.38  	
    3.39  	public:
    3.40  	
    3.41 -		void add(Map& map) {
    3.42 +		void attach(Map& map) {
    3.43  			if (map.registry) {
    3.44 -				map.registry->erase(map);
    3.45 +				map.registry->detach(map);
    3.46  			}
    3.47  			container.push_back(&map);
    3.48  			map.registry = this;
    3.49  			map.registry_index = container.size()-1;
    3.50 -			map.init(*graph);
    3.51 +			map.init();
    3.52  		} 
    3.53  	
    3.54 -		void erase(Map& map_base) {
    3.55 -			map_base.destroy(*graph);
    3.56 +		void detach(Map& map_base) {
    3.57 +			map_base.destroy();
    3.58  			container.back()->registry_index = map_base.registry_index; 
    3.59  			container[map_base.registry_index] = container.back();
    3.60  			container.pop_back();
    3.61  			map_base.registry = 0;
    3.62 +			map_base.graph = 0;
    3.63  		}
    3.64  	
    3.65  		
    3.66 @@ -82,11 +89,6 @@
    3.67  			}
    3.68  		}
    3.69  
    3.70 -		Graph& getGraph() {
    3.71 -			return *graph;
    3.72 -		}
    3.73 -
    3.74 -
    3.75  	};
    3.76  
    3.77  }
     4.1 --- a/src/work/deba/test_graph.h	Fri May 07 08:02:17 2004 +0000
     4.2 +++ b/src/work/deba/test_graph.h	Fri May 07 08:18:30 2004 +0000
     4.3 @@ -7,7 +7,7 @@
     4.4  
     4.5  #include "invalid.h"
     4.6  
     4.7 -#include "vector_map.h"
     4.8 +#include "vector_map_factory.h"
     4.9  
    4.10  namespace hugo {
    4.11  
    4.12 @@ -38,27 +38,19 @@
    4.13   
    4.14    private:
    4.15  
    4.16 + 
    4.17 +	public:
    4.18 +	
    4.19  		typedef MapRegistry<ListGraph, Node, NodeIt> NodeMapRegistry;
    4.20  		NodeMapRegistry node_maps;
    4.21  		
    4.22 +		
    4.23  		typedef MapRegistry<ListGraph, Edge, EdgeIt> EdgeMapRegistry;
    4.24  		EdgeMapRegistry edge_maps;
    4.25 +
    4.26 +		typedef VectorMapFactory<ListGraph, Edge, EdgeIt> EdgeMapFactory;
    4.27 +		typedef VectorMapFactory<ListGraph, Node, NodeIt> NodeMapFactory;
    4.28   
    4.29 -	public:
    4.30 - 
    4.31 -
    4.32 -    template <typename T>
    4.33 -    class NodeMap : public VectorMap<ListGraph, Node, NodeIt, T> {
    4.34 -		public:
    4.35 -			NodeMap(ListGraph& g) : VectorMap<ListGraph, Node, NodeIt, T>(g.node_maps) {}
    4.36 -		};
    4.37 -		
    4.38 -    template <typename T>
    4.39 -    class EdgeMap : public VectorMap<ListGraph, Edge, EdgeIt, T> {
    4.40 -		public:
    4.41 -			EdgeMap(ListGraph& g) : VectorMap<ListGraph, Edge, EdgeIt, T>(g.edge_maps) {}
    4.42 -		};
    4.43 -
    4.44  
    4.45      int node_id;
    4.46      int edge_id;
    4.47 @@ -215,8 +207,7 @@
    4.48  
    4.49      /* default constructor */
    4.50  
    4.51 -    ListGraph() : node_id(0), edge_id(0), _node_num(0), _edge_num(0), _first_node(0), _last_node(0),
    4.52 -			edge_maps(*this), node_maps(*this) { }
    4.53 +    ListGraph() : node_id(0), edge_id(0), _node_num(0), _edge_num(0), _first_node(0), _last_node(0){ }
    4.54      
    4.55      ~ListGraph() { 
    4.56        while (first<NodeIt>().valid()) erase(first<NodeIt>());
     5.1 --- a/src/work/deba/vector_map.h	Fri May 07 08:02:17 2004 +0000
     5.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.3 @@ -1,58 +0,0 @@
     5.4 -#ifndef VECTOR_MAP_H
     5.5 -#define VECTOR_MAP_H
     5.6 -
     5.7 -#include <vector>
     5.8 -#include <iostream>
     5.9 -
    5.10 -#include "map_base.h"
    5.11 -
    5.12 -namespace hugo {
    5.13 -
    5.14 -	template <typename G, typename K, typename KIt, typename V> 
    5.15 -	class VectorMap : public MapBase<G, K, KIt> {
    5.16 -	public:
    5.17 -		typedef V ValueType;
    5.18 -	
    5.19 -		VectorMap() {}
    5.20 -		VectorMap(typename MapBase<G, K, KIt>::Registry& r) : MapBase<G, K, KIt>(r) {}
    5.21 -	
    5.22 -	
    5.23 -		ValueType& operator[](const K& key) {
    5.24 -			int id = registry->getGraph().id(key);
    5.25 -			return container[id];
    5.26 -		} 
    5.27 -	
    5.28 -		const ValueType& operator[](const K& key) const {
    5.29 -			int id = registry->getGraph().id(key);
    5.30 -			return container[id];
    5.31 -		}
    5.32 -	
    5.33 -		const ValueType& get(const K& key) const {
    5.34 -			int id = registry->getGraph().id(key);
    5.35 -			return container[id];
    5.36 -		} 
    5.37 -	
    5.38 -		void set(const K& key, const ValueType& val) {
    5.39 -			int id = registry->getGraph().id(key);
    5.40 -			container[id] = val;
    5.41 -		}
    5.42 -	
    5.43 -		void add(const K& key) {
    5.44 -			int id = registry->getGraph().id(key);
    5.45 -			std::cerr << id << std::endl;
    5.46 -			if (id >= container.size()) {
    5.47 -				container.resize(id + 1);
    5.48 -			}
    5.49 -		}
    5.50 -	
    5.51 -		void erase(const K& key) {}
    5.52 -
    5.53 -	private:
    5.54 -		typedef std::vector<ValueType> Container;
    5.55 -	
    5.56 -		Container container;
    5.57 -	};
    5.58 -
    5.59 -}
    5.60 -
    5.61 -#endif
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/src/work/deba/vector_map_factory.h	Fri May 07 08:18:30 2004 +0000
     6.3 @@ -0,0 +1,71 @@
     6.4 +#ifndef VECTOR_MAP_H
     6.5 +#define VECTOR_MAP_H
     6.6 +
     6.7 +#include <vector>
     6.8 +#include <iostream>
     6.9 +
    6.10 +#include "map_base.h"
    6.11 +
    6.12 +namespace hugo {
    6.13 +	
    6.14 +	template <typename G, typename K, typename KIt>
    6.15 +	class VectorMapFactory {
    6.16 +	
    6.17 +	
    6.18 +	public:
    6.19 +		
    6.20 +		typedef G Graph;
    6.21 +		typedef K Key;
    6.22 +		typedef KIt KeyIt;
    6.23 +		
    6.24 +		template <typename V> 
    6.25 +		class VectorMap : public MapBase<G, K, KIt> {
    6.26 +		public:
    6.27 +			typedef V ValueType;
    6.28 +	
    6.29 +			VectorMap() {}
    6.30 +			
    6.31 +			VectorMap(Graph& g, MapRegistry<G, K, KIt>& r) 
    6.32 +				: MapBase<G, K, KIt>(g, r) {}
    6.33 +	
    6.34 +	
    6.35 +			ValueType& operator[](const K& key) {
    6.36 +				int id = graph->id(key);
    6.37 +				return container[id];
    6.38 +			} 
    6.39 +		
    6.40 +			const ValueType& operator[](const K& key) const {
    6.41 +				int id = graph->id(key);
    6.42 +				return container[id];
    6.43 +			}
    6.44 +	
    6.45 +			const ValueType& get(const K& key) const {
    6.46 +				int id = graph->id(key);
    6.47 +				return container[id];
    6.48 +			} 
    6.49 +		
    6.50 +			void set(const K& key, const ValueType& val) {
    6.51 +				int id = graph->id(key);
    6.52 +				container[id] = val;
    6.53 +			}
    6.54 +		
    6.55 +			void add(const K& key) {
    6.56 +				int id = graph->id(key);
    6.57 +				std::cerr << id << std::endl;
    6.58 +				if (id >= container.size()) {
    6.59 +					container.resize(id + 1);
    6.60 +				}
    6.61 +			}
    6.62 +		
    6.63 +			void erase(const K& key) {}
    6.64 +	
    6.65 +		private:
    6.66 +			typedef std::vector<ValueType> Container;
    6.67 +		
    6.68 +			Container container;
    6.69 +		};
    6.70 +		
    6.71 +	};
    6.72 +}
    6.73 +
    6.74 +#endif