From: Guido Kanschat Date: Sun, 6 Mar 2005 01:04:49 +0000 (+0000) Subject: new TransposeTable class X-Git-Tag: v8.0.0~14523 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=93cd2970ed9d11b313d2ea873074419ca87b97ba;p=dealii.git new TransposeTable class git-svn-id: https://svn.dealii.org/trunk@10005 0785d39b-7218-0410-832d-ea1e28bc413d --- diff --git a/deal.II/base/include/base/table.h b/deal.II/base/include/base/table.h index 28075d7f52..d80c14f591 100644 --- a/deal.II/base/include/base/table.h +++ b/deal.II/base/include/base/table.h @@ -2,7 +2,7 @@ // $Id$ // Version: $Name$ // -// Copyright (C) 2000, 2001, 2002, 2003, 2004 by the deal.II authors +// Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005 by the deal.II authors // // This file is subject to QPL and may not be distributed // without copyright and license information. Please refer @@ -707,11 +707,12 @@ namespace internal * dimensions. Of course, this is not possible. * * The way out of the first problem (and partly the second one as - * well) is to have derived class for each value of N that have a - * constructor with the right number of arguments, one for each - * dimension. These then transform their arguments into the data type - * this class wants to see, both for construction as well as access - * through the operator() function. + * well) is to have a common base class TableBase and a derived class + * for each value of N. This derived class has a constructor + * with the correct number of arguments, namely N. These then + * transform their arguments into the data type the base class (this + * class in fact) uses in the constructor as well as in + * element access through operator() functions. * * The second problem is that we would like to allow access through a * sequence of operator[] calls. This mostly because, as said, @@ -724,7 +725,7 @@ namespace internal * namespace. * * - * @section TableComp Comparison with the Tensor class + *

Comparison with the Tensor class

