Naming and coding style fixes and various other changes.
2 * src/lemon/map_registry.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Combinatorial Optimization Research Group, EGRES).
7 * Permission to use, modify and distribute this software is granted
8 * provided that this copyright notice appears in all copies. For
9 * precise terms see the accompanying LICENSE file.
11 * This software is provided "AS IS" with no warranty of any kind,
12 * express or implied, and with no claim as to its suitability for any
17 #ifndef LEMON_MAP_REGISTRY_H
18 #define LEMON_MAP_REGISTRY_H
22 ///\ingroup graphmapfactory
24 ///\brief Map registry for graph maps.
30 /// \addtogroup graphmapfactory
33 /// Map registry for graph maps.
36 * Registry class to register edge or node maps into the graph. The
37 * registry helps you to implement an observer pattern. If you add
38 * or erase an edge or node you must notify all the maps about the
41 * \param G The graph type to register maps.
42 * \param K The key type of the maps registered into the registry.
43 * \param KIt The key iterator type iterates on the keys.
45 * \author Balazs Dezso
48 template <typename G, typename K, typename KIt>
55 /// MapBase is the base class of the dynamic maps.
58 * MapBase is the base class of the dynamic maps.
59 * It defines the core modification operations on the maps and
60 * implements some helper functions.
68 typedef MapRegistry<G, K, KIt> Registry;
70 friend class MapRegistry<G, K, KIt>;
72 /// Default constructor.
75 * Default constructor for MapBase.
78 MapBase() : graph(0), registry(0) {}
80 /// Constructor to register map into a graph registry.
83 * Simple constructor to register dynamic map into a graph registry.
86 MapBase(const Graph& g, Registry& r) : graph(&g), registry(0) {
93 * Copy constructor to register into the registry.
94 * If the copiable map is registered into a registry
95 * the construated map will be registered to the same registry.
98 MapBase(const MapBase& copy) : graph(copy.graph), registry(0) {
100 copy.registry->attach(*this);
107 * Assign operator. This member detach first the map
108 * from the current registry and then it attach to the
109 * copiable map's registry if it exists.
111 const MapBase& operator=(const MapBase& copy) {
113 registry->detach(*this);
117 copy.registry->attach(*this);
125 * This member detach the map from the its registry if the
131 registry->detach(*this);
135 /// The graph of the map.
138 * Returns the graph that the map belongs to.
141 const Graph* getGraph() const { return graph; }
152 /// Helper function to implement constructors in the subclasses.
155 * Helper function to implement constructors in the subclasses.
156 * It adds all of the nodes or edges to the map via the
157 * \ref MapRegistry::MapBase::add add
161 virtual void init() {
162 for (KeyIt it(*graph); it != INVALID; ++it) {
168 /// Helper function to implement destructors in the subclasses.
171 * Helper function to implement destructors in the subclasses.
172 * It erases all of the nodes or edges of the map via the
173 * \ref MapRegistry::MapBase::erase erase
174 * member function. It can be used by the clear function also.
177 virtual void destroy() {
178 for (KeyIt it(*getGraph()); it != INVALID; ++it) {
183 /// The member function to add new node or edge to the map.
186 The add member function should be overloaded in the subclasses.
187 \e Add extends the map with the new node or edge.
190 virtual void add(const KeyType&) = 0;
193 /// The member function to erase a node or edge from the map.
196 The erase member function should be overloaded in the subclasses.
197 \e Erase removes the node or edge from the map.
200 virtual void erase(const KeyType&) = 0;
203 * The clear member function should be overloaded in the subclasses.
204 * \e Clear makes empty the data structure.
207 virtual void clear() = 0;
209 /// Exception class to throw at unsupported operation.
212 * Exception class to throw at unsupported operation.
213 * If the map does not support erasing or adding new
214 * node or key it must be throwed.
217 class NotSupportedOperationException {};
224 typedef std::vector<MapBase*> Container;
231 /// Default constructor.
234 * Default constructor of the \e MapRegistry.
235 * It creates an empty registry.
239 /// Copy Constructor of the MapRegistry.
242 * Copy constructor of the \e MapRegistry.
243 * The new registry does not steal
244 * the maps from the copiable registry.
245 * The new registry will be empty.
247 MapRegistry(const MapRegistry&) {}
252 * Assign operator. This registry does not steal the maps
253 * from the copiable registry. This registry will be an empty registry.
254 * This operator will be called when a graph is assigned.
256 MapRegistry& operator=(const MapRegistry&) {
257 typename Container::iterator it;
258 for (it = container.begin(); it != container.end(); ++it) {
268 * Destructor of the MapRegistry. It makes empty the attached map
269 * first then detachs them.
272 typename Container::iterator it;
273 for (it = container.begin(); it != container.end(); ++it) {
283 /// Attachs a map to the \e MapRegistry.
286 * Attachs a map into thr registry. If the map has been attached
287 * into an other registry it is detached from that automaticly.
289 void attach(MapBase& map) {
291 map.registry->detach(map);
293 container.push_back(&map);
295 map.registry_index = container.size()-1;
298 /// Detachs a map from the \e MapRegistry.
301 * Detachs a map from the \e MapRegistry.
303 void detach(MapBase& map) {
304 container.back()->registry_index = map.registry_index;
305 container[map.registry_index] = container.back();
306 container.pop_back();
311 /// Notify all the registered maps about a Key added.
314 * Notify all the registered maps about a Key added.
315 * This member should be called whenever a node or edge
316 * is added to the graph.
318 void add(const KeyType& key) {
319 typename Container::iterator it;
320 for (it = container.begin(); it != container.end(); ++it) {
325 /// Notify all the registered maps about a Key erased.
328 * Notify all the registered maps about a Key erased.
329 * This member should be called whenever a node or edge
330 * is erased from the graph.
332 void erase(const KeyType& key) {
333 typename Container::iterator it;
334 for (it = container.begin(); it != container.end(); ++it) {
340 /// Notify all the registered maps about all the Keys are erased.
343 * Notify all the registered maps about the map should be cleared.
344 * This member should be called whenever all of the nodes or edges
345 * are erased from the graph.
348 typename Container::iterator it;
349 for (it = container.begin(); it != container.end(); ++it) {