00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
#ifndef LEMON_MAP_REGISTRY_H
00018
#define LEMON_MAP_REGISTRY_H
00019
00020
#include <vector>
00021
00025
00026
using namespace std;
00027
00028
namespace lemon {
00029
00032
00039
template <
typename G,
typename K,
typename KIt>
00040 class MapRegistry {
00041
public:
00042
typedef G Graph;
00043
typedef K KeyType;
00044
typedef KIt KeyIt;
00045
00051 class MapBase {
00052
public:
00053
typedef G Graph;
00054
typedef K KeyType;
00055
typedef KIt KeyIt;
00056
00057
typedef MapRegistry<G, K, KIt> Registry;
00058
00059
friend class MapRegistry<G, K, KIt>;
00060
00065 MapBase() : graph(0), registry(0) {}
00066
00071 MapBase(
const Graph& g,
Registry& r) : graph(&g), registry(0) {
00072 r.
attach(*
this);
00073 }
00074
00079 MapBase(
const MapBase& copy) : graph(copy.graph), registry(0) {
00080
if (copy.
registry) {
00081 copy.
registry->
attach(*
this);
00082 }
00083 }
00084
00088 const MapBase&
operator=(
const MapBase& copy) {
00089
if (registry) {
00090 registry->
detach(*
this);
00091 }
00092 graph = copy.
graph;
00093
if (copy.
registry) {
00094 copy.
registry->
attach(*
this);
00095 }
00096
return *
this;
00097 }
00098
00099
00104 virtual ~MapBase() {
00105
if (registry) {
00106 registry->
detach(*
this);
00107 }
00108 }
00109
00110
00111
00112
00113
00114
const Graph* getGraph()
const {
return graph; }
00115
00116
protected:
00117
00118
const Graph* graph;
00119 Registry* registry;
00120
00121
int registry_index;
00122
00123
protected:
00124
00129 virtual void init() {
00130
for (KeyIt it(*graph); it !=
INVALID; ++it) {
00131
add(it);
00132 }
00133 }
00134
00139 virtual void destroy() {
00140
for (KeyIt it(*getGraph()); it !=
INVALID; ++it) {
00141
erase(it);
00142 }
00143 }
00144
00150
virtual void add(
const KeyType&) = 0;
00156
virtual void erase(
const KeyType&) = 0;
00157
00163
virtual void clear() = 0;
00164
00169 class NotSupportedOperationException {};
00170
00171 };
00172
00173
protected:
00174
00178 typedef std::vector<MapBase*>
Container;
00179
00183 Container container;
00184
00185
00186
public:
00187
00191 MapRegistry() {}
00192
00197 MapRegistry(
const MapRegistry&) {}
00198
00203 MapRegistry&
operator=(
const MapRegistry&) {
00204
typename Container::iterator it;
00205
for (it =
container.begin(); it !=
container.end(); ++it) {
00206 (*it)->destroy();
00207 (*it)->graph = 0;
00208 (*it)->registry = 0;
00209 }
00210 }
00211
00215 ~MapRegistry() {
00216
typename Container::iterator it;
00217
for (it =
container.begin(); it !=
container.end(); ++it) {
00218 (*it)->destroy();
00219 (*it)->registry = 0;
00220 (*it)->graph = 0;
00221 }
00222 }
00223
00224
00225
public:
00226
00231 void attach(
MapBase& map) {
00232
if (map.
registry) {
00233 map.
registry->detach(map);
00234 }
00235
container.push_back(&map);
00236 map.
registry =
this;
00237 map.
registry_index =
container.size()-1;
00238 }
00239
00243 void detach(
MapBase& map) {
00244
container.back()->registry_index = map.
registry_index;
00245
container[map.
registry_index] =
container.back();
00246
container.pop_back();
00247 map.
registry = 0;
00248 map.
graph = 0;
00249 }
00250
00251
00255 void add(KeyType& key) {
00256
typename Container::iterator it;
00257
for (it =
container.begin(); it !=
container.end(); ++it) {
00258 (*it)->add(key);
00259 }
00260 }
00261
00265 void erase(KeyType& key) {
00266
typename Container::iterator it;
00267
for (it =
container.begin(); it !=
container.end(); ++it) {
00268 (*it)->erase(key);
00269 }
00270 }
00271
00275 void clear() {
00276
typename Container::iterator it;
00277
for (it =
container.begin(); it !=
container.end(); ++it) {
00278 (*it)->clear();
00279 }
00280 }
00281 };
00282
00283
00285
00286
00287 }
00288
00289
#endif