Modify to compile with ++-style iterators.
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.
28 /// \addtogroup graphmapfactory
31 /// Map registry for graph maps.
34 * Registry class to register edge or node maps into the graph. The
35 * registry helps you to implement an observer pattern. If you add
36 * or erase an edge or node you must notify all the maps about the
39 * \param G The graph type to register maps.
40 * \param K The key type of the maps registered into the registry.
41 * \param KIt The key iterator type iterates on the keys.
43 * \author Balazs Dezso
46 template <typename G, typename K, typename KIt>
53 /// MapBase is the base class of the dynamic maps.
56 * MapBase is the base class of the dynamic maps.
57 * It defines the core modification operations on the maps and
58 * implements some helper functions.
66 typedef MapRegistry<G, K, KIt> Registry;
68 friend class MapRegistry<G, K, KIt>;
70 /// Default constructor.
73 * Default constructor for MapBase.
76 MapBase() : graph(0), registry(0) {}
78 /// Constructor to register map into a graph registry.
81 * Simple constructor to register dynamic map into a graph registry.
84 MapBase(const Graph& g, Registry& r) : graph(&g), registry(0) {
91 * Copy constructor to register into the registry.
92 * If the copiable map is registered into a registry
93 * the construated map will be registered to the same registry.
96 MapBase(const MapBase& copy) : graph(copy.graph), registry(0) {
98 copy.registry->attach(*this);
105 * Assign operator. This member detach first the map
106 * from the current registry and then it attach to the
107 * copiable map's registry if it exists.
109 const MapBase& operator=(const MapBase& copy) {
111 registry->detach(*this);
115 copy.registry->attach(*this);
123 * This member detach the map from the its registry if the
129 registry->detach(*this);
133 /// The graph of the map.
136 * Returns the graph that the map belongs to.
139 const Graph* getGraph() const { return graph; }
150 /// Helper function to implement constructors in the subclasses.
153 * Helper function to implement constructors in the subclasses.
154 * It adds all of the nodes or edges to the map via the
155 * \ref MapRegistry::MapBase::add add
159 virtual void init() {
160 for (KeyIt it(*graph); it != INVALID; ++it) {
166 /// Helper function to implement destructors in the subclasses.
169 * Helper function to implement destructors in the subclasses.
170 * It erases all of the nodes or edges of the map via the
171 * \ref MapRegistry::MapBase::erase erase
172 * member function. It can be used by the clear function also.
175 virtual void destroy() {
176 for (KeyIt it(*getGraph()); it != INVALID; ++it) {
181 /// The member function to add new node or edge to the map.
184 The add member function should be overloaded in the subclasses.
185 \e Add extends the map with the new node or edge.
188 virtual void add(const KeyType&) = 0;
191 /// The member function to erase a node or edge from the map.
194 The erase member function should be overloaded in the subclasses.
195 \e Erase removes the node or edge from the map.
198 virtual void erase(const KeyType&) = 0;
201 * The clear member function should be overloaded in the subclasses.
202 * \e Clear makes empty the data structure.
205 virtual void clear() = 0;
207 /// Exception class to throw at unsupported operation.
210 * Exception class to throw at unsupported operation.
211 * If the map does not support erasing or adding new
212 * node or key it must be throwed.
215 class NotSupportedOperationException {};
222 typedef std::vector<MapBase*> Container;
229 /// Default constructor.
232 * Default constructor of the \e MapRegistry.
233 * It creates an empty registry.
237 /// Copy Constructor of the MapRegistry.
240 * Copy constructor of the \e MapRegistry.
241 * The new registry does not steal
242 * the maps from the copiable registry.
243 * The new registry will be empty.
245 MapRegistry(const MapRegistry&) {}
250 * Assign operator. This registry does not steal the maps
251 * from the copiable registry. This registry will be an empty registry.
252 * This operator will be called when a graph is assigned.
254 MapRegistry& operator=(const MapRegistry&) {
255 typename Container::iterator it;
256 for (it = container.begin(); it != container.end(); ++it) {
266 * Destructor of the MapRegistry. It makes empty the attached map
267 * first then detachs them.
270 typename Container::iterator it;
271 for (it = container.begin(); it != container.end(); ++it) {
281 /// Attachs a map to the \e MapRegistry.
284 * Attachs a map into thr registry. If the map has been attached
285 * into an other registry it is detached from that automaticly.
287 void attach(MapBase& map) {
289 map.registry->detach(map);
291 container.push_back(&map);
293 map.registry_index = container.size()-1;
296 /// Detachs a map from the \e MapRegistry.
299 * Detachs a map from the \e MapRegistry.
301 void detach(MapBase& map) {
302 container.back()->registry_index = map.registry_index;
303 container[map.registry_index] = container.back();
304 container.pop_back();
309 /// Notify all the registered maps about a Key added.
312 * Notify all the registered maps about a Key added.
313 * This member should be called whenever a node or edge
314 * is added to the graph.
316 void add(const KeyType& key) {
317 typename Container::iterator it;
318 for (it = container.begin(); it != container.end(); ++it) {
323 /// Notify all the registered maps about a Key erased.
326 * Notify all the registered maps about a Key erased.
327 * This member should be called whenever a node or edge
328 * is erased from the graph.
330 void erase(const KeyType& key) {
331 typename Container::iterator it;
332 for (it = container.begin(); it != container.end(); ++it) {
338 /// Notify all the registered maps about all the Keys are erased.
341 * Notify all the registered maps about the map should be cleared.
342 * This member should be called whenever all of the nodes or edges
343 * are erased from the graph.
346 typename Container::iterator it;
347 for (it = container.begin(); it != container.end(); ++it) {