Skip to content
Snippets Groups Projects
Commit 1cec2fe5 authored by Simon Praetorius's avatar Simon Praetorius
Browse files

Add more constructors for the ReservedVector

parent 919365fc
Branches
Tags
1 merge request!1158Fillin missing functionality in ReservedVector and make it constexpr
Pipeline #53023 passed
Pipeline: Dune Nightly Test

#53024

    ......@@ -76,14 +76,33 @@ namespace Dune
    /** @{ Constructors */
    //! Default Constructor
    //! Constructs an empty vector
    constexpr ReservedVector()
    noexcept(std::is_nothrow_default_constructible_v<value_type>)
    : storage_()
    , size_(0)
    {}
    //! Constructor from an iterator range
    //! Constructs the vector with `count` elements that will be default-initialized.
    explicit constexpr ReservedVector(size_type count)
    noexcept(std::is_nothrow_default_constructible_v<value_type>)
    : storage_()
    , size_(count)
    {
    assert(count <= n);
    }
    //! Constructs the vector with `count` copies of elements with value `value`.
    constexpr ReservedVector(size_type count, const value_type& value)
    noexcept(std::is_nothrow_copy_assignable_v<value_type> &&
    noexcept(ReservedVector(count)))
    : ReservedVector(count)
    {
    for (size_type i=0; i<count; ++i)
    storage_[i] = value;
    }
    //! Constructs the vector from an iterator range `[first,last)`
    template<class InputIt,
    std::enable_if_t<std::is_convertible_v<typename std::iterator_traits<InputIt>::value_type, value_type>, int> = 0>
    constexpr ReservedVector(InputIt first, InputIt last)
    ......@@ -96,7 +115,7 @@ namespace Dune
    assert(first == last);
    }
    //! Constructor from an initializer list
    //! Constructs the vector from an initializer list
    constexpr ReservedVector(std::initializer_list<value_type> const& l)
    noexcept(std::is_nothrow_copy_assignable_v<value_type> &&
    noexcept(ReservedVector(l.begin(),l.end())))
    ......@@ -107,7 +126,7 @@ namespace Dune
    /** @{ Comparison */
    //! Compares the values in the vector for equality
    //! Compares the values in the vector `this` with `that` for equality
    constexpr bool operator== (const ReservedVector& that) const noexcept
    {
    if (size() != that.size())
    ......@@ -118,12 +137,13 @@ namespace Dune
    return true;
    }
    //! Compares the values in the vector `this` with `that` for not equality
    constexpr bool operator!= (const ReservedVector& that) const noexcept
    {
    return !(*this == that);
    }
    //! Lexicographically compares the values in the vector
    //! Lexicographically compares the values in the vector `this` with `that`
    constexpr bool operator< (const ReservedVector& that) const noexcept
    {
    for (size_type i=0; i<std::min(size(),that.size()); ++i) {
    ......@@ -133,16 +153,19 @@ namespace Dune
    return size() < that.size();
    }
    //! Lexicographically compares the values in the vector `this` with `that`
    constexpr bool operator> (const ReservedVector& that) const noexcept
    {
    return that < *this;
    }
    //! Lexicographically compares the values in the vector `this` with `that`
    constexpr bool operator<= (const ReservedVector& that) const noexcept
    {
    return !(*this > that);
    }
    //! Lexicographically compares the values in the vector `this` with `that`
    constexpr bool operator>= (const ReservedVector& that) const noexcept
    {
    return !(*this < that);
    ......
    ......@@ -85,6 +85,16 @@ int main() {
    rv2[3] == 4 &&
    rv2[4] == 5);
    // check size constructor
    Dune::ReservedVector<unsigned int, 8> rv3(7);
    test.check(rv3.size() == 7);
    test.check(rv3[6] == 0);
    // check size and value constructor
    Dune::ReservedVector<unsigned int, 8> rv4(5, 42);
    test.check(rv4.size() == 5);
    test.check(rv4[3] == 42);
    // check pop_back
    rv2.pop_back();
    test.check(rv2.size() == 4);
    ......
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Please register or to comment