# HG changeset patch # User deba # Date 1130945078 0 # Node ID a2a454f1232dc0b9b79ab443b42594a0cbf4b82a # Parent 5c76ebbb48187483e4ac8a21c53126592cbbf612 Swap col and row map diff -r 5c76ebbb4818 -r a2a454f1232d lemon/matrix_maps.h --- a/lemon/matrix_maps.h Wed Nov 02 15:23:46 2005 +0000 +++ b/lemon/matrix_maps.h Wed Nov 02 15:24:38 2005 +0000 @@ -35,98 +35,14 @@ /// /// Map for the coloumn view of the matrix. template - class MatrixColMap : public MapTraits<_MatrixMap> { + class MatrixRowMap : public MapTraits<_MatrixMap> { public: typedef _MatrixMap MatrixMap; typedef typename MatrixMap::SecondKey Key; typedef typename MatrixMap::Value Value; - MatrixColMap(MatrixMap& _matrix, typename MatrixMap::FirstKey _col) - : matrix(_matrix), col(_col) {} - - /// \brief Subscription operator - /// - /// Subscription operator. - typename MapTraits::ReturnValue - operator[](Key row) { - return matrix(col, row); - } - - /// \brief Setter function - /// - /// Setter function. - void set(Key row, const Value& val) { - matrix.set(col, row, val); - } - - /// \brief Subscription operator - /// - /// Subscription operator. - typename MapTraits::ConstReturnValue - operator[](Key row) const { - return matrix(col, row); - } - - private: - MatrixMap& matrix; - typename MatrixMap::FirstKey col; - }; - - /// \brief Map for the coloumn view of the matrix - /// - /// Map for the coloumn view of the matrix. - template - class ConstMatrixColMap : public MapTraits<_MatrixMap> { - public: - typedef _MatrixMap MatrixMap; - typedef typename MatrixMap::SecondKey Key; - typedef typename MatrixMap::Value Value; - - - ConstMatrixColMap(const MatrixMap& _matrix, - typename MatrixMap::FirstKey _col) - : matrix(_matrix), col(_col) {} - - /// \brief Subscription operator - /// - /// Subscription operator. - typename MapTraits::ConstReturnValue - operator[](Key row) const { - return matrix(col, row); - } - - private: - const MatrixMap& matrix; - typename MatrixMap::FirstKey col; - }; - - /// \brief Gives back a coloumn view of the matrix map - /// - /// Gives back a coloumn view of the matrix map. - template - MatrixColMap matrixColMap(MatrixMap& matrixMap, - typename MatrixMap::FirstKey col) { - return MatrixColMap(matrixMap, col); - } - - template - ConstMatrixColMap - matrixColMap(const MatrixMap& matrixMap, typename MatrixMap::FirstKey col) { - return ConstMatrixColMap(matrixMap, col); - } - - /// \brief Map for the row view of the matrix - /// - /// Map for the row view of the matrix. - template - class MatrixRowMap : public MapTraits<_MatrixMap> { - public: - typedef _MatrixMap MatrixMap; - typedef typename MatrixMap::FirstKey Key; - typedef typename MatrixMap::Value Value; - - MatrixRowMap(MatrixMap& _matrix, typename MatrixMap::SecondKey _row) + MatrixRowMap(MatrixMap& _matrix, typename MatrixMap::FirstKey _row) : matrix(_matrix), row(_row) {} /// \brief Subscription operator @@ -134,14 +50,14 @@ /// Subscription operator. typename MapTraits::ReturnValue operator[](Key col) { - return matrix(col, row); + return matrix(row, col); } /// \brief Setter function /// /// Setter function. void set(Key col, const Value& val) { - matrix.set(col, row, val); + matrix.set(row, col, val); } /// \brief Subscription operator @@ -149,12 +65,12 @@ /// Subscription operator. typename MapTraits::ConstReturnValue operator[](Key col) const { - return matrix(col, row); + return matrix(row, col); } private: MatrixMap& matrix; - typename MatrixMap::SecondKey row; + typename MatrixMap::FirstKey row; }; /// \brief Map for the row view of the matrix @@ -164,11 +80,12 @@ class ConstMatrixRowMap : public MapTraits<_MatrixMap> { public: typedef _MatrixMap MatrixMap; - typedef typename MatrixMap::FirstKey Key; + typedef typename MatrixMap::SecondKey Key; typedef typename MatrixMap::Value Value; + ConstMatrixRowMap(const MatrixMap& _matrix, - typename MatrixMap::SecondKey _row) + typename MatrixMap::FirstKey _row) : matrix(_matrix), row(_row) {} /// \brief Subscription operator @@ -176,12 +93,12 @@ /// Subscription operator. typename MapTraits::ConstReturnValue operator[](Key col) const { - return matrix(col, row); + return matrix(row, col); } private: const MatrixMap& matrix; - typename MatrixMap::SecondKey row; + typename MatrixMap::FirstKey row; }; /// \brief Gives back a row view of the matrix map @@ -189,16 +106,99 @@ /// Gives back a row view of the matrix map. template MatrixRowMap matrixRowMap(MatrixMap& matrixMap, - typename MatrixMap::SecondKey row) { + typename MatrixMap::FirstKey row) { return MatrixRowMap(matrixMap, row); } template - ConstMatrixRowMap - matrixRowMap(const MatrixMap& matrixMap, typename MatrixMap::SecondKey row) { + ConstMatrixRowMap + matrixRowMap(const MatrixMap& matrixMap, typename MatrixMap::FirstKey row) { return ConstMatrixRowMap(matrixMap, row); } + /// \brief Map for the row view of the matrix + /// + /// Map for the row view of the matrix. + template + class MatrixColMap : public MapTraits<_MatrixMap> { + public: + typedef _MatrixMap MatrixMap; + typedef typename MatrixMap::FirstKey Key; + typedef typename MatrixMap::Value Value; + + MatrixColMap(MatrixMap& _matrix, typename MatrixMap::SecondKey _col) + : matrix(_matrix), col(_col) {} + + /// \brief Subscription operator + /// + /// Subscription operator. + typename MapTraits::ReturnValue + operator[](Key row) { + return matrix(row, col); + } + + /// \brief Setter function + /// + /// Setter function. + void set(Key row, const Value& val) { + matrix.set(row, col, val); + } + + /// \brief Subscription operator + /// + /// Subscription operator. + typename MapTraits::ConstReturnValue + operator[](Key row) const { + return matrix(row, col); + } + + private: + MatrixMap& matrix; + typename MatrixMap::SecondKey col; + }; + + /// \brief Map for the col view of the matrix + /// + /// Map for the col view of the matrix. + template + class ConstMatrixColMap : public MapTraits<_MatrixMap> { + public: + typedef _MatrixMap MatrixMap; + typedef typename MatrixMap::FirstKey Key; + typedef typename MatrixMap::Value Value; + + ConstMatrixColMap(const MatrixMap& _matrix, + typename MatrixMap::SecondKey _col) + : matrix(_matrix), col(_col) {} + + /// \brief Subscription operator + /// + /// Subscription operator. + typename MapTraits::ConstReturnValue + operator[](Key row) const { + return matrix(row, col); + } + + private: + const MatrixMap& matrix; + typename MatrixMap::SecondKey col; + }; + + /// \brief Gives back a col view of the matrix map + /// + /// Gives back a col view of the matrix map. + template + MatrixColMap matrixColMap(MatrixMap& matrixMap, + typename MatrixMap::SecondKey col) { + return MatrixColMap(matrixMap, col); + } + + template + ConstMatrixColMap + matrixColMap(const MatrixMap& matrixMap, typename MatrixMap::SecondKey col) { + return ConstMatrixColMap(matrixMap, col); + } + /// \brief Container for store values for each ordered pair of graph items /// /// This data structure can strore for each pairs of the same item diff -r 5c76ebbb4818 -r a2a454f1232d test/matrix_maps_test.cc --- a/test/matrix_maps_test.cc Wed Nov 02 15:23:46 2005 +0000 +++ b/test/matrix_maps_test.cc Wed Nov 02 15:24:38 2005 +0000 @@ -53,15 +53,15 @@ int val = urandom(100); matrix.set(it, jt, val); check(matrix(it, jt) == val, "Wrong assign"); - check(matrix(it, jt) == matrixColMap(matrix, it)[jt], "Wrong colMap"); - check(matrix(it, jt) == matrixRowMap(matrix, jt)[it], "Wrong rowMap"); + check(matrix(it, jt) == matrixRowMap(matrix, it)[jt], "Wrong rowMap"); + check(matrix(it, jt) == matrixColMap(matrix, jt)[it], "Wrong colMap"); } } const IntMatrixMap& cm = matrix; for (Graph::NodeIt it(graph); it != INVALID; ++it) { for (Graph::NodeIt jt(graph); jt != INVALID; ++jt) { - check(cm(it, jt) == matrixColMap(cm, it)[jt], "Wrong colMap"); - check(cm(it, jt) == matrixRowMap(cm, jt)[it], "Wrong rowMap"); + check(cm(it, jt) == matrixRowMap(cm, it)[jt], "Wrong rowMap"); + check(cm(it, jt) == matrixColMap(cm, jt)[it], "Wrong colMap"); } } } @@ -95,15 +95,15 @@ matrix.set(it, jt, val); check(matrix(it, jt) == val, "Wrong assign"); check(matrix(jt, it) == val, "Wrong assign"); - check(matrix(it, jt) == matrixColMap(matrix, it)[jt], "Wrong colMap"); - check(matrix(it, jt) == matrixRowMap(matrix, jt)[it], "Wrong rowMap"); + check(matrix(it, jt) == matrixRowMap(matrix, it)[jt], "Wrong rowMap"); + check(matrix(it, jt) == matrixColMap(matrix, jt)[it], "Wrong colMap"); } } const IntMatrixMap& cm = matrix; for (Graph::NodeIt it(graph); it != INVALID; ++it) { for (Graph::NodeIt jt(graph); jt != INVALID; ++jt) { - check(cm(it, jt) == matrixColMap(cm, it)[jt], "Wrong colMap"); - check(cm(it, jt) == matrixRowMap(cm, jt)[it], "Wrong rowMap"); + check(cm(it, jt) == matrixRowMap(cm, it)[jt], "Wrong rowMap"); + check(cm(it, jt) == matrixColMap(cm, jt)[it], "Wrong colMap"); } } }