DUNE PDELab (git)

overflowarray.hh
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_OVERFLOWARRAY_HH
8#define DUNE_FUNCTIONS_COMMON_OVERFLOWARRAY_HH
9
10#include <algorithm>
11#include <iostream>
12#include <cstddef>
13#include <array>
14#include <initializer_list>
15
17
18
19
20namespace Dune::Functions {
21
22
46template<class BA, std::size_t maxSize = std::tuple_size_v<BA>>
48 public BA
49{
50 static constexpr std::size_t baseSize = std::tuple_size_v<BA>;
51
52public:
53 using BaseArray = BA;
54
55 using value_type = typename BaseArray::value_type;
56 using reference = value_type&;
57 using const_reference = const value_type&;
58 using pointer = value_type*;
59 using difference_type = std::ptrdiff_t;
60 using size_type = std::size_t;
63
64private:
65 using OverflowBuffer = std::array<value_type, maxSize-baseSize>;
66
67public:
68
69 OverflowArray() = default;
70
71 OverflowArray(const std::initializer_list<value_type>& l) {
72 assert(l.size() <= capacity());
73 size_ = l.size();
74 std::copy_n(l.begin(), size_, begin());
75 }
76
77 bool operator == (const OverflowArray& other) const {
78 if (size() != other.size())
79 return false;
80 for (size_type i=0; i<size(); ++i)
81 if ((*this)[i] != other[i])
82 return false;
83 return true;
84 }
85
87 void clear() {
88 size_ = 0;
89 }
90
97 void resize(size_type n) {
98 assert(n <= capacity());
99 size_ = n;
100 }
101
108 void push_back(const value_type& t) {
109 assert(size() < capacity());
110 (*this)[size_++] = t;
111 }
112
114 void pop_back() {
115 assert(size() > 0);
116 if (! empty())
117 size_--;
118 }
119
126 void push_front(const value_type& t) {
127 assert(size() < capacity());
128 for (size_type i=0; i<size(); i++)
129 (*this)[i+1] = (*this)[i];
130 (*this)[0] = t;
131 }
132
135 return iterator(*this, 0);
136 }
137
140 return const_iterator(*this, 0);
141 }
142
145 return iterator(*this, size());
146 }
147
150 return const_iterator(*this, size());
151 }
152
154 reference operator[] (size_type i) {
155 assert(i < size());
156 // If there's no padding between the base class and the overflow_ member,
157 // the compiler should be able to optimize this to
158 // return *(&BaseArray::operator[](0) + i);
159 if (i<baseSize)
160 return BaseArray::operator[](i);
161 return overflow_[i-baseSize];
162 }
163
165 const_reference operator[] (size_type i) const {
166 assert(i < size());
167 // If there's no padding between the base class and the overflow_ member,
168 // the compiler should be able to optimize this to
169 // return *(&BaseArray::operator[](0) + i);
170 if (i<baseSize)
171 return BaseArray::operator[](i);
172 return overflow_[i-baseSize];
173 }
174
176 reference front() {
177 assert(size() > 0);
178 return (*this)[0];
179 }
180
182 const_reference front() const {
183 assert(size() > 0);
184 return (*this)[0];
185 }
186
188 reference back() {
189 assert(size() > 0);
190 return (*this)[size()-1];
191 }
192
194 const_reference back() const {
195 assert(size() > 0);
196 return (*this)[size()-1];
197 }
198
200 size_type size () const {
201 return size_;
202 }
203
205 bool empty() const {
206 return size() == 0;
207 }
208
210 static constexpr size_type capacity() {
211 return maxSize;
212 }
213
215 static constexpr size_type max_size() {
216 return maxSize;
217 }
218
220 inline friend std::size_t hash_value(const OverflowArray& v) noexcept {
221 return hash_range(v.begin(), v.end());
222 }
223
225 friend std::ostream& operator<< (std::ostream& s, const OverflowArray& c) {
226 for (const auto& ci : c)
227 s << ci << " ";
228 return s;
229 }
230
231private:
232 OverflowBuffer overflow_;
233 size_type size_ = 0;
234};
235
236
237
238} // namespace Dune::Functions
239
240
241
242#endif // DUNE_FUNCTIONS_COMMON_OVERFLOWARRAY_HH
A dynamically sized array-like class with overflow.
Definition: overflowarray.hh:49
const_iterator end() const
Returns a const_iterator pointing to the end of the OverflowArray.
Definition: overflowarray.hh:149
friend std::size_t hash_value(const OverflowArray &v) noexcept
Compute hash value.
Definition: overflowarray.hh:220
void push_back(const value_type &t)
Appends an element to the end of the OverflowArray,.
Definition: overflowarray.hh:108
friend std::ostream & operator<<(std::ostream &s, const OverflowArray &c)
Write container to an output stream.
Definition: overflowarray.hh:225
iterator begin()
Returns a iterator pointing to the beginning of the OverflowArray.
Definition: overflowarray.hh:134
bool empty() const
Returns true if OverflowArray has no elements.
Definition: overflowarray.hh:205
size_type size() const
Returns number of elements in the OverflowArray.
Definition: overflowarray.hh:200
void pop_back()
Erases the last element of the OverflowArray, O(1) time.
Definition: overflowarray.hh:114
const_iterator begin() const
Returns a const_iterator pointing to the beginning of the OverflowArray.
Definition: overflowarray.hh:139
static constexpr size_type capacity()
Returns the capacity of the OverflowArray.
Definition: overflowarray.hh:210
static constexpr size_type max_size()
Returns the maximum length of the OverflowArray.
Definition: overflowarray.hh:215
const_reference front() const
Returns const reference to first element of OverflowArray.
Definition: overflowarray.hh:182
void clear()
Erases all elements.
Definition: overflowarray.hh:87
iterator end()
Returns an iterator pointing to the end of the OverflowArray.
Definition: overflowarray.hh:144
void resize(size_type n)
Specifies a new size for the OverflowArray.
Definition: overflowarray.hh:97
const_reference back() const
Returns const reference to last element of OverflowArray.
Definition: overflowarray.hh:194
reference back()
Returns reference to last element of OverflowArray.
Definition: overflowarray.hh:188
void push_front(const value_type &t)
Inserts an element to the begin of the OverflowArray,.
Definition: overflowarray.hh:126
reference front()
Returns reference to first element of OverflowArray.
Definition: overflowarray.hh:176
reference operator[](size_type i)
Returns reference to the i'th element.
Definition: overflowarray.hh:154
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.
std::size_t hash_range(It first, It last)
Hashes all elements in the range [first,last) and returns the combined hash.
Definition: hash.hh:322
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 24, 23:30, 2024)