# HG changeset patch # User deba # Date 1138349927 0 # Node ID 49fe71fce7fb367ce56126544fe5f786aa192f3c # Parent d9205a71132458bf4a1fe832d58c61c3981d9cc2 Making iterable bool map dynamic Changed interface diff -r d9205a711324 -r 49fe71fce7fb demo/graph_orientation.cc --- a/demo/graph_orientation.cc Fri Jan 27 08:17:25 2006 +0000 +++ b/demo/graph_orientation.cc Fri Jan 27 08:18:47 2006 +0000 @@ -70,7 +70,7 @@ ListGraph::NodeMap def(g); //deficiency of the nodes def = subMap(f,InDegMap(g)); - IterableBoolNodeMap active(g,false); + IterableBoolMap active(g,false); for(NodeIt n(g);n!=INVALID;++n) active[n]=(def[n]>0); ListGraph::EdgeMap rev(g,false); // rev[e]==true <=> e is be @@ -79,7 +79,7 @@ int nodeNum=countNodes(g); Node act; - while((act=IterableBoolNodeMap::TrueIt(active))!=INVALID) { + while((act=IterableBoolMap::TrueIt(active))!=INVALID) { std::cout << "Process node " << label[act] << " (def=" << def[act] << " lev=" << level[act] << "): "; diff -r d9205a711324 -r 49fe71fce7fb lemon/iterable_maps.h --- a/lemon/iterable_maps.h Fri Jan 27 08:17:25 2006 +0000 +++ b/lemon/iterable_maps.h Fri Jan 27 08:18:47 2006 +0000 @@ -28,424 +28,311 @@ namespace lemon { - - ///\todo This is only a static map! - ///\todo Undocumented. - ///\param BaseMap is an interger map. - template - class IterableBoolMap - { - public: - - typedef typename BaseMap::Key Key; - typedef bool Value; - - friend class RefType; - friend class FalseIt; - friend class TrueIt; + + ///\ingroup maps + /// + /// \brief Dynamic iterable bool map. + /// + /// This class provides a special graph map type which can store + /// for each graph item(node, edge, etc.) a bool value. For both + /// the true and the false it is possible to iterate on the keys which + /// mapped to the given value. + /// + /// \param _Graph The graph type. + /// \param _Item One of the graph's item type, the key of the map. + template + class IterableBoolMap + : protected ItemSetTraits<_Graph, _Item>::template Map::Parent { + private: + typedef _Graph Graph; + typedef _Item Item; + + typedef typename ItemSetTraits::ItemIt ItemIt; + typedef typename ItemSetTraits + ::template Map::Parent Parent; + + std::vector array; + int sep; + + const Graph& graph; private: - BaseMap &cref; - std::vector vals; - int sep; //map[e] is true <=> cref[e]>=sep - - bool isTrue(Key k) {return cref[k]>=sep;} - void swap(Key k, int s) - { - int ti=cref[k]; - Key tk=vals[s]; - cref[k]=s; vals[s]=k; - cref[tk]=ti; vals[ti]=tk; - } - void setTrue(Key k) { if(cref[k]=sep) { swap(k,sep); sep++; } } - - public: - ///\e - void set(Key k,Value v) { if(v) setTrue(k); else setFalse(k);} - ///Number of \c true items in the map - - ///Returns the number of \c true values in the map. - ///This is a constant time operation. - int countTrue() { return vals.size()-sep; } - ///Number of \c false items in the map - - ///Returns the number of \c false values in the map. - ///This is a constant time operation. - int countFalse() { return sep; } - - ///\e - class FalseIt - { - const IterableBoolMap &M; - int i; - public: - ///\e - explicit FalseIt(const IterableBoolMap &_M) : M(_M), i(0) { } - ///\e - FalseIt(Invalid) - : M(*((IterableBoolMap*)(0))), i(std::numeric_limits::max()) { } - ///\e - FalseIt &operator++() { ++i; return *this;} - ///\e - operator Key() const { return i=M.sep; } - }; - ///\e - class TrueIt - { - const IterableBoolMap &M; - int i; - public: - ///\e - explicit TrueIt(const IterableBoolMap &_M) - : M(_M), i(M.vals.size()-1) { } - ///\e - TrueIt(Invalid) - : M(*((IterableBoolMap*)(0))), i(-1) { } - ///\e - TrueIt &operator++() { --i; return *this;} - ///\e - operator Key() const { return i>=M.sep ? M.vals[i] : INVALID; } - ///\e - bool operator !=(Invalid) const { return i>=M.sep; } - ///\e - bool operator ==(Invalid) const { return i of the - /// given graph \c Graph. - /// In addition, this class provides two iterators called \ref TrueIt - /// and \ref FalseIt to iterate through the "true" and "false" nodes. - template - class IterableBoolNodeMap - { - typename Graph::template NodeMap cmap; - - public: - - typedef IterableBoolMap > BimType; - BimType imap; - - - typedef typename BimType::RefType RefType; - typedef typename Graph::Node Key; - typedef bool Value; - - friend class FalseIt; - friend class TrueIt; - - ///\e - IterableBoolNodeMap(const Graph &g,bool b=false) : cmap(g), imap(cmap,b) {} public: - ///\e - void set(Key k, bool v) { imap.set(k,v);} - ///Number of \c true items in the map - ///Returns the number of \c true values in the map. - ///This is a constant time operation. - int countTrue() { return imap.countTrue(); } - ///Number of \c false items in the map + /// Indicates that the map if reference map. + typedef True ReferenceMapTag; - ///Returns the number of \c false values in the map. - ///This is a constant time operation. - int countFalse() { return imap.countFalse(); } -#ifdef DOXYGEN - ///\e - bool &operator[](Key k) { return imap[k];} - ///\e - const bool &operator[](Key k) const { return imap[k];} -#else - Value operator[](Key k) const { return imap[k];} - RefType operator[](Key k) { return imap[k];} -#endif - ///Iterator for the "false" nodes - class FalseIt : public BimType::FalseIt - { + /// The key type + typedef Item Key; + /// The value type + typedef bool Value; + /// The const reference type. + typedef const Value& ConstReference; + + + /// \brief Refernce to the value of the map. + /// + /// This class is near to similar to the bool type. It can + /// be converted to bool and it has the same operators. + class Reference { + friend class IterableBoolMap; + private: + Reference(IterableBoolMap& map, const Key& key) + : _key(key), _map(map) {} public: - ///\e - explicit FalseIt(const IterableBoolNodeMap &m) - : BimType::FalseIt(m.imap) { } - ///\e - FalseIt(Invalid i) : BimType::FalseIt(i) { } + + Reference& operator=(const Reference& value) { + _map.set(_key, (bool)value); + return *this; + } + + operator bool() const { + return static_cast(_map)[_key]; + } + + Reference& operator=(bool value) { + _map.set(_key, value); + return *this; + } + Reference& operator&=(bool value) { + _map.set(_key, _map[_key] & value); + return *this; + } + Reference& operator|=(bool value) { + _map.set(_key, _map[_key] | value); + return *this; + } + Reference& operator^=(bool value) { + _map.set(_key, _map[_key] ^ value); + return *this; + } + private: + Key _key; + IterableBoolMap& _map; }; - ///Iterator for the "true" nodes - class TrueIt : public BimType::TrueIt - { + + /// \brief Constructor of the Map with a default value. + /// + /// Constructor of the Map with a default value. + IterableBoolMap(const Graph& _graph, bool def = false) + : Parent(_graph), graph(_graph) { + for (ItemIt it(graph); it != INVALID; ++it) { + Parent::set(it, array.size()); + array.push_back(it); + } + sep = (def ? array.size() : 0); + } + + /// \brief Const subscript operator of the map. + /// + /// Const subscript operator of the map. + bool operator[](const Item& item) const { + return position(item) < sep; + } + + /// \brief Subscript operator of the map. + /// + /// Subscript operator of the map. + Reference operator[](const Item& item) { + return Reference(*this, item); + } + + /// \brief Set operation of the map. + /// + /// Set operation of the map. + void set(const Item& item, bool value) { + int pos = position(item); + if (value) { + if (pos < sep) return; + Item tmp = array[sep]; + array[sep] = item; + Parent::set(item, sep); + array[pos] = tmp; + Parent::set(tmp, pos); + ++sep; + } else { + if (pos >= sep) return; + --sep; + Item tmp = array[sep]; + array[sep] = item; + Parent::set(item, sep); + array[pos] = tmp; + Parent::set(tmp, pos); + } + } + + /// \brief Returns the number of the items mapped to true. + /// + /// Returns the number of the items mapped to true. + int trueNum() const { + return sep; + } + + /// \brief Returns the number of the items mapped to false. + /// + /// Returns the number of the items mapped to false. + int falseNum() const { + return array.size() - sep; + } + + /// \brief Iterator for the keys mapped to true. + /// + /// Iterator for the keys mapped to true. It works + /// like a graph item iterator in the map, it can be converted + /// the item type of the map, incremented with \c ++ operator, and + /// if the iterator leave the last valid item it will be equal to + /// \c INVALID. + class TrueIt : public Item { public: - ///\e - explicit TrueIt(const IterableBoolNodeMap &m) - : BimType::TrueIt(m.imap) { } - ///\e - TrueIt(Invalid i) : BimType::TrueIt(i) { } - }; + typedef Item Parent; + + /// \brief Creates an iterator. + /// + /// Creates an iterator. It iterates on the + /// keys which mapped to true. + /// \param map The IterableIntMap + TrueIt(const IterableBoolMap& _map) + : Parent(_map.sep > 0 ? _map.array[_map.sep - 1] : INVALID), + map(&_map) {} + + /// \brief Invalid constructor \& conversion. + /// + /// This constructor initializes the item to be invalid. + /// \sa Invalid for more details. + TrueIt(Invalid) : Parent(INVALID), map(0) {} + + /// \brief Increment operator. + /// + /// Increment Operator. + TrueIt& operator++() { + int pos = map->position(*this); + Parent::operator=(pos > 0 ? map->array[pos - 1] : INVALID); + return *this; + } + + + private: + const IterableBoolMap* map; + }; + + /// \brief Iterator for the keys mapped to false. + /// + /// Iterator for the keys mapped to false. It works + /// like a graph item iterator in the map, it can be converted + /// the item type of the map, incremented with \c ++ operator, and + /// if the iterator leave the last valid item it will be equal to + /// \c INVALID. + class FalseIt : public Item { + public: + typedef Item Parent; + + /// \brief Creates an iterator. + /// + /// Creates an iterator. It iterates on the + /// keys which mapped to false. + /// \param map The IterableIntMap + FalseIt(const IterableBoolMap& _map) + : Parent(_map.sep < _map.array.size() ? _map.array.back() : INVALID), + map(&_map) {} + + /// \brief Invalid constructor \& conversion. + /// + /// This constructor initializes the item to be invalid. + /// \sa Invalid for more details. + FalseIt(Invalid) : Parent(INVALID), map(0) {} + + /// \brief Increment operator. + /// + /// Increment Operator. + FalseIt& operator++() { + int pos = map->position(*this); + Parent::operator=(pos > map->sep ? map->array[pos - 1] : INVALID); + return *this; + } + + private: + const IterableBoolMap* map; + }; + + protected: + + virtual void add(const Item& item) { + Parent::add(item); + Parent::set(item, array.size()); + array.push_back(item); + } + + virtual void add(const std::vector& items) { + Parent::add(items); + for (int i = 0; i < (int)items.size(); ++i) { + Parent::set(items[i], array.size()); + array.push_back(items[i]); + } + } + + virtual void erase(const Item& item) { + int pos = position(item); + if (pos < sep) { + --sep; + Parent::set(array[sep], pos); + array[pos] = array[sep]; + Parent::set(array.back(), sep); + array[sep] = array.back(); + array.pop_back(); + } else { + Parent::set(array.back(), pos); + array[pos] = array.back(); + array.pop_back(); + } + Parent::erase(item); + } + + virtual void erase(const std::vector& items) { + for (int i = 0; i < (int)items.size(); ++i) { + int pos = position(items[i]); + if (pos < sep) { + --sep; + Parent::set(array[sep], pos); + array[pos] = array[sep]; + Parent::set(array.back(), sep); + array[sep] = array.back(); + array.pop_back(); + } else { + Parent::set(array.back(), pos); + array[pos] = array.back(); + array.pop_back(); + } + } + Parent::erase(items); + } + + virtual void build() { + Parent::build(); + for (ItemIt it(graph); it != INVALID; ++it) { + Parent::set(it, array.size()); + array.push_back(it); + } + sep = 0; + } + + virtual void clear() { + array.clear(); + sep = 0; + Parent::clear(); + } + }; - - /// Iterable bool UpperNodeMap - - /// This map can be used in the same way - /// as the standard UpperNodeMap of the - /// given graph \c Graph. - /// In addition, this class provides two iterators called \ref TrueIt - /// and \ref FalseIt to iterate through the "true" and "false" nodes. - template - class IterableBoolUpperNodeMap - { - typename Graph::template UpperNodeMap cmap; - public: - - typedef IterableBoolMap > BimType; - BimType imap; - - - typedef typename BimType::RefType RefType; - typedef typename Graph::Node Key; - typedef bool Value; - - friend class FalseIt; - friend class TrueIt; - - ///\e - IterableBoolUpperNodeMap(const Graph &g,bool b=false) : cmap(g), imap(cmap,b) {} - - public: - ///\e - void set(Key k, bool v) { imap.set(k,v);} - ///Number of \c true items in the map - - ///Returns the number of \c true values in the map. - ///This is a constant time operation. - int countTrue() { return imap.countTrue(); } - ///Number of \c false items in the map - - ///Returns the number of \c false values in the map. - ///This is a constant time operation. - int countFalse() { return imap.countFalse(); } -#ifdef DOXYGEN - ///\e - bool &operator[](Key k) { return imap[k];} - ///\e - const bool &operator[](Key k) const { return imap[k];} -#else - Value operator[](Key k) const { return imap[k];} - RefType operator[](Key k) { return imap[k];} -#endif - ///Iterator for the "false" nodes - class FalseIt : public BimType::FalseIt - { - public: - ///\e - explicit FalseIt(const IterableBoolUpperNodeMap &m) - : BimType::FalseIt(m.imap) { } - ///\e - FalseIt(Invalid i) : BimType::FalseIt(i) { } - }; - ///Iterator for the "true" nodes - class TrueIt : public BimType::TrueIt - { - public: - ///\e - explicit TrueIt(const IterableBoolUpperNodeMap &m) - : BimType::TrueIt(m.imap) { } - ///\e - TrueIt(Invalid i) : BimType::TrueIt(i) { } - }; - }; - - /// Iterable bool LowerNodeMap - - /// This map can be used in the same way - /// as the standard LowerNodeMap of the - /// given graph \c Graph. - /// In addition, this class provides two iterators called \ref TrueIt - /// and \ref FalseIt to iterate through the "true" and "false" nodes. - template - class IterableBoolLowerNodeMap - { - typename Graph::template LowerNodeMap cmap; - - public: - - typedef IterableBoolMap > BimType; - BimType imap; - - - typedef typename BimType::RefType RefType; - typedef typename Graph::Node Key; - typedef bool Value; - - friend class FalseIt; - friend class TrueIt; - - ///\e - IterableBoolLowerNodeMap(const Graph &g,bool b=false) : cmap(g), imap(cmap,b) {} - - public: - ///\e - void set(Key k, bool v) { imap.set(k,v);} - ///Number of \c true items in the map - - ///Returns the number of \c true values in the map. - ///This is a constant time operation. - int countTrue() { return imap.countTrue(); } - ///Number of \c false items in the map - - ///Returns the number of \c false values in the map. - ///This is a constant time operation. - int countFalse() { return imap.countFalse(); } -#ifdef DOXYGEN - ///\e - bool &operator[](Key k) { return imap[k];} - ///\e - const bool &operator[](Key k) const { return imap[k];} -#else - Value operator[](Key k) const { return imap[k];} - RefType operator[](Key k) { return imap[k];} -#endif - ///Iterator for the "false" nodes - class FalseIt : public BimType::FalseIt - { - public: - ///\e - explicit FalseIt(const IterableBoolLowerNodeMap &m) - : BimType::FalseIt(m.imap) { } - ///\e - FalseIt(Invalid i) : BimType::FalseIt(i) { } - }; - ///Iterator for the "true" nodes - class TrueIt : public BimType::TrueIt - { - public: - ///\e - explicit TrueIt(const IterableBoolLowerNodeMap &m) - : BimType::TrueIt(m.imap) { } - ///\e - TrueIt(Invalid i) : BimType::TrueIt(i) { } - }; - }; - - /// Iterable bool EdgeMap - - /// This map can be used in the same way - /// as the standard EdgeMap of the - /// given graph \c Graph. - /// In addition, this class provides two iterators called \ref TrueIt - /// and \ref FalseIt to iterate through the "true" and "false" edges. - template - class IterableBoolEdgeMap - { - typename Graph::template EdgeMap cmap; - - public: - - typedef IterableBoolMap > BimType; - BimType imap; - - - typedef typename BimType::RefType RefType; - typedef typename Graph::Edge Key; - typedef bool Value; - - friend class FalseIt; - friend class TrueIt; - - ///\e - IterableBoolEdgeMap(const Graph &g,bool b=false) : cmap(g), imap(cmap,b) {} - - public: - ///\e - void set(Key k, bool v) { imap.set(k,v);} - ///Returns the number of \c true values in the map. - ///This is a constant time operation. - int countTrue() { return imap.countTrue(); } - ///Number of \c false items in the map - - ///Returns the number of \c false values in the map. - ///This is a constant time operation. - int countFalse() { return imap.countFalse(); } -#ifdef DOXYGEN - ///\e - bool &operator[](Key k) { return imap[k];} - ///\e - const bool &operator[](Key k) const { return imap[k];} -#else - Value operator[](Key k) const { return imap[k];} - RefType operator[](Key k) { return imap[k];} -#endif - ///Iterator for the "false" edges - class FalseIt : public BimType::FalseIt - { - public: - ///\e - explicit FalseIt(const IterableBoolEdgeMap &m) - : BimType::FalseIt(m.imap) { } - ///\e - FalseIt(Invalid i) : BimType::FalseIt(i) { } - }; - ///Iterator for the "true" edges - class TrueIt : public BimType::TrueIt - { - public: - ///\e - explicit TrueIt(const IterableBoolEdgeMap &m) - : BimType::TrueIt(m.imap) { } - ///\e - TrueIt(Invalid i) : BimType::TrueIt(i) { } - }; - }; - namespace _iterable_maps_bits { template struct IterableIntMapNode { - IterableIntMapNode() {} + IterableIntMapNode() : value(-1) {} IterableIntMapNode(int _value) : value(_value) {} Item prev, next; int value; @@ -482,7 +369,7 @@ /// /// Constructor of the Map. It set all values -1. explicit IterableIntMap(const Graph& graph) - : Parent(graph, _iterable_maps_bits::IterableIntMapNode<_Item>(-1)) {} + : Parent(graph) {} /// \brief Constructor of the Map with a given value. /// @@ -706,6 +593,13 @@ Parent::erase(key); } + virtual void erase(const std::vector& keys) { + for (int i = 0; i < keys.size(); ++i) { + unlace(keys[i]); + } + Parent::erase(keys); + } + virtual void clear() { first.clear(); Parent::clear();