Loading...
Searching...
No Matches
overflowarray.hh
Go to the documentation of this file.
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file AUTHORS.md
// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception OR LGPL-3.0-or-later
#ifndef DUNE_FUNCTIONS_COMMON_OVERFLOWARRAY_HH
#define DUNE_FUNCTIONS_COMMON_OVERFLOWARRAY_HH
#include <algorithm>
#include <iostream>
#include <cstddef>
#include <array>
#include <initializer_list>
#include <dune/common/genericiterator.hh>
namespace Dune::Functions {
template<class BA, std::size_t maxSize = std::tuple_size_v<BA>>
class OverflowArray :
public BA
{
static constexpr std::size_t baseSize = std::tuple_size_v<BA>;
public:
using BaseArray = BA;
using value_type = typename BaseArray::value_type;
using reference = value_type&;
using const_reference = const value_type&;
using pointer = value_type*;
using difference_type = std::ptrdiff_t;
using size_type = std::size_t;
using iterator = Dune::GenericIterator<OverflowArray, value_type>;
using const_iterator = Dune::GenericIterator<const OverflowArray, const value_type>;
private:
using OverflowBuffer = std::array<value_type, maxSize-baseSize>;
public:
OverflowArray() = default;
OverflowArray(const std::initializer_list<value_type>& l) {
assert(l.size() <= capacity());
size_ = l.size();
std::copy_n(l.begin(), size_, begin());
}
bool operator == (const OverflowArray& other) const {
if (size() != other.size())
return false;
for (size_type i=0; i<size(); ++i)
if ((*this)[i] != other[i])
return false;
return true;
}
void clear() {
size_ = 0;
}
void resize(size_type n) {
assert(n <= capacity());
size_ = n;
}
void push_back(const value_type& t) {
assert(size() < capacity());
(*this)[size_++] = t;
}
void pop_back() {
assert(size() > 0);
if (! empty())
size_--;
}
void push_front(const value_type& t) {
assert(size() < capacity());
for (size_type i=0; i<size(); i++)
(*this)[i+1] = (*this)[i];
(*this)[0] = t;
}
iterator begin() {
return iterator(*this, 0);
}
const_iterator begin() const {
return const_iterator(*this, 0);
}
iterator end() {
return iterator(*this, size());
}
const_iterator end() const {
return const_iterator(*this, size());
}
reference operator[] (size_type i) {
assert(i < size());
// If there's no padding between the base class and the overflow_ member,
// the compiler should be able to optimize this to
// return *(&BaseArray::operator[](0) + i);
if (i<baseSize)
return BaseArray::operator[](i);
return overflow_[i-baseSize];
}
const_reference operator[] (size_type i) const {
assert(i < size());
// If there's no padding between the base class and the overflow_ member,
// the compiler should be able to optimize this to
// return *(&BaseArray::operator[](0) + i);
if (i<baseSize)
return BaseArray::operator[](i);
return overflow_[i-baseSize];
}
reference front() {
assert(size() > 0);
return (*this)[0];
}
const_reference front() const {
assert(size() > 0);
return (*this)[0];
}
reference back() {
assert(size() > 0);
return (*this)[size()-1];
}
const_reference back() const {
assert(size() > 0);
return (*this)[size()-1];
}
size_type size () const {
return size_;
}
bool empty() const {
return size() == 0;
}
static constexpr size_type capacity() {
return maxSize;
}
static constexpr size_type max_size() {
return maxSize;
}
inline friend std::size_t hash_value(const OverflowArray& v) noexcept {
return hash_range(v.begin(), v.end());
}
friend std::ostream& operator<< (std::ostream& s, const OverflowArray& c) {
for (const auto& ci : c)
s << ci << " ";
return s;
}
private:
OverflowBuffer overflow_;
size_type size_ = 0;
};
} // namespace Dune::Functions
#endif // DUNE_FUNCTIONS_COMMON_OVERFLOWARRAY_HH
Dune::Functions
Definition monomialset.hh:19
Dune::Functions::OverflowArray::const_reference
const value_type & const_reference
Definition overflowarray.hh:57
Dune::Functions::OverflowArray::reference
value_type & reference
Definition overflowarray.hh:56
Dune::Functions::OverflowArray::end
const_iterator end() const
Returns a const_iterator pointing to the end of the OverflowArray.
Definition overflowarray.hh:149
Dune::Functions::OverflowArray::operator==
bool operator==(const OverflowArray &other) const
Definition overflowarray.hh:77
Dune::Functions::OverflowArray::hash_value
friend std::size_t hash_value(const OverflowArray &v) noexcept
Compute hash value.
Definition overflowarray.hh:220
Dune::Functions::OverflowArray::push_back
void push_back(const value_type &t)
Appends an element to the end of the OverflowArray,.
Definition overflowarray.hh:108
Dune::Functions::OverflowArray::operator<<
friend std::ostream & operator<<(std::ostream &s, const OverflowArray &c)
Write container to an output stream.
Definition overflowarray.hh:225
Dune::Functions::OverflowArray::begin
iterator begin()
Returns a iterator pointing to the beginning of the OverflowArray.
Definition overflowarray.hh:134
Dune::Functions::OverflowArray::empty
bool empty() const
Returns true if OverflowArray has no elements.
Definition overflowarray.hh:205
Dune::Functions::OverflowArray::size
size_type size() const
Returns number of elements in the OverflowArray.
Definition overflowarray.hh:200
Dune::Functions::OverflowArray::value_type
typename BaseArray::value_type value_type
Definition overflowarray.hh:55
Dune::Functions::OverflowArray::pop_back
void pop_back()
Erases the last element of the OverflowArray, O(1) time.
Definition overflowarray.hh:114
Dune::Functions::OverflowArray::iterator
Dune::GenericIterator< OverflowArray, value_type > iterator
Definition overflowarray.hh:61
Dune::Functions::OverflowArray::begin
const_iterator begin() const
Returns a const_iterator pointing to the beginning of the OverflowArray.
Definition overflowarray.hh:139
Dune::Functions::OverflowArray::difference_type
std::ptrdiff_t difference_type
Definition overflowarray.hh:59
Dune::Functions::OverflowArray::OverflowArray
OverflowArray()=default
Dune::Functions::OverflowArray::BaseArray
BA BaseArray
Definition overflowarray.hh:53
Dune::Functions::OverflowArray::capacity
static constexpr size_type capacity()
Returns the capacity of the OverflowArray.
Definition overflowarray.hh:210
Dune::Functions::OverflowArray::max_size
static constexpr size_type max_size()
Returns the maximum length of the OverflowArray.
Definition overflowarray.hh:215
Dune::Functions::OverflowArray::front
const_reference front() const
Returns const reference to first element of OverflowArray.
Definition overflowarray.hh:182
Dune::Functions::OverflowArray::clear
void clear()
Erases all elements.
Definition overflowarray.hh:87
Dune::Functions::OverflowArray::end
iterator end()
Returns an iterator pointing to the end of the OverflowArray.
Definition overflowarray.hh:144
Dune::Functions::OverflowArray::size_type
std::size_t size_type
Definition overflowarray.hh:60
Dune::Functions::OverflowArray::resize
void resize(size_type n)
Specifies a new size for the OverflowArray.
Definition overflowarray.hh:97
Dune::Functions::OverflowArray::pointer
value_type * pointer
Definition overflowarray.hh:58
Dune::Functions::OverflowArray::const_iterator
Dune::GenericIterator< const OverflowArray, const value_type > const_iterator
Definition overflowarray.hh:62
Dune::Functions::OverflowArray::back
const_reference back() const
Returns const reference to last element of OverflowArray.
Definition overflowarray.hh:194
Dune::Functions::OverflowArray::OverflowArray
OverflowArray(const std::initializer_list< value_type > &l)
Definition overflowarray.hh:71
Dune::Functions::OverflowArray::back
reference back()
Returns reference to last element of OverflowArray.
Definition overflowarray.hh:188
Dune::Functions::OverflowArray::push_front
void push_front(const value_type &t)
Inserts an element to the begin of the OverflowArray,.
Definition overflowarray.hh:126
Dune::Functions::OverflowArray::front
reference front()
Returns reference to first element of OverflowArray.
Definition overflowarray.hh:176
Dune::Functions::OverflowArray::operator[]
reference operator[](size_type i)
Returns reference to the i'th element.
Definition overflowarray.hh:154