[Lemon-commits] [lemon_svn] deba: r775 - hugo/trunk/src/work/deba
Lemon SVN
svn at lemon.cs.elte.hu
Mon Nov 6 20:41:26 CET 2006
Author: deba
Date: Mon May 10 15:49:35 2004
New Revision: 775
Added:
hugo/trunk/src/work/deba/pac_map_factory.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
hugo/trunk/src/work/deba/vector_map_factory.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 Mon May 10 15:49:35 2004
@@ -1,4 +1,5 @@
#include <iostream>
+#include <cstdlib>
#include "test_graph.h"
using namespace std;
@@ -7,9 +8,17 @@
int main() {
ListGraph g;
- ListGraph::NodeMapFactory::VectorMap<int> map(g, g.node_maps);
- ListGraph::Node node = g.addNode();
- map[node] = 12;
+ for (int i = 0; i < 3; ++i) {
+ ListGraph::Node node = g.addNode();
+ }
+ ListGraph::NodeMapFactory::Map<int> map(g, g.node_maps);
+ for (int i = 0; i < 10; ++i) {
+ ListGraph::Node node = g.addNode();
+ map[node] = rand()%100;
+ }
+ for (ListGraph::NodeIt it(g); g.valid(it); g.next(it)) {
+ cout << map[it] << endl;
+ }
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 Mon May 10 15:49:35 2004
@@ -1,12 +1,14 @@
#ifndef MAP_BASE_H
#define MAP_BASE_H
+using namespace std;
+
/**
- Template base class for implementing mapping on nodes.
- \param The first template parameter is the Graph class. The Graph
- must have an \emp node_maps member with \emp MapRegistry class.
- \param The second template parameter is the type of the class.
-
+ Template base class for implementing mapping on nodes and edges.
+ \param The first template parameter is the Graph class.
+ \param The second template parameter is the key type.
+ \param The third template parameter is an iterator on
+ the keys.
*/
@@ -24,7 +26,7 @@
public:
typedef G Graph;
typedef MapRegistry<G, K, KIt> Registry;
- typedef K KeyType;
+ typedef K Key;
typedef KIt KeyIt;
friend class Registry;
@@ -33,14 +35,14 @@
Default constructor.
*/
- MapBase() : registry(0) {}
+ MapBase() : graph(0), registry(0) {}
/**
Simple constructor to register into a graph registry.
*/
MapBase(Graph& g, Registry& r) : graph(&g), registry(0) {
- registry->attach(*this);
+ r.attach(*this);
}
/**
@@ -80,17 +82,16 @@
protected:
- Registry* registry;
Graph* graph;
+ Registry* registry;
int registry_index;
/**
- Helper function to implement the default constructor in the subclasses.
+ Helper function to implement constructors in the subclasses.
*/
virtual void init() {
-
for (KeyIt it(*graph); graph->valid(it); graph->next(it)) {
add(it);
}
@@ -111,14 +112,13 @@
\e Add extends the map with the new node.
*/
- virtual void add(const KeyType&) = 0;
-
+ virtual void add(const Key&) = 0;
/**
The erase member function should be overloaded in the subclasses.
\e Erase removes the node from the map.
*/
- virtual void erase(const KeyType&) = 0;
+ virtual void erase(const Key&) = 0;
/**
Exception class to throw at unsupported operation.
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 Mon May 10 15:49:35 2004
@@ -3,6 +3,8 @@
#include <vector>
+using namespace std;
+
namespace hugo {
template <typename G, typename K, typename KIt>
@@ -62,27 +64,25 @@
container.push_back(&map);
map.registry = this;
map.registry_index = container.size()-1;
- map.init();
}
- void detach(Map& map_base) {
- map_base.destroy();
- container.back()->registry_index = map_base.registry_index;
- container[map_base.registry_index] = container.back();
+ void detach(Map& map) {
+ container.back()->registry_index = map.registry_index;
+ container[map.registry_index] = container.back();
container.pop_back();
- map_base.registry = 0;
- map_base.graph = 0;
+ map.registry = 0;
+ map.graph = 0;
}
- void add(Key& key) {
+ virtual void add(Key& key) {
typename Container::iterator it;
for (it = container.begin(); it != container.end(); ++it) {
(*it)->add(key);
}
}
- void erase(Key& key) {
+ virtual void erase(Key& key) {
typename Container::iterator it;
for (it = container.begin(); it != container.end(); ++it) {
(*it)->erase(key);
Added: hugo/trunk/src/work/deba/pac_map_factory.h
==============================================================================
--- (empty file)
+++ hugo/trunk/src/work/deba/pac_map_factory.h Mon May 10 15:49:35 2004
@@ -0,0 +1,73 @@
+#ifndef PAC_MAP_FACTORY_H
+#define PAC_MAP_FACTORY_H
+
+#include "map_base.h"
+
+/**
+ Converter class to use the standard template
+ libary's pair associative containers as a graph map.
+*/
+
+namespace hugo {
+
+ template <typename G, typename K, typename KIt, template <typename, typename> class PAC>
+ class PacMapFactory {
+
+
+ public:
+
+ typedef G Graph;
+ typedef K Key;
+ typedef KIt KeyIt;
+
+ template <typename V>
+ class Map : public MapBase<G, K, KIt> {
+ public:
+ typedef V Value;
+
+ Map() {}
+
+ Map(Graph& g, MapRegistry<G, K, KIt>& r)
+ : MapBase<G, K, KIt>(g, r) {
+ init();
+ }
+
+ virtual ~Map() {
+ destroy();
+ }
+
+
+ V& operator[](const K& key) {
+ return container.find(key)->second;
+ }
+
+ const V& operator[](const K& key) const {
+ return container.find(key)->second;
+ }
+
+ const V& get(const K& key) const {
+ return container.find(key)->second;
+ }
+
+ void set(const K& key, const V& value) {
+ container.find(key)->second = value;
+ }
+
+ void add(const K& key) {
+ container.insert(key);
+ }
+
+ void erase(const K& key) {
+ container.erase(key);
+ }
+
+ private:
+ typedef PAC<K, V> Container;
+
+ Container container;
+ };
+
+ };
+}
+
+#endif
\ No newline at end of file
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 Mon May 10 15:49:35 2004
@@ -41,15 +41,17 @@
public:
+ typedef MapBase<ListGraph, Node, NodeIt> NodeMapBase;
typedef MapRegistry<ListGraph, Node, NodeIt> NodeMapRegistry;
+ typedef VectorMapFactory<ListGraph, Node, NodeIt> NodeMapFactory;
NodeMapRegistry node_maps;
-
-
- typedef MapRegistry<ListGraph, Edge, EdgeIt> EdgeMapRegistry;
- EdgeMapRegistry edge_maps;
+
+
+ typedef MapBase<ListGraph, Edge, EdgeIt> EdgeMapBase;
+ typedef MapRegistry<ListGraph, Edge, EdgeIt> EdgeMapRegistry;
typedef VectorMapFactory<ListGraph, Edge, EdgeIt> EdgeMapFactory;
- typedef VectorMapFactory<ListGraph, Node, NodeIt> NodeMapFactory;
+ EdgeMapRegistry edge_maps;
int node_id;
Modified: hugo/trunk/src/work/deba/vector_map_factory.h
==============================================================================
--- hugo/trunk/src/work/deba/vector_map_factory.h (original)
+++ hugo/trunk/src/work/deba/vector_map_factory.h Mon May 10 15:49:35 2004
@@ -2,7 +2,6 @@
#define VECTOR_MAP_H
#include <vector>
-#include <iostream>
#include "map_base.h"
@@ -19,39 +18,44 @@
typedef KIt KeyIt;
template <typename V>
- class VectorMap : public MapBase<G, K, KIt> {
+ class Map : public MapBase<G, K, KIt> {
public:
- typedef V ValueType;
+ typedef V Value;
- VectorMap() {}
+ Map() {}
- VectorMap(Graph& g, MapRegistry<G, K, KIt>& r)
- : MapBase<G, K, KIt>(g, r) {}
+ Map(Graph& g, MapRegistry<G, K, KIt>& r)
+ : MapBase<G, K, KIt>(g, r) {
+ init();
+ }
+
+ virtual ~Map() {
+ destroy();
+ }
- ValueType& operator[](const K& key) {
+ Value& operator[](const K& key) {
int id = graph->id(key);
return container[id];
}
- const ValueType& operator[](const K& key) const {
+ const Value& operator[](const K& key) const {
int id = graph->id(key);
return container[id];
}
- const ValueType& get(const K& key) const {
+ const Value& get(const K& key) const {
int id = graph->id(key);
return container[id];
}
- void set(const K& key, const ValueType& val) {
+ void set(const K& key, const Value& 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);
}
@@ -60,7 +64,7 @@
void erase(const K& key) {}
private:
- typedef std::vector<ValueType> Container;
+ typedef std::vector<Value> Container;
Container container;
};
More information about the Lemon-commits
mailing list