Skip to content
Snippets Groups Projects
Commit 67fcf531 authored by Ansgar Burchardt's avatar Ansgar Burchardt
Browse files

[!700] dense vectors: add some more methods expected for containers

Merge branch 'feature/make-vectors-more-like-containers' into 'master'

ref:core/dune-common This merge request makes dense vectors look a little bit
more like standard containers by adding front(), back() and empty() member
functions.

For dense vectors where the implementation uses a contiguous array, a data()
member function is also provided.

See merge request [!700]

  [!700]: gitlab.dune-project.org/core/dune-common/merge_requests/700
parents 25dd2d66 dda63bcd
No related branches found
No related tags found
1 merge request!700dense vectors: add some more methods expected for containers
Pipeline #20905 passed
......@@ -300,6 +300,36 @@ namespace Dune {
return asImp()[i];
}
//! return reference to first element
value_type& front()
{
return asImp()[0];
}
//! return reference to first element
const value_type& front() const
{
return asImp()[0];
}
//! return reference to last element
value_type& back()
{
return asImp()[size()-1];
}
//! return reference to last element
const value_type& back() const
{
return asImp()[size()-1];
}
//! checks whether the container is empty
bool empty() const
{
return size() == 0;
}
//! size method
size_type size() const
{
......
......@@ -156,6 +156,18 @@ namespace Dune {
return _data[i];
}
//! return pointer to underlying array
K* data() noexcept
{
return _data.data();
}
//! return pointer to underlying array
const K* data() const noexcept
{
return _data.data();
}
const container_type &container () const { return _data; }
container_type &container () { return _data; }
};
......
......@@ -196,6 +196,18 @@ namespace Dune {
DUNE_ASSERT_BOUNDS(i < SIZE);
return _data[i];
}
//! return pointer to underlying array
K* data() noexcept
{
return _data.data();
}
//! return pointer to underlying array
const K* data() const noexcept
{
return _data.data();
}
};
/** \brief Read a FieldVector from an input stream
......@@ -333,6 +345,18 @@ namespace Dune {
return _data;
}
//! return pointer to underlying array
K* data() noexcept
{
return &_data;
}
//! return pointer to underlying array
const K* data() const noexcept
{
return &_data;
}
//===== conversion operator
/** \brief Conversion operator */
......
......@@ -7,6 +7,7 @@
#include <complex>
#include <iostream>
#include <limits>
#include <memory>
#include <typeinfo>
#include <type_traits>
......@@ -123,6 +124,13 @@ struct FieldVectorMainTestCommons
// test container methods
typename FieldVector<ft,d>::size_type size = FieldVector<ft,d>::dimension;
FVECTORTEST_ASSERT(size == w.size());
if (w.size() > 0) {
FVECTORTEST_ASSERT(!w.empty());
FVECTORTEST_ASSERT(std::addressof(w[0]) == std::addressof(w.front()));
FVECTORTEST_ASSERT(std::addressof(w[0]) == w.data());
FVECTORTEST_ASSERT(std::addressof(w[d-1]) == std::addressof(w.back()));
}
}
};
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment