Changeset 1999:2ff283124dfc in lemon-0.x
- Timestamp:
- 03/06/06 11:28:37 (19 years ago)
- Branch:
- default
- Phase:
- public
- Convert:
- svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@2609
- Location:
- lemon
- Files:
-
- 1 added
- 1 deleted
- 19 edited
Legend:
- Unmodified
- Added
- Removed
-
lemon/Makefile.am
r1993 r1999 85 85 bits/alteration_notifier.h \ 86 86 bits/array_map.h \ 87 bits/base_extender.h \ 87 88 bits/default_map.h \ 88 bits/static_map.h \89 89 bits/vector_map.h \ 90 90 bits/map_extender.h \ -
lemon/bits/alteration_notifier.h
r1996 r1999 17 17 */ 18 18 19 #ifndef LEMON_ ALTERATION_NOTIFIER_H20 #define LEMON_ ALTERATION_NOTIFIER_H19 #ifndef LEMON_BITS_ALTERATION_NOTIFIER_H 20 #define LEMON_BITS_ALTERATION_NOTIFIER_H 21 21 22 22 #include <vector> 23 #include <algorithm> 23 24 #include <lemon/bits/utility.h> 24 25 25 26 ///\ingroup graphbits 26 27 ///\file 27 ///\brief Observer registryfor graph alteration observers.28 ///\brief Observer notifier for graph alteration observers. 28 29 29 30 namespace lemon { 30 31 31 /// \addtogroup graphbits 32 /// @{ 33 34 /// \brief Registry class to register objects observes alterations in 35 /// the graph. 36 /// 37 /// This class is a registry for the objects which observe the 38 /// alterations in a container. The alteration observers can be attached 39 /// to and detached from the registry. The observers have to inherit 40 /// from the \ref AlterationNotifier::ObserverBase and override 41 /// the virtual functions in that. 42 /// 43 /// The most important application of the alteration observing is the 44 /// dynamic map implementation. 45 /// 46 /// \param _Item The item type what the observers are observing, usually 47 /// edge or node. 32 /// \ingroup graphbits 33 /// 34 /// \brief Notifier class to notify observes about alterations in 35 /// a container. 36 /// 37 /// The simple graph's can be refered as two containers, one node container 38 /// and one edge container. But they are not standard containers they 39 /// does not store values directly they are just key continars for more 40 /// value containers which are the node and edge maps. 41 /// 42 /// The graph's node and edge sets can be changed as we add or erase 43 /// nodes and edges in the graph. Lemon would like to handle easily 44 /// that the node and edge maps should contain values for all nodes or 45 /// edges. If we want to check on every indicing if the map contains 46 /// the current indicing key that cause a drawback in the performance 47 /// in the library. We use an other solution we notify all maps about 48 /// an alteration in the graph, which cause only drawback on the 49 /// alteration of the graph. 50 /// 51 /// This class provides an interface to the container. The \e first() and \e 52 /// next() member functions make possible to iterate on the keys of the 53 /// container. The \e id() function returns an integer id for each key. 54 /// The \e maxId() function gives back an upper bound of the ids. 55 /// 56 /// For the proper functonality of this class, we should notify it 57 /// about each alteration in the container. The alterations have four type 58 /// as \e add(), \e erase(), \e build() and \e clear(). The \e add() and 59 /// \e erase() signals that only one or few items added or erased to or 60 /// from the graph. If all items are erased from the graph or from an empty 61 /// graph a new graph is builded then it can be signaled with the 62 /// clear() and build() members. Important rule that if we erase items 63 /// from graph we should first signal the alteration and after that erase 64 /// them from the container, on the other way on item addition we should 65 /// first extend the container and just after that signal the alteration. 66 /// 67 /// The alteration can be observed with a class inherited from the 68 /// \e ObserverBase nested class. The signals can be handled with 69 /// overriding the virtual functions defined in the base class. 70 /// The observer base can be attached to the notifier with the 71 /// \e attach() member and can be detached with detach() function. 72 /// 73 /// Alteration observers try to be exception safe. This can be achived 74 /// when the observers does not throw exception on \e erase() and 75 /// \e clear(). Less strict condition is that the \e erase() should 76 /// not throw exception after an \e add() with the same parameter and 77 /// the \e clear() should not throw exception after a \e build(). 78 /// 79 /// There are some place when the alteration observing is not completly 80 /// reliable. If we want to carry out the node degree in the graph 81 /// as in the \ref InDegMap and we use the reverseEdge that cause 82 /// unreliable functionality. Because the alteration observing signals 83 /// only erasing and adding but not the reversing it will stores bad 84 /// degrees. The sub graph adaptors cannot signal the alterations because 85 /// just a setting in the filter map can modify the graph and this cannot 86 /// be watched in any way. 87 /// 88 /// \param _Container The container which is observed. 89 /// \param _Item The item type which is obserbved. 48 90 /// 49 91 /// \author Balazs Dezso 50 92 51 template <typename _ Item>93 template <typename _Container, typename _Item> 52 94 class AlterationNotifier { 53 95 public: … … 55 97 typedef True Notifier; 56 98 99 typedef _Container Container; 57 100 typedef _Item Item; 58 101 59 /// ObserverBase is the base class for the observers.60 102 /// \brief ObserverBase is the base class for the observers. 103 /// 61 104 /// ObserverBase is the abstract base class for the observers. 62 105 /// It will be notified about an item was inserted into or … … 76 119 class ObserverBase { 77 120 protected: 78 typedef AlterationNotifier Registry;121 typedef AlterationNotifier Notifier; 79 122 80 123 friend class AlterationNotifier; … … 84 127 /// Default constructor for ObserverBase. 85 128 /// 86 ObserverBase() : registry(0) {} 87 129 ObserverBase() : notifier(0) {} 130 131 /// \brief Constructor which attach the observer into notifier. 132 /// 133 /// Constructor which attach the observer into notifier. 134 ObserverBase(AlterationNotifier& _notifier) { 135 attach(_notifier); 136 } 137 138 /// \brief Constructor which attach the obserever to the same notifier. 139 /// 140 /// Constructor which attach the obserever to the same notifier as 141 /// the other observer is attached to. 88 142 ObserverBase(const ObserverBase& copy) { 89 143 if (copy.attached()) { 90 copy.getRegistry()->attach(*this);144 attach(*copy.getNotifier()); 91 145 } 92 146 } 93 147 94 virtual ~ObserverBase() {} 148 /// \brief Destructor 149 virtual ~ObserverBase() { 150 if (attached()) { 151 detach(); 152 } 153 } 95 154 96 155 /// \brief Attaches the observer into an AlterationNotifier. … … 98 157 /// This member attaches the observer into an AlterationNotifier. 99 158 /// 100 void attach(AlterationNotifier& r) {101 registry = &r;102 registry->attach(*this);159 void attach(AlterationNotifier& _notifier) { 160 notifier = &_notifier; 161 notifier->attach(*this); 103 162 } 104 163 … … 108 167 /// 109 168 void detach() { 110 if (registry) { 111 registry->detach(*this); 112 } 113 } 114 115 116 /// Gives back a pointer to the registry what the map attached into. 117 118 /// This function gives back a pointer to the registry what the map 169 notifier->detach(*this); 170 } 171 172 173 /// \brief Gives back a pointer to the notifier which the map 119 174 /// attached into. 120 175 /// 121 Registry* getRegistry() const { return const_cast<Registry*>(registry); } 176 /// This function gives back a pointer to the notifier which the map 177 /// attached into. 178 /// 179 Notifier* getNotifier() const { return const_cast<Notifier*>(notifier); } 122 180 123 /// Gives back true when the observer is attached into a registry.124 bool attached() const { return registry!= 0; }181 /// Gives back true when the observer is attached into a notifier. 182 bool attached() const { return notifier != 0; } 125 183 126 184 private: … … 130 188 protected: 131 189 132 Registry* registry;133 int registry_index;190 Notifier* notifier; 191 int notifier_index; 134 192 135 193 /// \brief The member function to notificate the observer about an … … 150 208 151 209 virtual void add(const std::vector<Item>& items) { 152 for (int i = 0; i < (int)items.size(); ++i) { 153 add(items[i]); 154 } 210 int i; 211 try { 212 for (i = 0; i < (int)items.size(); ++i) { 213 add(items[i]); 214 } 215 } catch (...) { 216 for (int j = 0; j < i; ++j) { 217 add(items[j]); 218 } 219 throw; 220 } 155 221 } 156 222 … … 171 237 /// subclasses. 172 238 virtual void erase(const std::vector<Item>& items) { 173 174 175 239 for (int i = 0; i < (int)items.size(); ++i) { 240 erase(items[i]); 241 } 176 242 } 177 243 … … 183 249 /// overrided in the subclasses. 184 250 185 virtual void build() = 0; 251 virtual void build() { 252 Item it; 253 try { 254 for (notifier->first(it); it != INVALID; notifier->next(it)) { 255 add(it); 256 } 257 } catch (...) { 258 Item jt; 259 for (notifier->first(jt); jt != it; notifier->next(jt)) { 260 erase(jt); 261 } 262 throw; 263 } 264 } 186 265 187 266 /// \brief The member function to notificate the observer about all … … 190 269 /// The clear() member function notificates the observer about all 191 270 /// items are erased from the container. It have to be overrided in 192 /// the subclasses. 193 194 virtual void clear() = 0; 271 /// the subclasses. 272 virtual void clear() { 273 Item it; 274 for (notifier->first(it); it != INVALID; notifier->next(it)) { 275 erase(it); 276 } 277 } 195 278 196 279 }; 197 280 198 281 protected: 199 200 201 typedef std::vector<ObserverBase*> Container; 202 203 Container container;282 283 const Container* container; 284 285 typedef std::vector<ObserverBase*> Observers; 286 Observers observers; 204 287 205 288 206 289 public: 207 290 208 /// Default constructor. 209 291 /// \brief Default constructor. 210 292 /// 211 293 /// The default constructor of the AlterationNotifier. 212 /// It creates an empty registry. 213 AlterationNotifier() {} 214 215 /// Copy Constructor of the AlterationNotifier. 216 294 /// It creates an empty notifier. 295 AlterationNotifier() 296 : container(0) {} 297 298 /// \brief Constructor. 299 /// 300 /// Constructor with the observed container parameter. 301 AlterationNotifier(const Container& _container) 302 : container(&_container) {} 303 304 /// \brief Copy Constructor of the AlterationNotifier. 305 /// 217 306 /// Copy constructor of the AlterationNotifier. 218 /// It creates only an empty registry because the copiable 219 /// registry's observers have to be registered still into that registry. 220 AlterationNotifier(const AlterationNotifier&) {} 221 222 /// Assign operator. 307 /// It creates only an empty notifier because the copiable 308 /// notifier's observers have to be registered still into that notifier. 309 AlterationNotifier(const AlterationNotifier& _notifier) 310 : container(_notifier.container) {} 311 312 /// \brief Destructor. 313 /// 314 /// Destructor of the AlterationNotifier. 315 /// 316 ~AlterationNotifier() { 317 typename Observers::iterator it; 318 for (it = observers.begin(); it != observers.end(); ++it) { 319 (*it)->notifier = 0; 320 } 321 } 322 323 /// \brief Sets the container. 324 /// 325 /// Sets the container. 326 void setContainer(const Container& _container) { 327 container = &_container; 328 } 329 330 protected: 331 332 AlterationNotifier& operator=(const AlterationNotifier&); 333 334 public: 335 336 337 338 /// \brief First item in the container. 339 /// 340 /// Returns the first item in the container. It is 341 /// for start the iteration on the container. 342 void first(Item& item) const { 343 container->first(item); 344 } 345 346 /// \brief Next item in the container. 347 /// 348 /// Returns the next item in the container. It is 349 /// for iterate on the container. 350 void next(Item& item) const { 351 container->next(item); 352 } 353 354 /// \brief Returns the id of the item. 355 /// 356 /// Returns the id of the item provided by the container. 357 int id(const Item& item) const { 358 return container->id(item); 359 } 360 361 /// \brief Returns the maximum id of the container. 362 /// 363 /// Returns the maximum id of the container. 364 int maxId() const { 365 return container->maxId(Item()); 366 } 223 367 224 /// Assign operator for the AlterationNotifier.225 /// It makes the notifier only empty because the copiable226 /// notifier's observers have to be registered still into that registry.227 AlterationNotifier& operator=(const AlterationNotifier&) {228 typename Container::iterator it;229 for (it = container.begin(); it != container.end(); ++it) {230 (*it)->registry = 0;231 }232 }233 234 /// Destructor.235 236 /// Destructor of the AlterationNotifier.237 ///238 ~AlterationNotifier() {239 typename Container::iterator it;240 for (it = container.begin(); it != container.end(); ++it) {241 (*it)->registry = 0;242 }243 }244 245 246 368 protected: 247 369 248 370 void attach(ObserverBase& observer) { 249 container.push_back(&observer);250 observer. registry= this;251 observer. registry_index = container.size()-1;371 observers.push_back(&observer); 372 observer.notifier = this; 373 observer.notifier_index = observers.size() - 1; 252 374 } 253 375 254 void detach(ObserverBase& base) {255 container.back()->registry_index = base.registry_index;256 container[base.registry_index] = container.back();257 container.pop_back();258 base.registry= 0;376 void detach(ObserverBase& observer) { 377 observers.back()->notifier_index = observer.notifier_index; 378 observers[observer.notifier_index] = observers.back(); 379 observers.pop_back(); 380 observer.notifier = 0; 259 381 } 260 382 261 383 public: 262 384 263 /// \brief Notifies all the registe red observers about an Item added to264 /// the container. 265 /// 266 /// It notifies all the registe red observers about an Item added to385 /// \brief Notifies all the registed observers about an item added to 386 /// the container. 387 /// 388 /// It notifies all the registed observers about an item added to 267 389 /// the container. 268 390 /// 269 391 void add(const Item& item) { 270 typename Container::iterator it;392 typename Observers::iterator it; 271 393 try { 272 for (it = container.begin(); it != container.end(); ++it) {394 for (it = observers.begin(); it != observers.end(); ++it) { 273 395 (*it)->add(item); 274 396 } 275 397 } catch (...) { 276 typename Container::iterator jt;277 for (jt = container.begin(); jt != it; ++it) {398 typename Observers::iterator jt; 399 for (jt = observers.begin(); jt != it; ++jt) { 278 400 (*it)->erase(item); 279 401 } … … 282 404 } 283 405 284 /// \brief Notifies all the registe red observers about more Item added to285 /// the container. 286 /// 287 /// It notifies all the registe red observers about more Item added to406 /// \brief Notifies all the registed observers about more item added to 407 /// the container. 408 /// 409 /// It notifies all the registed observers about more item added to 288 410 /// the container. 289 411 /// 290 412 void add(const std::vector<Item>& items) { 291 typename Container::iterator it;413 typename Observers::iterator it; 292 414 try { 293 for (it = container.begin(); it != container.end(); ++it) {415 for (it = observers.begin(); it != observers.end(); ++it) { 294 416 (*it)->add(items); 295 417 } 296 418 } catch (...) { 297 typename Container::iterator jt;298 for (jt = container.begin(); jt != it; ++it) {419 typename Observers::iterator jt; 420 for (jt = observers.begin(); jt != it; ++jt) { 299 421 (*it)->erase(items); 300 422 } … … 303 425 } 304 426 305 /// \brief Notifies all the registe red observers about an Item erased from427 /// \brief Notifies all the registed observers about an item erased from 306 428 /// the container. 307 429 /// 308 /// It notifies all the registe red observers about an Item erased from430 /// It notifies all the registed observers about an item erased from 309 431 /// the container. 310 432 /// 311 433 void erase(const Item& key) { 312 typename Container::iterator it;313 for (it = container.begin(); it != container.end(); ++it) {434 typename Observers::iterator it; 435 for (it = observers.begin(); it != observers.end(); ++it) { 314 436 (*it)->erase(key); 315 437 } 316 438 } 317 439 318 /// \brief Notifies all the registe red observers about more Item erased440 /// \brief Notifies all the registed observers about more item erased 319 441 /// from the container. 320 442 /// 321 /// It notifies all the registe red observers about more Item erased from443 /// It notifies all the registed observers about more item erased from 322 444 /// the container. 323 445 /// 324 446 void erase(const std::vector<Item>& items) { 325 typename Container::iterator it;326 for (it = container.begin(); it != container.end(); ++it) {447 typename Observers::iterator it; 448 for (it = observers.begin(); it != observers.end(); ++it) { 327 449 (*it)->erase(items); 328 450 } 329 451 } 330 452 331 /// \brief Notifies all the registe red observers about the container is453 /// \brief Notifies all the registed observers about the container is 332 454 /// built. 333 455 /// 334 /// Notifies all the registe red observers about the container is built456 /// Notifies all the registed observers about the container is built 335 457 /// from an empty container. 336 458 void build() { 337 typename Container::iterator it;459 typename Observers::iterator it; 338 460 try { 339 for (it = container.begin(); it != container.end(); ++it) {461 for (it = observers.begin(); it != observers.end(); ++it) { 340 462 (*it)->build(); 341 463 } 342 464 } catch (...) { 343 typename Container::iterator jt;344 for (jt = container.begin(); jt != it; ++it) {465 typename Observers::iterator jt; 466 for (jt = observers.begin(); jt != it; ++jt) { 345 467 (*it)->clear(); 346 468 } … … 350 472 351 473 352 /// \brief Notifies all the registe red observers about all Items are474 /// \brief Notifies all the registed observers about all items are 353 475 /// erased. 354 476 /// 355 /// Notifies all the registe red observers about all Items are erased477 /// Notifies all the registed observers about all items are erased 356 478 /// from the container. 357 479 void clear() { 358 typename Container::iterator it;359 for (it = container.begin(); it != container.end(); ++it) {480 typename Observers::iterator it; 481 for (it = observers.begin(); it != observers.end(); ++it) { 360 482 (*it)->clear(); 361 483 } … … 363 485 }; 364 486 365 366 /// @}367 368 369 487 } 370 488 -
lemon/bits/array_map.h
r1996 r1999 21 21 22 22 #include <memory> 23 #include <lemon/bits/map_extender.h> 23 24 #include <lemon/bits/traits.h> 24 25 #include <lemon/bits/alteration_notifier.h> 25 #include <lemon/concept_check.h>26 #include <lemon/concept/maps.h>27 26 28 27 /// \ingroup graphbits 29 28 /// \file 30 /// \brief Graph maps that construct and destruct 31 /// their elements dynamically. 29 /// \brief Graph map based on the array storage. 32 30 33 31 namespace lemon { … … 38 36 /// 39 37 /// The ArrayMap template class is graph map structure what 40 /// automatically indates the map when a key is added to or erased from38 /// automatically updates the map when a key is added to or erased from 41 39 /// the map. This map uses the allocators to implement 42 40 /// the container functionality. 43 41 /// 44 /// The template parameter is the AlterationNotifier that the maps 45 /// will belong to and the Value. 46 47 template <typename _Graph, 48 typename _Item, 49 typename _Value> 50 class ArrayMap : public AlterationNotifier<_Item>::ObserverBase { 51 52 typedef _Item Item; 42 /// The template parameter is the Graph the current Item type and 43 /// the Value type of the map. 44 template <typename _Graph, typename _Item, typename _Value> 45 class ArrayMap 46 : public ItemSetTraits<_Graph, _Item>::ItemNotifier::ObserverBase { 53 47 public: 54 48 /// The graph type of the maps. 55 49 typedef _Graph Graph; 50 /// The item type of the map. 51 typedef _Item Item; 56 52 /// The reference map tag. 57 53 typedef True ReferenceMapTag; … … 61 57 /// The value type of the map. 62 58 typedef _Value Value; 59 63 60 /// The const reference type of the map. 64 61 typedef const _Value& ConstReference; … … 66 63 typedef _Value& Reference; 67 64 68 typedef const Value ConstValue; 69 typedef Value* Pointer; 70 typedef const Value* ConstPointer; 71 72 typedef AlterationNotifier<_Item> Registry; 65 /// The notifier type. 66 typedef typename ItemSetTraits<_Graph, _Item>::ItemNotifier Notifier; 73 67 74 68 /// The MapBase of the Map which imlements the core regisitry function. 75 typedef typename Registry::ObserverBase Parent;69 typedef typename Notifier::ObserverBase Parent; 76 70 77 78 79 71 private: 80 72 typedef std::allocator<Value> Allocator; 81 73 82 83 74 public: 84 75 … … 86 77 /// 87 78 /// Graph initialized map constructor. 88 ArrayMap(const Graph& _g) : graph(&_g) { 79 ArrayMap(const Graph& graph) { 80 Parent::attach(graph.getNotifier(Item())); 81 allocate_memory(); 82 Notifier* notifier = Parent::getNotifier(); 89 83 Item it; 90 attach(_g.getNotifier(Item())); 91 allocate_memory(); 92 for (graph->first(it); it != INVALID; graph->next(it)) { 93 int id = graph->id(it);; 84 for (notifier->first(it); it != INVALID; notifier->next(it)) { 85 int id = notifier->id(it);; 94 86 allocator.construct(&(values[id]), Value()); 95 87 } … … 99 91 /// 100 92 /// It constructs a map and initialize all of the the map. 101 ArrayMap(const Graph& _g, const Value& _v) : graph(&_g) { 93 ArrayMap(const Graph& graph, const Value& value) { 94 Parent::attach(graph.getNotifier(Item())); 95 allocate_memory(); 96 Notifier* notifier = Parent::getNotifier(); 102 97 Item it; 103 attach(_g.getNotifier(_Item())); 104 allocate_memory(); 105 for (graph->first(it); it != INVALID; graph->next(it)) { 106 int id = graph->id(it);; 107 allocator.construct(&(values[id]), _v); 98 for (notifier->first(it); it != INVALID; notifier->next(it)) { 99 int id = notifier->id(it);; 100 allocator.construct(&(values[id]), value); 108 101 } 109 102 } … … 112 105 /// 113 106 /// Constructor to copy a map of the same map type. 114 ArrayMap(const ArrayMap& copy) : Parent() , graph(copy.graph){107 ArrayMap(const ArrayMap& copy) : Parent() { 115 108 if (copy.attached()) { 116 attach(*copy.get Registry());109 attach(*copy.getNotifier()); 117 110 } 118 111 capacity = copy.capacity; 119 112 if (capacity == 0) return; 120 113 values = allocator.allocate(capacity); 114 Notifier* notifier = Parent::getNotifier(); 121 115 Item it; 122 for ( graph->first(it); it != INVALID; graph->next(it)) {123 int id = graph->id(it);;116 for (notifier->first(it); it != INVALID; notifier->next(it)) { 117 int id = notifier->id(it);; 124 118 allocator.construct(&(values[id]), copy.values[id]); 125 119 } … … 146 140 using Parent::attached; 147 141 148 const Graph* getGraph() {149 return graph;150 }151 152 153 142 public: 154 143 … … 158 147 /// actual keys of the graph. 159 148 Value& operator[](const Key& key) { 160 int id = graph->id(key);149 int id = Parent::getNotifier()->id(key); 161 150 return values[id]; 162 151 } … … 167 156 /// actual keys of the graph. 168 157 const Value& operator[](const Key& key) const { 169 int id = graph->id(key);158 int id = Parent::getNotifier()->id(key); 170 159 return values[id]; 171 160 } … … 181 170 protected: 182 171 183 /// Add a new key to the map. It called by the map registry. 184 172 /// \brief Adds a new key to the map. 173 /// 174 /// It adds a new key to the map. It called by the observer notifier 175 /// and it overrides the add() member function of the observer base. 185 176 virtual void add(const Key& key) { 186 int id = graph->id(key); 177 Notifier* notifier = Parent::getNotifier(); 178 int id = notifier->id(key); 187 179 if (id >= capacity) { 188 180 int new_capacity = (capacity == 0 ? 1 : capacity); … … 192 184 Value* new_values = allocator.allocate(new_capacity); 193 185 Item it; 194 for ( graph->first(it); it != INVALID; graph->next(it)) {195 int jd = graph->id(it);;186 for (notifier->first(it); it != INVALID; notifier->next(it)) { 187 int jd = notifier->id(it);; 196 188 if (id != jd) { 197 189 allocator.construct(&(new_values[jd]), values[jd]); … … 206 198 } 207 199 200 /// \brief Adds more new keys to the map. 201 /// 202 /// It adds more new keys to the map. It called by the observer notifier 203 /// and it overrides the add() member function of the observer base. 208 204 virtual void add(const std::vector<Key>& keys) { 205 Notifier* notifier = Parent::getNotifier(); 209 206 int max_id = -1; 210 207 for (int i = 0; i < (int)keys.size(); ++i) { 211 int id = graph->id(keys[i]);208 int id = notifier->id(keys[i]); 212 209 if (id > max_id) { 213 210 max_id = id; … … 221 218 Value* new_values = allocator.allocate(new_capacity); 222 219 Item it; 223 for ( graph->first(it); it != INVALID; graph->next(it)) {224 int id = graph->id(it);220 for (notifier->first(it); it != INVALID; notifier->next(it)) { 221 int id = notifier->id(it); 225 222 bool found = false; 226 223 for (int i = 0; i < (int)keys.size(); ++i) { 227 int jd = graph->id(keys[i]);224 int jd = notifier->id(keys[i]); 228 225 if (id == jd) { 229 226 found = true; … … 240 237 } 241 238 for (int i = 0; i < (int)keys.size(); ++i) { 242 int id = graph->id(keys[i]);239 int id = notifier->id(keys[i]); 243 240 allocator.construct(&(values[id]), Value()); 244 241 } 245 242 } 246 243 247 /// Erase a key from the map. It called by the map registry. 248 244 /// \brief Erase a key from the map. 245 /// 246 /// Erase a key from the map. It called by the observer notifier 247 /// and it overrides the erase() member function of the observer base. 249 248 virtual void erase(const Key& key) { 250 int id = graph->id(key);249 int id = Parent::getNotifier()->id(key); 251 250 allocator.destroy(&(values[id])); 252 251 } 253 252 253 /// \brief Erase more keys from the map. 254 /// 255 /// Erase more keys from the map. It called by the observer notifier 256 /// and it overrides the erase() member function of the observer base. 254 257 virtual void erase(const std::vector<Key>& keys) { 255 258 for (int i = 0; i < (int)keys.size(); ++i) { 256 int id = graph->id(keys[i]);259 int id = Parent::getNotifier()->id(keys[i]); 257 260 allocator.destroy(&(values[id])); 258 261 } 259 262 } 260 263 264 /// \brief Buildes the map. 265 /// 266 /// It buildes the map. It called by the observer notifier 267 /// and it overrides the build() member function of the observer base. 261 268 virtual void build() { 269 Notifier* notifier = Parent::getNotifier(); 262 270 allocate_memory(); 263 271 Item it; 264 for ( graph->first(it); it != INVALID; graph->next(it)) {265 int id = graph->id(it);;272 for (notifier->first(it); it != INVALID; notifier->next(it)) { 273 int id = notifier->id(it);; 266 274 allocator.construct(&(values[id]), Value()); 267 275 } 268 276 } 269 277 278 /// \brief Clear the map. 279 /// 280 /// It erase all items from the map. It called by the observer notifier 281 /// and it overrides the clear() member function of the observer base. 270 282 virtual void clear() { 283 Notifier* notifier = Parent::getNotifier(); 271 284 if (capacity != 0) { 272 285 Item it; 273 for ( graph->first(it); it != INVALID; graph->next(it)) {274 int id = graph->id(it);286 for (notifier->first(it); it != INVALID; notifier->next(it)) { 287 int id = notifier->id(it); 275 288 allocator.destroy(&(values[id])); 276 289 } … … 283 296 284 297 void allocate_memory() { 285 int max_id = graph->maxId(_Item());298 int max_id = Parent::getNotifier()->maxId(); 286 299 if (max_id == -1) { 287 300 capacity = 0; … … 296 309 } 297 310 298 const Graph* graph;299 311 int capacity; 300 312 Value* values; -
lemon/bits/default_map.h
r1996 r1999 17 17 */ 18 18 19 #ifndef LEMON_ DEFAULT_MAP_H20 #define LEMON_ DEFAULT_MAP_H19 #ifndef LEMON_BITS_DEFAULT_MAP_H 20 #define LEMON_BITS_DEFAULT_MAP_H 21 21 22 22 23 23 #include <lemon/bits/array_map.h> 24 24 #include <lemon/bits/vector_map.h> 25 #include <lemon/bits/static_map.h>26 25 27 26 ///\ingroup graphbits 28 27 ///\file 29 ///\brief Graph maps that construct and destruct 30 ///their elements dynamically. 28 ///\brief Graph maps that construct and destruct their elements dynamically. 31 29 32 30 namespace lemon { … … 152 150 153 151 /// \e 154 template < 155 typename _Graph, 156 typename _Item, 157 typename _Value> 152 template <typename _Graph, typename _Item, typename _Value> 158 153 class DefaultMap 159 154 : public DefaultMapSelector<_Graph, _Item, _Value>::Map { … … 165 160 typedef typename Parent::Value Value; 166 161 167 DefaultMap(const Graph& _g) : Parent(_g) {} 168 DefaultMap(const Graph& _g, const Value& _v) : Parent(_g, _v) {} 162 DefaultMap(const Graph& graph) : Parent(graph) {} 163 DefaultMap(const Graph& graph, const Value& value) 164 : Parent(graph, value) {} 169 165 170 166 }; -
lemon/bits/edge_set_extender.h
r1996 r1999 74 74 75 75 /// The edge observer registry. 76 typedef AlterationNotifier<Edge > EdgeNotifier;76 typedef AlterationNotifier<EdgeSetExtender, Edge> EdgeNotifier; 77 77 78 78 protected: … … 220 220 template <typename _Value> 221 221 class EdgeMap 222 : public IterableMapExtender<DefaultMap<Graph, Edge, _Value> > {222 : public MapExtender<DefaultMap<Graph, Edge, _Value> > { 223 223 public: 224 224 typedef EdgeSetExtender Graph; 225 typedef IterableMapExtender<DefaultMap<Graph, Edge, _Value> > Parent;225 typedef MapExtender<DefaultMap<Graph, Edge, _Value> > Parent; 226 226 227 227 EdgeMap(const Graph& _g) … … 265 265 } 266 266 267 EdgeSetExtender() { 268 edge_notifier.setContainer(*this); 269 } 267 270 268 271 ~EdgeSetExtender() { … … 331 334 } 332 335 333 typedef AlterationNotifier< Edge> EdgeNotifier;334 typedef AlterationNotifier<UEdge > UEdgeNotifier;336 typedef AlterationNotifier<UEdgeSetExtender, Edge> EdgeNotifier; 337 typedef AlterationNotifier<UEdgeSetExtender, UEdge> UEdgeNotifier; 335 338 336 339 … … 538 541 template <typename _Value> 539 542 class EdgeMap 540 : public IterableMapExtender<DefaultMap<Graph, Edge, _Value> > {543 : public MapExtender<DefaultMap<Graph, Edge, _Value> > { 541 544 public: 542 545 typedef UEdgeSetExtender Graph; 543 typedef IterableMapExtender<DefaultMap<Graph, Edge, _Value> > Parent;546 typedef MapExtender<DefaultMap<Graph, Edge, _Value> > Parent; 544 547 545 548 EdgeMap(const Graph& _g) … … 567 570 template <typename _Value> 568 571 class UEdgeMap 569 : public IterableMapExtender<DefaultMap<Graph, UEdge, _Value> > {572 : public MapExtender<DefaultMap<Graph, UEdge, _Value> > { 570 573 public: 571 574 typedef UEdgeSetExtender Graph; 572 typedef IterableMapExtender<DefaultMap<Graph, UEdge, _Value> > Parent;575 typedef MapExtender<DefaultMap<Graph, UEdge, _Value> > Parent; 573 576 574 577 UEdgeMap(const Graph& _g) … … 618 621 619 622 623 UEdgeSetExtender() { 624 edge_notifier.setContainer(*this); 625 uedge_notifier.setContainer(*this); 626 } 627 620 628 ~UEdgeSetExtender() { 621 getNotifier(Edge()).clear();622 getNotifier(UEdge()).clear();629 uedge_notifier.clear(); 630 edge_notifier.clear(); 623 631 } 624 632 -
lemon/bits/graph_extender.h
r1996 r1999 23 23 #include <lemon/error.h> 24 24 25 #include <lemon/bits/map_extender.h> 25 26 #include <lemon/bits/default_map.h> 27 28 #include <lemon/concept_check.h> 29 #include <lemon/concept/maps.h> 26 30 27 31 ///\ingroup graphbits … … 72 76 // Alterable extension 73 77 74 typedef AlterationNotifier< Node> NodeNotifier;75 typedef AlterationNotifier< Edge> EdgeNotifier;78 typedef AlterationNotifier<GraphExtender, Node> NodeNotifier; 79 typedef AlterationNotifier<GraphExtender, Edge> EdgeNotifier; 76 80 77 81 … … 215 219 template <typename _Value> 216 220 class NodeMap 217 : public IterableMapExtender<DefaultMap<Graph, Node, _Value> > {221 : public MapExtender<DefaultMap<Graph, Node, _Value> > { 218 222 public: 219 223 typedef GraphExtender Graph; 220 typedef IterableMapExtender<DefaultMap<Graph, Node, _Value> > Parent;221 222 NodeMap(const Graph& _g)223 : Parent( _g) {}224 NodeMap(const Graph& _g, const _Value& _v)225 : Parent( _g, _v) {}224 typedef MapExtender<DefaultMap<Graph, Node, _Value> > Parent; 225 226 NodeMap(const Graph& graph) 227 : Parent(graph) {} 228 NodeMap(const Graph& graph, const _Value& value) 229 : Parent(graph, value) {} 226 230 227 231 NodeMap& operator=(const NodeMap& cmap) { … … 239 243 NodeMap& operator=(const CMap& cmap) { 240 244 checkConcept<concept::ReadMap<Node, _Value>, CMap>(); 241 const typename Parent:: Graph* graph = Parent::getGraph();245 const typename Parent::Notifier* notifier = Parent::getNotifier(); 242 246 Node it; 243 for ( graph->first(it); it != INVALID; graph->next(it)) {247 for (notifier->first(it); it != INVALID; notifier->next(it)) { 244 248 Parent::set(it, cmap[it]); 245 249 } … … 251 255 template <typename _Value> 252 256 class EdgeMap 253 : public IterableMapExtender<DefaultMap<Graph, Edge, _Value> > {257 : public MapExtender<DefaultMap<Graph, Edge, _Value> > { 254 258 public: 255 259 typedef GraphExtender Graph; 256 typedef IterableMapExtender<DefaultMap<Graph, Edge, _Value> > Parent;257 258 EdgeMap(const Graph& _g)259 : Parent( _g) {}260 EdgeMap(const Graph& _g, const _Value& _v)261 : Parent( _g, _v) {}260 typedef MapExtender<DefaultMap<Graph, Edge, _Value> > Parent; 261 262 EdgeMap(const Graph& graph) 263 : Parent(graph) {} 264 EdgeMap(const Graph& graph, const _Value& value) 265 : Parent(graph, value) {} 262 266 263 267 EdgeMap& operator=(const EdgeMap& cmap) { … … 268 272 EdgeMap& operator=(const CMap& cmap) { 269 273 checkConcept<concept::ReadMap<Edge, _Value>, CMap>(); 270 const typename Parent:: Graph* graph = Parent::getGraph();274 const typename Parent::Notifier* notifier = Parent::getNotifier(); 271 275 Edge it; 272 for ( graph->first(it); it != INVALID; graph->next(it)) {276 for (notifier->first(it); it != INVALID; notifier->next(it)) { 273 277 Parent::set(it, cmap[it]); 274 278 } … … 320 324 } 321 325 326 GraphExtender() { 327 node_notifier.setContainer(*this); 328 edge_notifier.setContainer(*this); 329 } 330 322 331 323 332 ~GraphExtender() { 324 getNotifier(Edge()).clear();325 getNotifier(Node()).clear();333 edge_notifier.clear(); 334 node_notifier.clear(); 326 335 } 327 336 }; 328 329 /// \ingroup graphbits330 ///331 /// \brief BaseExtender for the UGraphs332 template <typename Base>333 class UGraphBaseExtender : public Base {334 335 public:336 337 typedef Base Parent;338 typedef typename Parent::Edge UEdge;339 typedef typename Parent::Node Node;340 341 typedef True UndirectedTag;342 343 class Edge : public UEdge {344 friend class UGraphBaseExtender;345 346 protected:347 bool forward;348 349 Edge(const UEdge &ue, bool _forward) :350 UEdge(ue), forward(_forward) {}351 352 public:353 Edge() {}354 355 /// Invalid edge constructor356 Edge(Invalid i) : UEdge(i), forward(true) {}357 358 bool operator==(const Edge &that) const {359 return forward==that.forward && UEdge(*this)==UEdge(that);360 }361 bool operator!=(const Edge &that) const {362 return forward!=that.forward || UEdge(*this)!=UEdge(that);363 }364 bool operator<(const Edge &that) const {365 return forward<that.forward ||366 (!(that.forward<forward) && UEdge(*this)<UEdge(that));367 }368 };369 370 371 372 using Parent::source;373 374 /// Source of the given Edge.375 Node source(const Edge &e) const {376 return e.forward ? Parent::source(e) : Parent::target(e);377 }378 379 using Parent::target;380 381 /// Target of the given Edge.382 Node target(const Edge &e) const {383 return e.forward ? Parent::target(e) : Parent::source(e);384 }385 386 /// \brief Directed edge from an undirected edge.387 ///388 /// Returns a directed edge corresponding to the specified UEdge.389 /// If the given bool is true the given undirected edge and the390 /// returned edge have the same source node.391 static Edge direct(const UEdge &ue, bool d) {392 return Edge(ue, d);393 }394 395 /// Returns whether the given directed edge is same orientation as the396 /// corresponding undirected edge.397 ///398 /// \todo reference to the corresponding point of the undirected graph399 /// concept. "What does the direction of an undirected edge mean?"400 static bool direction(const Edge &e) { return e.forward; }401 402 403 using Parent::first;404 using Parent::next;405 406 void first(Edge &e) const {407 Parent::first(e);408 e.forward=true;409 }410 411 void next(Edge &e) const {412 if( e.forward ) {413 e.forward = false;414 }415 else {416 Parent::next(e);417 e.forward = true;418 }419 }420 421 void firstOut(Edge &e, const Node &n) const {422 Parent::firstIn(e,n);423 if( UEdge(e) != INVALID ) {424 e.forward = false;425 }426 else {427 Parent::firstOut(e,n);428 e.forward = true;429 }430 }431 void nextOut(Edge &e) const {432 if( ! e.forward ) {433 Node n = Parent::target(e);434 Parent::nextIn(e);435 if( UEdge(e) == INVALID ) {436 Parent::firstOut(e, n);437 e.forward = true;438 }439 }440 else {441 Parent::nextOut(e);442 }443 }444 445 void firstIn(Edge &e, const Node &n) const {446 Parent::firstOut(e,n);447 if( UEdge(e) != INVALID ) {448 e.forward = false;449 }450 else {451 Parent::firstIn(e,n);452 e.forward = true;453 }454 }455 void nextIn(Edge &e) const {456 if( ! e.forward ) {457 Node n = Parent::source(e);458 Parent::nextOut(e);459 if( UEdge(e) == INVALID ) {460 Parent::firstIn(e, n);461 e.forward = true;462 }463 }464 else {465 Parent::nextIn(e);466 }467 }468 469 void firstInc(UEdge &e, bool &d, const Node &n) const {470 d = true;471 Parent::firstOut(e, n);472 if (e != INVALID) return;473 d = false;474 Parent::firstIn(e, n);475 }476 477 void nextInc(UEdge &e, bool &d) const {478 if (d) {479 Node s = Parent::source(e);480 Parent::nextOut(e);481 if (e != INVALID) return;482 d = false;483 Parent::firstIn(e, s);484 } else {485 Parent::nextIn(e);486 }487 }488 489 Node nodeFromId(int id) const {490 return Parent::nodeFromId(id);491 }492 493 Edge edgeFromId(int id) const {494 return direct(Parent::edgeFromId(id >> 1), bool(id & 1));495 }496 497 UEdge uEdgeFromId(int id) const {498 return Parent::edgeFromId(id >> 1);499 }500 501 int id(const Node &n) const {502 return Parent::id(n);503 }504 505 int id(const UEdge &e) const {506 return Parent::id(e);507 }508 509 int id(const Edge &e) const {510 return 2 * Parent::id(e) + int(e.forward);511 }512 513 int maxNodeId() const {514 return Parent::maxNodeId();515 }516 517 int maxEdgeId() const {518 return 2 * Parent::maxEdgeId() + 1;519 }520 521 int maxUEdgeId() const {522 return Parent::maxEdgeId();523 }524 525 526 int edgeNum() const {527 return 2 * Parent::edgeNum();528 }529 530 int uEdgeNum() const {531 return Parent::edgeNum();532 }533 534 Edge findEdge(Node source, Node target, Edge prev) const {535 if (prev == INVALID) {536 UEdge edge = Parent::findEdge(source, target);537 if (edge != INVALID) return direct(edge, true);538 edge = Parent::findEdge(target, source);539 if (edge != INVALID) return direct(edge, false);540 } else if (direction(prev)) {541 UEdge edge = Parent::findEdge(source, target, prev);542 if (edge != INVALID) return direct(edge, true);543 edge = Parent::findEdge(target, source);544 if (edge != INVALID) return direct(edge, false);545 } else {546 UEdge edge = Parent::findEdge(target, source, prev);547 if (edge != INVALID) return direct(edge, false);548 }549 return INVALID;550 }551 552 UEdge findUEdge(Node source, Node target, UEdge prev) const {553 if (prev == INVALID) {554 UEdge edge = Parent::findEdge(source, target);555 if (edge != INVALID) return edge;556 edge = Parent::findEdge(target, source);557 if (edge != INVALID) return edge;558 } else if (Parent::source(prev) == source) {559 UEdge edge = Parent::findEdge(source, target, prev);560 if (edge != INVALID) return edge;561 edge = Parent::findEdge(target, source);562 if (edge != INVALID) return edge;563 } else {564 UEdge edge = Parent::findEdge(target, source, prev);565 if (edge != INVALID) return edge;566 }567 return INVALID;568 }569 };570 571 337 572 338 /// \ingroup graphbits … … 630 396 // Alterable extension 631 397 632 typedef AlterationNotifier< Node> NodeNotifier;633 typedef AlterationNotifier< Edge> EdgeNotifier;634 typedef AlterationNotifier<U Edge> UEdgeNotifier;398 typedef AlterationNotifier<UGraphExtender, Node> NodeNotifier; 399 typedef AlterationNotifier<UGraphExtender, Edge> EdgeNotifier; 400 typedef AlterationNotifier<UGraphExtender, UEdge> UEdgeNotifier; 635 401 636 402 … … 843 609 template <typename _Value> 844 610 class NodeMap 845 : public IterableMapExtender<DefaultMap<Graph, Node, _Value> > {611 : public MapExtender<DefaultMap<Graph, Node, _Value> > { 846 612 public: 847 613 typedef UGraphExtender Graph; 848 typedef IterableMapExtender<DefaultMap<Graph, Node, _Value> > Parent;849 850 NodeMap(const Graph& _g)851 : Parent( _g) {}852 NodeMap(const Graph& _g, const _Value& _v)853 : Parent( _g, _v) {}614 typedef MapExtender<DefaultMap<Graph, Node, _Value> > Parent; 615 616 NodeMap(const Graph& graph) 617 : Parent(graph) {} 618 NodeMap(const Graph& graph, const _Value& value) 619 : Parent(graph, value) {} 854 620 855 621 NodeMap& operator=(const NodeMap& cmap) { … … 867 633 NodeMap& operator=(const CMap& cmap) { 868 634 checkConcept<concept::ReadMap<Node, _Value>, CMap>(); 869 const typename Parent:: Graph* graph = Parent::getGraph();635 const typename Parent::Notifier* notifier = Parent::getNotifier(); 870 636 Node it; 871 for ( graph->first(it); it != INVALID; graph->next(it)) {637 for (notifier->first(it); it != INVALID; notifier->next(it)) { 872 638 Parent::set(it, cmap[it]); 873 639 } … … 879 645 template <typename _Value> 880 646 class EdgeMap 881 : public IterableMapExtender<DefaultMap<Graph, Edge, _Value> > {647 : public MapExtender<DefaultMap<Graph, Edge, _Value> > { 882 648 public: 883 649 typedef UGraphExtender Graph; 884 typedef IterableMapExtender<DefaultMap<Graph, Edge, _Value> > Parent;885 886 EdgeMap(const Graph& _g)887 : Parent( _g) {}888 EdgeMap(const Graph& _g, const _Value& _v)889 : Parent( _g, _v) {}650 typedef MapExtender<DefaultMap<Graph, Edge, _Value> > Parent; 651 652 EdgeMap(const Graph& graph) 653 : Parent(graph) {} 654 EdgeMap(const Graph& graph, const _Value& value) 655 : Parent(graph, value) {} 890 656 891 657 EdgeMap& operator=(const EdgeMap& cmap) { … … 896 662 EdgeMap& operator=(const CMap& cmap) { 897 663 checkConcept<concept::ReadMap<Edge, _Value>, CMap>(); 898 const typename Parent:: Graph* graph = Parent::getGraph();664 const typename Parent::Notifier* notifier = Parent::getNotifier(); 899 665 Edge it; 900 for ( graph->first(it); it != INVALID; graph->next(it)) {666 for (notifier->first(it); it != INVALID; notifier->next(it)) { 901 667 Parent::set(it, cmap[it]); 902 668 } … … 908 674 template <typename _Value> 909 675 class UEdgeMap 910 : public IterableMapExtender<DefaultMap<Graph, UEdge, _Value> > {676 : public MapExtender<DefaultMap<Graph, UEdge, _Value> > { 911 677 public: 912 678 typedef UGraphExtender Graph; 913 typedef IterableMapExtender<DefaultMap<Graph, UEdge, _Value> > Parent;914 915 UEdgeMap(const Graph& _g)916 : Parent( _g) {}917 UEdgeMap(const Graph& _g, const _Value& _v)918 : Parent( _g, _v) {}679 typedef MapExtender<DefaultMap<Graph, UEdge, _Value> > Parent; 680 681 UEdgeMap(const Graph& graph) 682 : Parent(graph) {} 683 UEdgeMap(const Graph& graph, const _Value& value) 684 : Parent(graph, value) {} 919 685 920 686 UEdgeMap& operator=(const UEdgeMap& cmap) { … … 925 691 UEdgeMap& operator=(const CMap& cmap) { 926 692 checkConcept<concept::ReadMap<UEdge, _Value>, CMap>(); 927 const typename Parent:: Graph* graph = Parent::getGraph();928 UEdge it;929 for ( graph->first(it); it != INVALID; graph->next(it)) {693 const typename Parent::Notifier* notifier = Parent::getNotifier(); 694 Edge it; 695 for (notifier->first(it); it != INVALID; notifier->next(it)) { 930 696 Parent::set(it, cmap[it]); 931 697 } … … 982 748 } 983 749 750 UGraphExtender() { 751 node_notifier.setContainer(*this); 752 edge_notifier.setContainer(*this); 753 uedge_notifier.setContainer(*this); 754 } 755 984 756 ~UGraphExtender() { 985 getNotifier(Edge()).clear(); 986 getNotifier(UEdge()).clear(); 987 getNotifier(Node()).clear(); 988 } 989 990 }; 991 992 993 /// \ingroup graphbits 994 /// 995 /// \brief BaseExtender for the BpUGraphs 996 template <typename Base> 997 class BpUGraphBaseExtender : public Base { 998 public: 999 typedef Base Parent; 1000 typedef BpUGraphBaseExtender Graph; 1001 1002 typedef typename Parent::Node Node; 1003 typedef typename Parent::Edge UEdge; 1004 1005 1006 using Parent::first; 1007 using Parent::next; 1008 1009 using Parent::id; 1010 1011 class ANode : public Node { 1012 friend class BpUGraphBaseExtender; 1013 public: 1014 ANode() {} 1015 ANode(const Node& node) : Node(node) { 1016 LEMON_ASSERT(Parent::aNode(node) || node == INVALID, 1017 typename Parent::NodeSetError()); 1018 } 1019 ANode(Invalid) : Node(INVALID) {} 1020 }; 1021 1022 void first(ANode& node) const { 1023 Parent::firstANode(static_cast<Node&>(node)); 1024 } 1025 void next(ANode& node) const { 1026 Parent::nextANode(static_cast<Node&>(node)); 1027 } 1028 1029 int id(const ANode& node) const { 1030 return Parent::aNodeId(node); 1031 } 1032 1033 class BNode : public Node { 1034 friend class BpUGraphBaseExtender; 1035 public: 1036 BNode() {} 1037 BNode(const Node& node) : Node(node) { 1038 LEMON_ASSERT(Parent::bNode(node) || node == INVALID, 1039 typename Parent::NodeSetError()); 1040 } 1041 BNode(Invalid) : Node(INVALID) {} 1042 }; 1043 1044 void first(BNode& node) const { 1045 Parent::firstBNode(static_cast<Node&>(node)); 1046 } 1047 void next(BNode& node) const { 1048 Parent::nextBNode(static_cast<Node&>(node)); 1049 } 1050 1051 int id(const BNode& node) const { 1052 return Parent::aNodeId(node); 1053 } 1054 1055 Node source(const UEdge& edge) const { 1056 return aNode(edge); 1057 } 1058 Node target(const UEdge& edge) const { 1059 return bNode(edge); 1060 } 1061 1062 void firstInc(UEdge& edge, bool& direction, const Node& node) const { 1063 if (Parent::aNode(node)) { 1064 Parent::firstOut(edge, node); 1065 direction = true; 1066 } else { 1067 Parent::firstIn(edge, node); 1068 direction = static_cast<UEdge&>(edge) == INVALID; 1069 } 1070 } 1071 void nextInc(UEdge& edge, bool& direction) const { 1072 if (direction) { 1073 Parent::nextOut(edge); 1074 } else { 1075 Parent::nextIn(edge); 1076 if (edge == INVALID) direction = true; 1077 } 1078 } 1079 1080 int maxUEdgeId() const { 1081 return Parent::maxEdgeId(); 1082 } 1083 1084 UEdge uEdgeFromId(int id) const { 1085 return Parent::edgeFromId(id); 1086 } 1087 1088 class Edge : public UEdge { 1089 friend class BpUGraphBaseExtender; 1090 protected: 1091 bool forward; 1092 1093 Edge(const UEdge& edge, bool _forward) 1094 : UEdge(edge), forward(_forward) {} 1095 1096 public: 1097 Edge() {} 1098 Edge (Invalid) : UEdge(INVALID), forward(true) {} 1099 bool operator==(const Edge& i) const { 1100 return UEdge::operator==(i) && forward == i.forward; 1101 } 1102 bool operator!=(const Edge& i) const { 1103 return UEdge::operator!=(i) || forward != i.forward; 1104 } 1105 bool operator<(const Edge& i) const { 1106 return UEdge::operator<(i) || 1107 (!(i.forward<forward) && UEdge(*this)<UEdge(i)); 1108 } 1109 }; 1110 1111 void first(Edge& edge) const { 1112 Parent::first(static_cast<UEdge&>(edge)); 1113 edge.forward = true; 1114 } 1115 1116 void next(Edge& edge) const { 1117 if (!edge.forward) { 1118 Parent::next(static_cast<UEdge&>(edge)); 1119 } 1120 edge.forward = !edge.forward; 1121 } 1122 1123 void firstOut(Edge& edge, const Node& node) const { 1124 if (Parent::aNode(node)) { 1125 Parent::firstOut(edge, node); 1126 edge.forward = true; 1127 } else { 1128 Parent::firstIn(edge, node); 1129 edge.forward = static_cast<UEdge&>(edge) == INVALID; 1130 } 1131 } 1132 void nextOut(Edge& edge) const { 1133 if (edge.forward) { 1134 Parent::nextOut(edge); 1135 } else { 1136 Parent::nextIn(edge); 1137 edge.forward = static_cast<UEdge&>(edge) == INVALID; 1138 } 1139 } 1140 1141 void firstIn(Edge& edge, const Node& node) const { 1142 if (Parent::bNode(node)) { 1143 Parent::firstIn(edge, node); 1144 edge.forward = true; 1145 } else { 1146 Parent::firstOut(edge, node); 1147 edge.forward = static_cast<UEdge&>(edge) == INVALID; 1148 } 1149 } 1150 void nextIn(Edge& edge) const { 1151 if (edge.forward) { 1152 Parent::nextIn(edge); 1153 } else { 1154 Parent::nextOut(edge); 1155 edge.forward = static_cast<UEdge&>(edge) == INVALID; 1156 } 1157 } 1158 1159 Node source(const Edge& edge) const { 1160 return edge.forward ? Parent::aNode(edge) : Parent::bNode(edge); 1161 } 1162 Node target(const Edge& edge) const { 1163 return edge.forward ? Parent::bNode(edge) : Parent::aNode(edge); 1164 } 1165 1166 int id(const Edge& edge) const { 1167 return (Parent::id(edge) << 1) + (edge.forward ? 0 : 1); 1168 } 1169 Edge edgeFromId(int id) const { 1170 return Edge(Parent::fromId(id >> 1, UEdge()), (id & 1) == 0); 1171 } 1172 int maxEdgeId() const { 1173 return (Parent::maxId(UEdge()) << 1) + 1; 1174 } 1175 1176 bool direction(const Edge& edge) const { 1177 return edge.forward; 1178 } 1179 1180 Edge direct(const UEdge& edge, bool direction) const { 1181 return Edge(edge, direction); 1182 } 757 uedge_notifier.clear(); 758 edge_notifier.clear(); 759 node_notifier.clear(); 760 } 761 1183 762 }; 1184 763 … … 1246 825 } 1247 826 1248 typedef AlterationNotifier< Node>NodeNotifier;1249 typedef AlterationNotifier<B Node> BNodeNotifier;1250 typedef AlterationNotifier< ANode> ANodeNotifier;1251 typedef AlterationNotifier< Edge> EdgeNotifier;1252 typedef AlterationNotifier< UEdge> UEdgeNotifier;827 typedef AlterationNotifier<BpUGraphExtender, ANode> ANodeNotifier; 828 typedef AlterationNotifier<BpUGraphExtender, BNode> BNodeNotifier; 829 typedef AlterationNotifier<BpUGraphExtender, Node> NodeNotifier; 830 typedef AlterationNotifier<BpUGraphExtender, Edge> EdgeNotifier; 831 typedef AlterationNotifier<BpUGraphExtender, UEdge> UEdgeNotifier; 1253 832 1254 833 protected: 1255 834 1256 mutable NodeNotifier nodeNotifier;1257 mutable BNodeNotifier b NodeNotifier;1258 mutable ANodeNotifier aNodeNotifier;1259 mutable EdgeNotifier edge Notifier;1260 mutable UEdgeNotifier u EdgeNotifier;835 mutable ANodeNotifier anode_notifier; 836 mutable BNodeNotifier bnode_notifier; 837 mutable NodeNotifier node_notifier; 838 mutable EdgeNotifier edge_notifier; 839 mutable UEdgeNotifier uedge_notifier; 1261 840 1262 841 public: 1263 842 1264 843 NodeNotifier& getNotifier(Node) const { 1265 return nodeNotifier; 844 return node_notifier; 845 } 846 847 ANodeNotifier& getNotifier(ANode) const { 848 return anode_notifier; 1266 849 } 1267 850 1268 851 BNodeNotifier& getNotifier(BNode) const { 1269 return bNodeNotifier; 1270 } 1271 1272 ANodeNotifier& getNotifier(ANode) const { 1273 return aNodeNotifier; 852 return bnode_notifier; 1274 853 } 1275 854 1276 855 EdgeNotifier& getNotifier(Edge) const { 1277 return edge Notifier;856 return edge_notifier; 1278 857 } 1279 858 1280 859 UEdgeNotifier& getNotifier(UEdge) const { 1281 return u EdgeNotifier;860 return uedge_notifier; 1282 861 } 1283 862 … … 1512 1091 template <typename _Value> 1513 1092 class ANodeMap 1514 : public IterableMapExtender<DefaultMap<Graph, ANode, _Value> > {1093 : public MapExtender<DefaultMap<Graph, ANode, _Value> > { 1515 1094 public: 1516 1095 typedef BpUGraphExtender Graph; 1517 typedef IterableMapExtender<DefaultMap<Graph, ANode, _Value> > 1518 Parent; 1519 1520 ANodeMap(const Graph& _g) 1521 : Parent(_g) {} 1522 ANodeMap(const Graph& _g, const _Value& _v) 1523 : Parent(_g, _v) {} 1096 typedef MapExtender<DefaultMap<Graph, ANode, _Value> > Parent; 1097 1098 ANodeMap(const Graph& graph) 1099 : Parent(graph) {} 1100 ANodeMap(const Graph& graph, const _Value& value) 1101 : Parent(graph, value) {} 1524 1102 1525 1103 ANodeMap& operator=(const ANodeMap& cmap) { … … 1549 1127 template <typename _Value> 1550 1128 class BNodeMap 1551 : public IterableMapExtender<DefaultMap<Graph, BNode, _Value> > {1129 : public MapExtender<DefaultMap<Graph, BNode, _Value> > { 1552 1130 public: 1553 1131 typedef BpUGraphExtender Graph; 1554 typedef IterableMapExtender<DefaultMap<Graph, BNode, _Value> > 1555 Parent; 1556 1557 BNodeMap(const Graph& _g) 1558 : Parent(_g) {} 1559 BNodeMap(const Graph& _g, const _Value& _v) 1560 : Parent(_g, _v) {} 1132 typedef MapExtender<DefaultMap<Graph, BNode, _Value> > Parent; 1133 1134 BNodeMap(const Graph& graph) 1135 : Parent(graph) {} 1136 BNodeMap(const Graph& graph, const _Value& value) 1137 : Parent(graph, value) {} 1561 1138 1562 1139 BNodeMap& operator=(const BNodeMap& cmap) { … … 1595 1172 1596 1173 /// The reference type of the map; 1597 typedef typename BNodeMap<_Value>::Reference Reference; 1598 /// The pointer type of the map; 1599 typedef typename BNodeMap<_Value>::Pointer Pointer; 1600 1601 /// The const value type of the map. 1602 typedef const Value ConstValue; 1174 typedef typename ANodeMap<_Value>::Reference Reference; 1603 1175 /// The const reference type of the map; 1604 typedef typename BNodeMap<_Value>::ConstReference ConstReference; 1605 /// The pointer type of the map; 1606 typedef typename BNodeMap<_Value>::ConstPointer ConstPointer; 1176 typedef typename ANodeMap<_Value>::ConstReference ConstReference; 1607 1177 1608 1178 typedef True ReferenceMapTag; 1609 1179 1610 NodeMapBase(const Graph& _g)1611 : aNodeMap( _g), bNodeMap(_g) {}1612 NodeMapBase(const Graph& _g, const _Value& _v)1613 : aNodeMap( _g, _v), bNodeMap(_g, _v) {}1180 NodeMapBase(const Graph& graph) 1181 : aNodeMap(graph), bNodeMap(graph) {} 1182 NodeMapBase(const Graph& graph, const _Value& value) 1183 : aNodeMap(graph, value), bNodeMap(graph, value) {} 1614 1184 1615 1185 ConstReference operator[](const Key& node) const { … … 1650 1220 template <typename _Value> 1651 1221 class NodeMap 1652 : public IterableMapExtender<NodeMapBase<_Value> > {1222 : public MapExtender<NodeMapBase<_Value> > { 1653 1223 public: 1654 1224 typedef BpUGraphExtender Graph; 1655 typedef IterableMapExtender< NodeMapBase<_Value> > Parent;1656 1657 NodeMap(const Graph& _g)1658 : Parent( _g) {}1659 NodeMap(const Graph& _g, const _Value& _v)1660 : Parent( _g, _v) {}1225 typedef MapExtender< NodeMapBase<_Value> > Parent; 1226 1227 NodeMap(const Graph& graph) 1228 : Parent(graph) {} 1229 NodeMap(const Graph& graph, const _Value& value) 1230 : Parent(graph, value) {} 1661 1231 1662 1232 NodeMap& operator=(const NodeMap& cmap) { … … 1674 1244 NodeMap& operator=(const CMap& cmap) { 1675 1245 checkConcept<concept::ReadMap<Node, _Value>, CMap>(); 1676 const typename Parent:: Graph* graph = Parent::getGraph();1677 Node it;1678 for ( graph->first(it); it != INVALID; graph->next(it)) {1246 const typename Parent::Notifier* notifier = Parent::getNotifier(); 1247 Edge it; 1248 for (notifier->first(it); it != INVALID; notifier->next(it)) { 1679 1249 Parent::set(it, cmap[it]); 1680 1250 } … … 1688 1258 template <typename _Value> 1689 1259 class EdgeMap 1690 : public IterableMapExtender<DefaultMap<Graph, Edge, _Value> > {1260 : public MapExtender<DefaultMap<Graph, Edge, _Value> > { 1691 1261 public: 1692 1262 typedef BpUGraphExtender Graph; 1693 typedef IterableMapExtender<DefaultMap<Graph, Edge, _Value> > Parent;1694 1695 EdgeMap(const Graph& _g)1696 : Parent( _g) {}1697 EdgeMap(const Graph& _g, const _Value& _v)1698 : Parent( _g, _v) {}1263 typedef MapExtender<DefaultMap<Graph, Edge, _Value> > Parent; 1264 1265 EdgeMap(const Graph& graph) 1266 : Parent(graph) {} 1267 EdgeMap(const Graph& graph, const _Value& value) 1268 : Parent(graph, value) {} 1699 1269 1700 1270 EdgeMap& operator=(const EdgeMap& cmap) { … … 1705 1275 EdgeMap& operator=(const CMap& cmap) { 1706 1276 checkConcept<concept::ReadMap<Edge, _Value>, CMap>(); 1707 const typename Parent:: Graph* graph = Parent::getGraph();1277 const typename Parent::Notifier* notifier = Parent::getNotifier(); 1708 1278 Edge it; 1709 for ( graph->first(it); it != INVALID; graph->next(it)) {1279 for (notifier->first(it); it != INVALID; notifier->next(it)) { 1710 1280 Parent::set(it, cmap[it]); 1711 1281 } … … 1716 1286 template <typename _Value> 1717 1287 class UEdgeMap 1718 : public IterableMapExtender<DefaultMap<Graph, UEdge, _Value> > {1288 : public MapExtender<DefaultMap<Graph, UEdge, _Value> > { 1719 1289 public: 1720 1290 typedef BpUGraphExtender Graph; 1721 typedef IterableMapExtender<DefaultMap<Graph, UEdge, _Value> > 1722 Parent; 1723 1724 UEdgeMap(const Graph& _g) 1725 : Parent(_g) {} 1726 UEdgeMap(const Graph& _g, const _Value& _v) 1727 : Parent(_g, _v) {} 1291 typedef MapExtender<DefaultMap<Graph, UEdge, _Value> > Parent; 1292 1293 UEdgeMap(const Graph& graph) 1294 : Parent(graph) {} 1295 UEdgeMap(const Graph& graph, const _Value& value) 1296 : Parent(graph, value) {} 1728 1297 1729 1298 UEdgeMap& operator=(const UEdgeMap& cmap) { … … 1734 1303 UEdgeMap& operator=(const CMap& cmap) { 1735 1304 checkConcept<concept::ReadMap<UEdge, _Value>, CMap>(); 1736 const typename Parent:: Graph* graph = Parent::getGraph();1737 UEdge it;1738 for ( graph->first(it); it != INVALID; graph->next(it)) {1305 const typename Parent::Notifier* notifier = Parent::getNotifier(); 1306 Edge it; 1307 for (notifier->first(it); it != INVALID; notifier->next(it)) { 1739 1308 Parent::set(it, cmap[it]); 1740 1309 } … … 1802 1371 1803 1372 1373 BpUGraphExtender() { 1374 anode_notifier.setContainer(*this); 1375 bnode_notifier.setContainer(*this); 1376 node_notifier.setContainer(*this); 1377 edge_notifier.setContainer(*this); 1378 uedge_notifier.setContainer(*this); 1379 } 1380 1804 1381 ~BpUGraphExtender() { 1805 getNotifier(Edge()).clear();1806 getNotifier(UEdge()).clear();1807 getNotifier(Node()).clear();1808 getNotifier(BNode()).clear();1809 getNotifier(ANode()).clear();1382 uedge_notifier.clear(); 1383 edge_notifier.clear(); 1384 node_notifier.clear(); 1385 anode_notifier.clear(); 1386 bnode_notifier.clear(); 1810 1387 } 1811 1388 -
lemon/bits/map_extender.h
r1996 r1999 30 30 31 31 /// \ingroup graphbits 32 /// 33 /// \brief Extender for maps 32 34 template <typename _Map> 33 class IterableMapExtender : public _Map {35 class MapExtender : public _Map { 34 36 public: 35 37 36 38 typedef _Map Parent; 37 typedef IterableMapExtender Map;39 typedef MapExtender Map; 38 40 39 41 … … 50 52 friend class ConstMapIt; 51 53 52 protected:53 54 using Parent::getGraph;55 56 54 public: 57 55 58 IterableMapExtender(const Graph& graph) : Parent(graph) {} 56 MapExtender(const Graph& graph) 57 : Parent(graph) {} 59 58 60 IterableMapExtender(const Graph& graph, const Value& value)59 MapExtender(const Graph& graph, const Value& value) 61 60 : Parent(graph, value) {} 62 61 63 62 64 class MapIt : public Item SetTraits<Graph, Item>::ItemIt{63 class MapIt : public Item { 65 64 public: 66 65 67 typedef typename ItemSetTraits<Graph, Item>::ItemIt Parent; 68 66 typedef Item Parent; 69 67 typedef typename Map::Value Value; 70 68 71 MapIt(Map& _map) : Parent(*_map.getGraph()), map(_map) {} 69 MapIt() {} 70 71 MapIt(Invalid i) : Parent(i) { } 72 73 explicit MapIt(Map& _map) : map(_map) { 74 map.getNotifier()->first(*this); 75 } 76 77 MapIt(const Map& _map, const Item& item) 78 : Parent(item), map(_map) {} 79 80 MapIt& operator++() { 81 map.getNotifier()->next(*this); 82 return *this; 83 } 72 84 73 85 typename MapTraits<Map>::ConstReturnValue operator*() const { … … 88 100 }; 89 101 90 class ConstMapIt : public Item SetTraits<Graph, Key>::ItemIt{102 class ConstMapIt : public Item { 91 103 public: 92 104 93 typedef typename ItemSetTraits<Graph, Key>::ItemItParent;105 typedef Item Parent; 94 106 95 107 typedef typename Map::Value Value; 108 109 ConstMapIt() {} 96 110 97 ConstMapIt(const Map& _map) : Parent(*_map.getGraph()), map(_map) {} 111 ConstMapIt(Invalid i) : Parent(i) { } 112 113 explicit ConstMapIt(Map& _map) : map(_map) { 114 map.getNotifier()->first(*this); 115 } 116 117 ConstMapIt(const Map& _map, const Item& item) 118 : Parent(item), map(_map) {} 119 120 ConstMapIt& operator++() { 121 map.getNotifier()->next(*this); 122 return *this; 123 } 98 124 99 125 typename MapTraits<Map>::ConstReturnValue operator*() const { 100 126 return map[*this]; 101 127 } 128 102 129 protected: 103 130 const Map& map; 104 131 }; 105 132 106 class ItemIt : public ItemSetTraits<Graph, Key>::ItemIt{133 class ItemIt : Item { 107 134 public: 108 135 109 typedef typename ItemSetTraits<Graph, Key>::ItemIt Parent; 136 typedef Item Parent; 137 138 ItemIt() {} 110 139 111 ItemIt(Map& _map) : Parent(*_map.getGraph()) {} 140 ItemIt(Invalid i) : Parent(i) { } 141 142 explicit ItemIt(Map& _map) : map(_map) { 143 map->getNotifier()->first(*this); 144 } 145 146 ItemIt(const Map& _map, const Item& item) 147 : Parent(item), map(_map) {} 148 149 ItemIt& operator++() { 150 map.getNotifier()->next(*this); 151 return *this; 152 } 153 154 protected: 155 const Map& map; 112 156 113 157 }; -
lemon/bits/vector_map.h
r1996 r1999 17 17 */ 18 18 19 #ifndef LEMON_ VECTOR_MAP_H20 #define LEMON_ VECTOR_MAP_H19 #ifndef LEMON_BITS_VECTOR_MAP_H 20 #define LEMON_BITS_VECTOR_MAP_H 21 21 22 22 #include <vector> 23 23 #include <algorithm> 24 24 25 #include <lemon/bits/traits.h> 25 26 #include <lemon/bits/utility.h> 26 #include <lemon/bits/map_extender.h> 27 27 28 #include <lemon/bits/alteration_notifier.h> 28 #include <lemon/concept_check.h> 29 #include <lemon/concept/maps.h> 30 31 /// \ingroup graphbits 29 30 ///\ingroup graphbits 32 31 /// 33 32 ///\file 34 33 ///\brief Vector based graph maps. 35 36 34 namespace lemon { 37 35 … … 41 39 /// 42 40 /// The VectorMap template class is graph map structure what 43 /// automatically indates the map when a key is added to or erased from41 /// automatically updates the map when a key is added to or erased from 44 42 /// the map. This map factory uses the allocators to implement 45 43 /// the container functionality. This map factory 46 44 /// uses the std::vector to implement the container function. 47 45 /// 48 /// \param RegistryThe AlterationNotifier that will notify this map.46 /// \param Notifier The AlterationNotifier that will notify this map. 49 47 /// \param Item The item type of the graph items. 50 48 /// \param Value The value type of the map. 51 49 /// 52 /// \author Balazs Dezso 53 54 template < 55 typename _Graph, 56 typename _Item, 57 typename _Value 58 > 59 class VectorMap : public AlterationNotifier<_Item>::ObserverBase { 50 /// \author Balazs Dezso 51 template <typename _Graph, typename _Item, typename _Value> 52 class VectorMap 53 : public ItemSetTraits<_Graph, _Item>::ItemNotifier::ObserverBase { 60 54 private: 61 55 … … 67 61 /// The graph type of the map. 68 62 typedef _Graph Graph; 63 /// The item type of the map. 64 typedef _Item Item; 69 65 /// The reference map tag. 70 66 typedef True ReferenceMapTag; … … 75 71 typedef _Value Value; 76 72 77 typedef AlterationNotifier<_Item> Registry; 73 /// The notifier type. 74 typedef typename ItemSetTraits<_Graph, _Item>::ItemNotifier Notifier; 78 75 79 76 /// The map type. 80 77 typedef VectorMap Map; 81 78 /// The base class of the map. 82 typedef typename Registry::ObserverBase Parent;79 typedef typename Notifier::ObserverBase Parent; 83 80 84 81 /// The reference type of the map; 85 82 typedef typename Container::reference Reference; 86 /// The pointer type of the map;87 typedef typename Container::pointer Pointer;88 89 /// The const value type of the map.90 typedef const Value ConstValue;91 83 /// The const reference type of the map; 92 84 typedef typename Container::const_reference ConstReference; 93 /// The pointer type of the map; 94 typedef typename Container::const_pointer ConstPointer; 95 96 97 /// \brief Constructor to attach the new map into the registry. 98 /// 99 /// It constructs a map and attachs it into the registry. 85 86 87 /// \brief Constructor to attach the new map into the notifier. 88 /// 89 /// It constructs a map and attachs it into the notifier. 100 90 /// It adds all the items of the graph to the map. 101 VectorMap(const Graph& _g) : graph(&_g) {102 attach(_g.getNotifier(_Item()));103 build();91 VectorMap(const Graph& graph) { 92 Parent::attach(graph.getNotifier(Item())); 93 container.resize(Parent::getNotifier()->maxId() + 1); 104 94 } 105 95 … … 108 98 /// It constructs a map uses a given value to initialize the map. 109 99 /// It adds all the items of the graph to the map. 110 VectorMap(const Graph& _g, const Value& _v) : graph(&_g) {111 attach(_g.getNotifier(_Item()));112 container.resize( graph->maxId(_Item()) + 1, _v);100 VectorMap(const Graph& graph, const Value& value) { 101 Parent::attach(graph.getNotifier(Item())); 102 container.resize(Parent::getNotifier()->maxId() + 1, value); 113 103 } 114 104 … … 116 106 /// 117 107 /// Copy constructor. 118 VectorMap(const VectorMap& _copy) 119 : Parent(), graph(_copy.getGraph()) { 108 VectorMap(const VectorMap& _copy) : Parent() { 120 109 if (_copy.attached()) { 121 attach(*_copy.getRegistry());110 Parent::attach(*_copy.getNotifier()); 122 111 container = _copy.container; 123 112 } 124 113 } 125 114 126 /// \brief Destrcutor127 ///128 /// Destructor.129 virtual ~VectorMap() {130 if (attached()) {131 detach();132 }133 }134 135 136 115 private: 137 116 138 117 VectorMap& operator=(const VectorMap&); 139 140 protected:141 142 using Parent::attach;143 using Parent::detach;144 using Parent::attached;145 146 const Graph* getGraph() const {147 return graph;148 }149 118 150 119 public: … … 155 124 /// actual items of the graph. 156 125 Reference operator[](const Key& key) { 157 return container[ graph->id(key)];126 return container[Parent::getNotifier()->id(key)]; 158 127 } 159 128 … … 163 132 /// actual items of the graph. 164 133 ConstReference operator[](const Key& key) const { 165 return container[ graph->id(key)];134 return container[Parent::getNotifier()->id(key)]; 166 135 } 167 136 … … 178 147 /// \brief Adds a new key to the map. 179 148 /// 180 /// It adds a new key to the map. It called by the observer registry149 /// It adds a new key to the map. It called by the observer notifier 181 150 /// and it overrides the add() member function of the observer base. 182 151 virtual void add(const Key& key) { 183 int id = graph->id(key);152 int id = Parent::getNotifier()->id(key); 184 153 if (id >= (int)container.size()) { 185 154 container.resize(id + 1); … … 189 158 /// \brief Adds more new keys to the map. 190 159 /// 191 /// It adds more new keys to the map. It called by the observer registry160 /// It adds more new keys to the map. It called by the observer notifier 192 161 /// and it overrides the add() member function of the observer base. 193 162 virtual void add(const std::vector<Key>& keys) { 163 int max = container.size() - 1; 194 164 for (int i = 0; i < (int)keys.size(); ++i) { 195 add(keys[i]); 196 } 165 int id = Parent::getNotifier()->id(keys[i]); 166 if (id >= max) { 167 max = id; 168 } 169 } 170 container.resize(max + 1); 197 171 } 198 172 199 173 /// \brief Erase a key from the map. 200 174 /// 201 /// Erase a key from the map. It called by the observer registry175 /// Erase a key from the map. It called by the observer notifier 202 176 /// and it overrides the erase() member function of the observer base. 203 virtual void erase(const Key&) {} 177 virtual void erase(const Key& key) { 178 container[Parent::getNotifier()->id(key)] = Value(); 179 } 204 180 205 181 /// \brief Erase more keys from the map. 206 182 /// 207 /// Erase more keys from the map. It called by the observer registry183 /// Erase more keys from the map. It called by the observer notifier 208 184 /// and it overrides the erase() member function of the observer base. 209 virtual void erase(const std::vector<Key>&) {} 185 virtual void erase(const std::vector<Key>& keys) { 186 for (int i = 0; i < (int)keys.size(); ++i) { 187 container[Parent::getNotifier()->id(keys[i])] = Value(); 188 } 189 } 210 190 211 191 /// \brief Buildes the map. 212 192 /// 213 /// It buildes the map. It called by the observer registry193 /// It buildes the map. It called by the observer notifier 214 194 /// and it overrides the build() member function of the observer base. 215 195 virtual void build() { 216 container.resize(graph->maxId(_Item()) + 1); 196 int size = Parent::getNotifier()->maxId() + 1; 197 container.reserve(size); 198 container.resize(size); 217 199 } 218 200 219 201 /// \brief Clear the map. 220 202 /// 221 /// It erase all items from the map. It called by the observer registry203 /// It erase all items from the map. It called by the observer notifier 222 204 /// and it overrides the clear() member function of the observer base. 223 205 virtual void clear() { … … 228 210 229 211 Container container; 230 const Graph *graph;231 212 232 213 }; -
lemon/concept/graph_component.h
r1993 r1999 738 738 /// be registered into the notifier and whenever an alteration 739 739 /// occured in the graph all the observers will notified about it. 740 class AlterableGraphComponent : virtual public Base GraphComponent {740 class AlterableGraphComponent : virtual public BaseIterableGraphComponent { 741 741 public: 742 742 743 743 /// The edge observer registry. 744 typedef AlterationNotifier<Edge> EdgeNotifier; 744 typedef AlterationNotifier<AlterableGraphComponent, Edge> 745 EdgeNotifier; 745 746 /// The node observer registry. 746 typedef AlterationNotifier<Node> NodeNotifier; 747 typedef AlterationNotifier<AlterableGraphComponent, Node> 748 NodeNotifier; 747 749 748 750 /// \brief Gives back the edge alteration notifier. -
lemon/dag_shortest_path.h
r1993 r1999 330 330 typedef typename _Traits::OperationTraits OperationTraits; 331 331 /// \brief The Node weight map. 332 typedef typename Graph:: NodeMap<Value> WeightMap;332 typedef typename Graph::template NodeMap<Value> WeightMap; 333 333 private: 334 334 /// Pointer to the underlying graph -
lemon/edge_set.h
r1990 r1999 572 572 typedef typename Parent::NodesImplBase NodesImplBase; 573 573 574 void eraseNode(const Node&) { 574 void eraseNode(const Node& node) { 575 if (Parent::InEdgeIt(*this, node) == INVALID && 576 Parent::OutEdgeIt(*this, node) == INVALID) { 577 return; 578 } 575 579 throw UnsupportedOperation(); 576 580 } … … 663 667 typedef typename Parent::NodesImplBase NodesImplBase; 664 668 665 void eraseNode(const Node&) { 669 void eraseNode(const Node& node) { 670 if (Parent::IncEdgeIt(*this, node) == INVALID) { 671 return; 672 } 666 673 throw UnsupportedOperation(); 667 674 } -
lemon/full_graph.h
r1995 r1999 22 22 #include <cmath> 23 23 24 24 #include <lemon/bits/base_extender.h> 25 25 #include <lemon/bits/graph_extender.h> 26 27 26 28 27 #include <lemon/bits/invalid.h> -
lemon/graph_adaptor.h
r1993 r1999 31 31 #include <lemon/maps.h> 32 32 33 #include <lemon/bits/base_extender.h> 33 34 #include <lemon/bits/graph_adaptor_extender.h> 34 35 #include <lemon/bits/graph_extender.h> … … 938 939 939 940 UndirGraphAdaptorBase() 940 : Parent(), edge_notifier(), edge_notifier_proxy(edge_notifier) {}941 : edge_notifier(*this), edge_notifier_proxy(edge_notifier) {} 941 942 942 943 void setGraph(_Graph& graph) { … … 948 949 949 950 ~UndirGraphAdaptorBase() { 950 getNotifier(Edge()).clear(); 951 edge_notifier.clear(); 952 } 953 954 int maxId(typename Parent::Edge) const { 955 return Parent::maxEdgeId(); 951 956 } 952 957 … … 959 964 using Parent::getNotifier; 960 965 961 typedef AlterationNotifier< Edge> EdgeNotifier;966 typedef AlterationNotifier<UndirGraphAdaptorBase, Edge> EdgeNotifier; 962 967 EdgeNotifier& getNotifier(Edge) const { return edge_notifier; } 963 968 -
lemon/graph_utils.h
r1993 r1999 1190 1190 Map::build(); 1191 1191 Item it; 1192 const typename Map:: Graph* graph = Map::getGraph();1193 for ( graph->first(it); it != INVALID; graph->next(it)) {1192 const typename Map::Notifier* notifier = Map::getNotifier(); 1193 for (notifier->first(it); it != INVALID; notifier->next(it)) { 1194 1194 Map::set(it, invMap.size()); 1195 1195 invMap.push_back(it); … … 1498 1498 template <typename _Graph> 1499 1499 class InDegMap 1500 : protected AlterationNotifier<typename _Graph::Edge>::ObserverBase { 1500 : protected ItemSetTraits<_Graph, typename _Graph::Edge> 1501 ::ItemNotifier::ObserverBase { 1501 1502 1502 1503 public: … … 1505 1506 typedef int Value; 1506 1507 typedef typename Graph::Node Key; 1508 1509 typedef typename ItemSetTraits<_Graph, typename _Graph::Edge> 1510 ::ItemNotifier::ObserverBase Parent; 1507 1511 1508 1512 private: … … 1534 1538 /// Constructor for creating in-degree map. 1535 1539 InDegMap(const Graph& _graph) : graph(_graph), deg(_graph) { 1536 AlterationNotifier<typename _Graph::Edge> 1537 ::ObserverBase::attach(graph.getNotifier(typename _Graph::Edge())); 1540 Parent::attach(graph.getNotifier(typename _Graph::Edge())); 1538 1541 1539 1542 for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) { 1540 1543 deg[it] = countInEdges(graph, it); 1541 1544 } 1542 }1543 1544 virtual ~InDegMap() {1545 AlterationNotifier<typename _Graph::Edge>::1546 ObserverBase::detach();1547 1545 } 1548 1546 … … 1612 1610 template <typename _Graph> 1613 1611 class OutDegMap 1614 : protected AlterationNotifier<typename _Graph::Edge>::ObserverBase { 1615 1616 public: 1612 : protected ItemSetTraits<_Graph, typename _Graph::Edge> 1613 ::ItemNotifier::ObserverBase { 1614 1615 public: 1616 1617 typedef typename ItemSetTraits<_Graph, typename _Graph::Edge> 1618 ::ItemNotifier::ObserverBase Parent; 1617 1619 1618 1620 typedef _Graph Graph; … … 1647 1649 /// Constructor for creating out-degree map. 1648 1650 OutDegMap(const Graph& _graph) : graph(_graph), deg(_graph) { 1649 AlterationNotifier<typename _Graph::Edge> 1650 ::ObserverBase::attach(graph.getNotifier(typename _Graph::Edge())); 1651 Parent::attach(graph.getNotifier(typename _Graph::Edge())); 1651 1652 1652 1653 for(typename _Graph::NodeIt it(graph); it != INVALID; ++it) { … … 1655 1656 } 1656 1657 1657 virtual ~OutDegMap() {1658 AlterationNotifier<typename _Graph::Edge>::1659 ObserverBase::detach();1660 }1661 1662 1658 /// Gives back the out-degree of a Node. 1663 1659 int operator[](const Key& key) const { -
lemon/grid_ugraph.h
r1993 r1999 24 24 #include <lemon/bits/utility.h> 25 25 26 #include <lemon/bits/base_extender.h> 26 27 #include <lemon/bits/graph_extender.h> 27 28 -
lemon/list_graph.h
r1995 r1999 24 24 ///\brief ListGraph, ListUGraph classes. 25 25 26 #include <lemon/bits/base_extender.h> 26 27 #include <lemon/bits/graph_extender.h> 27 28 … … 321 322 ///\sa concept::ErasableGraph. 322 323 323 class ListGraph : public ExtendedListGraphBase 324 { 324 class ListGraph : public ExtendedListGraphBase { 325 325 public: 326 327 typedef ExtendedListGraphBase Parent; 328 326 329 /// Changes the target of \c e to \c n 327 330 … … 447 450 ///\warning Edge and node deletions cannot be restored. 448 451 ///\warning Snapshots cannot be nested. 449 class Snapshot : protected AlterationNotifier<Node>::ObserverBase,450 protected AlterationNotifier<Edge>::ObserverBase452 class Snapshot : protected Parent::NodeNotifier::ObserverBase, 453 protected Parent::EdgeNotifier::ObserverBase 451 454 { 452 455 public: … … 460 463 461 464 462 465 protected: 463 466 464 467 ListGraph *g; … … 491 494 void regist(ListGraph &_g) { 492 495 g=&_g; 493 AlterationNotifier<Node>::ObserverBase:: 494 attach(g->getNotifier(Node())); 495 AlterationNotifier<Edge>::ObserverBase:: 496 attach(g->getNotifier(Edge())); 496 Parent::NodeNotifier::ObserverBase::attach(g->getNotifier(Node())); 497 Parent::EdgeNotifier::ObserverBase::attach(g->getNotifier(Edge())); 497 498 } 498 499 499 500 void deregist() { 500 AlterationNotifier<Node>::ObserverBase:: 501 detach(); 502 AlterationNotifier<Edge>::ObserverBase:: 503 detach(); 501 Parent::NodeNotifier::ObserverBase::detach(); 502 Parent::EdgeNotifier::ObserverBase::detach(); 504 503 g=0; 505 504 } -
lemon/matrix_maps.h
r1993 r1999 210 210 template <typename _Graph, typename _Item, typename _Value> 211 211 class DynamicMatrixMap 212 : protected AlterationNotifier<_Item>::ObserverBase { 213 public: 214 typedef typename AlterationNotifier<_Item>::ObserverBase Parent; 212 : protected ItemSetTraits<_Graph, _Item>::ItemNotifier::ObserverBase { 213 public: 214 typedef typename ItemSetTraits<_Graph, _Item>::ItemNotifier::ObserverBase 215 Parent; 215 216 216 217 typedef _Graph Graph; … … 236 237 /// Creates an item matrix for the given graph. 237 238 DynamicMatrixMap(const Graph& _graph) 238 : graph(_graph),values(size(_graph.maxId(Key()) + 1)) {239 Parent::attach( graph.getNotifier(Key()));239 : values(size(_graph.maxId(Key()) + 1)) { 240 Parent::attach(_graph.getNotifier(Key())); 240 241 } 241 242 … … 245 246 /// pairs of keys the given parameter. 246 247 DynamicMatrixMap(const Graph& _graph, const Value& _val) 247 : graph(_graph), values(size(_graph.maxId(Key()) + 1), _val) { 248 Parent::attach(graph.getNotifier(Key())); 249 } 250 251 ~DynamicMatrixMap() { 252 if (Parent::attached()) { 253 Parent::detach(); 254 } 248 : values(size(_graph.maxId(Key()) + 1), _val) { 249 Parent::attach(_graph.getNotifier(Key())); 255 250 } 256 251 … … 260 255 /// Gives back the value assigned to the \c first - \c second ordered pair. 261 256 ConstReference operator()(const Key& first, const Key& second) const { 262 return values[index(graph.id(first), graph.id(second))]; 257 return values[index(Parent::getNotifier()->id(first), 258 Parent::getNotifier()->id(second))]; 263 259 } 264 260 … … 268 264 /// Gives back the value assigned to the \c first - \c second ordered pair. 269 265 Reference operator()(const Key& first, const Key& second) { 270 return values[index(graph.id(first), graph.id(second))]; 266 return values[index(Parent::getNotifier()->id(first), 267 Parent::getNotifier()->id(second))]; 271 268 } 272 269 … … 275 272 /// Setter function for the matrix map. 276 273 void set(const Key& first, const Key& second, const Value& val) { 277 values[index(graph.id(first), graph.id(second))] = val; 274 values[index(Parent::getNotifier()->id(first), 275 Parent::getNotifier()->id(second))] = val; 278 276 } 279 277 … … 293 291 294 292 virtual void add(const Key& key) { 295 if (size( graph.id(key) + 1) >= (int)values.size()) {296 values.resize(size( graph.id(key) + 1));293 if (size(Parent::getNotifier()->id(key) + 1) >= (int)values.size()) { 294 values.resize(size(Parent::getNotifier()->id(key) + 1)); 297 295 } 298 296 } … … 301 299 302 300 virtual void build() { 303 values.resize(size( graph.maxId(Key()) + 1));301 values.resize(size(Parent::getNotifier()->maxId() + 1)); 304 302 } 305 303 … … 309 307 310 308 private: 311 const Graph& graph;312 309 std::vector<Value> values; 313 310 }; … … 321 318 template <typename _Graph, typename _Item, typename _Value> 322 319 class DynamicSymMatrixMap 323 : protected AlterationNotifier<_Item>::ObserverBase { 324 public: 325 typedef typename AlterationNotifier<_Item>::ObserverBase Parent; 320 : protected ItemSetTraits<_Graph, _Item>::ItemNotifier::ObserverBase { 321 public: 322 typedef typename ItemSetTraits<_Graph, _Item>::ItemNotifier::ObserverBase 323 Parent; 326 324 327 325 typedef _Graph Graph; … … 347 345 /// Creates an item matrix for the given graph. 348 346 DynamicSymMatrixMap(const Graph& _graph) 349 : graph(_graph),values(size(_graph.maxId(Key()) + 1)) {350 Parent::attach( graph.getNotifier(Key()));347 : values(size(_graph.maxId(Key()) + 1)) { 348 Parent::attach(_graph.getNotifier(Key())); 351 349 } 352 350 … … 356 354 /// pairs of keys the given parameter. 357 355 DynamicSymMatrixMap(const Graph& _graph, const Value& _val) 358 : graph(_graph), values(size(_graph.maxId(Key()) + 1), _val) { 359 Parent::attach(graph.getNotifier(Key())); 360 } 361 362 ~DynamicSymMatrixMap() { 363 if (Parent::attached()) { 364 Parent::detach(); 365 } 356 : values(size(_graph.maxId(Key()) + 1), _val) { 357 Parent::attach(_graph.getNotifier(Key())); 366 358 } 367 359 … … 372 364 /// pair. 373 365 ConstReference operator()(const Key& first, const Key& second) const { 374 return values[index(graph.id(first), graph.id(second))]; 366 return values[index(Parent::getNotifier()->id(first), 367 Parent::getNotifier()->id(second))]; 375 368 } 376 369 … … 381 374 /// pair. 382 375 Reference operator()(const Key& first, const Key& second) { 383 return values[index(graph.id(first), graph.id(second))]; 376 return values[index(Parent::getNotifier()->id(first), 377 Parent::getNotifier()->id(second))]; 384 378 } 385 379 … … 388 382 /// Setter function for the matrix map. 389 383 void set(const Key& first, const Key& second, const Value& val) { 390 values[index(graph.id(first), graph.id(second))] = val; 384 values[index(Parent::getNotifier()->id(first), 385 Parent::getNotifier()->id(second))] = val; 391 386 } 392 387 … … 406 401 407 402 virtual void add(const Key& key) { 408 if (size( graph.id(key) + 1) >= (int)values.size()) {409 values.resize(size( graph.id(key) + 1));403 if (size(Parent::getNotifier()->id(key) + 1) >= (int)values.size()) { 404 values.resize(size(Parent::getNotifier()->id(key) + 1)); 410 405 } 411 406 } … … 414 409 415 410 virtual void build() { 416 values.resize(size( graph.maxId(Key()) + 1));411 values.resize(size(Parent::getNotifier()->maxId() + 1)); 417 412 } 418 413 … … 422 417 423 418 private: 424 const Graph& graph;425 419 std::vector<Value> values; 426 420 }; -
lemon/smart_graph.h
r1995 r1999 28 28 #include <lemon/bits/invalid.h> 29 29 30 #include <lemon/bits/base_extender.h> 30 31 #include <lemon/bits/graph_extender.h> 31 32 -
lemon/xy.h
r1993 r1999 151 151 152 152 }; 153 154 ///Returns an xy 155 156 ///Returns an xy 157 ///\relates xy 158 template <typename T> 159 inline xy<T> make_xy(const T& x, const T& y) { 160 return xy<T>(x, y); 161 } 153 162 154 163 ///Returns a vector multiplied by a scalar
Note: See TracChangeset
for help on using the changeset viewer.