lemon/bits/map_extender.h
changeset 2031 080d51024ac5
parent 1999 2ff283124dfc
child 2260 4274224f8a7d
     1.1 --- a/lemon/bits/map_extender.h	Mon Apr 03 09:24:38 2006 +0000
     1.2 +++ b/lemon/bits/map_extender.h	Mon Apr 03 09:45:23 2006 +0000
     1.3 @@ -23,6 +23,9 @@
     1.4  
     1.5  #include <lemon/bits/traits.h>
     1.6  
     1.7 +#include <lemon/concept_check.h>
     1.8 +#include <lemon/concept/maps.h>
     1.9 +
    1.10  ///\file
    1.11  ///\brief Extenders for iterable maps.
    1.12  
    1.13 @@ -59,6 +62,15 @@
    1.14      MapExtender(const Graph& graph, const Value& value) 
    1.15        : Parent(graph, value) {}
    1.16  
    1.17 +    MapExtender& operator=(const MapExtender& cmap) {
    1.18 +      return operator=<MapExtender>(cmap);
    1.19 +    }
    1.20 +
    1.21 +    template <typename CMap>
    1.22 +    MapExtender& operator=(const CMap& cmap) {
    1.23 +      Parent::operator=(cmap);
    1.24 +      return *this;
    1.25 +    } 
    1.26  
    1.27      class MapIt : public Item {
    1.28      public:
    1.29 @@ -130,7 +142,7 @@
    1.30        const Map& map;
    1.31      };
    1.32  
    1.33 -    class ItemIt : Item {
    1.34 +    class ItemIt : public Item {
    1.35      public:
    1.36        
    1.37        typedef Item Parent;
    1.38 @@ -140,7 +152,7 @@
    1.39        ItemIt(Invalid i) : Parent(i) { }
    1.40  
    1.41        explicit ItemIt(Map& _map) : map(_map) {
    1.42 -        map->getNotifier()->first(*this);
    1.43 +        map.getNotifier()->first(*this);
    1.44        }
    1.45  
    1.46        ItemIt(const Map& _map, const Item& item) 
    1.47 @@ -157,6 +169,153 @@
    1.48      };
    1.49    };
    1.50  
    1.51 +  /// \ingroup graphbits
    1.52 +  /// 
    1.53 +  /// \brief Extender for maps which use a subset of the items.
    1.54 +  template <typename _Graph, typename _Map>
    1.55 +  class SubMapExtender : public _Map {
    1.56 +  public:
    1.57 +
    1.58 +    typedef _Map Parent;
    1.59 +    typedef SubMapExtender Map;
    1.60 +
    1.61 +    typedef _Graph Graph;
    1.62 +
    1.63 +    typedef typename Parent::Key Item;
    1.64 +
    1.65 +    typedef typename Parent::Key Key;
    1.66 +    typedef typename Parent::Value Value;
    1.67 +
    1.68 +    class MapIt;
    1.69 +    class ConstMapIt;
    1.70 +
    1.71 +    friend class MapIt;
    1.72 +    friend class ConstMapIt;
    1.73 +
    1.74 +  public:
    1.75 +
    1.76 +    SubMapExtender(const Graph& _graph) 
    1.77 +      : Parent(_graph), graph(_graph) {}
    1.78 +
    1.79 +    SubMapExtender(const Graph& _graph, const Value& _value) 
    1.80 +      : Parent(_graph, _value), graph(_graph) {}
    1.81 +
    1.82 +    SubMapExtender& operator=(const SubMapExtender& cmap) {
    1.83 +      return operator=<MapExtender>(cmap);
    1.84 +    }
    1.85 +
    1.86 +    template <typename CMap>
    1.87 +    SubMapExtender& operator=(const CMap& cmap) {
    1.88 +      checkConcept<concept::ReadMap<Key, Value>, CMap>();
    1.89 +      Item it;
    1.90 +      for (graph.first(it); it != INVALID; graph.next(it)) {
    1.91 +        Parent::set(it, cmap[it]);
    1.92 +      }
    1.93 +      return *this;
    1.94 +    } 
    1.95 +
    1.96 +    class MapIt : public Item {
    1.97 +    public:
    1.98 +      
    1.99 +      typedef Item Parent;
   1.100 +      typedef typename Map::Value Value;
   1.101 +      
   1.102 +      MapIt() {}
   1.103 +
   1.104 +      MapIt(Invalid i) : Parent(i) { }
   1.105 +
   1.106 +      explicit MapIt(Map& _map) : map(_map) {
   1.107 +        map.graph.first(*this);
   1.108 +      }
   1.109 +
   1.110 +      MapIt(const Map& _map, const Item& item) 
   1.111 +	: Parent(item), map(_map) {}
   1.112 +
   1.113 +      MapIt& operator++() { 
   1.114 +	map.graph.next(*this);
   1.115 +	return *this; 
   1.116 +      }
   1.117 +      
   1.118 +      typename MapTraits<Map>::ConstReturnValue operator*() const {
   1.119 +	return map[*this];
   1.120 +      }
   1.121 +
   1.122 +      typename MapTraits<Map>::ReturnValue operator*() {
   1.123 +	return map[*this];
   1.124 +      }
   1.125 +      
   1.126 +      void set(const Value& value) {
   1.127 +	map.set(*this, value);
   1.128 +      }
   1.129 +      
   1.130 +    protected:
   1.131 +      Map& map;
   1.132 +      
   1.133 +    };
   1.134 +
   1.135 +    class ConstMapIt : public Item {
   1.136 +    public:
   1.137 +
   1.138 +      typedef Item Parent;
   1.139 +
   1.140 +      typedef typename Map::Value Value;
   1.141 +      
   1.142 +      ConstMapIt() {}
   1.143 +
   1.144 +      ConstMapIt(Invalid i) : Parent(i) { }
   1.145 +
   1.146 +      explicit ConstMapIt(Map& _map) : map(_map) {
   1.147 +        map.graph.first(*this);
   1.148 +      }
   1.149 +
   1.150 +      ConstMapIt(const Map& _map, const Item& item) 
   1.151 +	: Parent(item), map(_map) {}
   1.152 +
   1.153 +      ConstMapIt& operator++() { 
   1.154 +	map.graph.next(*this);
   1.155 +	return *this; 
   1.156 +      }
   1.157 +
   1.158 +      typename MapTraits<Map>::ConstReturnValue operator*() const {
   1.159 +	return map[*this];
   1.160 +      }
   1.161 +
   1.162 +    protected:
   1.163 +      const Map& map;
   1.164 +    };
   1.165 +
   1.166 +    class ItemIt : public Item {
   1.167 +    public:
   1.168 +      
   1.169 +      typedef Item Parent;
   1.170 +      
   1.171 +      ItemIt() {}
   1.172 +
   1.173 +      ItemIt(Invalid i) : Parent(i) { }
   1.174 +
   1.175 +      explicit ItemIt(Map& _map) : map(_map) {
   1.176 +        map.graph.first(*this);
   1.177 +      }
   1.178 +
   1.179 +      ItemIt(const Map& _map, const Item& item) 
   1.180 +	: Parent(item), map(_map) {}
   1.181 +
   1.182 +      ItemIt& operator++() { 
   1.183 +	map.graph.next(*this);
   1.184 +	return *this; 
   1.185 +      }
   1.186 +
   1.187 +    protected:
   1.188 +      const Map& map;
   1.189 +      
   1.190 +    };
   1.191 +    
   1.192 +  private:
   1.193 +
   1.194 +    const Graph& graph;
   1.195 +    
   1.196 +  };
   1.197 +
   1.198  }
   1.199  
   1.200  #endif