From aad0eac6789263aa9b0d0d8100ca7d38f60fb50e Mon Sep 17 00:00:00 2001
From: Christian Engwer <christi@dune-project.org>
Date: Tue, 18 Mar 2014 17:33:21 +0100
Subject: [PATCH] Add constructor via initialiser list to FieldMatrix and
 FieldVector

kudos to Elias Pipping, for details see (FS #1166)
as the new Dune requirements are >= g++ 4.4 we are sure that initializer_list is available.
---
 dune/common/fmatrix.hh          | 10 ++++++++++
 dune/common/fvector.hh          | 10 ++++++++++
 dune/common/test/fmatrixtest.cc | 13 +++++++++++++
 dune/common/test/fvectortest.cc | 10 ++++++++++
 4 files changed, 43 insertions(+)

diff --git a/dune/common/fmatrix.hh b/dune/common/fmatrix.hh
index 2c7e2112d..95190a0b5 100644
--- a/dune/common/fmatrix.hh
+++ b/dune/common/fmatrix.hh
@@ -7,6 +7,7 @@
 #include <cmath>
 #include <cstddef>
 #include <iostream>
+#include <initializer_list>
 
 #include <dune/common/exceptions.hh>
 #include <dune/common/fvector.hh>
@@ -96,6 +97,15 @@ namespace Dune
       DenseMatrixAssigner< FieldMatrix< K, ROWS, COLS >, Other >::apply( *this, other );
     }
 
+    FieldMatrix (std::initializer_list<std::initializer_list<K> > const &ll)
+    {
+      assert(ll.size() == rows);
+      size_t i = 0;
+      for (typename std::initializer_list<std::initializer_list<K> >::
+             const_iterator lit = ll.begin(); lit != ll.end(); ++lit)
+        _data[i++] = *lit;
+    }
+
     //===== assignment
     using Base::operator=;
 
diff --git a/dune/common/fvector.hh b/dune/common/fvector.hh
index 76c77bc2c..710de61fd 100644
--- a/dune/common/fvector.hh
+++ b/dune/common/fvector.hh
@@ -10,6 +10,7 @@
 #include <complex>
 #include <cstring>
 #include <utility>
+#include<initializer_list>
 
 #include <dune/common/std/constexpr.hh>
 
@@ -129,6 +130,15 @@ namespace Dune {
     FieldVector (const FieldVector & x) : _data(x._data)
     {}
 
+    FieldVector (std::initializer_list<K> const &l)
+    {
+      assert(l.size() == dimension);
+      size_t i = 0;
+      for (typename std::initializer_list<K>::const_iterator it = l.begin();
+           it != l.end(); ++it)
+        _data[i++] = *it;
+    }
+
     /**
      * \brief Copy constructor from a second vector of possibly different type
      *
diff --git a/dune/common/test/fmatrixtest.cc b/dune/common/test/fmatrixtest.cc
index 2fb4389e0..48e659cde 100644
--- a/dune/common/test/fmatrixtest.cc
+++ b/dune/common/test/fmatrixtest.cc
@@ -553,11 +553,24 @@ void test_interface()
   checkMatrixInterface< FMatrix, Traits >( m );
 }
 
+void test_initialisation()
+{
+  Dune::FieldMatrix<int, 2, 2> const A = {
+    { 1, 2 },
+    { 3, 4 }
+  };
+  assert(A[0][0] == 1);
+  assert(A[0][1] == 2);
+  assert(A[1][0] == 3);
+  assert(A[1][1] == 4);
+}
+
 int main()
 {
   try {
     test_nan();
     test_infinity_norms();
+    test_initialisation();
 
     // test 1 x 1 matrices
     test_interface<float, 1, 1>();
diff --git a/dune/common/test/fvectortest.cc b/dune/common/test/fvectortest.cc
index 81ac1913f..b1dd3d0f5 100644
--- a/dune/common/test/fvectortest.cc
+++ b/dune/common/test/fvectortest.cc
@@ -342,6 +342,15 @@ test_infinity_norms()
   assert(std::abs(v.infinity_norm_real()-14.0) < 1e-10); // max(7,14)
 }
 
+void
+test_initialisation()
+{
+  Dune::FieldVector<int, 2> const b = { 1, 2 };
+
+  assert(b[0] == 1);
+  assert(b[1] == 2);
+}
+
 int main()
 {
   try {
@@ -351,6 +360,7 @@ int main()
 
     test_nan();
     test_infinity_norms();
+    test_initialisation();
   } catch (Dune::Exception& e) {
     std::cerr << e << std::endl;
     return 1;
-- 
GitLab