]> https://gitweb.dealii.org/ - dealii-svn.git/commitdiff
new class SliceVector
authorguido <guido@0785d39b-7218-0410-832d-ea1e28bc413d>
Thu, 16 Sep 2004 09:43:55 +0000 (09:43 +0000)
committerguido <guido@0785d39b-7218-0410-832d-ea1e28bc413d>
Thu, 16 Sep 2004 09:43:55 +0000 (09:43 +0000)
git-svn-id: https://svn.dealii.org/trunk@9621 0785d39b-7218-0410-832d-ea1e28bc413d

deal.II/base/include/base/slice_vector.h [new file with mode: 0644]
tests/base/Makefile
tests/base/slice_vector.cc [new file with mode: 0644]
tests/results/i686-pc-linux-gnu+gcc3.2/base/slice_vector.output [new file with mode: 0644]

diff --git a/deal.II/base/include/base/slice_vector.h b/deal.II/base/include/base/slice_vector.h
new file mode 100644 (file)
index 0000000..95b9f5b
--- /dev/null
@@ -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 <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
+
index f4e763a76361cc5ac2ea02578f1bc5c991de8564..1a7520906ecf535ee87434b5feba5e7d8c176787 100644 (file)
@@ -29,6 +29,7 @@ polynomial1d.exe   : polynomial1d.g.$(OBJEXT)                       $(lib-base.g
 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)
@@ -39,7 +40,8 @@ hierarchical.exe   : hierarchical.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
diff --git a/tests/base/slice_vector.cc b/tests/base/slice_vector.cc
new file mode 100644 (file)
index 0000000..5dce51d
--- /dev/null
@@ -0,0 +1,83 @@
+#//---------------------------------------------------------------------
+//    $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);
+}
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 (file)
index 0000000..271b2dc
--- /dev/null
@@ -0,0 +1,25 @@
+
+       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!!!

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.