From: Guido Kanschat Date: Mon, 21 May 2001 18:01:21 +0000 (+0000) Subject: new class vector2d X-Git-Tag: v8.0.0~19111 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b7d35bcf34f7a358601fc4881a4379c20f01f511;p=dealii.git new class vector2d git-svn-id: https://svn.dealii.org/trunk@4683 0785d39b-7218-0410-832d-ea1e28bc413d --- diff --git a/deal.II/base/include/base/vector2d.h b/deal.II/base/include/base/vector2d.h new file mode 100644 index 0000000000..eb249eb1aa --- /dev/null +++ b/deal.II/base/include/base/vector2d.h @@ -0,0 +1,454 @@ +//----------------------------------------------------------------------- +// $Id$ +// Version: $Name$ +// +// Copyright (C) 2000, 2001 by the deal.II authors +// +// This file is subject to QPL and may not be distributed +// without copyright and license information. Please refer +// to the file deal.II/doc/license.html for the text and +// further information on this license. +// +//----------------------------------------------------------------------- +#ifndef __deal2__vector2d_h +#define __deal2__vector2d_h + +#include +#include + +/** + * Two-dimensional array of arbitrary data. + * + * This is an implementation of a two-dimensional array with access by + * pairs of coordinates. + * + * The name is chosen to be @p{vector2d} to be conformant with the + * standard C++ classes. This, although array would have been + * preferrable, since a vector should be element of a vector space. + * + * @author Guido Kanschat, 2001 + */ +template +class vector2d : public Subscriptor +{ + public: + /** + * Constructor for a quadratic + * @p{rows} by @p{rows} array. The + * standard constructor creates an + * empty object. + */ + vector2d (const unsigned int rows = 0); + + /** + * Constructor for a @p{rows} by + * @p{cols} array. + */ + vector2d (const unsigned int rows, + const unsigned int cols); + + /** + * Copy-constructor for deep copy. + */ + vector2d (const vector2d&); + /** + * Constructor initializing from + * an array of data elements. The array + * is arranged line by line. No + * range checking is performed. + */ + vector2d (const unsigned int rows, + const unsigned int cols, + const T* entries); + + /** + * Assignment operator. + * Copy all elements of @p{src} + * into the matrix. The size is + * adjusted if needed. + * + * We can't use the other, templatized + * version since if we don't declare + * this one, the compiler will happily + * generate a predefined copy + * operator which is not what we want. + */ + vector2d& operator = (const vector2d& src); + + /** + * Assignment operator. + * Copy all elements of @p{src} + * into the array. The size is + * adjusted if needed. + * + * This function requires that the + * type @p{T2} is convertible to + * @p{T}. + */ + template + vector2d& operator = (const vector2d& src); + + /** + * Set dimension to $m\times n$ and + * allocate memory if necessary. Forget + * the previous content of the array. + */ + void reinit (const unsigned int m, + const unsigned int n); + + /** + * Set dimension to $n\times n$ and + * allocate memory if necessary. Forget + * the previous content of the array. + */ + void reinit (const unsigned int n); + + /** + * Set the dimensions to the same + * as another array. The other + * array will not be copied, + * though. The entries of this + * array will be zero. + */ + template + void reinit (const vector2d&); + + /** + * Number of rows. + */ + unsigned int n_rows () const; + + /** + * Number of columns. + */ + unsigned int n_cols () const; + + /** + * Return the value of the + * element at position @p{(i,j)}. + */ + T operator() (const unsigned int i, + const unsigned int j) const; + + /** + * Return a read-write reference to + * the element at position @p{(i,j)}. + */ + T & operator() (const unsigned int i, + const unsigned int j); + + + /** + * Set all entries to their + * default value (zero). + */ + void clear (); + + /** + * Fill array with an array of elements. + * The array is arranged line by line. No + * range checking is performed. + */ + template + void fill (const T2* entries); + + /** + * Determine an estimate for the + * memory consumption (in bytes) + * of this object. + */ + unsigned int memory_consumption () const; + + protected: + /** + * Return a read-write reference to the + * element @p{(i,j)}. + * + * This function does no bounds + * checking and is only to be used + * internally and in functions + * already checked. + */ + T & el (const unsigned int i, const unsigned int j); + + /** + * Return the value of the element @p{(i,j)}. + * + * This function does no bounds checking + * and is only to be used + * internally and in functions + * already checked. + */ + T el (const unsigned int i, const unsigned int j) const; + + /** + * Direct read-only access to + * data field. Used by + * @ref{FullMatrix} (there even + * with a cast from const), + * otherwise, keep away! + */ + const T* data () const; + + private: + /** + * Component-array. + */ + T* val; + /** + * Size of array. + */ + unsigned int val_size; + + /** + * Number of Columns + */ + unsigned int num_cols; + + /** + * Number of Rows + */ + unsigned int num_rows; +}; + + +template +inline +void +vector2d::clear () +{ + if (val != 0) + std::fill_n (val, num_rows*num_cols, T()); +}; + + +template +template +inline +void +vector2d::fill (const T2* entries) +{ + if (val_size != 0) + std::copy (entries, entries+(num_rows*num_cols), val); +} + + +template +void +vector2d::reinit (const unsigned int mm, + const unsigned int nn) +{ + num_cols = nn; + num_rows = mm; + + // if zero size was given: free all + // memory + if ((num_cols==0) || (num_rows == 0)) + { + if (val != 0) + delete[] val; + + val = 0; + val_size = 0; + + // set both sizes to zero, even + // if one was previously + // nonzero. This simplifies + // some Assertions. + num_cols = num_rows = 0; + + return; + }; + + // if new size is nonzero: + // if necessary: allocate + // additional memory + if (val_size +void +vector2d::reinit (const unsigned int n) +{ + reinit (n, n); +}; + + +template +template +void +vector2d::reinit (const vector2d &B) +{ + reinit (B.n_rows(), B.n_cols()); +}; + + +template +vector2d::vector2d (const unsigned int m, + const unsigned int n) : + val (0), + val_size (0), + num_cols (0), + num_rows (0) +{ + reinit (m,n); +}; + + +template +vector2d::vector2d (const unsigned int m) : + val (0), + val_size (0), + num_cols (0), + num_rows (0) +{ + reinit (m,m); +}; + + + +template +vector2d::vector2d (const unsigned int m, + const unsigned int n, + const T* entries) : + val (0), + val_size (0), + num_cols (0), + num_rows (0) +{ + reinit (m,n); + + if (num_cols*num_rows != 0) + std::copy (entries, entries+num_rows*num_cols, val); +}; + + +template +vector2d::vector2d (const vector2d &m) : + Subscriptor (), + val (0), + val_size (0), + num_cols (0), + num_rows (0) +{ + reinit (m.num_rows, m.num_cols); + if (num_cols*num_rows != 0) + std::copy (&m.val[0], &m.val[num_rows*num_cols], + &val[0]); +}; + + +template +vector2d& +vector2d::operator = (const vector2d& m) +{ + reinit (m); + if (num_cols*num_rows != 0) + std::copy (&m.val[0], &m.val[num_rows*num_cols], + &val[0]); + + return *this; +} + + +template +template +vector2d& +vector2d::operator = (const vector2d& m) +{ + reinit(m); + if (num_cols*num_rows != 0) + copy (&m.val[0], &m.val[num_rows*num_cols], + &val[0]); + + return *this; +} + + +template +inline +unsigned int +vector2d::n_rows () const +{ + return num_rows; + +} + + +template +inline +unsigned int +vector2d::n_cols () const +{ + return num_cols; + +} + +template +inline T & +vector2d::el (const unsigned int i, const unsigned int j) +{ + return val[i*n_cols()+j]; +}; + + + +template +inline T +vector2d::el (const unsigned int i, const unsigned int j) const +{ + return val[i*n_cols()+j]; +}; + + +template +inline T +vector2d::operator() (const unsigned int i, const unsigned int j) const +{ + Assert (i +inline T & +vector2d::operator() (const unsigned int i, const unsigned int j) +{ + Assert (i +inline +const T * +vector2d::data () const +{ + return val; +} + + +template +inline +unsigned int +vector2d::memory_consumption () const +{ + return sizeof (*this) + val_size * sizeof (T); +} + +#endif