]> https://gitweb.dealii.org/ - dealii.git/commitdiff
AlignedVector: add a range constructor.
authorDavid Wells <drwells@email.unc.edu>
Thu, 27 Jun 2024 18:02:30 +0000 (14:02 -0400)
committerDavid Wells <drwells@email.unc.edu>
Thu, 26 Sep 2024 18:59:00 +0000 (14:59 -0400)
include/deal.II/base/aligned_vector.h
tests/base/aligned_vector_01.cc

index 1b24729fc3452b83257f8bac94cc9aeeee651b98..84e74980d3f15b4ab328f769a8ac56a8d99f63a1 100644 (file)
@@ -78,6 +78,21 @@ public:
    */
   AlignedVector();
 
+  /**
+   * Range constructor.
+   *
+   * @note Unlike std::vector, this constructor uses random-access iterators so
+   * that the copy may be parallelized.
+   *
+   * @dealiiOperationIsMultithreaded
+   */
+  template <
+    typename RandomAccessIterator,
+    typename = std::enable_if_t<std::is_convertible_v<
+      typename std::iterator_traits<RandomAccessIterator>::iterator_category,
+      std::random_access_iterator_tag>>>
+  AlignedVector(RandomAccessIterator begin, RandomAccessIterator end);
+
   /**
    * Set the vector size to the given size and initializes all elements with
    * T().
@@ -1183,6 +1198,23 @@ inline AlignedVector<T>::AlignedVector()
 
 
 
+template <class T>
+template <typename RandomAccessIterator, typename>
+inline AlignedVector<T>::AlignedVector(RandomAccessIterator begin,
+                                       RandomAccessIterator end)
+  : elements(nullptr, Deleter(this))
+  , used_elements_end(nullptr)
+  , allocated_elements_end(nullptr)
+  , replicated_across_communicator(false)
+{
+  allocate_and_move(0u, end - begin, end - begin);
+  used_elements_end = allocated_elements_end;
+  dealii::internal::AlignedVectorCopyConstruct<RandomAccessIterator, T>(begin,
+                                                                        end,
+                                                                        data());
+}
+
+
 template <class T>
 inline AlignedVector<T>::AlignedVector(const size_type size, const T &init)
   : elements(nullptr, Deleter(this))
index cbd6f446e7a0bbb1260684fe66db3a09b5ed3033..af0831ef6f4ebae8f247240fda38e6993c0ca67b 100644 (file)
@@ -18,8 +18,9 @@
 
 #include <deal.II/base/aligned_vector.h>
 
-#include "../tests.h"
+#include <deque>
 
+#include "../tests.h"
 
 void
 test()
@@ -39,6 +40,28 @@ test()
   b.push_back(27);
   a.insert_back(b.begin(), b.end());
 
+  // Check the range constructor with and without conversion.
+  {
+    VEC c(b.begin(), b.end());
+    AssertThrow(c == b, ExcInternalError());
+
+    std::vector<int> temp(b.begin(), b.end());
+    VEC              d(temp.begin(), temp.end());
+    AssertThrow(c == d, ExcInternalError());
+  }
+
+  // Check the range constructor with a random-access iterator which is not
+  // contiguous
+  {
+    // Use a large enough deque to guarantee that we have multiple blocks
+    std::deque<unsigned int> temp(8192);
+    std::iota(temp.begin(), temp.end(), 0u);
+
+    VEC e(temp.begin(), temp.end());
+    AssertThrow(std::equal(e.begin(), e.end(), temp.begin()),
+                ExcInternalError());
+  }
+
   deallog << "Insertion: ";
   for (unsigned int i = 0; i < a.size(); ++i)
     deallog << a[i] << ' ';

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.