lemon/maps.h
changeset 1788 614ce2dd3cba
parent 1725 22752dd6c693
child 1808 c499025ca638
equal deleted inserted replaced
15:30ad47f9c249 16:98a025c87cea
    15  */
    15  */
    16 
    16 
    17 #ifndef LEMON_MAPS_H
    17 #ifndef LEMON_MAPS_H
    18 #define LEMON_MAPS_H
    18 #define LEMON_MAPS_H
    19 
    19 
       
    20 #include <iterator>
       
    21 
    20 #include <lemon/utility.h>
    22 #include <lemon/utility.h>
    21 #include <lemon/traits.h>
    23 #include <lemon/traits.h>
    22 
    24 
    23 ///\file
    25 ///\file
    24 ///\ingroup maps
    26 ///\ingroup maps
   840   public:
   842   public:
   841     typedef MapBase<typename M::Key, bool> Parent;
   843     typedef MapBase<typename M::Key, bool> Parent;
   842     typedef typename Parent::Key Key;
   844     typedef typename Parent::Key Key;
   843     typedef typename Parent::Value Value;
   845     typedef typename Parent::Value Value;
   844 
   846 
   845     ///Constructor
   847     /// Constructor
   846     NotMap(const M &_m) : m(_m) {};
   848     NotMap(const M &_m) : m(_m) {};
   847     Value operator[](Key k) const {return !m[k];}
   849     Value operator[](Key k) const {return !m[k];}
   848   };
   850   };
   849   
   851   
   850   ///Returns a \ref NotMap class
   852   ///Returns a \ref NotMap class
   854   template <typename M> 
   856   template <typename M> 
   855   inline NotMap<M> notMap(const M &m) {
   857   inline NotMap<M> notMap(const M &m) {
   856     return NotMap<M>(m);
   858     return NotMap<M>(m);
   857   }
   859   }
   858 
   860 
       
   861   /// \brief Writeable bool map for store each true assigned elements.
       
   862   ///
       
   863   /// Writeable bool map for store each true assigned elements. It will
       
   864   /// copies all the true setted keys to the given iterator.
       
   865   ///
       
   866   /// \note The container of the iterator should contain for each element.
       
   867   template <typename _Iterator>
       
   868   class StoreBoolMap {
       
   869   public:
       
   870     typedef _Iterator Iterator;
       
   871 
       
   872     typedef typename std::iterator_traits<Iterator>::value_type Key;
       
   873     typedef bool Value;
       
   874 
       
   875     /// Constructor
       
   876     StoreBoolMap(Iterator it) : _begin(it), _end(it) {}
       
   877 
       
   878     /// Gives back the given first setted iterator.
       
   879     Iterator begin() const {
       
   880       return _begin;
       
   881     }
       
   882  
       
   883     /// Gives back the iterator after the last setted.
       
   884     Iterator end() const {
       
   885       return _end;
       
   886     }
       
   887 
       
   888     /// Setter function of the map
       
   889     void set(const Key& key, Value value) {
       
   890       if (value) {
       
   891 	*_end++ = key;
       
   892       }
       
   893     }
       
   894     
       
   895   private:
       
   896     Iterator _begin, _end;
       
   897   };
       
   898 
       
   899   /// \brief Writeable bool map for store each true assigned elements in 
       
   900   /// a back insertable container.
       
   901   ///
       
   902   /// Writeable bool map for store each true assigned elements in a back 
       
   903   /// insertable container. It will push back all the true setted keys into
       
   904   /// the container.
       
   905   template <typename Container>
       
   906   class BackInserterBoolMap {
       
   907   public:
       
   908     typedef typename Container::value_type Key;
       
   909     typedef bool Value;
       
   910 
       
   911     /// Constructor
       
   912     BackInserterBoolMap(Container& _container) : container(_container) {}
       
   913 
       
   914     /// Setter function of the map
       
   915     void set(const Key& key, Value value) {
       
   916       if (value) {
       
   917 	container.push_back(key);
       
   918       }
       
   919     }
       
   920     
       
   921   private:
       
   922     Container& container;    
       
   923   };
       
   924 
       
   925   /// \brief Writeable bool map for store each true assigned elements in 
       
   926   /// a front insertable container.
       
   927   ///
       
   928   /// Writeable bool map for store each true assigned elements in a front 
       
   929   /// insertable container. It will push front all the true setted keys into
       
   930   /// the container.
       
   931   template <typename Container>
       
   932   class FrontInserterBoolMap {
       
   933   public:
       
   934     typedef typename Container::value_type Key;
       
   935     typedef bool Value;
       
   936 
       
   937     /// Constructor
       
   938     FrontInserterBoolMap(Container& _container) : container(_container) {}
       
   939 
       
   940     /// Setter function of the map
       
   941     void set(const Key& key, Value value) {
       
   942       if (value) {
       
   943 	container.push_front(key);
       
   944       }
       
   945     }
       
   946     
       
   947   private:
       
   948     Container& container;    
       
   949   };
       
   950 
       
   951   /// \brief Writeable bool map for store each true assigned elements in 
       
   952   /// an insertable container.
       
   953   ///
       
   954   /// Writeable bool map for store each true assigned elements in an 
       
   955   /// insertable container. It will insert all the true setted keys into
       
   956   /// the container.
       
   957   template <typename Container>
       
   958   class InserterBoolMap {
       
   959   public:
       
   960     typedef typename Container::value_type Key;
       
   961     typedef bool Value;
       
   962 
       
   963     /// Constructor
       
   964     InserterBoolMap(Container& _container) : container(_container) {}
       
   965 
       
   966     /// Setter function of the map
       
   967     void set(const Key& key, Value value) {
       
   968       if (value) {
       
   969 	container.insert(key);
       
   970       }
       
   971     }
       
   972     
       
   973   private:
       
   974     Container& container;    
       
   975   };
       
   976 
       
   977   /// \brief Fill the true setted elements with a given value.
       
   978   ///
       
   979   /// Writeable bool map for fill the true setted elements with a given value.
       
   980   /// The value can be setted 
       
   981   /// the container.
       
   982   template <typename Map>
       
   983   class FillBoolMap {
       
   984   public:
       
   985     typedef typename Map::Key Key;
       
   986     typedef bool Value;
       
   987 
       
   988     /// Constructor
       
   989     FillBoolMap(Map& _map, const typename Map::Value& _fill) 
       
   990       : map(_map), fill(_fill) {}
       
   991 
       
   992     /// Constructor
       
   993     FillBoolMap(Map& _map) 
       
   994       : map(_map), fill() {}
       
   995 
       
   996     /// Gives back the current fill value
       
   997     typename Map::Value fillValue() const {
       
   998       return fill;
       
   999     } 
       
  1000 
       
  1001     /// Sets the current fill value
       
  1002     void fillValue(const typename Map::Value& _fill) {
       
  1003       fill = _fill;
       
  1004     } 
       
  1005 
       
  1006     /// Setter function of the map
       
  1007     void set(const Key& key, Value value) {
       
  1008       if (value) {
       
  1009 	map.set(key, fill);
       
  1010       }
       
  1011     }
       
  1012     
       
  1013   private:
       
  1014     Map& map;
       
  1015     typename Map::Value fill;
       
  1016   };
       
  1017 
       
  1018 
       
  1019   /// \brief Writeable bool map which stores for each true assigned elements  
       
  1020   /// the setting order number.
       
  1021   ///
       
  1022   /// Writeable bool map which stores for each true assigned elements  
       
  1023   /// the setting order number.
       
  1024   template <typename Map>
       
  1025   class SettingOrderBoolMap {
       
  1026   public:
       
  1027     typedef typename Map::Key Key;
       
  1028     typedef bool Value;
       
  1029 
       
  1030     /// Constructor
       
  1031     SettingOrderBoolMap(Map& _map) 
       
  1032       : map(_map), counter(0) {}
       
  1033 
       
  1034     /// Number of setted keys.
       
  1035     int num() const {
       
  1036       return counter;
       
  1037     }
       
  1038 
       
  1039     /// Setter function of the map
       
  1040     void set(const Key& key, Value value) {
       
  1041       if (value) {
       
  1042 	map.set(key, counter++);
       
  1043       }
       
  1044     }
       
  1045     
       
  1046   private:
       
  1047     Map& map;
       
  1048     int counter;
       
  1049   };
       
  1050 
   859   /// @}
  1051   /// @}
   860 }
  1052 }
   861 
  1053 
   862 #endif // LEMON_MAPS_H
  1054 #endif // LEMON_MAPS_H