(none)
authordeba
Mon, 10 May 2004 13:49:35 +0000
changeset 595e10b5e9419ef
parent 594 23a608ba40ab
child 596 c43e7d0f075b
(none)
src/work/deba/main.cpp
src/work/deba/map_base.h
src/work/deba/map_registry.h
src/work/deba/pac_map_factory.h
src/work/deba/test_graph.h
src/work/deba/vector_map_factory.h
     1.1 --- a/src/work/deba/main.cpp	Mon May 10 09:12:28 2004 +0000
     1.2 +++ b/src/work/deba/main.cpp	Mon May 10 13:49:35 2004 +0000
     1.3 @@ -1,4 +1,5 @@
     1.4  #include <iostream>
     1.5 +#include <cstdlib>
     1.6  #include "test_graph.h"
     1.7  
     1.8  using namespace std;
     1.9 @@ -7,9 +8,17 @@
    1.10  
    1.11  int main() {
    1.12  	ListGraph g;
    1.13 -	ListGraph::NodeMapFactory::VectorMap<int> map(g, g.node_maps);
    1.14 -	ListGraph::Node node = g.addNode();
    1.15 -	map[node] = 12;	
    1.16 +	for (int i = 0; i < 3; ++i) {
    1.17 +		ListGraph::Node node = g.addNode();
    1.18 +	}
    1.19 +	ListGraph::NodeMapFactory::Map<int> map(g, g.node_maps);
    1.20 +	for (int i = 0; i < 10; ++i) {
    1.21 +		ListGraph::Node node = g.addNode();
    1.22 +		map[node] = rand()%100;
    1.23 +	}
    1.24 +	for (ListGraph::NodeIt it(g); g.valid(it); g.next(it)) {
    1.25 +		cout << map[it] << endl;
    1.26 +	}
    1.27  	return 0;
    1.28  }
    1.29  
     2.1 --- a/src/work/deba/map_base.h	Mon May 10 09:12:28 2004 +0000
     2.2 +++ b/src/work/deba/map_base.h	Mon May 10 13:49:35 2004 +0000
     2.3 @@ -1,12 +1,14 @@
     2.4  #ifndef MAP_BASE_H
     2.5  #define MAP_BASE_H
     2.6  
     2.7 +using namespace std;
     2.8 +
     2.9  /**
    2.10 -	Template base class for implementing mapping on nodes.
    2.11 -	\param The first template parameter is the Graph class. The Graph
    2.12 -		must have an \emp node_maps member with \emp MapRegistry class.
    2.13 -	\param The second template parameter is the  type of the class.
    2.14 -	
    2.15 +	Template base class for implementing mapping on nodes and edges.
    2.16 +	\param The first template parameter is the Graph class.
    2.17 +	\param The second template parameter is the key type.
    2.18 +	\param The third template parameter is an iterator on
    2.19 +		the keys.
    2.20  */
    2.21  
    2.22  
    2.23 @@ -24,7 +26,7 @@
    2.24  	public:
    2.25  		typedef G Graph;
    2.26  		typedef MapRegistry<G, K, KIt> Registry;
    2.27 -		typedef K KeyType;
    2.28 +		typedef K Key;
    2.29  		typedef KIt KeyIt;
    2.30  	
    2.31  		friend class Registry;
    2.32 @@ -33,14 +35,14 @@
    2.33  			Default constructor.
    2.34  		*/	
    2.35  		
    2.36 -		MapBase() : registry(0) {}
    2.37 +		MapBase() : graph(0), registry(0) {}
    2.38  
    2.39  		/** 
    2.40  			Simple constructor to register into a graph registry.
    2.41  		*/
    2.42  	
    2.43  		MapBase(Graph& g, Registry& r) : graph(&g), registry(0) {
    2.44 -			registry->attach(*this);
    2.45 +			r.attach(*this);
    2.46  		}
    2.47  
    2.48  		/** 
    2.49 @@ -80,17 +82,16 @@
    2.50  	
    2.51  	protected:
    2.52  		
    2.53 +		Graph* graph;
    2.54  		Registry* registry;
    2.55 -		Graph* graph;
    2.56  
    2.57  		int registry_index;
    2.58  	
    2.59  		/**
    2.60 -			Helper function to implement the default constructor in the subclasses.
    2.61 +			Helper function to implement constructors in the subclasses.
    2.62  		*/
    2.63  	
    2.64  		virtual void init() {
    2.65 -
    2.66  			for (KeyIt it(*graph); graph->valid(it); graph->next(it)) {
    2.67  				add(it);
    2.68  			}
    2.69 @@ -111,14 +112,13 @@
    2.70  			\e Add extends the map with the new node.
    2.71  		*/
    2.72  	
    2.73 -		virtual void add(const KeyType&) = 0;
    2.74 -	
    2.75 +		virtual void add(const Key&) = 0;	
    2.76  		/** 
    2.77  			The erase member function should be overloaded in the subclasses.
    2.78  			\e Erase removes the node from the map.
    2.79  		*/
    2.80  	
    2.81 -		virtual void erase(const KeyType&) = 0;
    2.82 +		virtual void erase(const Key&) = 0;
    2.83  	
    2.84  		/**
    2.85  			Exception class to throw at unsupported operation.
     3.1 --- a/src/work/deba/map_registry.h	Mon May 10 09:12:28 2004 +0000
     3.2 +++ b/src/work/deba/map_registry.h	Mon May 10 13:49:35 2004 +0000
     3.3 @@ -3,6 +3,8 @@
     3.4  
     3.5  #include <vector>
     3.6  
     3.7 +using namespace std;
     3.8 +
     3.9  
    3.10  namespace hugo {
    3.11  	template <typename G, typename K, typename KIt>
    3.12 @@ -62,27 +64,25 @@
    3.13  			container.push_back(&map);
    3.14  			map.registry = this;
    3.15  			map.registry_index = container.size()-1;
    3.16 -			map.init();
    3.17  		} 
    3.18  	
    3.19 -		void detach(Map& map_base) {
    3.20 -			map_base.destroy();
    3.21 -			container.back()->registry_index = map_base.registry_index; 
    3.22 -			container[map_base.registry_index] = container.back();
    3.23 +		void detach(Map& map) {
    3.24 +			container.back()->registry_index = map.registry_index; 
    3.25 +			container[map.registry_index] = container.back();
    3.26  			container.pop_back();
    3.27 -			map_base.registry = 0;
    3.28 -			map_base.graph = 0;
    3.29 +			map.registry = 0;
    3.30 +			map.graph = 0;
    3.31  		}
    3.32  	
    3.33  		
    3.34 -		void add(Key& key) {
    3.35 +		virtual void add(Key& key) {
    3.36  			typename Container::iterator it;
    3.37  			for (it = container.begin(); it != container.end(); ++it) {
    3.38  				(*it)->add(key);
    3.39  			}
    3.40  		}	
    3.41  		
    3.42 -		void erase(Key& key) {
    3.43 +		virtual void erase(Key& key) {
    3.44  			typename Container::iterator it;
    3.45  			for (it = container.begin(); it != container.end(); ++it) {
    3.46  				(*it)->erase(key);
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/src/work/deba/pac_map_factory.h	Mon May 10 13:49:35 2004 +0000
     4.3 @@ -0,0 +1,73 @@
     4.4 +#ifndef PAC_MAP_FACTORY_H
     4.5 +#define PAC_MAP_FACTORY_H
     4.6 +
     4.7 +#include "map_base.h"
     4.8 +
     4.9 +/**
    4.10 +	Converter class to use the standard template
    4.11 +	libary's pair associative containers as a graph map.
    4.12 +*/
    4.13 +
    4.14 +namespace hugo {
    4.15 +	
    4.16 +	template <typename G, typename K, typename KIt, template <typename, typename> class PAC>
    4.17 +	class PacMapFactory {
    4.18 +	
    4.19 +	
    4.20 +	public:
    4.21 +		
    4.22 +		typedef G Graph;
    4.23 +		typedef K Key;
    4.24 +		typedef KIt KeyIt;
    4.25 +		
    4.26 +		template <typename V> 
    4.27 +		class Map : public MapBase<G, K, KIt> {
    4.28 +		public:
    4.29 +			typedef V Value;
    4.30 +	
    4.31 +			Map() {}
    4.32 +			
    4.33 +			Map(Graph& g, MapRegistry<G, K, KIt>& r) 
    4.34 +				: MapBase<G, K, KIt>(g, r) {
    4.35 +				init();
    4.36 +			}
    4.37 +				
    4.38 +			virtual ~Map() {
    4.39 +				destroy();
    4.40 +			}
    4.41 +	
    4.42 +	
    4.43 +			V& operator[](const K& key) {
    4.44 +				return container.find(key)->second;
    4.45 +			} 
    4.46 +		
    4.47 +			const V& operator[](const K& key) const {
    4.48 +				return container.find(key)->second;
    4.49 +			}
    4.50 +	
    4.51 +			const V& get(const K& key) const {
    4.52 +				return container.find(key)->second;
    4.53 +			} 
    4.54 +		
    4.55 +			void set(const K& key, const V& value) {
    4.56 +				container.find(key)->second = value;
    4.57 +			}
    4.58 +		
    4.59 +			void add(const K& key) {
    4.60 +				container.insert(key);
    4.61 +			}
    4.62 +		
    4.63 +			void erase(const K& key) {
    4.64 +				container.erase(key);
    4.65 +			}
    4.66 +	
    4.67 +		private:
    4.68 +			typedef PAC<K, V> Container;
    4.69 +		
    4.70 +			Container container;
    4.71 +		};
    4.72 +		
    4.73 +	};
    4.74 +}
    4.75 +
    4.76 +#endif
    4.77 \ No newline at end of file
     5.1 --- a/src/work/deba/test_graph.h	Mon May 10 09:12:28 2004 +0000
     5.2 +++ b/src/work/deba/test_graph.h	Mon May 10 13:49:35 2004 +0000
     5.3 @@ -41,15 +41,17 @@
     5.4   
     5.5  	public:
     5.6  	
     5.7 +		typedef MapBase<ListGraph, Node, NodeIt> NodeMapBase;
     5.8  		typedef MapRegistry<ListGraph, Node, NodeIt> NodeMapRegistry;
     5.9 +		typedef VectorMapFactory<ListGraph, Node, NodeIt> NodeMapFactory;
    5.10  		NodeMapRegistry node_maps;
    5.11 -		
    5.12 -		
    5.13 +
    5.14 +
    5.15 +
    5.16 +		typedef MapBase<ListGraph, Edge, EdgeIt> EdgeMapBase;
    5.17  		typedef MapRegistry<ListGraph, Edge, EdgeIt> EdgeMapRegistry;
    5.18 +		typedef VectorMapFactory<ListGraph, Edge, EdgeIt> EdgeMapFactory;
    5.19  		EdgeMapRegistry edge_maps;
    5.20 -
    5.21 -		typedef VectorMapFactory<ListGraph, Edge, EdgeIt> EdgeMapFactory;
    5.22 -		typedef VectorMapFactory<ListGraph, Node, NodeIt> NodeMapFactory;
    5.23   
    5.24  
    5.25      int node_id;
     6.1 --- a/src/work/deba/vector_map_factory.h	Mon May 10 09:12:28 2004 +0000
     6.2 +++ b/src/work/deba/vector_map_factory.h	Mon May 10 13:49:35 2004 +0000
     6.3 @@ -2,7 +2,6 @@
     6.4  #define VECTOR_MAP_H
     6.5  
     6.6  #include <vector>
     6.7 -#include <iostream>
     6.8  
     6.9  #include "map_base.h"
    6.10  
    6.11 @@ -19,39 +18,44 @@
    6.12  		typedef KIt KeyIt;
    6.13  		
    6.14  		template <typename V> 
    6.15 -		class VectorMap : public MapBase<G, K, KIt> {
    6.16 +		class Map : public MapBase<G, K, KIt> {
    6.17  		public:
    6.18 -			typedef V ValueType;
    6.19 +			typedef V Value;
    6.20  	
    6.21 -			VectorMap() {}
    6.22 +			Map() {}
    6.23  			
    6.24 -			VectorMap(Graph& g, MapRegistry<G, K, KIt>& r) 
    6.25 -				: MapBase<G, K, KIt>(g, r) {}
    6.26 +			Map(Graph& g, MapRegistry<G, K, KIt>& r) 
    6.27 +				: MapBase<G, K, KIt>(g, r) {
    6.28 +				init();
    6.29 +			}
    6.30 +				
    6.31 +			virtual ~Map() {
    6.32 +				destroy();
    6.33 +			}
    6.34  	
    6.35  	
    6.36 -			ValueType& operator[](const K& key) {
    6.37 +			Value& operator[](const K& key) {
    6.38  				int id = graph->id(key);
    6.39  				return container[id];
    6.40  			} 
    6.41  		
    6.42 -			const ValueType& operator[](const K& key) const {
    6.43 +			const Value& operator[](const K& key) const {
    6.44  				int id = graph->id(key);
    6.45  				return container[id];
    6.46  			}
    6.47  	
    6.48 -			const ValueType& get(const K& key) const {
    6.49 +			const Value& get(const K& key) const {
    6.50  				int id = graph->id(key);
    6.51  				return container[id];
    6.52  			} 
    6.53  		
    6.54 -			void set(const K& key, const ValueType& val) {
    6.55 +			void set(const K& key, const Value& val) {
    6.56  				int id = graph->id(key);
    6.57  				container[id] = val;
    6.58  			}
    6.59  		
    6.60  			void add(const K& key) {
    6.61  				int id = graph->id(key);
    6.62 -				std::cerr << id << std::endl;
    6.63  				if (id >= container.size()) {
    6.64  					container.resize(id + 1);
    6.65  				}
    6.66 @@ -60,7 +64,7 @@
    6.67  			void erase(const K& key) {}
    6.68  	
    6.69  		private:
    6.70 -			typedef std::vector<ValueType> Container;
    6.71 +			typedef std::vector<Value> Container;
    6.72  		
    6.73  			Container container;
    6.74  		};