lemon/iterable_maps.h
changeset 1809 029cc4f638d1
parent 1759 0bb3fb3baffd
child 1810 474d093466a5
equal deleted inserted replaced
3:c351d75e187e 4:42dc0c7ee65e
    28 
    28 
    29 
    29 
    30 namespace lemon {
    30 namespace lemon {
    31   
    31   
    32   ///\todo This is only a static map!
    32   ///\todo This is only a static map!
       
    33   ///\todo Undocumented.
    33   ///\param BaseMap is an interger map.
    34   ///\param BaseMap is an interger map.
    34   template<class BaseMap>
    35   template<class BaseMap>
    35   class IterableBoolMap
    36   class IterableBoolMap
    36   {
    37   {
    37   public:
    38   public:
    61     void setFalse(Key k) { if(cref[k]>=sep) { swap(k,sep); sep++; } }
    62     void setFalse(Key k) { if(cref[k]>=sep) { swap(k,sep); sep++; } }
    62   
    63   
    63   public:
    64   public:
    64     ///\e
    65     ///\e
    65     void set(Key k,Value v) { if(v) setTrue(k); else setFalse(k);}
    66     void set(Key k,Value v) { if(v) setTrue(k); else setFalse(k);}
       
    67     ///Number of \c true items in the map
       
    68 
       
    69     ///Returns the number of \c true values in the map.
       
    70     ///This is a constant time operation.
       
    71     int countTrue() { return vals.size()-sep; }
       
    72     ///Number of \c false items in the map
       
    73 
       
    74     ///Returns the number of \c false values in the map.
       
    75     ///This is a constant time operation.
       
    76     int countFalse() { return sep; }
    66 
    77 
    67     ///\e
    78     ///\e
    68     class FalseIt
    79     class FalseIt
    69     {
    80     {
    70       const IterableBoolMap &M;
    81       const IterableBoolMap &M;
    71       int i;
    82       int i;
    72     public:
    83     public:
       
    84       ///\e
    73       explicit FalseIt(const IterableBoolMap &_M) : M(_M), i(0) { }
    85       explicit FalseIt(const IterableBoolMap &_M) : M(_M), i(0) { }
       
    86       ///\e
    74       FalseIt(Invalid)
    87       FalseIt(Invalid)
    75 	: M(*((IterableBoolMap*)(0))), i(std::numeric_limits<int>::max()) { }
    88 	: M(*((IterableBoolMap*)(0))), i(std::numeric_limits<int>::max()) { }
       
    89       ///\e
    76       FalseIt &operator++() { ++i; return *this;}
    90       FalseIt &operator++() { ++i; return *this;}
       
    91       ///\e
    77       operator Key() const { return i<M.sep ? M.vals[i] : INVALID; }
    92       operator Key() const { return i<M.sep ? M.vals[i] : INVALID; }
       
    93       ///\e
    78       bool operator !=(Invalid) const { return i<M.sep; }
    94       bool operator !=(Invalid) const { return i<M.sep; }
       
    95       ///\e
    79       bool operator ==(Invalid) const { return i>=M.sep; }
    96       bool operator ==(Invalid) const { return i>=M.sep; }
    80     };
    97     };
    81     ///\e
    98     ///\e
    82     class TrueIt
    99     class TrueIt
    83     {
   100     {
    84       const IterableBoolMap &M;
   101       const IterableBoolMap &M;
    85       int i;
   102       int i;
    86     public:
   103     public:
       
   104       ///\e
    87       explicit TrueIt(const IterableBoolMap &_M)
   105       explicit TrueIt(const IterableBoolMap &_M)
    88 	: M(_M), i(M.vals.size()-1) { }
   106 	: M(_M), i(M.vals.size()-1) { }
       
   107       ///\e
    89       TrueIt(Invalid)
   108       TrueIt(Invalid)
    90 	: M(*((IterableBoolMap*)(0))), i(-1) { }
   109 	: M(*((IterableBoolMap*)(0))), i(-1) { }
       
   110       ///\e
    91       TrueIt &operator++() { --i; return *this;}
   111       TrueIt &operator++() { --i; return *this;}
       
   112       ///\e
    92       operator Key() const { return i>=M.sep ? M.vals[i] : INVALID; }
   113       operator Key() const { return i>=M.sep ? M.vals[i] : INVALID; }
       
   114       ///\e
    93       bool operator !=(Invalid) const { return i>=M.sep; }
   115       bool operator !=(Invalid) const { return i>=M.sep; }
       
   116       ///\e
    94       bool operator ==(Invalid) const { return i<M.sep; }
   117       bool operator ==(Invalid) const { return i<M.sep; }
    95     };
   118     };
    96   
   119   
    97     ///\e
   120     ///\e
    98     class RefType
   121     class RefType
   120 	vals.push_back(i->first);
   143 	vals.push_back(i->first);
   121 	sep++;
   144 	sep++;
   122       }
   145       }
   123       if(init) sep=0;
   146       if(init) sep=0;
   124     }
   147     }
       
   148     ///\e
   125     RefType operator[] (Key k) { return RefType(*this,k);}  
   149     RefType operator[] (Key k) { return RefType(*this,k);}  
       
   150     ///\e
   126     Value operator[] (Key k) const { return isTrue(k);}  
   151     Value operator[] (Key k) const { return isTrue(k);}  
   127   };
   152   };
   128 
   153 
   129 
   154 
   130 
   155 
   161     IterableBoolNodeMap(const Graph &g,bool b=false) : cmap(g), imap(cmap,b) {}
   186     IterableBoolNodeMap(const Graph &g,bool b=false) : cmap(g), imap(cmap,b) {}
   162 
   187 
   163   public:
   188   public:
   164     ///\e
   189     ///\e
   165     void set(Key k, bool v) { imap.set(k,v);}
   190     void set(Key k, bool v) { imap.set(k,v);}
       
   191     ///Number of \c true items in the map
       
   192 
       
   193     ///Returns the number of \c true values in the map.
       
   194     ///This is a constant time operation.
       
   195     int countTrue() { return imap.countTrue(); }
       
   196     ///Number of \c false items in the map
       
   197 
       
   198     ///Returns the number of \c false values in the map.
       
   199     ///This is a constant time operation.
       
   200     int countFalse() { return imap.countFalse(); }
   166 #ifdef DOXYGEN
   201 #ifdef DOXYGEN
   167     ///\e
   202     ///\e
   168     bool &operator[](Key k) { return imap[k];}  
   203     bool &operator[](Key k) { return imap[k];}  
   169     ///\e
   204     ///\e
   170     const bool &operator[](Key k) const { return imap[k];}  
   205     const bool &operator[](Key k) const { return imap[k];}  
   174 #endif
   209 #endif
   175     ///Iterator for the "false" nodes
   210     ///Iterator for the "false" nodes
   176     class FalseIt : public BimType::FalseIt
   211     class FalseIt : public BimType::FalseIt
   177     {
   212     {
   178     public:
   213     public:
       
   214       ///\e
   179       explicit FalseIt(const IterableBoolNodeMap &m)
   215       explicit FalseIt(const IterableBoolNodeMap &m)
   180 	: BimType::FalseIt(m.imap) { }
   216 	: BimType::FalseIt(m.imap) { }
       
   217       ///\e
   181       FalseIt(Invalid i) : BimType::FalseIt(i) { }
   218       FalseIt(Invalid i) : BimType::FalseIt(i) { }
   182     };
   219     };
   183     ///Iterator for the "true" nodes
   220     ///Iterator for the "true" nodes
   184     class TrueIt : public BimType::TrueIt
   221     class TrueIt : public BimType::TrueIt
   185     {
   222     {
   186     public:
   223     public:
       
   224       ///\e
   187       explicit TrueIt(const IterableBoolNodeMap &m)
   225       explicit TrueIt(const IterableBoolNodeMap &m)
   188 	: BimType::TrueIt(m.imap) { }
   226 	: BimType::TrueIt(m.imap) { }
       
   227       ///\e
   189       TrueIt(Invalid i) : BimType::TrueIt(i) { }
   228       TrueIt(Invalid i) : BimType::TrueIt(i) { }
   190     };  
   229     };  
   191   };
   230   };
   192 
   231 
   193   /// Iterable bool EdgeMap
   232   /// Iterable bool EdgeMap
   219     IterableBoolEdgeMap(const Graph &g,bool b=false) : cmap(g), imap(cmap,b) {}
   258     IterableBoolEdgeMap(const Graph &g,bool b=false) : cmap(g), imap(cmap,b) {}
   220 
   259 
   221   public:
   260   public:
   222     ///\e
   261     ///\e
   223     void set(Key k, bool v) { imap.set(k,v);}
   262     void set(Key k, bool v) { imap.set(k,v);}
       
   263     ///Returns the number of \c true values in the map.
       
   264     ///This is a constant time operation.
       
   265     int countTrue() { return imap.countTrue(); }
       
   266     ///Number of \c false items in the map
       
   267 
       
   268     ///Returns the number of \c false values in the map.
       
   269     ///This is a constant time operation.
       
   270     int countFalse() { return imap.countFalse(); }
   224 #ifdef DOXYGEN
   271 #ifdef DOXYGEN
   225     ///\e
   272     ///\e
   226     bool &operator[](Key k) { return imap[k];}  
   273     bool &operator[](Key k) { return imap[k];}  
   227     ///\e
   274     ///\e
   228     const bool &operator[](Key k) const { return imap[k];}  
   275     const bool &operator[](Key k) const { return imap[k];}  
   232 #endif
   279 #endif
   233     ///Iterator for the "false" edges
   280     ///Iterator for the "false" edges
   234     class FalseIt : public BimType::FalseIt
   281     class FalseIt : public BimType::FalseIt
   235     {
   282     {
   236     public:
   283     public:
       
   284       ///\e
   237       explicit FalseIt(const IterableBoolEdgeMap &m)
   285       explicit FalseIt(const IterableBoolEdgeMap &m)
   238 	: BimType::FalseIt(m.imap) { }
   286 	: BimType::FalseIt(m.imap) { }
       
   287       ///\e
   239       FalseIt(Invalid i) : BimType::FalseIt(i) { }
   288       FalseIt(Invalid i) : BimType::FalseIt(i) { }
   240     };
   289     };
   241     ///Iterator for the "true" edges
   290     ///Iterator for the "true" edges
   242     class TrueIt : public BimType::TrueIt
   291     class TrueIt : public BimType::TrueIt
   243     {
   292     {
   244     public:
   293     public:
       
   294       ///\e
   245       explicit TrueIt(const IterableBoolEdgeMap &m)
   295       explicit TrueIt(const IterableBoolEdgeMap &m)
   246 	: BimType::TrueIt(m.imap) { }
   296 	: BimType::TrueIt(m.imap) { }
       
   297       ///\e
   247       TrueIt(Invalid i) : BimType::TrueIt(i) { }
   298       TrueIt(Invalid i) : BimType::TrueIt(i) { }
   248     };  
   299     };  
   249   };
   300   };
   250 
   301 
   251 
   302