lemon/iterable_maps.h
changeset 1812 a6f019fa6e7a
parent 1805 d284f81f02a5
child 1873 d73c7f115f53
equal deleted inserted replaced
4:42dc0c7ee65e 5:c2d9473f5782
   134   
   134   
   135   public:
   135   public:
   136     explicit IterableBoolMap(BaseMap &_m,bool init=false) : cref(_m)
   136     explicit IterableBoolMap(BaseMap &_m,bool init=false) : cref(_m)
   137     {
   137     {
   138       sep=0;
   138       sep=0;
   139       for(typename BaseMap::MapSet::iterator i=cref.mapSet().begin();
   139       for(typename BaseMap::MapIt i(cref);i!=INVALID; ++i) {
   140 	  i!=cref.mapSet().end();
   140 	i.set(sep);
   141 	  ++i) {
   141 	vals.push_back(i);
   142 	i->second=sep;
       
   143 	vals.push_back(i->first);
       
   144 	sep++;
   142 	sep++;
   145       }
   143       }
   146       if(init) sep=0;
   144       if(init) sep=0;
   147     }
   145     }
   148     ///\e
   146     ///\e
   301 
   299 
   302 
   300 
   303   namespace _iterable_maps_bits {
   301   namespace _iterable_maps_bits {
   304     template <typename Item>
   302     template <typename Item>
   305     struct IterableIntMapNode {
   303     struct IterableIntMapNode {
   306       IterableIntMapNode() : value(-1) {}
   304       IterableIntMapNode() {}
       
   305       IterableIntMapNode(int _value) : value(_value) {}
   307       Item prev, next;
   306       Item prev, next;
   308       int value;
   307       int value;
   309     };
   308     };
   310   }
   309   }
   311 
   310 
   312   ///\ingroup maps
   311   ///\ingroup maps
   313   ///
   312   ///
   314   /// \brief Dynamic iterable integer map.
   313   /// \brief Dynamic iterable integer map.
   315   ///
   314   ///
   316   /// \todo Document please
   315   /// This class provides a special graph map type which can store
       
   316   /// for each graph item(node, edge, etc.) an integer value. For each
       
   317   /// non negative value it is possible to iterate on the keys which
       
   318   /// mapped to the given value.
       
   319   /// 
       
   320   /// \param _Graph The graph type.
       
   321   /// \param _Item One of the graph's item type, the key of the map.
   317   template <typename _Graph, typename _Item>
   322   template <typename _Graph, typename _Item>
   318   class IterableIntMap : protected ItemSetTraits<_Graph, _Item> 
   323   class IterableIntMap : protected ItemSetTraits<_Graph, _Item> 
   319   ::template Map<_iterable_maps_bits::IterableIntMapNode<_Item> >::Parent {
   324   ::template Map<_iterable_maps_bits::IterableIntMapNode<_Item> >::Parent {
   320   public:
   325   public:
   321     typedef typename ItemSetTraits<_Graph, _Item> 
   326     typedef typename ItemSetTraits<_Graph, _Item> 
   322     ::template Map<_iterable_maps_bits::IterableIntMapNode<_Item> >
   327     ::template Map<_iterable_maps_bits::IterableIntMapNode<_Item> >
   323     ::Parent Parent;
   328     ::Parent Parent;
   324 
   329 
       
   330     /// The key type
   325     typedef _Item Key;
   331     typedef _Item Key;
       
   332     /// The value type
   326     typedef int Value;
   333     typedef int Value;
       
   334     /// The graph type
   327     typedef _Graph Graph;
   335     typedef _Graph Graph;
   328 
   336 
   329     explicit IterableIntMap(const Graph& graph) : Parent(graph) {}
   337     /// \brief Constructor of the Map.
       
   338     ///
       
   339     /// Constructor of the Map. It set all values -1.
       
   340     explicit IterableIntMap(const Graph& graph) 
       
   341       : Parent(graph, _iterable_maps_bits::IterableIntMapNode<_Item>(-1)) {}
       
   342 
       
   343     /// \brief Constructor of the Map with a given value.
       
   344     ///
       
   345     /// Constructor of the Map with a given value.
       
   346     explicit IterableIntMap(const Graph& graph, int value) 
       
   347       : Parent(graph, _iterable_maps_bits::IterableIntMapNode<_Item>(value)) {
       
   348       if (value >= 0) {
       
   349 	for (typename Parent::ItemIt it(*this); it != INVALID; ++it) {
       
   350 	  lace(it);
       
   351 	}
       
   352       }
       
   353     }
   330 
   354 
   331   private:
   355   private:
   332     
   356     
   333     void unlace(const Key& key) {
   357     void unlace(const Key& key) {
   334       typename Parent::Value& node = Parent::operator[](key);
   358       typename Parent::Value& node = Parent::operator[](key);
   360       first[node.value] = key;
   384       first[node.value] = key;
   361     }
   385     }
   362 
   386 
   363   public:
   387   public:
   364 
   388 
       
   389     /// Indicates that the map if reference map.
   365     typedef True ReferenceMapTag;
   390     typedef True ReferenceMapTag;
   366 
   391 
       
   392     /// \brief Refernce to the value of the map.
       
   393     ///
       
   394     /// This class is near to similar to the int type. It can
       
   395     /// be converted to int and it has the same operators.
   367     class Reference {
   396     class Reference {
   368       friend class IterableIntMap;
   397       friend class IterableIntMap;
   369     private:
   398     private:
   370       Reference(IterableIntMap& map, const Key& key) 
   399       Reference(IterableIntMap& map, const Key& key) 
   371 	: _key(key), _map(map) {} 
   400 	: _key(key), _map(map) {} 
   445 
   474 
   446     private:
   475     private:
   447       Key _key;
   476       Key _key;
   448       IterableIntMap& _map; 
   477       IterableIntMap& _map; 
   449     };
   478     };
   450     
   479 
       
   480     /// The const reference type.    
   451     typedef const Value& ConstReference;
   481     typedef const Value& ConstReference;
   452 
   482 
       
   483     /// \brief Gives back the maximal value plus one.
       
   484     ///
       
   485     /// Gives back the maximal value plus one.
   453     int size() const {
   486     int size() const {
   454       return (int)first.size();
   487       return (int)first.size();
   455     }
   488     }
   456     
   489     
       
   490     /// \brief Set operation of the map.
       
   491     ///
       
   492     /// Set operation of the map.
   457     void set(const Key& key, const Value& value) {
   493     void set(const Key& key, const Value& value) {
   458       unlace(key);
   494       unlace(key);
   459       Parent::operator[](key).value = value;
   495       Parent::operator[](key).value = value;
   460       lace(key);
   496       lace(key);
   461     }
   497     }
   462 
   498 
       
   499     /// \brief Const subscript operator of the map.
       
   500     ///
       
   501     /// Const subscript operator of the map.
   463     const Value& operator[](const Key& key) const {
   502     const Value& operator[](const Key& key) const {
   464       return Parent::operator[](key).value;
   503       return Parent::operator[](key).value;
   465     }
   504     }
   466 
   505 
       
   506     /// \brief Subscript operator of the map.
       
   507     ///
       
   508     /// Subscript operator of the map.
   467     Reference operator[](const Key& key) {
   509     Reference operator[](const Key& key) {
   468       return Reference(*this, key);
   510       return Reference(*this, key);
   469     }
   511     }
   470 
   512 
       
   513     /// \brief Iterator for the keys with the same value.
       
   514     ///
       
   515     /// Iterator for the keys with the same value. It works
       
   516     /// like a graph item iterator in the map, it can be converted
       
   517     /// the item type of the map, incremented with \c ++ operator, and
       
   518     /// if the iterator leave the last valid item it will be equal to 
       
   519     /// \c INVALID.
   471     class ItemIt : public _Item {
   520     class ItemIt : public _Item {
   472     public:
   521     public:
   473       typedef _Item Parent;
   522       typedef _Item Parent;
   474 
   523 
       
   524       /// \brief Invalid constructor \& conversion.
       
   525       ///
       
   526       /// This constructor initializes the item to be invalid.
       
   527       /// \sa Invalid for more details.
   475       ItemIt(Invalid) : Parent(INVALID), _map(0) {}
   528       ItemIt(Invalid) : Parent(INVALID), _map(0) {}
   476 
   529 
       
   530       /// \brief Creates an iterator with a value.
       
   531       ///
       
   532       /// Creates an iterator with a value. It iterates on the 
       
   533       /// keys which have the given value.
       
   534       /// \param map The IterableIntMap
       
   535       /// \param value The value
   477       ItemIt(const IterableIntMap& map, int value) : _map(&map) {
   536       ItemIt(const IterableIntMap& map, int value) : _map(&map) {
   478 	if (value < 0 || value >= (int)_map->first.size()) {	  
   537 	if (value < 0 || value >= (int)_map->first.size()) {	  
   479 	  Parent::operator=(INVALID);
   538 	  Parent::operator=(INVALID);
   480 	} else {
   539 	} else {
   481 	  Parent::operator=(_map->first[value]);
   540 	  Parent::operator=(_map->first[value]);
   482 	}
   541 	}
   483       } 
   542       } 
   484 
   543 
       
   544       /// \brief Increment operator.
       
   545       ///
       
   546       /// Increment Operator.
   485       ItemIt& operator++() {
   547       ItemIt& operator++() {
   486 	Parent::operator=(_map->IterableIntMap::Parent::
   548 	Parent::operator=(_map->IterableIntMap::Parent::
   487 			  operator[](static_cast<Parent&>(*this)).next);
   549 			  operator[](static_cast<Parent&>(*this)).next);
   488 	return *this;
   550 	return *this;
   489       }
   551       }