lemon/maps.h
changeset 1778 4ba7965386fb
parent 1725 22752dd6c693
child 1808 c499025ca638
     1.1 --- a/lemon/maps.h	Fri Nov 04 19:07:15 2005 +0000
     1.2 +++ b/lemon/maps.h	Fri Nov 04 19:19:52 2005 +0000
     1.3 @@ -17,6 +17,8 @@
     1.4  #ifndef LEMON_MAPS_H
     1.5  #define LEMON_MAPS_H
     1.6  
     1.7 +#include <iterator>
     1.8 +
     1.9  #include <lemon/utility.h>
    1.10  #include <lemon/traits.h>
    1.11  
    1.12 @@ -842,7 +844,7 @@
    1.13      typedef typename Parent::Key Key;
    1.14      typedef typename Parent::Value Value;
    1.15  
    1.16 -    ///Constructor
    1.17 +    /// Constructor
    1.18      NotMap(const M &_m) : m(_m) {};
    1.19      Value operator[](Key k) const {return !m[k];}
    1.20    };
    1.21 @@ -856,6 +858,196 @@
    1.22      return NotMap<M>(m);
    1.23    }
    1.24  
    1.25 +  /// \brief Writeable bool map for store each true assigned elements.
    1.26 +  ///
    1.27 +  /// Writeable bool map for store each true assigned elements. It will
    1.28 +  /// copies all the true setted keys to the given iterator.
    1.29 +  ///
    1.30 +  /// \note The container of the iterator should contain for each element.
    1.31 +  template <typename _Iterator>
    1.32 +  class StoreBoolMap {
    1.33 +  public:
    1.34 +    typedef _Iterator Iterator;
    1.35 +
    1.36 +    typedef typename std::iterator_traits<Iterator>::value_type Key;
    1.37 +    typedef bool Value;
    1.38 +
    1.39 +    /// Constructor
    1.40 +    StoreBoolMap(Iterator it) : _begin(it), _end(it) {}
    1.41 +
    1.42 +    /// Gives back the given first setted iterator.
    1.43 +    Iterator begin() const {
    1.44 +      return _begin;
    1.45 +    }
    1.46 + 
    1.47 +    /// Gives back the iterator after the last setted.
    1.48 +    Iterator end() const {
    1.49 +      return _end;
    1.50 +    }
    1.51 +
    1.52 +    /// Setter function of the map
    1.53 +    void set(const Key& key, Value value) {
    1.54 +      if (value) {
    1.55 +	*_end++ = key;
    1.56 +      }
    1.57 +    }
    1.58 +    
    1.59 +  private:
    1.60 +    Iterator _begin, _end;
    1.61 +  };
    1.62 +
    1.63 +  /// \brief Writeable bool map for store each true assigned elements in 
    1.64 +  /// a back insertable container.
    1.65 +  ///
    1.66 +  /// Writeable bool map for store each true assigned elements in a back 
    1.67 +  /// insertable container. It will push back all the true setted keys into
    1.68 +  /// the container.
    1.69 +  template <typename Container>
    1.70 +  class BackInserterBoolMap {
    1.71 +  public:
    1.72 +    typedef typename Container::value_type Key;
    1.73 +    typedef bool Value;
    1.74 +
    1.75 +    /// Constructor
    1.76 +    BackInserterBoolMap(Container& _container) : container(_container) {}
    1.77 +
    1.78 +    /// Setter function of the map
    1.79 +    void set(const Key& key, Value value) {
    1.80 +      if (value) {
    1.81 +	container.push_back(key);
    1.82 +      }
    1.83 +    }
    1.84 +    
    1.85 +  private:
    1.86 +    Container& container;    
    1.87 +  };
    1.88 +
    1.89 +  /// \brief Writeable bool map for store each true assigned elements in 
    1.90 +  /// a front insertable container.
    1.91 +  ///
    1.92 +  /// Writeable bool map for store each true assigned elements in a front 
    1.93 +  /// insertable container. It will push front all the true setted keys into
    1.94 +  /// the container.
    1.95 +  template <typename Container>
    1.96 +  class FrontInserterBoolMap {
    1.97 +  public:
    1.98 +    typedef typename Container::value_type Key;
    1.99 +    typedef bool Value;
   1.100 +
   1.101 +    /// Constructor
   1.102 +    FrontInserterBoolMap(Container& _container) : container(_container) {}
   1.103 +
   1.104 +    /// Setter function of the map
   1.105 +    void set(const Key& key, Value value) {
   1.106 +      if (value) {
   1.107 +	container.push_front(key);
   1.108 +      }
   1.109 +    }
   1.110 +    
   1.111 +  private:
   1.112 +    Container& container;    
   1.113 +  };
   1.114 +
   1.115 +  /// \brief Writeable bool map for store each true assigned elements in 
   1.116 +  /// an insertable container.
   1.117 +  ///
   1.118 +  /// Writeable bool map for store each true assigned elements in an 
   1.119 +  /// insertable container. It will insert all the true setted keys into
   1.120 +  /// the container.
   1.121 +  template <typename Container>
   1.122 +  class InserterBoolMap {
   1.123 +  public:
   1.124 +    typedef typename Container::value_type Key;
   1.125 +    typedef bool Value;
   1.126 +
   1.127 +    /// Constructor
   1.128 +    InserterBoolMap(Container& _container) : container(_container) {}
   1.129 +
   1.130 +    /// Setter function of the map
   1.131 +    void set(const Key& key, Value value) {
   1.132 +      if (value) {
   1.133 +	container.insert(key);
   1.134 +      }
   1.135 +    }
   1.136 +    
   1.137 +  private:
   1.138 +    Container& container;    
   1.139 +  };
   1.140 +
   1.141 +  /// \brief Fill the true setted elements with a given value.
   1.142 +  ///
   1.143 +  /// Writeable bool map for fill the true setted elements with a given value.
   1.144 +  /// The value can be setted 
   1.145 +  /// the container.
   1.146 +  template <typename Map>
   1.147 +  class FillBoolMap {
   1.148 +  public:
   1.149 +    typedef typename Map::Key Key;
   1.150 +    typedef bool Value;
   1.151 +
   1.152 +    /// Constructor
   1.153 +    FillBoolMap(Map& _map, const typename Map::Value& _fill) 
   1.154 +      : map(_map), fill(_fill) {}
   1.155 +
   1.156 +    /// Constructor
   1.157 +    FillBoolMap(Map& _map) 
   1.158 +      : map(_map), fill() {}
   1.159 +
   1.160 +    /// Gives back the current fill value
   1.161 +    typename Map::Value fillValue() const {
   1.162 +      return fill;
   1.163 +    } 
   1.164 +
   1.165 +    /// Sets the current fill value
   1.166 +    void fillValue(const typename Map::Value& _fill) {
   1.167 +      fill = _fill;
   1.168 +    } 
   1.169 +
   1.170 +    /// Setter function of the map
   1.171 +    void set(const Key& key, Value value) {
   1.172 +      if (value) {
   1.173 +	map.set(key, fill);
   1.174 +      }
   1.175 +    }
   1.176 +    
   1.177 +  private:
   1.178 +    Map& map;
   1.179 +    typename Map::Value fill;
   1.180 +  };
   1.181 +
   1.182 +
   1.183 +  /// \brief Writeable bool map which stores for each true assigned elements  
   1.184 +  /// the setting order number.
   1.185 +  ///
   1.186 +  /// Writeable bool map which stores for each true assigned elements  
   1.187 +  /// the setting order number.
   1.188 +  template <typename Map>
   1.189 +  class SettingOrderBoolMap {
   1.190 +  public:
   1.191 +    typedef typename Map::Key Key;
   1.192 +    typedef bool Value;
   1.193 +
   1.194 +    /// Constructor
   1.195 +    SettingOrderBoolMap(Map& _map) 
   1.196 +      : map(_map), counter(0) {}
   1.197 +
   1.198 +    /// Number of setted keys.
   1.199 +    int num() const {
   1.200 +      return counter;
   1.201 +    }
   1.202 +
   1.203 +    /// Setter function of the map
   1.204 +    void set(const Key& key, Value value) {
   1.205 +      if (value) {
   1.206 +	map.set(key, counter++);
   1.207 +      }
   1.208 +    }
   1.209 +    
   1.210 +  private:
   1.211 +    Map& map;
   1.212 +    int counter;
   1.213 +  };
   1.214 +
   1.215    /// @}
   1.216  }
   1.217