Loading...
Searching...
No Matches
reserveddeque.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_RESERVEDDEQUE_HH
#define DUNE_FUNCTIONS_COMMON_RESERVEDDEQUE_HH
#include <algorithm>
#include <iostream>
#include <cstddef>
#include <initializer_list>
#include <dune/common/genericiterator.hh>
#ifdef CHECK_RESERVEDDEQUE
#define CHECKSIZE(X) assert(X)
#else
#define CHECKSIZE(X) {}
#endif
namespace Dune {
namespace Functions {
template<class T, int n>
class ReservedDeque
{
public:
typedef T value_type;
typedef T* pointer;
typedef T& reference;
typedef const T& const_reference;
typedef size_t size_type;
typedef std::ptrdiff_t difference_type;
typedef Dune::GenericIterator<ReservedDeque, value_type> iterator;
typedef Dune::GenericIterator<const ReservedDeque, const value_type> const_iterator;
ReservedDeque() :
size_(0),
first_(0)
{}
ReservedDeque(std::initializer_list<T> const &l)
{
assert(l.size() <= n);// Actually, this is not needed any more!
size_ = l.size();
std::copy_n(l.begin(), size_, data_);
}
void clear()
{
first_ = 0;
size_ = 0;
}
void resize(size_t s)
{
CHECKSIZE(s<=n);
size_ = s;
}
void push_back(const T& t)
{
CHECKSIZE(size_<n);
data_[size_++ % n] = t;
}
void push_front(const T& t)
{
CHECKSIZE(size_<n);
++size_;
first_ = (first_+(n-1)) % n;
data_[first_] = t;
}
void pop_back()
{
if (! empty())
size_--;
}
void pop_front()
{
if (! empty())
{
size_--;
first_ = (++first_) % n;
}
}
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)
{
CHECKSIZE(size_>i);
return data_[(first_ + i) % n];
}
const_reference operator[] (size_type i) const
{
CHECKSIZE(size_>i);
return data_[(first_ + i) % n];
}
reference front()
{
CHECKSIZE(size_>0);
return data_[first_];
}
const_reference front() const
{
CHECKSIZE(size_>0);
return data_[first_];
}
reference back()
{
CHECKSIZE(size_>0);
return data_[(first_ + size_-1) % n];
}
const_reference back() const
{
CHECKSIZE(size_>0);
return data_[(first_ + size_-1) % n];
}
size_type size () const
{
return size_;
}
bool empty() const
{
return size_==0;
}
static constexpr size_type capacity()
{
return n;
}
static constexpr size_type max_size()
{
return n;
}
friend std::ostream& operator<< (std::ostream& s, const ReservedDeque& v)
{
for (size_t i=0; i<v.size(); i++)
s << v[i] << " ";
return s;
}
private:
T data_[n];
size_type first_;
size_type size_;
};
} // end namespace Functions
} // end namespace Dune
#undef CHECKSIZE
#endif // DUNE_FUNCTIONS_COMMON_RESERVEDDEQUE_HH
CHECKSIZE
#define CHECKSIZE(X)
Definition reserveddeque.hh:24
Dune
Definition monomialset.hh:19
Dune::Functions::ReservedDeque::pop_front
void pop_front()
Erases the first element of the vector, O(1) time.
Definition reserveddeque.hh:130
Dune::Functions::ReservedDeque::capacity
static constexpr size_type capacity()
Returns current capacity (allocated memory) of the vector.
Definition reserveddeque.hh:218
Dune::Functions::ReservedDeque::clear
void clear()
Erases all elements.
Definition reserveddeque.hh:93
Dune::Functions::ReservedDeque::iterator
Dune::GenericIterator< ReservedDeque, value_type > iterator
Iterator used to iterate through a vector.
Definition reserveddeque.hh:67
Dune::Functions::ReservedDeque::value_type
T value_type
The type of object, T, stored in the vector.
Definition reserveddeque.hh:55
Dune::Functions::ReservedDeque::ReservedDeque
ReservedDeque()
Constructor.
Definition reserveddeque.hh:76
Dune::Functions::ReservedDeque::empty
bool empty() const
Returns true if vector has no elements.
Definition reserveddeque.hh:212
Dune::Functions::ReservedDeque::back
const_reference back() const
Returns const reference to last element of vector.
Definition reserveddeque.hh:195
Dune::Functions::ReservedDeque::operator<<
friend std::ostream & operator<<(std::ostream &s, const ReservedDeque &v)
Send ReservedDeque to an output stream.
Definition reserveddeque.hh:232
Dune::Functions::ReservedDeque::push_front
void push_front(const T &t)
Prepends an element to the begin of a deque if size<capacity, O(1) time.
Definition reserveddeque.hh:114
Dune::Functions::ReservedDeque::operator[]
reference operator[](size_type i)
Returns reference to the i'th element.
Definition reserveddeque.hh:160
Dune::Functions::ReservedDeque::front
reference front()
Returns reference to first element of vector.
Definition reserveddeque.hh:174
Dune::Functions::ReservedDeque::pointer
T * pointer
Pointer to T.
Definition reserveddeque.hh:57
Dune::Functions::ReservedDeque::front
const_reference front() const
Returns const reference to first element of vector.
Definition reserveddeque.hh:181
Dune::Functions::ReservedDeque::end
iterator end()
Returns an iterator pointing to the end of the vector.
Definition reserveddeque.hh:150
Dune::Functions::ReservedDeque::back
reference back()
Returns reference to last element of vector.
Definition reserveddeque.hh:188
Dune::Functions::ReservedDeque::pop_back
void pop_back()
Erases the last element of the vector, O(1) time.
Definition reserveddeque.hh:123
Dune::Functions::ReservedDeque::const_reference
const T & const_reference
Const reference to T.
Definition reserveddeque.hh:61
Dune::Functions::ReservedDeque::max_size
static constexpr size_type max_size()
Returns the maximum length of the vector.
Definition reserveddeque.hh:224
Dune::Functions::ReservedDeque::difference_type
std::ptrdiff_t difference_type
A signed integral type.
Definition reserveddeque.hh:65
Dune::Functions::ReservedDeque::reference
T & reference
Reference to T.
Definition reserveddeque.hh:59
Dune::Functions::ReservedDeque::const_iterator
Dune::GenericIterator< const ReservedDeque, const value_type > const_iterator
Const iterator used to iterate through a vector.
Definition reserveddeque.hh:69
Dune::Functions::ReservedDeque::size
size_type size() const
Returns number of elements in the vector.
Definition reserveddeque.hh:206
Dune::Functions::ReservedDeque::end
const_iterator end() const
Returns a const_iterator pointing to the end of the vector.
Definition reserveddeque.hh:155
Dune::Functions::ReservedDeque::resize
void resize(size_t s)
Specifies a new size for the vector.
Definition reserveddeque.hh:100
Dune::Functions::ReservedDeque::begin
iterator begin()
Returns a iterator pointing to the beginning of the vector.
Definition reserveddeque.hh:140
Dune::Functions::ReservedDeque::begin
const_iterator begin() const
Returns a const_iterator pointing to the beginning of the vector.
Definition reserveddeque.hh:145
Dune::Functions::ReservedDeque::ReservedDeque
ReservedDeque(std::initializer_list< T > const &l)
Definition reserveddeque.hh:81
Dune::Functions::ReservedDeque::size_type
size_t size_type
An unsigned integral type.
Definition reserveddeque.hh:63
Dune::Functions::ReservedDeque::push_back
void push_back(const T &t)
Appends an element to the end of a vector, up to the maximum size n, O(1) time.
Definition reserveddeque.hh:107