From: guido Date: Thu, 16 Sep 2004 09:43:55 +0000 (+0000) Subject: new class SliceVector X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2ed321fd570694ba7ddd0ef50f682e02e5ff90a1;p=dealii-svn.git new class SliceVector git-svn-id: https://svn.dealii.org/trunk@9621 0785d39b-7218-0410-832d-ea1e28bc413d --- diff --git a/deal.II/base/include/base/slice_vector.h b/deal.II/base/include/base/slice_vector.h new file mode 100644 index 0000000000..95b9f5b456 --- /dev/null +++ b/deal.II/base/include/base/slice_vector.h @@ -0,0 +1,113 @@ +//--------------------------------------------------------------------- +// $Id$ +// Version: $Name$ +// +// Copyright (C) 2004 by the deal 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__slice_vector_h +#define __deal2__slice_vector_h + + +#include +#include + +/** + * Filter a range out of any object having a random access operator + * [] (unsigned int). + * + * The use of this object is straight forward. It reduplicates the + * random access operator of the VECTOR and adds an offset to + * every index. + * + * Some precautions have to be taken if it is used for a constant + * vector: the SliceVector object has to be constant, too. The + * appropriate initalization sequence is like this: + * + * @code + * void f(const std::vector& v) + * { + * const SliceVector slice(v,...); + * ... + * } + * @endcode + * + * @author Guido Kanschat, 2004 + */ +template +class SliceVector +{ + public: + SliceVector(VECTOR& v); + SliceVector(VECTOR& v, + unsigned int start, + unsigned int length); + + unsigned int size() const; + + typename VECTOR::reference operator[] (unsigned int); + typename VECTOR::const_reference operator[] (unsigned int) const; + private: + VECTOR& v; + unsigned int start; + unsigned int length; +}; + + +template +inline +SliceVector::SliceVector(VECTOR& v) + : v(v), start(0), length(v.size()) +{} + + +template +inline +SliceVector::SliceVector(VECTOR& v, + unsigned int start, + unsigned int length) + : v(v), start(start), length(length) +{ + Assert((start+length<=v.size()), + ExcIndexRange(length, 0, v.size()-start+1)); +} + + +template +inline +unsigned int +SliceVector::size() const +{ + return length; +} + + +template +inline +typename VECTOR::reference +SliceVector::operator[](unsigned int i) +{ + Assert ((i +inline +typename VECTOR::const_reference +SliceVector::operator[](unsigned int i) const +{ + Assert ((i +#include + +#include +#include +#include + +void f(const std::vector& v) +{ + const SliceVector > s(v,2,3); + + for (unsigned int i=0;i *old_cerr_buf = std::cerr.rdbuf(); +#else + streambuf *old_cerr_buf = std::cerr.rdbuf(); +#endif + std::cerr.rdbuf(logfile.rdbuf()); + + std::vector v(7); + + for (unsigned int i=0;i > s(v, 3, 4); + + for (unsigned int i=0;i > s2(v, 3, 5); + int n = s[4]; + n += 3; + + std::cerr.rdbuf(old_cerr_buf); +} diff --git a/tests/results/i686-pc-linux-gnu+gcc3.2/base/slice_vector.output b/tests/results/i686-pc-linux-gnu+gcc3.2/base/slice_vector.output new file mode 100644 index 0000000000..271b2dc929 --- /dev/null +++ b/tests/results/i686-pc-linux-gnu+gcc3.2/base/slice_vector.output @@ -0,0 +1,25 @@ + + 0 1 2 0 1 2 3 + 2 0 1 +-------------------------------------------------------- +An error occurred in file in function + SliceVector::SliceVector(VECTOR&, unsigned int, unsigned int) [with VECTOR = std::vector >] +The violated condition was: + (start+length<=v.size()) +The name and call sequence of the exception was: + ExcIndexRange(length, 0, v.size()-start+1) +Additional Information: +Index 5 is not in [0,5[ +-------------------------------------------------------- +DEAL::Abort!!! +-------------------------------------------------------- +An error occurred in file in function + typename VECTOR::reference SliceVector::operator[](unsigned int) [with VECTOR = std::vector >] +The violated condition was: + (i