COIN-OR::LEMON - Graph Library

Changeset 2039:dacc4ce9474d in lemon-0.x


Ignore:
Timestamp:
04/06/06 11:33:29 (18 years ago)
Author:
Balazs Dezso
Branch:
default
Phase:
public
Convert:
svn:c9d7d8f5-90d6-0310-b91f-818b3a526b0e/lemon/trunk@2678
Message:

Commiting The DynamicAsymMatrixMap? from Nagy Jano

+ MatrixMapTraits?

Files:
3 edited

Legend:

Unmodified
Added
Removed
  • lemon/bits/traits.h

    r1996 r2039  
    240240 };
    241241
     242  template <typename MatrixMap, typename Enable = void>
     243  struct MatrixMapTraits {
     244    typedef False ReferenceMapTag;
     245
     246    typedef typename MatrixMap::FirstKey FirstKey;
     247    typedef typename MatrixMap::SecondKey SecondKey;
     248    typedef typename MatrixMap::Value Value;
     249
     250    typedef const Value ConstReturnValue;
     251    typedef const Value ReturnValue;
     252  };
     253
     254  template <typename MatrixMap>
     255  struct MatrixMapTraits<
     256    MatrixMap, typename enable_if<typename MatrixMap::ReferenceMapTag,
     257                                  void>::type >
     258  {
     259    typedef True ReferenceMapTag;
     260   
     261    typedef typename MatrixMap::FirstKey FirstKey;
     262    typedef typename MatrixMap::SecondKey SecondKey;
     263    typedef typename MatrixMap::Value Value;
     264
     265    typedef typename MatrixMap::ConstReference ConstReturnValue;
     266    typedef typename MatrixMap::Reference ReturnValue;
     267
     268    typedef typename MatrixMap::ConstReference ConstReference;
     269    typedef typename MatrixMap::Reference Reference;
     270 };
     271
    242272  // Indicators for the tags
    243273
  • lemon/matrix_maps.h

    r1999 r2039  
    2525#include <lemon/maps.h>
    2626
     27#include <lemon/concept/matrix_maps.h>
    2728
    2829/// \file
     
    3839  /// Map for the coloumn view of the matrix.
    3940  template <typename _MatrixMap>
    40   class MatrixRowMap : public MapTraits<_MatrixMap> {
     41  class MatrixRowMap : public MatrixMapTraits<_MatrixMap> {
    4142  public:
    4243    typedef _MatrixMap MatrixMap;
     
    5152    ///
    5253    /// Subscription operator.
    53     typename MapTraits<MatrixMap>::ReturnValue
     54    typename MatrixMapTraits<MatrixMap>::ReturnValue
    5455    operator[](Key col) {
    5556      return matrix(row, col);
     
    6667    ///
    6768    /// Subscription operator.
    68     typename MapTraits<MatrixMap>::ConstReturnValue
     69    typename MatrixMapTraits<MatrixMap>::ConstReturnValue
    6970    operator[](Key col) const {
    7071      return matrix(row, col);
     
    8081  /// Map for the row view of the matrix.
    8182  template <typename _MatrixMap>
    82   class ConstMatrixRowMap : public MapTraits<_MatrixMap> {
     83  class ConstMatrixRowMap : public MatrixMapTraits<_MatrixMap> {
    8384  public:
    8485    typedef _MatrixMap MatrixMap;
     
    9495    ///
    9596    /// Subscription operator.
    96     typename MapTraits<MatrixMap>::ConstReturnValue
     97    typename MatrixMapTraits<MatrixMap>::ConstReturnValue
    9798    operator[](Key col) const {
    9899      return matrix(row, col);
     
    123124  /// Map for the row view of the matrix.
    124125  template <typename _MatrixMap>
    125   class MatrixColMap : public MapTraits<_MatrixMap> {
     126  class MatrixColMap : public MatrixMapTraits<_MatrixMap> {
    126127  public:
    127128    typedef _MatrixMap MatrixMap;
     
    135136    ///
    136137    /// Subscription operator.
    137     typename MapTraits<MatrixMap>::ReturnValue
     138    typename MatrixMapTraits<MatrixMap>::ReturnValue
    138139    operator[](Key row) {
    139140      return matrix(row, col);
     
    150151    ///
    151152    /// Subscription operator.
    152     typename MapTraits<MatrixMap>::ConstReturnValue
     153    typename MatrixMapTraits<MatrixMap>::ConstReturnValue
    153154    operator[](Key row) const {
    154155      return matrix(row, col);
     
    164165  /// Map for the col view of the matrix.
    165166  template <typename _MatrixMap>
    166   class ConstMatrixColMap : public MapTraits<_MatrixMap> {
     167  class ConstMatrixColMap : public MatrixMapTraits<_MatrixMap> {
    167168  public:
    168169    typedef _MatrixMap MatrixMap;
     
    177178    ///
    178179    /// Subscription operator.
    179     typename MapTraits<MatrixMap>::ConstReturnValue
     180    typename MatrixMapTraits<MatrixMap>::ConstReturnValue
    180181    operator[](Key row) const {
    181182      return matrix(row, col);
     
    250251    }
    251252
     253    ///\brief The assignement operator.
     254    ///
     255    ///It allow to assign a map to an other.
     256    DynamicMatrixMap& operator=(const DynamicMatrixMap& _cmap){
     257      return operator=<DynamicMatrixMap>(_cmap);
     258    }
     259     
     260    ///\brief Template assignement operator.
     261    ///
     262    ///It copy the element of the given map to its own container.  The
     263    ///type of the two map shall be the same.
     264    template <typename CMap>
     265    DynamicMatrixMap& operator=(const CMap& _cmap){
     266      checkConcept<concept::ReadMatrixMap<FirstKey, SecondKey, Value>, CMap>();
     267      typename Parent::Notifier* notifier = Parent::getNotifier();
     268      Key first, second;
     269      for(notifier->first(first); first != INVALID;
     270          notifier->next(first)){
     271        for(notifier->first(second); second != INVALID;
     272            notifier->next(second)){
     273          set(first, second, _cmap(first, second));
     274        }
     275      }
     276      return *this;
     277    }
     278
    252279    /// \brief Gives back the value assigned to the \c first - \c second
    253280    /// ordered pair.
     
    358385    }
    359386
     387
     388    ///\brief The assignement operator.
     389    ///
     390    ///It allow to assign a map to an other.
     391    DynamicSymMatrixMap& operator=(const DynamicSymMatrixMap& _cmap){
     392      return operator=<DynamicSymMatrixMap>(_cmap);
     393    }
     394     
     395    ///\brief Template assignement operator.
     396    ///
     397    ///It copy the element of the given map to its own container.  The
     398    ///type of the two map shall be the same.
     399    template <typename CMap>
     400    DynamicSymMatrixMap& operator=(const CMap& _cmap){
     401      checkConcept<concept::ReadMatrixMap<FirstKey, SecondKey, Value>, CMap>();
     402      typename Parent::Notifier* notifier = Parent::getNotifier();
     403      Key first, second;
     404      for(notifier->first(first); first != INVALID;
     405          notifier->next(first)){
     406        for(notifier->first(second); second != first;
     407            notifier->next(second)){
     408          set(first, second, _cmap(first, second));
     409        }
     410        set(first, first, _cmap(first, first));       
     411      }
     412      return *this;
     413    }
     414
    360415    /// \brief Gives back the value assigned to the \c first - \c second
    361416    /// unordered pair.
     
    419474    std::vector<Value> values;
    420475  };
     476 
     477  ///\brief Dynamic Asymmetric Matrix Map.
     478  ///
     479  ///Dynamic Asymmetric Matrix Map.  Container for store values for each
     480  ///ordered pair of containers items.  This data structure can store
     481  ///data with different key types from different container types. It
     482  ///increases the size of the container if the linked containers
     483  ///content change, so it is updated automaticly whenever it is
     484  ///needed.
     485  ///
     486  ///This map meet with the concept::ReferenceMatrixMap<typename K1,
     487  ///typename K2, typename V, typename R, typename CR> called as
     488  ///"ReferenceMatrixMap".
     489  ///
     490  ///\param _FirstContainer the desired type of first container. It is
     491  ///ususally a Graph type, but can be any type with alteration
     492  ///property.
     493  /// 
     494  ///\param _FirstContainerItem the nested type of the
     495  ///FirstContainer. It is usually a graph item as Node, Edge,
     496  ///etc. This type will be the FirstKey type.
     497  ///
     498  ///\param _SecondContainer the desired type of the second
     499  ///container. It is usualy a Graph type, but can be any type with
     500  ///alteration property.
     501  ///
     502  ///\param _SecondContainerItem the nested type of the
     503  ///SecondContainer. It is usually a graph item such as Node, Edge,
     504  ///UEdge, etc. This type will be the SecondKey type.
     505  ///
     506  ///\param _Value the type of the strored values in the container.
     507  ///
     508  /// \author Janos Nagy
     509  template <typename _FirstContainer, typename _FirstContainerItem,
     510            typename _SecondContainer, typename _SecondContainerItem,
     511            typename _Value>
     512  class DynamicAsymMatrixMap{
     513  public:
     514
     515    ///The first key type.
     516    typedef _FirstContainerItem FirstKey;
     517     
     518    ///The second key type.
     519    typedef _SecondContainerItem SecondKey;
     520     
     521    ///The value type of the map.
     522    typedef _Value Value;
     523     
     524    ///Indicates it is a reference map.
     525    typedef True ReferenceMapTag;
     526   
     527  protected:
     528     
     529    ///\brief Proxy class for the first key type.
     530    ///
     531    ///The proxy class belongs to the FirstKey type. It is necessary because
     532    ///if one want use the same conatainer types and same nested types but on
     533    ///other instances of containers than due to the type equiality of nested
     534    ///types it requires a proxy mechanism.
     535    class FirstKeyProxy
     536      : protected
     537    ItemSetTraits<_FirstContainer,_FirstContainerItem>::
     538    ItemNotifier::ObserverBase
     539    {
     540       
     541    public:
     542
     543      friend class DynamicAsymMatrixMap;
     544         
     545      ///Constructor.
     546      FirstKeyProxy(DynamicAsymMatrixMap& _map) : _owner(_map) { }
     547    protected:
     548
     549      ///\brief Add a new FirstKey to the map.
     550      ///
     551      ///It adds a new FirstKey to the map. It is called by the
     552      ///observer notifier and it is ovverride the add() virtual
     553      ///member function in the observer base. It will call the
     554      ///maps addFirstKey() function.
     555      virtual void add(const FirstKey& _firstKey){
     556        _owner.addFirstKey(_firstKey);
     557      }
     558         
     559      ///\brief Add more new FirstKey to the map.
     560      ///
     561      ///It adds more new FirstKey to the map. It is called by the
     562      ///observer notifier and it is ovverride the add() virtual
     563      ///member function in the observer base. It will call the
     564      ///map's addFirstKeys() function.
     565      virtual void add(const std::vector<FirstKey>& _firstKeys){
     566        _owner.addFirstKeys(_firstKeys);
     567      }
     568         
     569      ///\brief Erase a FirstKey from the map.
     570      ///
     571      ///Erase a FirstKey from the map. It called by the observer
     572      ///notifier and it overrides the erase() virtual member
     573      ///function of the observer base. It will call the map's
     574      ///eraseFirstKey() function.
     575      virtual void erase(const FirstKey& _firstKey){
     576        _owner.eraseFirstKey(_firstKey);
     577      }
     578         
     579      ///\brief Erase more FirstKey from the map.
     580      ///
     581      ///Erase more FirstKey from the map. It called by the
     582      ///observer notifier and it overrides the erase() virtual
     583      ///member function of the observer base. It will call the
     584      ///map's eraseFirstKeys() function.
     585      virtual void erase(const std::vector<FirstKey>& _firstKeys){
     586        _owner.eraseFirstKeys(_firstKeys);
     587      }
     588         
     589      ///\brief Builds the map.
     590      ///
     591      ///It buildes the map. It called by the observer notifier
     592      ///and it overrides the build() virtual member function of
     593      ///the observer base.  It will call the map's build()
     594      ///function.
     595      virtual void build() {
     596        _owner.build();
     597        //_owner.buildFirst();
     598      }
     599         
     600      ///\brief Clear the map.
     601      ///
     602      ///It erases all items from the map. It called by the
     603      ///observer notifier and it overrides the clear() virtual
     604      ///memeber function of the observer base. It will call the
     605      ///map's clear() function.
     606      virtual void clear() {
     607        _owner.clear();
     608        //_owner.clearFirst();
     609      }
     610    private:
     611         
     612      ///The map type for it is linked.
     613      DynamicAsymMatrixMap& _owner;
     614    };///END OF FIRSTKEYPROXY
     615     
     616      ///\brief Proxy class for the second key type.
     617      ///
     618      ///The proxy class belongs to the SecondKey type. It is
     619      ///necessary because if one want use the same conatainer types
     620      ///and same nested types but on other instances of containers
     621      ///than due to the type equiality of nested types it requires a
     622      ///proxy mechanism.
     623    class SecondKeyProxy
     624      : protected
     625    ItemSetTraits<_SecondContainer, _SecondContainerItem>::
     626    ItemNotifier::ObserverBase {
     627       
     628    public:
     629
     630      friend class DynamicAsymMatrixMap;
     631      ///Constructor.
     632      SecondKeyProxy(DynamicAsymMatrixMap& _map) : _owner(_map) { }
     633
     634    protected:
     635         
     636      ///\brief Add a new SecondKey to the map.
     637      ///
     638      ///It adds a new SecondKey to the map. It is called by the
     639      ///observer notifier and it is ovverride the add() virtual
     640      ///member function in the observer base. It will call the
     641      ///maps addSecondKey() function.
     642      virtual void add(const SecondKey& _secondKey){
     643        _owner.addSecondKey(_secondKey);
     644      }
     645   
     646      ///\brief Add more new SecondKey to the map.
     647      ///
     648      ///It adds more new SecondKey to the map. It is called by
     649      ///the observer notifier and it is ovverride the add()
     650      ///virtual member function in the observer base. It will
     651      ///call the maps addSecondKeys() function.
     652      virtual void add(const std::vector<SecondKey>& _secondKeys){
     653        _owner.addSecondKeys(_secondKeys);
     654      }
     655         
     656      ///\brief Erase a SecondKey from the map.
     657      ///
     658      ///Erase a SecondKey from the map. It called by the observer
     659      ///notifier and it overrides the erase() virtual member
     660      ///function of the observer base. It will call the map's
     661      ///eraseSecondKey() function.
     662      virtual void erase(const SecondKey& _secondKey){
     663        _owner.eraseSecondKey(_secondKey);
     664      }
     665         
     666      ///\brief Erase more SecondKeys from the map.
     667      ///
     668      ///Erase more SecondKey from the map. It called by the
     669      ///observer notifier and it overrides the erase() virtual
     670      ///member function of the observer base. It will call the
     671      ///map's eraseSecondKeys() function.
     672      virtual void erase(const std::vector<SecondKey>& _secondKeys){
     673        _owner.eraseSecondKeys(_secondKeys);
     674      }
     675         
     676      ///\brief Builds the map.
     677      ///
     678      ///It buildes the map. It called by the observer notifier
     679      ///and it overrides the build() virtual member function of
     680      ///the observer base.  It will call the map's build()
     681      ///function.
     682      virtual void build() {
     683        _owner.build();
     684      }
     685         
     686      ///\brief Clear the map.
     687      ///
     688      ///It erases all items from the map. It called by the
     689      ///observer notifier and it overrides the clear() virtual
     690      ///memeber function of the observer base. It will call the
     691      ///map's clear() function.
     692      virtual void clear() {
     693        _owner.clear();
     694        //_owner.clearFirst();
     695      }
     696    private:
     697         
     698      ///The type of map for which it is attached.
     699      DynamicAsymMatrixMap& _owner;
     700    };///END OF SECONDKEYPROXY
     701     
     702  private:
     703   
     704    /// \e
     705    typedef std::vector<Value> Container;
     706     
     707    ///The type of constainer which stores the values of the map.
     708    typedef std::vector<Container> DContainer;
     709
     710    ///The std:vector type which contains the data
     711    DContainer values;
     712     
     713    ///Member for the first proxy class
     714    FirstKeyProxy _first_key_proxy;
     715     
     716    ///Member for the second proxy class
     717    SecondKeyProxy _second_key_proxy;
     718
     719  public:
     720   
     721    ///The refernce type of the map.
     722    typedef typename Container::reference Reference;
     723     
     724    ///The const reference type of the constainer.
     725    typedef typename Container::const_reference ConstReference;
     726
     727    ///\brief Constructor what create the map for the two containers type.
     728    ///
     729    ///Creates the matrix map and initialize the values with Value()
     730    DynamicAsymMatrixMap(const _FirstContainer& _firstContainer,
     731                  const _SecondContainer& _secondContainer)
     732      : values(DContainer(_firstContainer.maxId(FirstKey())+1,
     733                          Container(_secondContainer.maxId(SecondKey())+1))),
     734        _first_key_proxy(*this),
     735        _second_key_proxy(*this)
     736    {
     737      _first_key_proxy.attach(_firstContainer.getNotifier(FirstKey()));
     738      _second_key_proxy.attach(_secondContainer.getNotifier(SecondKey()));
     739    }
     740
     741    ///\brief Constructor what create the map for the two containers type.
     742    ///
     743    ///Creates the matrix map and initialize the values with the given _value
     744    DynamicAsymMatrixMap(const _FirstContainer& _firstContainer,
     745                  const _SecondContainer& _secondContainer,
     746                  const Value& _value)
     747      : values(DContainer(_firstContainer.maxId(FirstKey())+1,
     748                          Container(_secondContainer.maxId(SecondKey())+1,
     749                                    _value))),
     750        _first_key_proxy(*this),
     751        _second_key_proxy(*this)
     752    {
     753      _first_key_proxy.attach(_firstContainer.getNotifier(FirstKey()));
     754      _second_key_proxy.attach(_secondContainer.getNotifier(SecondKey()));
     755    }
     756     
     757    ///\brief Copy constructor.
     758    ///
     759    ///The copy constructor of the map.
     760    DynamicAsymMatrixMap(const DynamicAsymMatrixMap& _copy)
     761      : _first_key_proxy(*this), _second_key_proxy(*this) {
     762      if(_copy._first_key_proxy.attached() &&
     763         _copy._second_key_proxy.attached()){
     764        _first_key_proxy.attach(*_copy._first_key_proxy.getNotifier());
     765        _second_key_proxy.attach(*_copy._second_key_proxy.getNotifier());
     766        values = _copy.values;
     767      }
     768    }
     769     
     770    ///\brief Destructor
     771    ///
     772    ///Destructor what detach() from the attached objects.  May this
     773    ///function is not necessary because the destructor of
     774    ///ObserverBase do the same.
     775    ~DynamicAsymMatrixMap() {
     776      if(_first_key_proxy.attached()){
     777        _first_key_proxy.detach();
     778      }
     779      if(_second_key_proxy.attached()){
     780        _second_key_proxy.detach();
     781      }
     782    }
     783     
     784    ///\brief Gives back the value assigned to the \c first - \c
     785    ///second ordered pair.
     786    ///
     787    ///Gives back the value assigned to the \c first - \c second
     788    ///ordered pair.
     789    Reference operator()(const FirstKey& _first, const SecondKey& _second) {
     790      return values[_first_key_proxy.getNotifier()->id(_first)]
     791        [_second_key_proxy.getNotifier()->id(_second)];
     792    }
     793
     794    ///\brief Gives back the value assigned to the \c first - \c
     795    ///second ordered pair.
     796    ///
     797    ///Gives back the value assigned to the \c first - \c second
     798    ///ordered pair.
     799    ConstReference operator()(const FirstKey& _first,
     800                              const SecondKey& _second) const {
     801      return values[_first_key_proxy.getNotifier()->id(_first)]
     802        [_second_key_proxy.getNotifier()->id(_second)];
     803    }
     804
     805    ///\brief Setter function for this matrix map.
     806    ///
     807    ///Setter function for this matrix map.
     808    void set(const FirstKey& first, const SecondKey& second,
     809             const Value& value){
     810      values[_first_key_proxy.getNotifier()->id(first)]
     811        [_second_key_proxy.getNotifier()->id(second)] = value;
     812    }
     813
     814    ///\brief The assignement operator.
     815    ///
     816    ///It allow to assign a map to an other. It
     817    DynamicAsymMatrixMap& operator=(const DynamicAsymMatrixMap& _cmap){
     818      return operator=<DynamicAsymMatrixMap>(_cmap);
     819    }
     820     
     821    ///\brief Template assignement operator.
     822    ///
     823    ///It copy the element of the given map to its own container.  The
     824    ///type of the two map shall be the same.
     825    template <typename CMap>
     826    DynamicAsymMatrixMap& operator=(const CMap& _cdmap){
     827      checkConcept<concept::ReadMatrixMap<FirstKey, SecondKey, Value>, CMap>();
     828      const typename FirstKeyProxy::Notifier* notifierFirstKey =
     829        _first_key_proxy.getNotifier();
     830      const typename SecondKeyProxy::Notifier* notifierSecondKey =
     831        _second_key_proxy.getNotifier();
     832      FirstKey itemFirst;
     833      SecondKey itemSecond;
     834      for(notifierFirstKey->first(itemFirst); itemFirst != INVALID;
     835          notifierFirstKey->next(itemFirst)){
     836        for(notifierSecondKey->first(itemSecond); itemSecond != INVALID;
     837            notifierSecondKey->next(itemSecond)){
     838          set(itemFirst, itemSecond, _cdmap(itemFirst,itemSecond));
     839        }
     840      }
     841      return *this;
     842    }
     843     
     844  protected:
     845   
     846    ///\brief Add a new FirstKey to the map.
     847    ///
     848    ///It adds a new FirstKey to the map. It is called by the observer
     849    ///class belongs to the FirstKey type.
     850    void addFirstKey(const FirstKey& firstKey) {
     851      int size = (int)values.size();
     852      if( _first_key_proxy.getNotifier()->id(firstKey)+1 >= size ){
     853        values.resize(_first_key_proxy.getNotifier()->id(firstKey)+1);
     854        if( (int)values[0].size() != 0 ){
     855          int innersize = (int)values[0].size();
     856          for(int i=size; i!=(int)values.size();++i){
     857            (values[i]).resize(innersize);
     858          }
     859        }else if(_second_key_proxy.getNotifier()->maxId() >= 0){
     860          int innersize = _second_key_proxy.getNotifier()->maxId();
     861          for(int i = 0; i != (int)values.size(); ++i){
     862            values[0].resize(innersize);
     863          }
     864        }
     865      }
     866    }
     867
     868    ///\brief Adds more new FirstKeys to the map.
     869    ///
     870    ///It adds more new FirstKeys to the map. It called by the
     871    ///observer class belongs to the FirstKey type.
     872    void addFirstKeys(const std::vector<FirstKey>& firstKeys){
     873      int max = values.size() - 1;
     874      for(int i=0; i != (int)firstKeys.size(); ++i){
     875        int id = _first_key_proxy.getNotifier()->id(firstKeys[i]);
     876        if(max < id){
     877          max = id;
     878        }
     879      }
     880      int size = (int)values.size();
     881      if(max >= size){
     882        values.resize(max + 1);
     883        if( (int)values[0].size() != 0){
     884          int innersize = (int)values[0].size();
     885          for(int i = size; i != (max + 1); ++i){
     886            values[i].resize(innersize);
     887          }
     888        }else if(_second_key_proxy.getNotifier()->maxId() >= 0){
     889          int innersize = _second_key_proxy.getNotifier()->maxId();
     890          for(int i = 0; i != (int)values.size(); ++i){
     891            values[i].resize(innersize);
     892          }
     893        }
     894      }
     895    }
     896
     897    ///\brief Add a new SecondKey to the map.
     898    ///
     899    ///It adds a new SecondKey to the map. It is called by the
     900    ///observer class belongs to the SecondKey type.
     901    void addSecondKey(const SecondKey& secondKey) {
     902      if(values.size() == 0){
     903        return;
     904      }
     905      int id = _second_key_proxy.getNotifier()->id(secondKey);
     906      if(id >= (int)values[0].size()){
     907        for(int i=0;i!=(int)values.size();++i){
     908          values[i].resize(id+1);
     909        }
     910      }
     911    }
     912       
     913    ///\brief Adds more new SecondKeys to the map.
     914    ///
     915    ///It adds more new SecondKeys to the map. It called by the
     916    ///observer class belongs to the SecondKey type.
     917    void addSecondKeys(const std::vector<SecondKey>& secondKeys){
     918      if(values.size() == 0){
     919        return;
     920      }
     921      int max = values[0].size();
     922      for(int i = 0; i != (int)secondKeys.size(); ++i){
     923        int id = _second_key_proxy.getNotifier()->id(secondKeys[i]);
     924        if(max < id){
     925          max = id;
     926        }
     927      }
     928      if(max > (int)values[0].size()){
     929        for(int i = 0; i != (int)values.size(); ++i){
     930          values[i].resize(max + 1);
     931        }
     932      }
     933    }
     934   
     935    ///\brief Erase a FirstKey from the map.
     936    ///
     937    ///Erase a FirstKey from the map. It called by the observer
     938    ///class belongs to the FirstKey type.
     939    void eraseFirstKey(const FirstKey& first) {
     940      int id = _first_key_proxy.getNotifier()->id(first);
     941      for(int i = 0; i != (int)values[id].size(); ++i){
     942        values[id][i] = Value();
     943      }
     944    }
     945       
     946    ///\brief Erase more FirstKey from the map.
     947    ///
     948    ///Erase more FirstKey from the map. It called by the observer
     949    ///class belongs to the FirstKey type.
     950    void eraseFirstKeys(const std::vector<FirstKey>& firstKeys) {
     951      for(int j = 0; j != (int)firstKeys.size(); ++j){
     952        int id = _first_key_proxy.getNotifier()->id(firstKeys[j]);
     953        for(int i = 0; i != (int)values[id].size(); ++i){
     954          values[id][i] = Value();
     955        }
     956      }
     957    }
     958
     959    ///\brief Erase a SecondKey from the map.
     960    ///
     961    ///Erase a SecondKey from the map. It called by the observer class
     962    ///belongs to the SecondKey type.
     963    void eraseSecondKey(const SecondKey& second) {
     964      if(values.size() == 0){
     965        return;
     966      }
     967      int id = _second_key_proxy.getNotifier()->id(second);
     968      for(int i = 0; i != (int)values.size(); ++i){
     969        values[i][id] = Value();
     970      }
     971    }
     972       
     973    ///\brief Erase more SecondKey from the map.
     974    ///
     975    ///Erase more SecondKey from the map. It called by the observer
     976    ///class belongs to the SecondKey type.
     977    void eraseSecondKeys(const std::vector<SecondKey>& secondKeys) {
     978      if(values.size() == 0){
     979        return;
     980      }
     981      for(int j = 0; j != (int)secondKeys.size(); ++j){
     982        int id = _second_key_proxy.getNotifier()->id(secondKeys[j]);
     983        for(int i = 0; i != (int)values.size(); ++i){
     984          values[i][id] = Value();
     985        }
     986      }
     987    }
     988
     989    ///\brief Builds the map.
     990    ///
     991    ///It buildes the map. It is called by the observer class belongs
     992    ///to the FirstKey or SecondKey type.
     993    void build() {
     994      values.resize(_first_key_proxy.getNotifier()->maxId());
     995      for(int i=0; i!=(int)values.size(); ++i){
     996        values[i].resize(_second_key_proxy.getNotifier()->maxId());
     997      }
     998    }
     999   
     1000    ///\brief Clear the map.
     1001    ///
     1002    ///It erases all items from the map. It is called by the observer class
     1003    ///belongs to the FirstKey or SecondKey type.
     1004    void clear() {
     1005      for(int i=0; i!=(int)values.size(); ++i) {
     1006        values[i].clear();
     1007      }
     1008      values.clear();
     1009    }
     1010 
     1011  };
     1012
     1013
    4211014
    4221015}
  • test/matrix_maps_test.cc

    r1956 r2039  
    4141  typedef SmartGraph Graph;
    4242  typedef Graph::Node Node;
     43  typedef Graph::Edge Edge;
    4344
    4445  { // checking MatrixMap for int
     
    125126  }
    126127
     128  { // checking MatrixMap for int
     129    typedef DynamicAsymMatrixMap<Graph, Node, Graph, Edge, int> IntMatrixMap;
     130    checkConcept<ReferenceMatrixMap<Node, Edge, int,
     131      IntMatrixMap::Reference, IntMatrixMap::ConstReference>,
     132      IntMatrixMap>();
     133
     134  }
     135
     136  { // checking MatrixMap for bool
     137    typedef DynamicAsymMatrixMap<Graph, Node, Graph, Edge, bool> BoolMatrixMap;
     138    checkConcept<ReferenceMatrixMap<Node, Edge, bool,
     139      BoolMatrixMap::Reference, BoolMatrixMap::ConstReference>,
     140      BoolMatrixMap>();
     141
     142  }
     143
     144  {
     145    Graph graph1, graph2;
     146    typedef DynamicAsymMatrixMap<Graph, Node, Graph, Edge, int> IntMatrixMap;
     147    IntMatrixMap matrix(graph1, graph2);
     148    for (int i = 0; i < 10; ++i) {
     149      graph1.addNode();
     150    }
     151    graph2.addNode();
     152    for (int i = 0; i < 20; ++i) {
     153      graph2.addEdge(Graph::NodeIt(graph2), Graph::NodeIt(graph2));
     154    }
     155    for (Graph::NodeIt it(graph1); it != INVALID; ++it) {
     156      for (Graph::EdgeIt jt(graph2); jt != INVALID; ++jt) {
     157        int val = urandom(100);
     158        matrix.set(it, jt, val);
     159        check(matrix(it, jt) == val, "Wrong assign");
     160        check(matrix(it, jt) == matrixRowMap(matrix, it)[jt], "Wrong rowMap");
     161        check(matrix(it, jt) == matrixColMap(matrix, jt)[it], "Wrong colMap");
     162      }
     163    }
     164    const IntMatrixMap& cm = matrix;
     165    for (Graph::NodeIt it(graph1); it != INVALID; ++it) {
     166      for (Graph::EdgeIt jt(graph2); jt != INVALID; ++jt) {
     167        check(cm(it, jt) == matrixRowMap(cm, it)[jt], "Wrong rowMap");
     168        check(cm(it, jt) == matrixColMap(cm, jt)[it], "Wrong colMap");
     169      }
     170    }
     171  }
     172
     173  { // checking MatrixMap for int
     174    typedef DynamicAsymMatrixMap<Graph, Node, Graph, Node, int> IntMatrixMap;
     175    checkConcept<ReferenceMatrixMap<Node, Node, int,
     176      IntMatrixMap::Reference, IntMatrixMap::ConstReference>,
     177      IntMatrixMap>();
     178
     179  }
     180
     181  { // checking MatrixMap for bool
     182    typedef DynamicAsymMatrixMap<Graph, Node, Graph, Node, bool> BoolMatrixMap;
     183    checkConcept<ReferenceMatrixMap<Node, Node, bool,
     184      BoolMatrixMap::Reference, BoolMatrixMap::ConstReference>,
     185      BoolMatrixMap>();
     186
     187  }
     188
     189  {
     190    Graph graph;
     191    typedef DynamicAsymMatrixMap<Graph, Node, Graph, Node, int> IntMatrixMap;
     192    IntMatrixMap matrix(graph, graph);
     193    for (int i = 0; i < 10; ++i) {
     194      graph.addNode();
     195    }
     196    for (int i = 0; i < 20; ++i) {
     197      graph.addEdge(Graph::NodeIt(graph), Graph::NodeIt(graph));
     198    }
     199    for (Graph::NodeIt it(graph); it != INVALID; ++it) {
     200      for (Graph::NodeIt jt(graph); jt != INVALID; ++jt) {
     201        int val = urandom(100);
     202        matrix.set(it, jt, val);
     203        check(matrix(it, jt) == val, "Wrong assign");
     204        check(matrix(it, jt) == matrixRowMap(matrix, it)[jt], "Wrong rowMap");
     205        check(matrix(it, jt) == matrixColMap(matrix, jt)[it], "Wrong colMap");
     206      }
     207    }
     208    const IntMatrixMap& cm = matrix;
     209    for (Graph::NodeIt it(graph); it != INVALID; ++it) {
     210      for (Graph::NodeIt jt(graph); jt != INVALID; ++jt) {
     211        check(cm(it, jt) == matrixRowMap(cm, it)[jt], "Wrong rowMap");
     212        check(cm(it, jt) == matrixColMap(cm, jt)[it], "Wrong colMap");
     213      }
     214    }
     215  }
     216
    127217  std::cout << __FILE__ ": All tests passed.\n";
    128218
Note: See TracChangeset for help on using the changeset viewer.