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

make AllTrueBitSetVector an infinite range

parent d4b9a66f
No related branches found
No related tags found
1 merge request!100Added extended AllTrueBitSetVector class
#pragma once
#include <cstddef>
#include <iterator>
namespace AMDiS
{
/// \brief Vector-like container of bools returning always true
/// \brief Vector-like container of bools returning always true.
/**
* Model of an infinite range of bools of value `true` with vector-like element
* access by `operator[]`.
**/
class AllTrueBitSetVector
{
public:
using value_type = bool;
struct const_iterator
{
constexpr const_iterator(bool sentinel = false) noexcept : sentinel_(sentinel) {}
using value_type = bool;
using reference = bool;
using difference_type = std::ptrdiff_t;
using iterator_category = std::forward_iterator_tag;
constexpr bool operator*() const noexcept { return true; }
constexpr const_iterator& operator++() noexcept { return *this; }
constexpr const_iterator operator++(int) noexcept { return *this; }
/// Comparison of the iterator is always true if both are sentinel or both are const_iterator
friend constexpr bool operator==(const_iterator a, const_iterator b) noexcept
{
return a.sentinel_ == b.sentinel_;
}
bool sentinel_ = false;
/// Comparison of the iterator is always true
constexpr bool operator==(const_iterator) const noexcept { return true; }
constexpr bool operator!=(const_iterator) const noexcept { return false; }
};
/// Constexpr default constructor
constexpr AllTrueBitSetVector() noexcept = default;
public: // element access
/// Converting to true
constexpr operator bool() const noexcept
{
return true;
}
/// Access any element of the container returning always true.
/// Access to any element of the container returns always true.
template <class Index>
constexpr const AllTrueBitSetVector& operator[](Index const& /*index*/) const noexcept
constexpr AllTrueBitSetVector const& at(Index const& /*index*/) const noexcept
{
return *this;
}
/// Access to any element of the container returns always true.
template <class Index>
constexpr AllTrueBitSetVector const& operator[](Index const& /*index*/) const noexcept
{
return *this;
}
/// Access the front element of the infinite range
constexpr AllTrueBitSetVector const& front() const noexcept
{
return *this;
}
public: // iterators
/// Return iterator that always redirects to a bool true.
constexpr const_iterator begin() const noexcept { return const_iterator{false}; }
constexpr const_iterator end() const noexcept { return const_iterator{true}; }
constexpr const_iterator begin() const noexcept { return const_iterator{}; }
/// Return iterator that always redirects to a bool true.
constexpr const_iterator cbegin() const noexcept { return const_iterator{false}; }
constexpr const_iterator cend() const noexcept { return const_iterator{true}; }
constexpr const_iterator cbegin() const noexcept { return const_iterator{}; }
public: // capacity and modifier
/// Resize does nothing
template <class Size>
constexpr void resize(Size const& /*size*/) const noexcept { /* do nothing */ }
constexpr void resize(Size const& /*size*/) const noexcept
{
/* do nothing */
}
/// The container is always non-empty
constexpr bool empty() const noexcept
{
return false;
}
};
} // end namespace AMDiS
#include <type_traits>
#include <amdis/utility/AllTrueBitSetVector.hpp>
#include "Tests.hpp"
using namespace AMDiS;
int main(int argc, char** argv)
{
constexpr auto bitSet = AllTrueBitSetVector{};
// The bitset is not empty
AMDIS_TEST(!bitSet.empty());
// All entries are true
std::size_t i = 0;
for (auto it = bitSet.begin(); i < 10; ++it, ++i)
AMDIS_TEST(*it);
for (i = 0; i < 10; ++i) {
AMDIS_TEST(bitSet[i]);
AMDIS_TEST(bitSet.at(i));
}
// The front entry is true
AMDIS_TEST(bitSet.front());
// resize has not effect
bitSet.resize(0);
AMDIS_TEST(!bitSet.empty());
// use bitset element as template parameter
auto iconst = std::integral_constant<bool, bitSet[0]>{};
return report_errors();
}
......@@ -5,6 +5,9 @@ dune_add_test(SOURCES AccessTest.cpp
dune_add_test(SOURCES AdaptInfoTest.cpp
LINK_LIBRARIES amdis)
dune_add_test(SOURCES AllTrueBitSetVectorTest.cpp
LINK_LIBRARIES amdis)
foreach(_GRID RANGE 7)
dune_add_test(NAME "BackupRestoreTest_${_GRID}"
SOURCES BackupRestoreTest.cpp
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment