[Lemon-commits] [lemon_svn] deba: r911 - hugo/trunk/src/work/deba

Lemon SVN svn at lemon.cs.elte.hu
Mon Nov 6 20:42:10 CET 2006


Author: deba
Date: Fri Jun  4 13:52:53 2004
New Revision: 911

Added:
   hugo/trunk/src/work/deba/map_defines.h
Modified:
   hugo/trunk/src/work/deba/array_map_factory.h
   hugo/trunk/src/work/deba/main.cpp
   hugo/trunk/src/work/deba/test_graph.h

Log:


Modified: hugo/trunk/src/work/deba/array_map_factory.h
==============================================================================
--- hugo/trunk/src/work/deba/array_map_factory.h	(original)
+++ hugo/trunk/src/work/deba/array_map_factory.h	Fri Jun  4 13:52:53 2004
@@ -3,121 +3,125 @@
 
 #include <memory>
 
-#include "map_base.h"
 
 #include <iostream>
 using namespace std;
 
 namespace hugo {
 	
-	template <typename G, typename K, typename KIt>
-	class ArrayMapFactory {
-	
-	
-	public:
+  template <typename MapRegistry> class ArrayMapFactory {
 		
-		typedef G Graph;
-		typedef K Key;
-		typedef KIt KeyIt;
-		
-		template <typename V, typename A = allocator<V> > 
-		class Map : public MapBase<G, K, KIt> {
-		public:
-			typedef V Value;
-			typedef typename _Alloc_traits<V, A>::_Alloc_type _Alloc_type;
+  public:
+		
+    typedef typename MapRegistry::Graph Graph;
+    typedef typename MapRegistry::Key Key;
+    typedef typename MapRegistry::KeyIt KeyIt;
+
+    typedef typename MapRegistry::MapBase MapBase;
+		
+    template <typename V, typename A = std::allocator<V> > 
+      class Map : public MapBase {
+    
+	public:
+
+	typedef V Value;
+	typedef A Allocator;
 
 	
-			Map() : values(0), capacity(0) {}
+	Map() : values(0), capacity(0) {}
 			
-			Map(Graph& g, MapRegistry<G, K, KIt>& r) 
-				: MapBase<G, K, KIt>(g, r) {
-				int max_id = -1;
-				for (KeyIt it(*graph); graph->valid(it); graph->next(it)) {
-					int id = graph->id(it);
-					if (id > max_id) {
-						max_id = id;
-					}				
-				}
-				if (max_id == -1) {
-					capacity = 0;
-					values = 0;
-					return;
-				}
-				int capacity = 1;
-				while (capacity <= max_id) {
-					capacity <<= 1;
-				}
-				Value* values = reinterpret_cast<Value*>(new char[capacity*sizeof(Value)]);
-				for (KeyIt it(*graph); graph->valid(it); graph->next(it)) {
-					int id = graph->id(it);
-					new(&(values[id])) Value();
-				}								
-				cerr << capacity << endl;	
-			}
+	Map(Graph& g, MapRegistry& r) : MapBase(g, r) {
+	  int max_id = -1;
+	  for (KeyIt it(*graph); graph->valid(it); graph->next(it)) {
+	    int id = graph->id(it);
+	    if (id > max_id) {
+	      max_id = id;
+	    }			
+	  }
+	  if (max_id == -1) {
+	    capacity = 0;
+	    values = 0;
+	    return;
+	  }
+	  capacity = 1;
+	  while (capacity <= max_id) {
+	    capacity <<= 1;
+	  }
+	  values = allocator.allocate(capacity);
+	  for (KeyIt it(*graph); graph->valid(it); graph->next(it)) {
+	    int id = graph->id(it);
+	    allocator.construct(&(values[id]), Value());
+	  }								
+	}
+
+	Map(const Map& copy) : MapBase(*copy.graph, *copy.registry) {
+	  capacity = copy.capacity;
+	  values = allocator.allocate(capacity);
+	  for (KeyIt it(*graph); graph->valid(it); graph->next(it)) {
+	    int id = graph->id(it);
+	    allocator.construct(&(values[id]), copy.values[id]);
+	  }
+	}
 				
-			virtual ~Map() {
-				destroy();
-				delete[] reinterpret_cast<char*>(values);
-				values = 0;
-				capacity = 0;
-			}
-	
-	
-			Value& operator[](const K& key) {
-				int id = graph->id(key);
-				return values[id];
-			} 
-		
-			const Value& operator[](const K& key) const {
-				int id = graph->id(key);
-				return values[id];
-			}
-	
-			const Value& get(const K& key) const {
-				int id = graph->id(key);
-				return values[id];
-			} 
-		
-			void set(const K& key, const Value& val) {
-				int id = graph->id(key);
-				values[id] = val;
-			}
-		
-			void add(const K& key) {
-				cerr << capacity << endl;
-				int id = graph->id(key);
-				if (id >= capacity) {
-					int new_capacity = (capacity == 0 ? 1 : capacity);
-					while (new_capacity <= id) {
-						new_capacity <<= 1;
-					}
-					Value* new_values = reinterpret_cast<Value*>(new char[new_capacity*sizeof(Value)]);;
-					for (KeyIt it(*graph); graph->valid(it); graph->next(it)) {
-						int jd = graph->id(it);
-						if (id != jd) {
-							new(&(new_values[jd])) Value(values[jd]);
-						}
-					}
-					if (capacity != 0) delete[] reinterpret_cast<char *>(values);
-					values = new_values;
-					capacity = new_capacity;
-				}
-				cerr << id << ' ' << capacity << endl;
-				new(&(values[id])) Value();
-			}
-		
-			void erase(const K& key) {
-				int id = graph->id(key);
-				values[id].~Value();
-			}
-	
-		private:
-			int capacity;
-			Value* values;
-								
-		};
-		
-	};
+	virtual ~Map() {
+	  destroy();
+	  allocator.deallocate(values, capacity);
+	}
+	
+	
+	Value& operator[](const Key& key) {
+	  int id = graph->id(key);
+	  return values[id];
+	} 
+		
+	const Value& operator[](const Key& key) const {
+	  int id = graph->id(key);
+	  return values[id];
+	}
+	
+	const Value& get(const Key& key) const {
+	  int id = graph->id(key);
+	  return values[id];
+	} 
+		
+	void set(const Key& key, const Value& val) {
+	  int id = graph->id(key);
+	  values[id] = val;
+	}
+		
+	void add(const Key& key) {
+	  int id = graph->id(key);
+	  if (id >= capacity) {
+	    int new_capacity = (capacity == 0 ? 1 : capacity);
+	    while (new_capacity <= id) {
+	      new_capacity <<= 1;
+	    }
+	    Value* new_values = allocator.allocate(new_capacity);;
+	    for (KeyIt it(*graph); graph->valid(it); graph->next(it)) {
+	      int jd = graph->id(it);
+	      if (id != jd) {
+		allocator.construct(&(new_values[jd]), values[jd]);
+		allocator.destroy(&(values[jd]));
+	      }
+	    }
+	    if (capacity != 0) allocator.deallocate(values, capacity);
+	    values = new_values;
+	    capacity = new_capacity;
+	  }
+	  allocator.construct(&(values[id]), Value());
+	}
+		
+	void erase(const Key& key) {
+	  int id = graph->id(key);
+	  allocator.destroy(&(values[id]));
+	}
+	
+	private:
+	int capacity;
+	Value* values;
+	Allocator allocator;
+      };		
+  };
 }
 
 #endif

Modified: hugo/trunk/src/work/deba/main.cpp
==============================================================================
--- hugo/trunk/src/work/deba/main.cpp	(original)
+++ hugo/trunk/src/work/deba/main.cpp	Fri Jun  4 13:52:53 2004
@@ -11,7 +11,7 @@
   for (int i = 0; i < 10; ++i) {
     ListGraph::Node node = g.addNode();
   }
-  ListGraph::NodeMapFactory::Map<int> map(g, g.node_maps);
+  ListGraph::NodeMap<int> map(g);
   for (int i = 0; i < 10; ++i) {
     ListGraph::Node node = g.addNode();
     map[node] = rand()%100;

Added: hugo/trunk/src/work/deba/map_defines.h
==============================================================================
--- (empty file)
+++ hugo/trunk/src/work/deba/map_defines.h	Fri Jun  4 13:52:53 2004
@@ -0,0 +1,47 @@
+#ifndef MAP_DEFINES_H
+#define MAP_DEFINES_H
+
+#define CREATE_EDGE_MAP_REGISTRY \
+typedef MapRegistry<Graph, Edge, EdgeIt> EdgeMapRegistry; \
+EdgeMapRegistry edge_maps;
+
+#define CREATE_NODE_MAP_REGISTRY \
+typedef MapRegistry<Graph, Node, NodeIt> NodeMapRegistry; \
+NodeMapRegistry node_maps;
+
+#define CREATE_MAP_REGISTRIES \
+CREATE_NODE_MAP_REGISTRY \
+CREATE_EDGE_MAP_REGISTRY
+
+#define CREATE_NODE_MAP_FACTORY(TemplateFactory) \
+typedef TemplateFactory<NodeMapRegistry> NodeMapFactory;
+
+#define CREATE_EDGE_MAP_FACTORY(TemplateFactory) \
+typedef TemplateFactory<EdgeMapRegistry> EdgeMapFactory;
+
+#define CREATE_MAP_FACTORIES(TemplateFactory) \
+CREATE_NODE_MAP_FACTORY(TemplateFactory) \
+CREATE_EDGE_MAP_FACTORY(TemplateFactory) 
+
+#define IMPORT_NODE_MAP(Factory) \
+template <typename V> \
+class NodeMap : public Factory::Map<V> { \
+public: \
+NodeMap() {} \
+NodeMap(Graph& g) : Factory::Map<V>(g, g.node_maps) {} \
+};
+
+#define IMPORT_EDGE_MAP(Factory) \
+template <typename V> \
+class EdgeMap : public Factory::Map<V> { \
+public: \
+EdgeMap() {} \
+EdgeMap(Graph& g) : Factory::Map<V>(g, g.edge_maps) {} \
+};
+
+#define CREATE_MAPS(TemplateFactory) \
+CREATE_MAP_FACTORIES(TemplateFactory) \
+IMPORT_NODE_MAP(NodeMapFactory) \
+IMPORT_EDGE_MAP(EdgeMapFactory)
+
+#endif

Modified: hugo/trunk/src/work/deba/test_graph.h
==============================================================================
--- hugo/trunk/src/work/deba/test_graph.h	(original)
+++ hugo/trunk/src/work/deba/test_graph.h	Fri Jun  4 13:52:53 2004
@@ -7,7 +7,10 @@
 
 #include "invalid.h"
 
-#include "vector_map_factory.h"
+#include "map_registry.h"
+#include "array_map_factory.h"
+
+#include "map_defines.h"
 
 namespace hugo {
 
@@ -30,27 +33,12 @@
     class InEdgeIt;
     class SymEdgeIt;
     
-    //    template <typename T> class NodeMap;
-    //    template <typename T> class EdgeMap;
-  private:
-    //    template <typename T> friend class NodeMap;
-    //   template <typename T> friend class EdgeMap;
- 
-  private:
-
+    typedef ListGraph Graph;
  
-  public:
-	
-    typedef MapRegistry<ListGraph, Node, NodeIt> NodeMapRegistry;
-    typedef VectorMapFactory<NodeMapRegistry> NodeMapFactory;
-    NodeMapRegistry node_maps;
-
-
+    CREATE_MAP_REGISTRIES
+    CREATE_MAPS(ArrayMapFactory)
 
-    typedef MapRegistry<ListGraph, Edge, EdgeIt> EdgeMapRegistry;
-    typedef VectorMapFactory<EdgeMapRegistry> EdgeMapFactory;
-    EdgeMapRegistry edge_maps;
- 
+  private:
 
     int node_id;
     int edge_id;



More information about the Lemon-commits mailing list