lemon/matrix_maps.h
changeset 2388 c6d537888fe5
parent 2384 805c5a2a36dd
child 2391 14a343be7a5a
equal deleted inserted replaced
15:d4d5d0452bc3 16:f12e538b2d4c
   347     static int size(int s) {
   347     static int size(int s) {
   348       return s * s;
   348       return s * s;
   349     }
   349     }
   350 
   350 
   351     virtual void add(const Key& key) {
   351     virtual void add(const Key& key) {
   352       if (size(Parent::notifier()->id(key) + 1) >= (int)values.size()) {
   352       if (size(Parent::notifier()->id(key) + 1) >= int(values.size())) {
   353 	values.resize(size(Parent::notifier()->id(key) + 1));	
   353 	values.resize(size(Parent::notifier()->id(key) + 1));	
   354       }
   354       }
   355     }
   355     }
   356 
   356 
   357     virtual void add(const std::vector<Key>& keys) {
   357     virtual void add(const std::vector<Key>& keys) {
   358       int new_size = 0;
   358       int new_size = 0;
   359       for (int i = 0; i < (int)keys.size(); ++i) {
   359       for (int i = 0; i < int(keys.size()); ++i) {
   360         if (size(Parent::notifier()->id(keys[i]) + 1) >= new_size) {
   360         if (size(Parent::notifier()->id(keys[i]) + 1) >= new_size) {
   361           new_size = size(Parent::notifier()->id(keys[i]) + 1);	
   361           new_size = size(Parent::notifier()->id(keys[i]) + 1);	
   362         }
   362         }
   363       }
   363       }
   364       if (new_size > (int)values.size()) {
   364       if (new_size > int(values.size())) {
   365         values.resize(new_size);
   365         values.resize(new_size);
   366       }
   366       }
   367     }
   367     }
   368 
   368 
   369     virtual void erase(const Key&) {}
   369     virtual void erase(const Key&) {}
   502     static int size(int s) {
   502     static int size(int s) {
   503       return s * (s + 1) / 2;
   503       return s * (s + 1) / 2;
   504     }
   504     }
   505 
   505 
   506     virtual void add(const Key& key) {
   506     virtual void add(const Key& key) {
   507       if (size(Parent::notifier()->id(key) + 1) >= (int)values.size()) {
   507       if (size(Parent::notifier()->id(key) + 1) >= int(values.size())) {
   508 	values.resize(size(Parent::notifier()->id(key) + 1));	
   508 	values.resize(size(Parent::notifier()->id(key) + 1));	
   509       }
   509       }
   510     }
   510     }
   511 
   511 
   512     virtual void add(const std::vector<Key>& keys) {
   512     virtual void add(const std::vector<Key>& keys) {
   513       int new_size = 0;
   513       int new_size = 0;
   514       for (int i = 0; i < (int)keys.size(); ++i) {
   514       for (int i = 0; i < int(keys.size()); ++i) {
   515         if (size(Parent::notifier()->id(keys[i]) + 1) >= new_size) {
   515         if (size(Parent::notifier()->id(keys[i]) + 1) >= new_size) {
   516           new_size = size(Parent::notifier()->id(keys[i]) + 1);	
   516           new_size = size(Parent::notifier()->id(keys[i]) + 1);	
   517         }
   517         }
   518       }
   518       }
   519       if (new_size > (int)values.size()) {
   519       if (new_size > int(values.size())) {
   520         values.resize(new_size);
   520         values.resize(new_size);
   521       }
   521       }
   522     }
   522     }
   523 
   523 
   524     virtual void erase(const Key&) {}
   524     virtual void erase(const Key&) {}
   913     ///\brief Add a new FirstKey to the map.
   913     ///\brief Add a new FirstKey to the map.
   914     ///
   914     ///
   915     ///It adds a new FirstKey to the map. It is called by the observer
   915     ///It adds a new FirstKey to the map. It is called by the observer
   916     ///class belongs to the FirstKey type.
   916     ///class belongs to the FirstKey type.
   917     void addFirstKey(const FirstKey& firstKey) {
   917     void addFirstKey(const FirstKey& firstKey) {
   918       int size = (int)values.size();
   918       int size = int(values.size());
   919       if( _first_key_proxy.notifier()->id(firstKey)+1 >= size ){
   919       if( _first_key_proxy.notifier()->id(firstKey)+1 >= size ){
   920         values.resize(_first_key_proxy.notifier()->id(firstKey)+1);
   920         values.resize(_first_key_proxy.notifier()->id(firstKey)+1);
   921         if( (int)values[0].size() != 0 ){
   921         if( int(values[0].size()) != 0 ){
   922           int innersize = (int)values[0].size();
   922           int innersize = int(values[0].size());
   923           for(int i=size; i!=(int)values.size();++i){
   923           for(int i = size; i < int(values.size());++i){
   924             (values[i]).resize(innersize);
   924             (values[i]).resize(innersize);
   925           }
   925           }
   926         }else if(_second_key_proxy.notifier()->maxId() >= 0){
   926         }else if(_second_key_proxy.notifier()->maxId() >= 0){
   927           int innersize = _second_key_proxy.notifier()->maxId();
   927           int innersize = _second_key_proxy.notifier()->maxId();
   928           for(int i = 0; i != (int)values.size(); ++i){
   928           for(int i = 0; i < int(values.size()); ++i){
   929             values[0].resize(innersize);
   929             values[0].resize(innersize);
   930           }
   930           }
   931         }
   931         }
   932       }
   932       }
   933     }
   933     }
   936     ///
   936     ///
   937     ///It adds more new FirstKeys to the map. It called by the
   937     ///It adds more new FirstKeys to the map. It called by the
   938     ///observer class belongs to the FirstKey type.
   938     ///observer class belongs to the FirstKey type.
   939     void addFirstKeys(const std::vector<FirstKey>& firstKeys){
   939     void addFirstKeys(const std::vector<FirstKey>& firstKeys){
   940       int max = values.size() - 1;
   940       int max = values.size() - 1;
   941       for(int i=0; i != (int)firstKeys.size(); ++i){
   941       for(int i = 0; i < int(firstKeys.size()); ++i){
   942         int id = _first_key_proxy.notifier()->id(firstKeys[i]);
   942         int id = _first_key_proxy.notifier()->id(firstKeys[i]);
   943         if(max < id){
   943         if(max < id){
   944           max = id;
   944           max = id;
   945         }
   945         }
   946       }
   946       }
   947       int size = (int)values.size();
   947       int size = int(values.size());
   948       if(max >= size){
   948       if(max >= size){
   949         values.resize(max + 1);
   949         values.resize(max + 1);
   950         if( (int)values[0].size() != 0){
   950         if( int(values[0].size()) != 0){
   951           int innersize = (int)values[0].size();
   951           int innersize = int(values[0].size());
   952           for(int i = size; i != (max + 1); ++i){
   952           for(int i = size; i < (max + 1); ++i){
   953             values[i].resize(innersize);
   953             values[i].resize(innersize);
   954           }
   954           }
   955         }else if(_second_key_proxy.notifier()->maxId() >= 0){
   955         }else if(_second_key_proxy.notifier()->maxId() >= 0){
   956           int innersize = _second_key_proxy.notifier()->maxId();
   956           int innersize = _second_key_proxy.notifier()->maxId();
   957           for(int i = 0; i != (int)values.size(); ++i){
   957           for(int i = 0; i < int(values.size()); ++i){
   958             values[i].resize(innersize);
   958             values[i].resize(innersize);
   959           }
   959           }
   960         }
   960         }
   961       }
   961       }
   962     }
   962     }
   968     void addSecondKey(const SecondKey& secondKey) {
   968     void addSecondKey(const SecondKey& secondKey) {
   969       if(values.size() == 0){
   969       if(values.size() == 0){
   970         return;
   970         return;
   971       }
   971       }
   972       int id = _second_key_proxy.notifier()->id(secondKey);
   972       int id = _second_key_proxy.notifier()->id(secondKey);
   973       if(id >= (int)values[0].size()){
   973       if(id >= int(values[0].size())){
   974         for(int i=0;i!=(int)values.size();++i){
   974         for(int i = 0; i < int(values.size());++i){
   975           values[i].resize(id+1);
   975           values[i].resize(id+1);
   976         }
   976         }
   977       }
   977       }
   978     }
   978     }
   979         
   979         
   984     void addSecondKeys(const std::vector<SecondKey>& secondKeys){
   984     void addSecondKeys(const std::vector<SecondKey>& secondKeys){
   985       if(values.size() == 0){
   985       if(values.size() == 0){
   986         return;
   986         return;
   987       }
   987       }
   988       int max = values[0].size();
   988       int max = values[0].size();
   989       for(int i = 0; i != (int)secondKeys.size(); ++i){
   989       for(int i = 0; i < int(secondKeys.size()); ++i){
   990         int id = _second_key_proxy.notifier()->id(secondKeys[i]);
   990         int id = _second_key_proxy.notifier()->id(secondKeys[i]);
   991         if(max < id){
   991         if(max < id){
   992           max = id;
   992           max = id;
   993         }
   993         }
   994       }
   994       }
   995       if(max > (int)values[0].size()){
   995       if(max > int(values[0].size())){
   996         for(int i = 0; i != (int)values.size(); ++i){
   996         for(int i = 0; i < int(values.size()); ++i){
   997           values[i].resize(max + 1);
   997           values[i].resize(max + 1);
   998         }
   998         }
   999       }
   999       }
  1000     }
  1000     }
  1001     
  1001     
  1003     ///
  1003     ///
  1004     ///Erase a FirstKey from the map. It called by the observer
  1004     ///Erase a FirstKey from the map. It called by the observer
  1005     ///class belongs to the FirstKey type.
  1005     ///class belongs to the FirstKey type.
  1006     void eraseFirstKey(const FirstKey& first) {
  1006     void eraseFirstKey(const FirstKey& first) {
  1007       int id = _first_key_proxy.notifier()->id(first);
  1007       int id = _first_key_proxy.notifier()->id(first);
  1008       for(int i = 0; i != (int)values[id].size(); ++i){
  1008       for(int i = 0; i < int(values[id].size()); ++i){
  1009         values[id][i] = Value();
  1009         values[id][i] = Value();
  1010       }
  1010       }
  1011     }
  1011     }
  1012         
  1012         
  1013     ///\brief Erase more FirstKey from the map.
  1013     ///\brief Erase more FirstKey from the map.
  1014     ///
  1014     ///
  1015     ///Erase more FirstKey from the map. It called by the observer
  1015     ///Erase more FirstKey from the map. It called by the observer
  1016     ///class belongs to the FirstKey type.
  1016     ///class belongs to the FirstKey type.
  1017     void eraseFirstKeys(const std::vector<FirstKey>& firstKeys) {
  1017     void eraseFirstKeys(const std::vector<FirstKey>& firstKeys) {
  1018       for(int j = 0; j != (int)firstKeys.size(); ++j){
  1018       for(int j = 0; j < int(firstKeys.size()); ++j){
  1019         int id = _first_key_proxy.notifier()->id(firstKeys[j]);
  1019         int id = _first_key_proxy.notifier()->id(firstKeys[j]);
  1020         for(int i = 0; i != (int)values[id].size(); ++i){
  1020         for(int i = 0; i < int(values[id].size()); ++i){
  1021           values[id][i] = Value();
  1021           values[id][i] = Value();
  1022         }
  1022         }
  1023       }
  1023       }
  1024     }
  1024     }
  1025 
  1025 
  1030     void eraseSecondKey(const SecondKey& second) {
  1030     void eraseSecondKey(const SecondKey& second) {
  1031       if(values.size() == 0){
  1031       if(values.size() == 0){
  1032         return;
  1032         return;
  1033       }
  1033       }
  1034       int id = _second_key_proxy.notifier()->id(second);
  1034       int id = _second_key_proxy.notifier()->id(second);
  1035       for(int i = 0; i != (int)values.size(); ++i){
  1035       for(int i = 0; i < int(values.size()); ++i){
  1036         values[i][id] = Value();
  1036         values[i][id] = Value();
  1037       }
  1037       }
  1038     }
  1038     }
  1039         
  1039         
  1040     ///\brief Erase more SecondKey from the map.
  1040     ///\brief Erase more SecondKey from the map.
  1043     ///class belongs to the SecondKey type.
  1043     ///class belongs to the SecondKey type.
  1044     void eraseSecondKeys(const std::vector<SecondKey>& secondKeys) {
  1044     void eraseSecondKeys(const std::vector<SecondKey>& secondKeys) {
  1045       if(values.size() == 0){
  1045       if(values.size() == 0){
  1046         return;
  1046         return;
  1047       }
  1047       }
  1048       for(int j = 0; j != (int)secondKeys.size(); ++j){
  1048       for(int j = 0; j < int(secondKeys.size()); ++j){
  1049         int id = _second_key_proxy.notifier()->id(secondKeys[j]);
  1049         int id = _second_key_proxy.notifier()->id(secondKeys[j]);
  1050         for(int i = 0; i != (int)values.size(); ++i){
  1050         for(int i = 0; i < int(values.size()); ++i){
  1051           values[i][id] = Value();
  1051           values[i][id] = Value();
  1052         }
  1052         }
  1053       }
  1053       }
  1054     }
  1054     }
  1055 
  1055 
  1057     ///
  1057     ///
  1058     ///It buildes the map. It is called by the observer class belongs
  1058     ///It buildes the map. It is called by the observer class belongs
  1059     ///to the FirstKey or SecondKey type.
  1059     ///to the FirstKey or SecondKey type.
  1060     void build() {
  1060     void build() {
  1061       values.resize(_first_key_proxy.notifier()->maxId());
  1061       values.resize(_first_key_proxy.notifier()->maxId());
  1062       for(int i=0; i!=(int)values.size(); ++i){
  1062       for(int i = 0; i< int(values.size()); ++i){
  1063         values[i].resize(_second_key_proxy.notifier()->maxId());
  1063         values[i].resize(_second_key_proxy.notifier()->maxId());
  1064       }
  1064       }
  1065     }
  1065     }
  1066     
  1066     
  1067     ///\brief Clear the map.
  1067     ///\brief Clear the map.
  1068     ///
  1068     ///
  1069     ///It erases all items from the map. It is called by the observer class
  1069     ///It erases all items from the map. It is called by the observer class
  1070     ///belongs to the FirstKey or SecondKey type.
  1070     ///belongs to the FirstKey or SecondKey type.
  1071     void clear() {
  1071     void clear() {
  1072       for(int i=0; i!=(int)values.size(); ++i) {
  1072       for(int i = 0; i < int(values.size()); ++i) {
  1073         values[i].clear();
  1073         values[i].clear();
  1074       }
  1074       }
  1075       values.clear();
  1075       values.clear();
  1076     }
  1076     }
  1077  
  1077