[Lemon-commits] [lemon_svn] deba: r746 - hugo/trunk/src/work/deba
Lemon SVN
svn at lemon.cs.elte.hu
Mon Nov 6 20:41:17 CET 2006
Author: deba
Date: Fri May 7 10:18:30 2004
New Revision: 746
Added:
hugo/trunk/src/work/deba/vector_map_factory.h
- copied, changed from r508, /hugo/trunk/src/work/deba/vector_map.h
Removed:
hugo/trunk/src/work/deba/vector_map.h
Modified:
hugo/trunk/src/work/deba/main.cpp
hugo/trunk/src/work/deba/map_base.h
hugo/trunk/src/work/deba/map_registry.h
hugo/trunk/src/work/deba/test_graph.h
Log:
Modified: hugo/trunk/src/work/deba/main.cpp
==============================================================================
--- hugo/trunk/src/work/deba/main.cpp (original)
+++ hugo/trunk/src/work/deba/main.cpp Fri May 7 10:18:30 2004
@@ -7,7 +7,7 @@
int main() {
ListGraph g;
- ListGraph::NodeMap<int> map(g);
+ ListGraph::NodeMapFactory::VectorMap<int> map(g, g.node_maps);
ListGraph::Node node = g.addNode();
map[node] = 12;
return 0;
Modified: hugo/trunk/src/work/deba/map_base.h
==============================================================================
--- hugo/trunk/src/work/deba/map_base.h (original)
+++ hugo/trunk/src/work/deba/map_base.h Fri May 7 10:18:30 2004
@@ -39,17 +39,17 @@
Simple constructor to register into a graph registry.
*/
- MapBase(Registry& r) : registry(0) {
- registry->add(*this);
+ MapBase(Graph& g, Registry& r) : graph(&g), registry(0) {
+ registry->attach(*this);
}
/**
Copy constructor with registering into the map.
*/
- MapBase(const MapBase& copy) : registry(0) {
- if (registry) {
- registry->add(*this);
+ MapBase(const MapBase& copy) : registry(0), graph(copy.graph) {
+ if (copy.registry) {
+ copy.registry->attach(*this);
}
}
@@ -59,11 +59,11 @@
const MapBase& operator=(const MapBase& copy) {
if (registry) {
- registry->erase(*this);
+ registry->detach(*this);
}
- registry = copy.registry;
- if (registry) {
- registry->add(*this);
+ graph = copy.graph;
+ if (copy.registry) {
+ copy.registry->attach(*this);
}
}
@@ -74,13 +74,14 @@
virtual ~MapBase() {
if (registry) {
- registry->erase(*this);
+ registry->detach(*this);
}
}
protected:
Registry* registry;
+ Graph* graph;
int registry_index;
@@ -88,9 +89,9 @@
Helper function to implement the default constructor in the subclasses.
*/
- virtual void init(Graph& g) {
+ virtual void init() {
- for (KeyIt it(g); g.valid(it); g.next(it)) {
+ for (KeyIt it(*graph); graph->valid(it); graph->next(it)) {
add(it);
}
}
@@ -99,8 +100,8 @@
Helper function to implement the destructor in the subclasses.
*/
- virtual void destroy(Graph& g) {
- for (KeyIt it(g); g.valid(it); g.next(it)) {
+ virtual void destroy() {
+ for (KeyIt it(*graph); graph->valid(it); graph->next(it)) {
erase(it);
}
}
Modified: hugo/trunk/src/work/deba/map_registry.h
==============================================================================
--- hugo/trunk/src/work/deba/map_registry.h (original)
+++ hugo/trunk/src/work/deba/map_registry.h Fri May 7 10:18:30 2004
@@ -28,43 +28,50 @@
typedef std::vector<Map*> Container;
Container container;
- Graph* graph;
-
public:
- MapRegistry(Graph& g) : container(0), graph(&g) {}
+ MapRegistry() {}
+
+ MapRegistry(const MapRegistry&) {}
+
+ MapRegistry& operator=(const MapRegistry&) {
+ for (it = container.begin(); it != container.end(); ++it) {
+ (*it)->destroy();
+ (*it)->graph = 0;
+ (*it)->registry = 0;
+ }
+ }
~MapRegistry() {
typename Container::iterator it;
for (it = container.begin(); it != container.end(); ++it) {
- (*it)->destroy(*graph);
+ (*it)->destroy();
(*it)->registry = 0;
+ (*it)->graph = 0;
}
}
- private:
- MapRegistry(const MapRegistry& ) {}
- MapRegistry& operator=(const MapRegistry& ) {}
public:
- void add(Map& map) {
+ void attach(Map& map) {
if (map.registry) {
- map.registry->erase(map);
+ map.registry->detach(map);
}
container.push_back(&map);
map.registry = this;
map.registry_index = container.size()-1;
- map.init(*graph);
+ map.init();
}
- void erase(Map& map_base) {
- map_base.destroy(*graph);
+ void detach(Map& map_base) {
+ map_base.destroy();
container.back()->registry_index = map_base.registry_index;
container[map_base.registry_index] = container.back();
container.pop_back();
map_base.registry = 0;
+ map_base.graph = 0;
}
@@ -82,11 +89,6 @@
}
}
- Graph& getGraph() {
- return *graph;
- }
-
-
};
}
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 May 7 10:18:30 2004
@@ -7,7 +7,7 @@
#include "invalid.h"
-#include "vector_map.h"
+#include "vector_map_factory.h"
namespace hugo {
@@ -38,27 +38,19 @@
private:
+
+ public:
+
typedef MapRegistry<ListGraph, Node, NodeIt> NodeMapRegistry;
NodeMapRegistry node_maps;
+
typedef MapRegistry<ListGraph, Edge, EdgeIt> EdgeMapRegistry;
EdgeMapRegistry edge_maps;
-
- public:
-
-
- template <typename T>
- class NodeMap : public VectorMap<ListGraph, Node, NodeIt, T> {
- public:
- NodeMap(ListGraph& g) : VectorMap<ListGraph, Node, NodeIt, T>(g.node_maps) {}
- };
-
- template <typename T>
- class EdgeMap : public VectorMap<ListGraph, Edge, EdgeIt, T> {
- public:
- EdgeMap(ListGraph& g) : VectorMap<ListGraph, Edge, EdgeIt, T>(g.edge_maps) {}
- };
+ typedef VectorMapFactory<ListGraph, Edge, EdgeIt> EdgeMapFactory;
+ typedef VectorMapFactory<ListGraph, Node, NodeIt> NodeMapFactory;
+
int node_id;
int edge_id;
@@ -215,8 +207,7 @@
/* default constructor */
- ListGraph() : node_id(0), edge_id(0), _node_num(0), _edge_num(0), _first_node(0), _last_node(0),
- edge_maps(*this), node_maps(*this) { }
+ ListGraph() : node_id(0), edge_id(0), _node_num(0), _edge_num(0), _first_node(0), _last_node(0){ }
~ListGraph() {
while (first<NodeIt>().valid()) erase(first<NodeIt>());
Copied: hugo/trunk/src/work/deba/vector_map_factory.h (from r508, /hugo/trunk/src/work/deba/vector_map.h)
==============================================================================
--- /hugo/trunk/src/work/deba/vector_map.h (original)
+++ hugo/trunk/src/work/deba/vector_map_factory.h Fri May 7 10:18:30 2004
@@ -7,52 +7,65 @@
#include "map_base.h"
namespace hugo {
-
- template <typename G, typename K, typename KIt, typename V>
- class VectorMap : public MapBase<G, K, KIt> {
- public:
- typedef V ValueType;
- VectorMap() {}
- VectorMap(typename MapBase<G, K, KIt>::Registry& r) : MapBase<G, K, KIt>(r) {}
+ template <typename G, typename K, typename KIt>
+ class VectorMapFactory {
- ValueType& operator[](const K& key) {
- int id = registry->getGraph().id(key);
- return container[id];
- }
-
- const ValueType& operator[](const K& key) const {
- int id = registry->getGraph().id(key);
- return container[id];
- }
-
- const ValueType& get(const K& key) const {
- int id = registry->getGraph().id(key);
- return container[id];
- }
-
- void set(const K& key, const ValueType& val) {
- int id = registry->getGraph().id(key);
- container[id] = val;
- }
-
- void add(const K& key) {
- int id = registry->getGraph().id(key);
- std::cerr << id << std::endl;
- if (id >= container.size()) {
- container.resize(id + 1);
+ public:
+
+ typedef G Graph;
+ typedef K Key;
+ typedef KIt KeyIt;
+
+ template <typename V>
+ class VectorMap : public MapBase<G, K, KIt> {
+ public:
+ typedef V ValueType;
+
+ VectorMap() {}
+
+ VectorMap(Graph& g, MapRegistry<G, K, KIt>& r)
+ : MapBase<G, K, KIt>(g, r) {}
+
+
+ ValueType& operator[](const K& key) {
+ int id = graph->id(key);
+ return container[id];
+ }
+
+ const ValueType& operator[](const K& key) const {
+ int id = graph->id(key);
+ return container[id];
}
- }
- void erase(const K& key) {}
-
- private:
- typedef std::vector<ValueType> Container;
+ const ValueType& get(const K& key) const {
+ int id = graph->id(key);
+ return container[id];
+ }
+
+ void set(const K& key, const ValueType& val) {
+ int id = graph->id(key);
+ container[id] = val;
+ }
+
+ void add(const K& key) {
+ int id = graph->id(key);
+ std::cerr << id << std::endl;
+ if (id >= container.size()) {
+ container.resize(id + 1);
+ }
+ }
+
+ void erase(const K& key) {}
- Container container;
+ private:
+ typedef std::vector<ValueType> Container;
+
+ Container container;
+ };
+
};
-
}
#endif
More information about the Lemon-commits
mailing list