DUNE PDELab (git)

reserveddeque.hh
Go to the documentation of this file.
1// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2// vi: set et ts=4 sw=2 sts=2:
3
4// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file AUTHORS.md
5// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception OR LGPL-3.0-or-later
6
7#ifndef DUNE_FUNCTIONS_COMMON_RESERVEDDEQUE_HH
8#define DUNE_FUNCTIONS_COMMON_RESERVEDDEQUE_HH
9
14#include <algorithm>
15#include <iostream>
16#include <cstddef>
17#include <initializer_list>
18
20
21#ifdef CHECK_RESERVEDDEQUE
22#define CHECKSIZE(X) assert(X)
23#else
24#define CHECKSIZE(X) {}
25#endif
26
27namespace Dune {
28namespace Functions {
29
47 template<class T, int n>
49 {
50 public:
51
55 typedef T value_type;
57 typedef T* pointer;
59 typedef T& reference;
61 typedef const T& const_reference;
63 typedef size_t size_type;
65 typedef std::ptrdiff_t difference_type;
70
77 size_(0),
78 first_(0)
79 {}
80
81 ReservedDeque(std::initializer_list<T> const &l)
82 {
83 assert(l.size() <= n);// Actually, this is not needed any more!
84 size_ = l.size();
85 std::copy_n(l.begin(), size_, data_);
86 }
87
93 void clear()
94 {
95 first_ = 0;
96 size_ = 0;
97 }
98
100 void resize(size_t s)
101 {
102 CHECKSIZE(s<=n);
103 size_ = s;
104 }
105
107 void push_back(const T& t)
108 {
109 CHECKSIZE(size_<n);
110 data_[size_++ % n] = t;
111 }
112
114 void push_front(const T& t)
115 {
116 CHECKSIZE(size_<n);
117 ++size_;
118 first_ = (first_+(n-1)) % n;
119 data_[first_] = t;
120 }
121
123 void pop_back()
124 {
125 if (! empty())
126 size_--;
127 }
128
131 {
132 if (! empty())
133 {
134 size_--;
135 first_ = (++first_) % n;
136 }
137 }
138
141 return iterator(*this, 0);
142 }
143
146 return const_iterator(*this, 0);
147 }
148
151 return iterator(*this, size_);
152 }
153
156 return const_iterator(*this, size_);
157 }
158
161 {
162 CHECKSIZE(size_>i);
163 return data_[(first_ + i) % n];
164 }
165
168 {
169 CHECKSIZE(size_>i);
170 return data_[(first_ + i) % n];
171 }
172
175 {
176 CHECKSIZE(size_>0);
177 return data_[first_];
178 }
179
182 {
183 CHECKSIZE(size_>0);
184 return data_[first_];
185 }
186
189 {
190 CHECKSIZE(size_>0);
191 return data_[(first_ + size_-1) % n];
192 }
193
196 {
197 CHECKSIZE(size_>0);
198 return data_[(first_ + size_-1) % n];
199 }
200
207 {
208 return size_;
209 }
210
212 bool empty() const
213 {
214 return size_==0;
215 }
216
218 static constexpr size_type capacity()
219 {
220 return n;
221 }
222
224 static constexpr size_type max_size()
225 {
226 return n;
227 }
228
232 friend std::ostream& operator<< (std::ostream& s, const ReservedDeque& v)
233 {
234 for (size_t i=0; i<v.size(); i++)
235 s << v[i] << " ";
236 return s;
237 }
238
239 private:
240 T data_[n];
241 size_type first_;
242 size_type size_;
243 };
244
245} // end namespace Functions
246} // end namespace Dune
247
248#undef CHECKSIZE
249
250#endif // DUNE_FUNCTIONS_COMMON_RESERVEDDEQUE_HH
A double-ended queue (deque) class with statically reserved memory.
Definition: reserveddeque.hh:49
void pop_front()
Erases the first element of the vector, O(1) time.
Definition: reserveddeque.hh:130
static constexpr size_type capacity()
Returns current capacity (allocated memory) of the vector.
Definition: reserveddeque.hh:218
void clear()
Erases all elements.
Definition: reserveddeque.hh:93
Dune::GenericIterator< ReservedDeque, value_type > iterator
Iterator used to iterate through a vector.
Definition: reserveddeque.hh:67
T value_type
The type of object, T, stored in the vector.
Definition: reserveddeque.hh:55
ReservedDeque()
Constructor.
Definition: reserveddeque.hh:76
bool empty() const
Returns true if vector has no elements.
Definition: reserveddeque.hh:212
const_reference back() const
Returns const reference to last element of vector.
Definition: reserveddeque.hh:195
friend std::ostream & operator<<(std::ostream &s, const ReservedDeque &v)
Send ReservedDeque to an output stream.
Definition: reserveddeque.hh:232
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
reference operator[](size_type i)
Returns reference to the i'th element.
Definition: reserveddeque.hh:160
reference front()
Returns reference to first element of vector.
Definition: reserveddeque.hh:174
T * pointer
Pointer to T.
Definition: reserveddeque.hh:57
const_reference front() const
Returns const reference to first element of vector.
Definition: reserveddeque.hh:181
iterator end()
Returns an iterator pointing to the end of the vector.
Definition: reserveddeque.hh:150
reference back()
Returns reference to last element of vector.
Definition: reserveddeque.hh:188
void pop_back()
Erases the last element of the vector, O(1) time.
Definition: reserveddeque.hh:123
const T & const_reference
Const reference to T.
Definition: reserveddeque.hh:61
static constexpr size_type max_size()
Returns the maximum length of the vector.
Definition: reserveddeque.hh:224
std::ptrdiff_t difference_type
A signed integral type.
Definition: reserveddeque.hh:65
T & reference
Reference to T.
Definition: reserveddeque.hh:59
Dune::GenericIterator< const ReservedDeque, const value_type > const_iterator
Const iterator used to iterate through a vector.
Definition: reserveddeque.hh:69
size_type size() const
Returns number of elements in the vector.
Definition: reserveddeque.hh:206
const_iterator end() const
Returns a const_iterator pointing to the end of the vector.
Definition: reserveddeque.hh:155
void resize(size_t s)
Specifies a new size for the vector.
Definition: reserveddeque.hh:100
iterator begin()
Returns a iterator pointing to the beginning of the vector.
Definition: reserveddeque.hh:140
const_iterator begin() const
Returns a const_iterator pointing to the beginning of the vector.
Definition: reserveddeque.hh:145
size_t size_type
An unsigned integral type.
Definition: reserveddeque.hh:63
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
Generic class for stl-conforming iterators for container classes with operator[].
Definition: genericiterator.hh:153
Implements a generic iterator class for writing stl conformant iterators.
Dune namespace.
Definition: alignedallocator.hh:13
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 12, 23:30, 2024)