* * In some way, this class is similar to the Tensor class, in * that it templatizes on the number of dimensions. However, there are @@ -1255,6 +1256,139 @@ class Table<2,T> : public TableBase<2,T> +/** + * A class representing a transpose two-dimensional table, i.e. a + * matrix of objects (not necessarily only numbers) in column first + * numbering (FORTRAN convention). + * + * This class copies the functions of Table<2,T>, but the element + * access and the dimensions will be for the transpose ordering of the + * data field in TableBase. + * + * @author Guido Kanschat, 2005 + */ +template +class TransposeTable : public TableBase<2,T> +{ + public: + /** + * Default constructor. Set all + * dimensions to zero. + */ + TransposeTable (); + + /** + * Constructor. Pass down the + * given dimensions to the base + * class. + */ + TransposeTable (const unsigned int size1, + const unsigned int size2); + + /** + * Reinitialize the object. This + * function is mostly here for + * compatibility with the earlier + * vector2d class. Passes + * down to the base class by + * converting the arguments to + * the data type requested by the + * base class. + */ + void reinit (const unsigned int size1, + const unsigned int size2); + + /** + * Direct access to one element + * of the table by specifying all + * indices at the same time. Range + * checks are performed. + * + * This version of the function + * only allows read access. + */ + const T & operator () (const unsigned int i, + const unsigned int j) const; + + + /** + * Direct access to one element + * of the table by specifying all + * indices at the same time. Range + * checks are performed. + * + * This version of the function + * allows read-write access. + */ + T & operator () (const unsigned int i, + const unsigned int j); + + + /** + * Number of rows. This function + * really makes only sense since + * we have a two-dimensional + * object here. + */ + unsigned int n_rows () const; + + /** + * Number of columns. This function + * really makes only sense since + * we have a two-dimensional + * object here. + */ + unsigned int n_cols () const; + + protected: + /** + * Return a read-write reference + * to the element (i,j). + * + * This function does no bounds + * checking and is only to be + * used internally and in + * functions already checked. + * + * These functions are mainly + * here for compatibility with a + * former implementation of these + * table classes for 2d arrays, + * then called vector2d. + */ + T & el (const unsigned int i, + const unsigned int j); + + /** + * Return the value of the + * element (i,j) as a + * read-only reference. + * + * This function does no bounds + * checking and is only to be + * used internally and in + * functions already checked. + * + * We return the requested value + * as a constant reference rather + * than by value since this + * object may hold data types + * that may be large, and we + * don't know here whether + * copying is expensive or not. + * + * These functions are mainly + * here for compatibility with a + * former implementation of these + * table classes for 2d arrays, + * then called vector2d. + */ + const T & el (const unsigned int i, + const unsigned int j) const; +}; + + + /** * A class representing a three-dimensional table of objects (not * necessarily only numbers). @@ -2429,7 +2563,7 @@ Table<1,T>::operator () (const unsigned int i) } - +//---------------------------------------------------------------------- template Table<2,T>::Table () @@ -2554,6 +2688,106 @@ Table<2,T>::n_cols () const +//---------------------------------------------------------------------- + +template +TransposeTable::TransposeTable () +{} + + + +template +TransposeTable::TransposeTable (const unsigned int size1, + const unsigned int size2) + : + TableBase<2,T> (TableIndices<2> (size2, size1)) +{} + + + +template +void +TransposeTable::reinit (const unsigned int size1, + const unsigned int size2) +{ + this->TableBase<2,T>::reinit (TableIndices<2> (size2, size1)); +} + + + +template +inline +const T & +TransposeTable::operator () (const unsigned int i, + const unsigned int j) const +{ + Assert (i < this->table_size[1], + ExcIndexRange (i, 0, this->table_size[1])); + Assert (j < this->table_size[0], + ExcIndexRange (j, 0, this->table_size[0])); + return this->val[j*this->table_size[1]+i]; +} + + + +template +inline +T & +TransposeTable::operator () (const unsigned int i, + const unsigned int j) +{ + Assert (i < this->table_size[1], + ExcIndexRange (i, 0, this->table_size[1])); + Assert (j < this->table_size[0], + ExcIndexRange (j, 0, this->table_size[0])); + return this->val[j*this->table_size[1]+i]; +} + + + +template +inline +const T & +TransposeTable::el (const unsigned int i, + const unsigned int j) const +{ + return this->val[j*this->table_size[1]+i]; +} + + + +template +inline +T & +TransposeTable::el (const unsigned int i, + const unsigned int j) +{ + return this->val[j*this->table_size[1]+i]; +} + + + +template +inline +unsigned int +TransposeTable::n_rows () const +{ + return this->table_size[1]; +} + + + +template +inline +unsigned int +TransposeTable::n_cols () const +{ + return this->table_size[0]; +} + + + +//---------------------------------------------------------------------- template diff --git a/tests/base/table.cc b/tests/base/table.cc index 8edab04d8e..5f662ed777 100644 --- a/tests/base/table.cc +++ b/tests/base/table.cc @@ -2,7 +2,7 @@ // $Id$ // Version: $Name$ // -// Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004 by the deal.II authors +// Copyright (C) 1998 - 2005 by the deal.II authors // // This file is subject to QPL and may not be distributed // without copyright and license information. Please refer @@ -40,7 +40,7 @@ main () deallog.attach(logfile); deallog.depth_console(0); - // first check: a square table + // a square table if (true) { Table<2,double> Td(3,3); @@ -70,7 +70,7 @@ main () }; }; - // second check: a rectangular table + // a rectangular table if (true) { Table<2,double> Td(4,3); @@ -105,7 +105,37 @@ main () }; - // third check: a 1d-table + // a transposed rectangular table + if (true) + { + TransposeTable Td(4,3); + TransposeTable Ti(4,3); + + // Fill the float matrix in row + // first ordering + for (unsigned int i=0; i<12; ++i) + { + Td(i/3,i%3) = entries[i]; + }; + + // This fills the integer + // matrix in column first + // ordering + Ti.fill(entries); + // Output both matrices... + // They should be different + for (unsigned int i=0; i<4; ++i) + { + for (unsigned int j=0; j<3; ++j) + { + logfile << '\t' << Ti(i,j) << '/' << Td(i,j); + } + logfile << std::endl; + } + } + + + // a 1d-table if (true) { const unsigned int N=10; @@ -128,7 +158,7 @@ main () }; }; - // fourth check: a 3d-table + // a 3d-table if (true) { const unsigned int I=4, J=3, K=2; @@ -170,7 +200,7 @@ main () ExcInternalError()); }; - // fifth check: a 4d-table + // a 4d-table if (true) { const unsigned int I=5, J=4, K=3, L=2; @@ -223,7 +253,7 @@ main () static_cast(L))); }; - // sixth check: a 5d-table + // 5d-table if (true) { const unsigned int I=6, J=2, K=3, L=4, M=5; @@ -272,7 +302,7 @@ main () static_cast(M))); }; - // seventh check: a 6d-table + // a 6d-table if (true) { const unsigned int I=6, J=2, K=4, L=3, M=5, N=7;