Swap col and row map
authordeba
Wed, 02 Nov 2005 15:24:38 +0000
changeset 1751a2a454f1232d
parent 1750 5c76ebbb4818
child 1752 dce1f28ac595
Swap col and row map
lemon/matrix_maps.h
test/matrix_maps_test.cc
     1.1 --- a/lemon/matrix_maps.h	Wed Nov 02 15:23:46 2005 +0000
     1.2 +++ b/lemon/matrix_maps.h	Wed Nov 02 15:24:38 2005 +0000
     1.3 @@ -35,98 +35,14 @@
     1.4    ///
     1.5    /// Map for the coloumn view of the matrix.
     1.6    template <typename _MatrixMap>
     1.7 -  class MatrixColMap : public MapTraits<_MatrixMap> {
     1.8 +  class MatrixRowMap : public MapTraits<_MatrixMap> {
     1.9    public:
    1.10      typedef _MatrixMap MatrixMap;
    1.11      typedef typename MatrixMap::SecondKey Key;
    1.12      typedef typename MatrixMap::Value Value;
    1.13  
    1.14  
    1.15 -    MatrixColMap(MatrixMap& _matrix, typename MatrixMap::FirstKey _col) 
    1.16 -      : matrix(_matrix), col(_col) {}
    1.17 -
    1.18 -    /// \brief Subscription operator
    1.19 -    ///
    1.20 -    /// Subscription operator.
    1.21 -    typename MapTraits<MatrixMap>::ReturnValue
    1.22 -    operator[](Key row) {
    1.23 -      return matrix(col, row);
    1.24 -    }
    1.25 -
    1.26 -    /// \brief Setter function
    1.27 -    ///
    1.28 -    /// Setter function.
    1.29 -    void set(Key row, const Value& val) {
    1.30 -      matrix.set(col, row, val);
    1.31 -    }
    1.32 -      
    1.33 -    /// \brief Subscription operator
    1.34 -    ///
    1.35 -    /// Subscription operator.
    1.36 -    typename MapTraits<MatrixMap>::ConstReturnValue
    1.37 -    operator[](Key row) const {
    1.38 -      return matrix(col, row);
    1.39 -    }
    1.40 -
    1.41 -  private:
    1.42 -    MatrixMap& matrix;
    1.43 -    typename MatrixMap::FirstKey col;
    1.44 -  };
    1.45 -
    1.46 -  /// \brief Map for the coloumn view of the matrix
    1.47 -  ///
    1.48 -  /// Map for the coloumn view of the matrix.
    1.49 -  template <typename _MatrixMap>
    1.50 -  class ConstMatrixColMap : public MapTraits<_MatrixMap> {
    1.51 -  public:
    1.52 -    typedef _MatrixMap MatrixMap;
    1.53 -    typedef typename MatrixMap::SecondKey Key;
    1.54 -    typedef typename MatrixMap::Value Value;
    1.55 -
    1.56 -
    1.57 -    ConstMatrixColMap(const MatrixMap& _matrix, 
    1.58 -		      typename MatrixMap::FirstKey _col) 
    1.59 -      : matrix(_matrix), col(_col) {}
    1.60 -
    1.61 -    /// \brief Subscription operator
    1.62 -    ///
    1.63 -    /// Subscription operator.
    1.64 -    typename MapTraits<MatrixMap>::ConstReturnValue
    1.65 -    operator[](Key row) const {
    1.66 -      return matrix(col, row);
    1.67 -    }
    1.68 -
    1.69 -  private:
    1.70 -    const MatrixMap& matrix;
    1.71 -    typename MatrixMap::FirstKey col;
    1.72 -  };
    1.73 -
    1.74 -  /// \brief Gives back a coloumn view of the matrix map
    1.75 -  ///
    1.76 -  /// Gives back a coloumn view of the matrix map.
    1.77 -  template <typename MatrixMap>
    1.78 -  MatrixColMap<MatrixMap> matrixColMap(MatrixMap& matrixMap,
    1.79 -				       typename MatrixMap::FirstKey col) {
    1.80 -    return MatrixColMap<MatrixMap>(matrixMap, col);
    1.81 -  }
    1.82 -
    1.83 -  template <typename MatrixMap>
    1.84 -  ConstMatrixColMap<MatrixMap>
    1.85 -  matrixColMap(const MatrixMap& matrixMap, typename MatrixMap::FirstKey col) {
    1.86 -    return ConstMatrixColMap<MatrixMap>(matrixMap, col);
    1.87 -  }
    1.88 -
    1.89 -  /// \brief Map for the row view of the matrix
    1.90 -  ///
    1.91 -  /// Map for the row view of the matrix.
    1.92 -  template <typename _MatrixMap>
    1.93 -  class MatrixRowMap : public MapTraits<_MatrixMap> {
    1.94 -  public:
    1.95 -    typedef _MatrixMap MatrixMap;
    1.96 -    typedef typename MatrixMap::FirstKey Key;
    1.97 -    typedef typename MatrixMap::Value Value;
    1.98 -
    1.99 -    MatrixRowMap(MatrixMap& _matrix, typename MatrixMap::SecondKey _row) 
   1.100 +    MatrixRowMap(MatrixMap& _matrix, typename MatrixMap::FirstKey _row) 
   1.101        : matrix(_matrix), row(_row) {}
   1.102  
   1.103      /// \brief Subscription operator
   1.104 @@ -134,14 +50,14 @@
   1.105      /// Subscription operator.
   1.106      typename MapTraits<MatrixMap>::ReturnValue
   1.107      operator[](Key col) {
   1.108 -      return matrix(col, row);
   1.109 +      return matrix(row, col);
   1.110      }
   1.111  
   1.112      /// \brief Setter function
   1.113      ///
   1.114      /// Setter function.
   1.115      void set(Key col, const Value& val) {
   1.116 -      matrix.set(col, row, val);
   1.117 +      matrix.set(row, col, val);
   1.118      }
   1.119        
   1.120      /// \brief Subscription operator
   1.121 @@ -149,12 +65,12 @@
   1.122      /// Subscription operator.
   1.123      typename MapTraits<MatrixMap>::ConstReturnValue
   1.124      operator[](Key col) const {
   1.125 -      return matrix(col, row);
   1.126 +      return matrix(row, col);
   1.127      }
   1.128  
   1.129    private:
   1.130      MatrixMap& matrix;
   1.131 -    typename MatrixMap::SecondKey row;
   1.132 +    typename MatrixMap::FirstKey row;
   1.133    };
   1.134  
   1.135    /// \brief Map for the row view of the matrix
   1.136 @@ -164,11 +80,12 @@
   1.137    class ConstMatrixRowMap : public MapTraits<_MatrixMap> {
   1.138    public:
   1.139      typedef _MatrixMap MatrixMap;
   1.140 -    typedef typename MatrixMap::FirstKey Key;
   1.141 +    typedef typename MatrixMap::SecondKey Key;
   1.142      typedef typename MatrixMap::Value Value;
   1.143  
   1.144 +
   1.145      ConstMatrixRowMap(const MatrixMap& _matrix, 
   1.146 -		      typename MatrixMap::SecondKey _row) 
   1.147 +		      typename MatrixMap::FirstKey _row) 
   1.148        : matrix(_matrix), row(_row) {}
   1.149  
   1.150      /// \brief Subscription operator
   1.151 @@ -176,12 +93,12 @@
   1.152      /// Subscription operator.
   1.153      typename MapTraits<MatrixMap>::ConstReturnValue
   1.154      operator[](Key col) const {
   1.155 -      return matrix(col, row);
   1.156 +      return matrix(row, col);
   1.157      }
   1.158  
   1.159    private:
   1.160      const MatrixMap& matrix;
   1.161 -    typename MatrixMap::SecondKey row;
   1.162 +    typename MatrixMap::FirstKey row;
   1.163    };
   1.164  
   1.165    /// \brief Gives back a row view of the matrix map
   1.166 @@ -189,16 +106,99 @@
   1.167    /// Gives back a row view of the matrix map.
   1.168    template <typename MatrixMap>
   1.169    MatrixRowMap<MatrixMap> matrixRowMap(MatrixMap& matrixMap,
   1.170 -				       typename MatrixMap::SecondKey row) {
   1.171 +				       typename MatrixMap::FirstKey row) {
   1.172      return MatrixRowMap<MatrixMap>(matrixMap, row);
   1.173    }
   1.174  
   1.175    template <typename MatrixMap>
   1.176 -  ConstMatrixRowMap<MatrixMap> 
   1.177 -  matrixRowMap(const MatrixMap& matrixMap, typename MatrixMap::SecondKey row) {
   1.178 +  ConstMatrixRowMap<MatrixMap>
   1.179 +  matrixRowMap(const MatrixMap& matrixMap, typename MatrixMap::FirstKey row) {
   1.180      return ConstMatrixRowMap<MatrixMap>(matrixMap, row);
   1.181    }
   1.182  
   1.183 +  /// \brief Map for the row view of the matrix
   1.184 +  ///
   1.185 +  /// Map for the row view of the matrix.
   1.186 +  template <typename _MatrixMap>
   1.187 +  class MatrixColMap : public MapTraits<_MatrixMap> {
   1.188 +  public:
   1.189 +    typedef _MatrixMap MatrixMap;
   1.190 +    typedef typename MatrixMap::FirstKey Key;
   1.191 +    typedef typename MatrixMap::Value Value;
   1.192 +
   1.193 +    MatrixColMap(MatrixMap& _matrix, typename MatrixMap::SecondKey _col) 
   1.194 +      : matrix(_matrix), col(_col) {}
   1.195 +
   1.196 +    /// \brief Subscription operator
   1.197 +    ///
   1.198 +    /// Subscription operator.
   1.199 +    typename MapTraits<MatrixMap>::ReturnValue
   1.200 +    operator[](Key row) {
   1.201 +      return matrix(row, col);
   1.202 +    }
   1.203 +
   1.204 +    /// \brief Setter function
   1.205 +    ///
   1.206 +    /// Setter function.
   1.207 +    void set(Key row, const Value& val) {
   1.208 +      matrix.set(row, col, val);
   1.209 +    }
   1.210 +      
   1.211 +    /// \brief Subscription operator
   1.212 +    ///
   1.213 +    /// Subscription operator.
   1.214 +    typename MapTraits<MatrixMap>::ConstReturnValue
   1.215 +    operator[](Key row) const {
   1.216 +      return matrix(row, col);
   1.217 +    }
   1.218 +
   1.219 +  private:
   1.220 +    MatrixMap& matrix;
   1.221 +    typename MatrixMap::SecondKey col;
   1.222 +  };
   1.223 +
   1.224 +  /// \brief Map for the col view of the matrix
   1.225 +  ///
   1.226 +  /// Map for the col view of the matrix.
   1.227 +  template <typename _MatrixMap>
   1.228 +  class ConstMatrixColMap : public MapTraits<_MatrixMap> {
   1.229 +  public:
   1.230 +    typedef _MatrixMap MatrixMap;
   1.231 +    typedef typename MatrixMap::FirstKey Key;
   1.232 +    typedef typename MatrixMap::Value Value;
   1.233 +
   1.234 +    ConstMatrixColMap(const MatrixMap& _matrix, 
   1.235 +		      typename MatrixMap::SecondKey _col) 
   1.236 +      : matrix(_matrix), col(_col) {}
   1.237 +
   1.238 +    /// \brief Subscription operator
   1.239 +    ///
   1.240 +    /// Subscription operator.
   1.241 +    typename MapTraits<MatrixMap>::ConstReturnValue
   1.242 +    operator[](Key row) const {
   1.243 +      return matrix(row, col);
   1.244 +    }
   1.245 +
   1.246 +  private:
   1.247 +    const MatrixMap& matrix;
   1.248 +    typename MatrixMap::SecondKey col;
   1.249 +  };
   1.250 +
   1.251 +  /// \brief Gives back a col view of the matrix map
   1.252 +  ///
   1.253 +  /// Gives back a col view of the matrix map.
   1.254 +  template <typename MatrixMap>
   1.255 +  MatrixColMap<MatrixMap> matrixColMap(MatrixMap& matrixMap,
   1.256 +				       typename MatrixMap::SecondKey col) {
   1.257 +    return MatrixColMap<MatrixMap>(matrixMap, col);
   1.258 +  }
   1.259 +
   1.260 +  template <typename MatrixMap>
   1.261 +  ConstMatrixColMap<MatrixMap> 
   1.262 +  matrixColMap(const MatrixMap& matrixMap, typename MatrixMap::SecondKey col) {
   1.263 +    return ConstMatrixColMap<MatrixMap>(matrixMap, col);
   1.264 +  }
   1.265 +
   1.266    /// \brief Container for store values for each ordered pair of graph items
   1.267    ///
   1.268    /// This data structure can strore for each pairs of the same item
     2.1 --- a/test/matrix_maps_test.cc	Wed Nov 02 15:23:46 2005 +0000
     2.2 +++ b/test/matrix_maps_test.cc	Wed Nov 02 15:24:38 2005 +0000
     2.3 @@ -53,15 +53,15 @@
     2.4  	int val = urandom(100);
     2.5  	matrix.set(it, jt, val);
     2.6  	check(matrix(it, jt) == val, "Wrong assign");
     2.7 -	check(matrix(it, jt) == matrixColMap(matrix, it)[jt], "Wrong colMap");
     2.8 -	check(matrix(it, jt) == matrixRowMap(matrix, jt)[it], "Wrong rowMap");
     2.9 +	check(matrix(it, jt) == matrixRowMap(matrix, it)[jt], "Wrong rowMap");
    2.10 +	check(matrix(it, jt) == matrixColMap(matrix, jt)[it], "Wrong colMap");
    2.11        }
    2.12      }
    2.13      const IntMatrixMap& cm = matrix;
    2.14      for (Graph::NodeIt it(graph); it != INVALID; ++it) {
    2.15        for (Graph::NodeIt jt(graph); jt != INVALID; ++jt) {
    2.16 -	check(cm(it, jt) == matrixColMap(cm, it)[jt], "Wrong colMap");
    2.17 -	check(cm(it, jt) == matrixRowMap(cm, jt)[it], "Wrong rowMap");
    2.18 +	check(cm(it, jt) == matrixRowMap(cm, it)[jt], "Wrong rowMap");
    2.19 +	check(cm(it, jt) == matrixColMap(cm, jt)[it], "Wrong colMap");
    2.20        }
    2.21      }
    2.22    }
    2.23 @@ -95,15 +95,15 @@
    2.24  	matrix.set(it, jt, val);
    2.25  	check(matrix(it, jt) == val, "Wrong assign");
    2.26  	check(matrix(jt, it) == val, "Wrong assign");
    2.27 -	check(matrix(it, jt) == matrixColMap(matrix, it)[jt], "Wrong colMap");
    2.28 -	check(matrix(it, jt) == matrixRowMap(matrix, jt)[it], "Wrong rowMap");
    2.29 +	check(matrix(it, jt) == matrixRowMap(matrix, it)[jt], "Wrong rowMap");
    2.30 +	check(matrix(it, jt) == matrixColMap(matrix, jt)[it], "Wrong colMap");
    2.31        }
    2.32      }
    2.33      const IntMatrixMap& cm = matrix;
    2.34      for (Graph::NodeIt it(graph); it != INVALID; ++it) {
    2.35        for (Graph::NodeIt jt(graph); jt != INVALID; ++jt) {
    2.36 -	check(cm(it, jt) == matrixColMap(cm, it)[jt], "Wrong colMap");
    2.37 -	check(cm(it, jt) == matrixRowMap(cm, jt)[it], "Wrong rowMap");
    2.38 +	check(cm(it, jt) == matrixRowMap(cm, it)[jt], "Wrong rowMap");
    2.39 +	check(cm(it, jt) == matrixColMap(cm, jt)[it], "Wrong colMap");
    2.40        }
    2.41      }
    2.42    }