iterable_maps.h header hes been added. Up to now it contains an iterable bool
authoralpar
Mon, 12 Sep 2005 05:31:55 +0000
changeset 1677a9f923a4d998
parent 1676 c3e416514759
child 1678 b7b7125a5fe8
iterable_maps.h header hes been added. Up to now it contains an iterable bool
map and specialized versions for Node and Edge maps.
lemon/Makefile.am
lemon/iterable_maps.h
     1.1 --- a/lemon/Makefile.am	Thu Sep 08 14:35:22 2005 +0000
     1.2 +++ b/lemon/Makefile.am	Mon Sep 12 05:31:55 2005 +0000
     1.3 @@ -35,6 +35,7 @@
     1.4  	graph_utils.h \
     1.5  	graph_to_eps.h \
     1.6  	invalid.h \
     1.7 +	iterable_maps.h \
     1.8  	kruskal.h \
     1.9  	list_graph.h \
    1.10  	lp.h \
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/lemon/iterable_maps.h	Mon Sep 12 05:31:55 2005 +0000
     2.3 @@ -0,0 +1,250 @@
     2.4 +/* -*- C++ -*-
     2.5 + * lemon/iterable_maps.h - Part of LEMON, a generic C++ optimization library
     2.6 + *
     2.7 + * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     2.8 + * (Egervary Research Group on Combinatorial Optimization, EGRES).
     2.9 + *
    2.10 + * Permission to use, modify and distribute this software is granted
    2.11 + * provided that this copyright notice appears in all copies. For
    2.12 + * precise terms see the accompanying LICENSE file.
    2.13 + *
    2.14 + * This software is provided "AS IS" with no warranty of any kind,
    2.15 + * express or implied, and with no claim as to its suitability for any
    2.16 + * purpose.
    2.17 + *
    2.18 + */
    2.19 +
    2.20 +#include <vector>
    2.21 +#include <limits>
    2.22 +
    2.23 +///\ingroup maps
    2.24 +///\file
    2.25 +///\brief Maps that makes it possible to iterate through the keys having
    2.26 +///a certain value
    2.27 +///
    2.28 +///
    2.29 +
    2.30 +
    2.31 +namespace lemon {
    2.32 +  
    2.33 +  ///\todo This is only a static map!
    2.34 +  ///\param BaseMap is an interger map.
    2.35 +  template<class BaseMap>
    2.36 +  class IterableBoolMap
    2.37 +  {
    2.38 +  public:
    2.39 +  
    2.40 +    typedef typename BaseMap::Key Key;
    2.41 +    typedef bool Value;
    2.42 +  
    2.43 +    friend class RefType;
    2.44 +    friend class FalseIt;
    2.45 +    friend class TrueIt;
    2.46 +
    2.47 +  private:
    2.48 +    BaseMap &cref;
    2.49 +    std::vector<Key> vals;
    2.50 +    int sep;           //map[e] is true <=> cref[e]>=sep
    2.51 +  
    2.52 +    bool isTrue(Key k) {return cref[k]>=sep;}
    2.53 +    void swap(Key k, int s) 
    2.54 +    {
    2.55 +      int ti=cref[k];
    2.56 +      Key tk=vals[s];
    2.57 +      cref[k]=s; vals[s]=k;
    2.58 +      cref[tk]=ti; vals[ti]=tk;
    2.59 +    }  
    2.60 +
    2.61 +    void setTrue(Key k) { if(cref[k]<sep) { sep--; swap(k,sep); } }
    2.62 +    void setFalse(Key k) { if(cref[k]>=sep) { swap(k,sep); sep++; } }
    2.63 +  
    2.64 +  public:
    2.65 +    ///\e
    2.66 +    void set(Key k,Value v) { if(v) setTrue(k); else setFalse(k);}
    2.67 +
    2.68 +    ///\e
    2.69 +    class FalseIt
    2.70 +    {
    2.71 +      const IterableBoolMap &M;
    2.72 +      int i;
    2.73 +    public:
    2.74 +      explicit FalseIt(const IterableBoolMap &_M) : M(_M), i(0) { }
    2.75 +      FalseIt(Invalid)
    2.76 +	: M(*((IterableBoolMap*)(0))), i(std::numeric_limits<int>::max()) { }
    2.77 +      FalseIt &operator++() { ++i; return *this;}
    2.78 +      operator Key() const { return i<M.sep ? M.vals[i] : INVALID; }
    2.79 +      bool operator !=(Invalid) const { return i<M.sep; }
    2.80 +      bool operator ==(Invalid) const { return i>=M.sep; }
    2.81 +    };
    2.82 +    ///\e
    2.83 +    class TrueIt
    2.84 +    {
    2.85 +      const IterableBoolMap &M;
    2.86 +      int i;
    2.87 +    public:
    2.88 +      explicit TrueIt(const IterableBoolMap &_M)
    2.89 +	: M(_M), i(M.vals.size()-1) { }
    2.90 +      TrueIt(Invalid)
    2.91 +	: M(*((IterableBoolMap*)(0))), i(-1) { }
    2.92 +      TrueIt &operator++() { --i; return *this;}
    2.93 +      operator Key() const { return i>=M.sep ? M.vals[i] : INVALID; }
    2.94 +      bool operator !=(Invalid) const { return i>=M.sep; }
    2.95 +      bool operator ==(Invalid) const { return i<M.sep; }
    2.96 +    };
    2.97 +  
    2.98 +    ///\e
    2.99 +    class RefType
   2.100 +    {
   2.101 +      IterableBoolMap &M;
   2.102 +      Key k;
   2.103 +    public:
   2.104 +      RefType(IterableBoolMap &_M,Key _k) : M(_M), k(_k) { }
   2.105 +    
   2.106 +      operator Value() const 
   2.107 +      {
   2.108 +	return M.isTrue(k);
   2.109 +      }
   2.110 +      Value operator = (Value v) const { M.set(k,v); return v; }
   2.111 +    };
   2.112 +  
   2.113 +  public:
   2.114 +    explicit IterableBoolMap(BaseMap &_m,bool init=false) : cref(_m)
   2.115 +    {
   2.116 +      sep=0;
   2.117 +      for(typename BaseMap::MapSet::iterator i=cref.mapSet().begin();
   2.118 +	  i!=cref.mapSet().end();
   2.119 +	  ++i) {
   2.120 +	i->second=sep;
   2.121 +	vals.push_back(i->first);
   2.122 +	sep++;
   2.123 +      }
   2.124 +      if(init) sep=0;
   2.125 +    }
   2.126 +    RefType operator[] (Key k) { return RefType(*this,k);}  
   2.127 +    Value operator[] (Key k) const { return isTrue(k);}  
   2.128 +  };
   2.129 +
   2.130 +
   2.131 +
   2.132 +
   2.133 +  /// \addtogroup graph_maps
   2.134 +  /// @{
   2.135 +
   2.136 +  /// Iterable bool NodeMap
   2.137 +
   2.138 +  /// This map can be used in the same way
   2.139 +  /// as the standard NodeMap<bool> of the
   2.140 +  /// given graph \c Graph. 
   2.141 +  /// In addition, this class provides two iterators called \ref TrueIt
   2.142 +  /// and \ref FalseIt to iterate through the "true" and "false" nodes.
   2.143 +  template <class Graph>
   2.144 +  class IterableBoolNodeMap
   2.145 +  {
   2.146 +    typename Graph::NodeMap<int> cmap;
   2.147 +  
   2.148 +  public:
   2.149 +  
   2.150 +    typedef IterableBoolMap<typename Graph::NodeMap<int> > BimType;
   2.151 +    BimType imap;
   2.152 +
   2.153 +
   2.154 +    typedef typename BimType::RefType RefType;
   2.155 +    typedef typename Graph::Node Key;
   2.156 +    typedef bool Value;
   2.157 +  
   2.158 +    friend class FalseIt;
   2.159 +    friend class TrueIt;
   2.160 +  
   2.161 +    ///\e
   2.162 +    IterableBoolNodeMap(const Graph &g,bool b=false) : cmap(g), imap(cmap,b) {}
   2.163 +
   2.164 +  public:
   2.165 +    ///\e
   2.166 +    void set(Key k, bool v) { imap.set(k,v);}
   2.167 +#ifdef DOXYGEN
   2.168 +    ///\e
   2.169 +    bool &operator[](Key k) { return imap[k];}  
   2.170 +    ///\e
   2.171 +    const bool &operator[](Key k) const { return imap[k];}  
   2.172 +#else
   2.173 +    Value operator[](Key k) const { return imap[k];}  
   2.174 +    RefType operator[](Key k) { return imap[k];}  
   2.175 +#endif
   2.176 +    ///Iterator for the "false" nodes
   2.177 +    class FalseIt : public BimType::FalseIt
   2.178 +    {
   2.179 +    public:
   2.180 +      explicit FalseIt(const IterableBoolNodeMap &m)
   2.181 +	: BimType::FalseIt(m.imap) { }
   2.182 +      FalseIt(Invalid i) : BimType::FalseIt(i) { }
   2.183 +    };
   2.184 +    ///Iterator for the "true" nodes
   2.185 +    class TrueIt : public BimType::TrueIt
   2.186 +    {
   2.187 +    public:
   2.188 +      explicit TrueIt(const IterableBoolNodeMap &m)
   2.189 +	: BimType::TrueIt(m.imap) { }
   2.190 +      TrueIt(Invalid i) : BimType::TrueIt(i) { }
   2.191 +    };  
   2.192 +  };
   2.193 +
   2.194 +  /// Iterable bool EdgeMap
   2.195 +
   2.196 +  /// This map can be used in the same way
   2.197 +  /// as the standard EdgeMap<bool> of the
   2.198 +  /// given graph \c Graph. 
   2.199 +  /// In addition, this class provides two iterators called \ref TrueIt
   2.200 +  /// and \ref FalseIt to iterate through the "true" and "false" edges.
   2.201 +  template <class Graph>
   2.202 +  class IterableBoolEdgeMap
   2.203 +  {
   2.204 +    typename Graph::EdgeMap<int> cmap;
   2.205 +  
   2.206 +  public:
   2.207 +  
   2.208 +    typedef IterableBoolMap<typename Graph::EdgeMap<int> > BimType;
   2.209 +    BimType imap;
   2.210 +
   2.211 +
   2.212 +    typedef typename BimType::RefType RefType;
   2.213 +    typedef typename Graph::Edge Key;
   2.214 +    typedef bool Value;
   2.215 +  
   2.216 +    friend class FalseIt;
   2.217 +    friend class TrueIt;
   2.218 +  
   2.219 +    ///\e
   2.220 +    IterableBoolEdgeMap(const Graph &g,bool b=false) : cmap(g), imap(cmap,b) {}
   2.221 +
   2.222 +  public:
   2.223 +    ///\e
   2.224 +    void set(Key k, bool v) { imap.set(k,v);}
   2.225 +#ifdef DOXYGEN
   2.226 +    ///\e
   2.227 +    bool &operator[](Key k) { return imap[k];}  
   2.228 +    ///\e
   2.229 +    const bool &operator[](Key k) const { return imap[k];}  
   2.230 +#else
   2.231 +    Value operator[](Key k) const { return imap[k];}  
   2.232 +    RefType operator[](Key k) { return imap[k];}  
   2.233 +#endif
   2.234 +    ///Iterator for the "false" edges
   2.235 +    class FalseIt : public BimType::FalseIt
   2.236 +    {
   2.237 +    public:
   2.238 +      explicit FalseIt(const IterableBoolEdgeMap &m)
   2.239 +	: BimType::FalseIt(m.imap) { }
   2.240 +      FalseIt(Invalid i) : BimType::FalseIt(i) { }
   2.241 +    };
   2.242 +    ///Iterator for the "true" edges
   2.243 +    class TrueIt : public BimType::TrueIt
   2.244 +    {
   2.245 +    public:
   2.246 +      explicit TrueIt(const IterableBoolEdgeMap &m)
   2.247 +	: BimType::TrueIt(m.imap) { }
   2.248 +      TrueIt(Invalid i) : BimType::TrueIt(i) { }
   2.249 +    };  
   2.250 +  };
   2.251 +
   2.252 +  /// @}
   2.253 +}