Index: lemon/bits/alteration_notifier.h
===================================================================
--- lemon/bits/alteration_notifier.h	(revision 2188)
+++ lemon/bits/alteration_notifier.h	(revision 2305)
@@ -21,4 +21,5 @@
 
 #include <vector>
+#include <list>
 
 #include <lemon/bits/utility.h>
@@ -174,6 +175,5 @@
       ///
       void attach(AlterationNotifier& _notifier) {
-	notifier = &_notifier;
-	notifier->attach(*this);
+	_notifier.attach(*this);
       }
       
@@ -204,5 +204,5 @@
       
       Notifier* notifier;
-      int notifier_index;
+      typename std::list<ObserverBase*>::iterator index;
 
       /// \brief The member function to notificate the observer about an
@@ -212,5 +212,4 @@
       /// is added to the container. It have to be overrided in the
       /// subclasses.
-	
       virtual void add(const Item&) = 0;
 
@@ -221,18 +220,5 @@
       /// is added to the container. It have to be overrided in the
       /// subclasses.
-
-      virtual void add(const std::vector<Item>& items) {
-        int i;
-        try {
-          for (i = 0; i < (int)items.size(); ++i) {
-            add(items[i]);
-          }
-        } catch (...) {
-          for (int j = 0; j < i; ++j) {
-            add(items[j]);
-          }          
-          throw;
-        }
-      }
+      virtual void add(const std::vector<Item>& items) = 0;
 
       /// \brief The member function to notificate the observer about an
@@ -241,6 +227,5 @@
       /// The erase() member function notificates the observer about an
       /// item is erased from the container. It have to be overrided in
-      /// the subclasses.
-	
+      /// the subclasses.	
       virtual void erase(const Item&) = 0;
 
@@ -251,9 +236,5 @@
       /// is erased from the container. It have to be overrided in the
       /// subclasses.
-      virtual void erase(const std::vector<Item>& items) {
-        for (int i = 0; i < (int)items.size(); ++i) {
-          erase(items[i]);
-        }
-      }
+      virtual void erase(const std::vector<Item>& items) = 0;
 
       /// \brief The member function to notificate the observer about the
@@ -264,18 +245,5 @@
       /// overrided in the subclasses.
 
-      virtual void build() {
-        Item it;
-        try {
-          for (notifier->first(it); it != INVALID; notifier->next(it)) {
-            add(it);
-          }
-        } catch (...) {
-          Item jt;
-          for (notifier->first(jt); jt != it; notifier->next(jt)) {
-            erase(jt);
-          }
-          throw;
-        }
-      }
+      virtual void build() = 0;
 
       /// \brief The member function to notificate the observer about all
@@ -285,10 +253,5 @@
       /// items are erased from the container. It have to be overrided in
       /// the subclasses.      
-      virtual void clear() {
-        Item it;
-        for (notifier->first(it); it != INVALID; notifier->next(it)) {
-          erase(it);
-        }
-      }
+      virtual void clear() = 0;
 
     };
@@ -298,5 +261,5 @@
     const Container* container;
 
-    typedef std::vector<ObserverBase*> Observers; 
+    typedef std::list<ObserverBase*> Observers; 
     Observers observers;
 
@@ -384,13 +347,11 @@
 
     void attach(ObserverBase& observer) {
-      observers.push_back(&observer);
+      observer.index = observers.insert(observers.begin(), &observer);
       observer.notifier = this;
-      observer.notifier_index = observers.size() - 1;
     } 
 
     void detach(ObserverBase& observer) {
-      observers.back()->notifier_index = observer.notifier_index; 
-      observers[observer.notifier_index] = observers.back();
-      observers.pop_back();
+      observers.erase(observer.index);
+      observer.index = observers.end();
       observer.notifier = 0;
     }
