--- /dev/null
+//---------------------------------------------------------------------
+// $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 <base/config.h>
+#include <base/exceptions.h>
+
+/**
+ * Filter a range out of any object having a random access operator
+ * <tt>[] (unsigned int)</tt>.
+ *
+ * The use of this object is straight forward. It reduplicates the
+ * random access operator of the <tt>VECTOR</tt> 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<int>& v)
+ * {
+ * const SliceVector slice(v,...);
+ * ...
+ * }
+ * @endcode
+ *
+ * @author Guido Kanschat, 2004
+ */
+template <class VECTOR>
+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 <class VECTOR>
+inline
+SliceVector<VECTOR>::SliceVector(VECTOR& v)
+ : v(v), start(0), length(v.size())
+{}
+
+
+template <class VECTOR>
+inline
+SliceVector<VECTOR>::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 <class VECTOR>
+inline
+unsigned int
+SliceVector<VECTOR>::size() const
+{
+ return length;
+}
+
+
+template <class VECTOR>
+inline
+typename VECTOR::reference
+SliceVector<VECTOR>::operator[](unsigned int i)
+{
+ Assert ((i<length), ExcIndexRange(i, 0, length));
+
+ return v[start+i];
+}
+
+
+template <class VECTOR>
+inline
+typename VECTOR::const_reference
+SliceVector<VECTOR>::operator[](unsigned int i) const
+{
+ Assert ((i<length), ExcIndexRange(i, 0, length));
+
+ return v[start+i];
+}
+
+
+#endif
+
quadrature_test.exe: quadrature_test.g.$(OBJEXT) $(lib-base.g)
quadrature_selector.exe: quadrature_selector.g.$(OBJEXT) $(lib-base.g)
reference.exe : reference.g.$(OBJEXT) $(lib-base.g)
+slice_vector.exe : slice_vector.g.$(OBJEXT) $(lib-base.g)
table.exe : table.g.$(OBJEXT) $(lib-base.g)
tensor.exe : tensor.g.$(OBJEXT) $(lib-base.g)
timer.exe : timer.g.$(OBJEXT) $(lib-base.g)
bdm.exe : bdm.g.$(OBJEXT) $(lib-base.g)
auto_derivative_function.exe : auto_derivative_function.g.$(OBJEXT) $(libraries) $(lib-base.g)
-tests = logtest reference quadrature_test quadrature_selector table tensor \
+tests = logtest reference quadrature_test quadrature_selector \
+ slice_vector table tensor \
timer threads polynomial1d polynomial_test \
auto_derivative_function anisotropic_1 anisotropic_2 \
hierarchical data_out_base
--- /dev/null
+#//---------------------------------------------------------------------
+// $Id$
+// Version: $Name$
+//
+// Copyright (C) 2004 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.
+//
+//---------------------------------------------------------------------
+
+
+#include <base/slice_vector.h>
+#include <base/logstream.h>
+
+#include <vector>
+#include <fstream>
+#include <iostream>
+
+void f(const std::vector<int>& v)
+{
+ const SliceVector<const std::vector<int> > s(v,2,3);
+
+ for (unsigned int i=0;i<s.size();++i)
+ std::cerr << '\t' << s[i];
+ std::cerr << std::endl;
+}
+
+
+int main()
+{
+ std::ofstream logfile("slice_vector.output");
+ deallog.attach(logfile);
+ deallog.depth_console(0);
+
+ // we do something rather weird in
+ // this file: bind the buffer of
+ // cerr to the log file, so that
+ // the output of the Assert* macros
+ // that are written to std::cerr
+ // end up in the logfiles.
+ //
+ // so make sure we store a pointer
+ // to the old buffer, switch to the
+ // buffer of the logfile, and at
+ // the end of main() switch
+ // back. note that if we don't
+ // switch back, we get a segfault
+ // later on in the destruction of
+ // std::cerr, since it tries to do
+ // something with its buffer, but
+ // that is already gone by then
+#if __GNUC__ != 2
+ std::basic_streambuf<char> *old_cerr_buf = std::cerr.rdbuf();
+#else
+ streambuf *old_cerr_buf = std::cerr.rdbuf();
+#endif
+ std::cerr.rdbuf(logfile.rdbuf());
+
+ std::vector<int> v(7);
+
+ for (unsigned int i=0;i<v.size();++i)
+ v[i] = i;
+
+ SliceVector<std::vector<int> > s(v, 3, 4);
+
+ for (unsigned int i=0;i<s.size();++i)
+ s[i] = i;
+
+ for (unsigned int i=0;i<v.size();++i)
+ std::cerr << '\t' << v[i];
+ std::cerr << std::endl;
+
+ f(v);
+
+ SliceVector<std::vector<int> > s2(v, 3, 5);
+ int n = s[4];
+ n += 3;
+
+ std::cerr.rdbuf(old_cerr_buf);
+}
--- /dev/null
+
+ 0 1 2 0 1 2 3
+ 2 0 1
+--------------------------------------------------------
+An error occurred in file <slice_vector.h> in function
+ SliceVector<VECTOR>::SliceVector(VECTOR&, unsigned int, unsigned int) [with VECTOR = std::vector<int, std::allocator<int> >]
+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 <slice_vector.h> in function
+ typename VECTOR::reference SliceVector<VECTOR>::operator[](unsigned int) [with VECTOR = std::vector<int, std::allocator<int> >]
+The violated condition was:
+ (i<length)
+The name and call sequence of the exception was:
+ ExcIndexRange(i, 0, length)
+Additional Information:
+Index 4 is not in [0,4[
+--------------------------------------------------------
+DEAL::Abort!!!