void reinit (const IndexSet &locally_stored_indices,
const bool omit_zeroing_entries = false);
+#ifdef DEAL_II_WITH_CXX11
+ /**
+ * Apply the functor @p func to each element of the vector. The functor
+ * should look like
+ * <code>
+ * struct Functor
+ * {
+ * void operator() (Number &value);
+ * };
+ * </code>
+ *
+ * @note This function requires C++11 and read_write_vector.templates.h
+ * needs to be included.
+ */
+ template <typename Functor>
+ void apply(const Functor &func);
+#endif
+
/**
* Swap the contents of this vector and the other vector @p v. One could
* do this operation with a temporary variable and copying over the data
* Make all other ReadWriteVector types friends.
*/
template <typename Number2> friend class ReadWriteVector;
+
+#ifdef DEAL_II_WITH_CXX11
+ private:
+ /**
+ * This class provides a wrapper around a Functor which acts on
+ * single elements of the vector. This is necessary to use
+ * tbb::parallel_for which requires a TBBForFunctor.
+ */
+ template <typename Functor>
+ class FunctorTemplate
+ {
+ public:
+ /**
+ * Constructor. Take a functor and store a copy of it.
+ */
+ FunctorTemplate(ReadWriteVector<Number> &parent,
+ const Functor &functor);
+
+ /**
+ * Evaluate the element with the stored copy of the functor.
+ */
+ virtual void operator() (const size_type begin,
+ const size_type end);
+
+ private:
+ /**
+ * Alias to the ReadWriteVector object that owns the FunctorTemplate.
+ */
+ ReadWriteVector &parent;
+
+ /**
+ * Copy of the functor.
+ */
+ const Functor &functor;
+ };
+#endif
};
/*@}*/
}
}
+
+
+#ifdef DEAL_II_WITH_CXX11
+ template <typename Number>
+ template <typename Functor>
+ inline
+ ReadWriteVector<Number>::FunctorTemplate<Functor>::FunctorTemplate(
+ ReadWriteVector<Number> &parent,
+ const Functor &functor)
+ :
+ parent(parent),
+ functor(functor)
+ {}
+
+
+
+ template <typename Number>
+ template <typename Functor>
+ void
+ ReadWriteVector<Number>::FunctorTemplate<Functor>::operator() (const size_type begin,
+ const size_type end)
+ {
+ for (size_type i=begin; i<end; ++i)
+ functor(parent.val[i]);
+ }
+#endif
+
#endif // ifndef DOXYGEN
} // end of namespace LinearAlgebra
+#ifdef DEAL_II_WITH_CXX11
+ template <typename Number>
+ template <typename Functor>
+ void
+ ReadWriteVector<Number>::apply(const Functor &func)
+ {
+ FunctorTemplate<Functor> functor(*this, func);
+ internal::parallel_for(functor, n_elements(), thread_loop_partitioner);
+ }
+#endif
+
+
+
template <typename Number>
ReadWriteVector<Number> &
ReadWriteVector<Number>::operator= (const ReadWriteVector<Number> &in_vector)
--- /dev/null
+// ---------------------------------------------------------------------
+//
+// Copyright (C) 2016 by the deal.II authors
+//
+// This file is part of the deal.II library.
+//
+// The deal.II library is free software; you can use it, redistribute
+// it, and/or modify it under the terms of the GNU Lesser General
+// Public License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+// The full text of the license can be found in the file LICENSE at
+// the top level of the deal.II distribution.
+//
+// ---------------------------------------------------------------------
+
+// Check the functor interface
+
+
+#include "../tests.h"
+#include <deal.II/base/index_set.h>
+#include <deal.II/base/logstream.h>
+#include <deal.II/lac/read_write_vector.h>
+#include <deal.II/lac/read_write_vector.templates.h>
+
+#include <fstream>
+
+struct Functor
+{
+ void operator() (double &value) const
+ {
+ value *= 2.;
+ }
+};
+
+void test()
+{
+ const unsigned int size = 25;
+ LinearAlgebra::ReadWriteVector<double> vector(size);
+ for (unsigned int i=0; i<size; ++i)
+ vector[i] = i;
+
+ Functor functor;
+ vector.apply(functor);
+ for (unsigned int i=0; i<size; ++i)
+ deallog << vector[i] << std::endl;
+}
+
+int main()
+{
+ std::ofstream logfile("output");
+ deallog.attach(logfile);
+ test();
+
+ return 0;
+}