@@ -405,13 +366,13 @@
     /// 
     void add(const Item& item) {
-      typename Observers::iterator it;
+      typename Observers::reverse_iterator it;
       try {
-        for (it = observers.begin(); it != observers.end(); ++it) {
+        for (it = observers.rbegin(); it != observers.rend(); ++it) {
           (*it)->add(item);
         }
       } catch (...) {
         typename Observers::iterator jt;
-        for (jt = observers.begin(); jt != it; ++jt) {
-          (*it)->erase(item);
+        for (jt = it.base(); jt != observers.end(); ++jt) {
+          (*jt)->erase(item);
         }
         throw;
@@ -426,13 +387,13 @@
     /// 
     void add(const std::vector<Item>& items) {
-      typename Observers::iterator it;
+      typename Observers::reverse_iterator it;
       try {
-        for (it = observers.begin(); it != observers.end(); ++it) {
+        for (it = observers.rbegin(); it != observers.rend(); ++it) {
           (*it)->add(items);
         }
       } catch (...) {
         typename Observers::iterator jt;
-        for (jt = observers.begin(); jt != it; ++jt) {
-          (*it)->erase(items);
+        for (jt = it.base(); jt != observers.end(); ++jt) {
+          (*jt)->erase(items);
         }
         throw;
@@ -446,12 +407,14 @@
     /// the container.
     /// 
-    void erase(const Item& key) throw() {
-      int i = 0;
-      while (i != (int)observers.size()) {
+    void erase(const Item& item) throw() {
+      typename Observers::iterator it = observers.begin();
+      while (it != observers.end()) {
         try {
-          observers[i]->erase(key);
-          ++i;
+          (*it)->erase(item);
+          ++it;
         } catch (const ImmediateDetach&) {
-          observers[i]->detach();
+          it = observers.erase(it);
+          (*it)->index = observers.end();
+          (*it)->notifier = 0;
         }
       }
@@ -465,11 +428,13 @@
     /// 
     void erase(const std::vector<Item>& items) {
-      int i = 0;
-      while (i != (int)observers.size()) {
+      typename Observers::iterator it = observers.begin();
+      while (it != observers.end()) {
         try {
-          observers[i]->erase(items);
-          ++i;
+          (*it)->erase(items);
+          ++it;
         } catch (const ImmediateDetach&) {
-          observers[i]->detach();
+          it = observers.erase(it);
+          (*it)->index = observers.end();
+          (*it)->notifier = 0;
         }
       }
@@ -482,13 +447,13 @@
     /// from an empty container.
     void build() {
-      typename Observers::iterator it;
+      typename Observers::reverse_iterator it;
       try {
-        for (it = observers.begin(); it != observers.end(); ++it) {
+        for (it = observers.rbegin(); it != observers.rend(); ++it) {
           (*it)->build();
         }
       } catch (...) {
         typename Observers::iterator jt;
-        for (jt = observers.begin(); jt != it; ++jt) {
-          (*it)->clear();
+        for (jt = it.base(); jt != observers.end(); ++jt) {
+          (*jt)->clear();
         }
         throw;
@@ -502,11 +467,13 @@
     /// from the container.
     void clear() {
-      int i = 0;
-      while (i != (int)observers.size()) {
+      typename Observers::iterator it = observers.begin();
+      while (it != observers.end()) {
         try {
-          observers[i]->clear();
-          ++i;
+          (*it)->clear();
+          ++it;
         } catch (const ImmediateDetach&) {
-          observers[i]->detach();
+          it = observers.erase(it);
+          (*it)->index = observers.end();
+          (*it)->notifier = 0;
         }
       }
Index: lemon/matrix_maps.h
===================================================================
--- lemon/matrix_maps.h	(revision 2260)
+++ lemon/matrix_maps.h	(revision 2305)
@@ -355,5 +355,19 @@
     }
 
+    virtual void add(const std::vector<Key>& keys) {
+      int new_size = 0;
+      for (int i = 0; i < (int)keys.size(); ++i) {
+        if (size(Parent::getNotifier()->id(keys[i]) + 1) >= new_size) {
+          new_size = size(Parent::getNotifier()->id(keys[i]) + 1);	
+        }
+      }
+      if (new_size > (int)values.size()) {
+        values.resize(new_size);
+      }
+    }
+
     virtual void erase(const Key&) {}
+
+    virtual void erase(const std::vector<Key>&) {}
 
     virtual void build() {
@@ -496,5 +510,19 @@
     }
 
+    virtual void add(const std::vector<Key>& keys) {
+      int new_size = 0;
+      for (int i = 0; i < (int)keys.size(); ++i) {
+        if (size(Parent::getNotifier()->id(keys[i]) + 1) >= new_size) {
+          new_size = size(Parent::getNotifier()->id(keys[i]) + 1);	
+        }
+      }
+      if (new_size > (int)values.size()) {
+        values.resize(new_size);
+      }
+    }
+
     virtual void erase(const Key&) {}
+
+    virtual void erase(const std::vector<Key>&) {}
 
     virtual void build() {
