DUNE PDELab (git)

fixedcapacitystack.hh
1// -*- tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2// vi: set et ts=8 sw=2 sts=2:
3// SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
4// SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-GPL-2.0-only-with-PDELab-exception
5
6#ifndef DUNE_TYPETREE_FIXEDCAPACITYSTACK_HH
7#define DUNE_TYPETREE_FIXEDCAPACITYSTACK_HH
8
9#include <array>
10#include <cassert>
11
12namespace Dune {
13 namespace TypeTree {
14
15
19
20 template<typename T>
21 class FixedCapacityStackView
22 {
23
24 public:
25
26 struct Impl
27 {
28
29 Impl(T* data, std::size_t capacity)
30 : _data(data)
31 , _size(0)
32 , _capacity(capacity)
33 {}
34
35 T * const _data;
36 std::size_t _size;
37 const std::size_t _capacity;
38 };
39
40 FixedCapacityStackView(Impl& impl)
41 : _impl(impl)
42 {}
43
44 public:
45
46 std::size_t size() const
47 {
48 return _impl._size;
49 }
50
51 std::size_t capacity() const
52 {
53 return _impl._capacity;
54 }
55
56 bool empty() const
57 {
58 return _impl._size == 0;
59 }
60
61 bool full() const
62 {
63 return _impl._size == _impl._capacity;
64 }
65
66 void push_back(const T& t)
67 {
68 assert(!full());
69 _impl._data[_impl._size++] = t;
70 }
71
72 void pop_back()
73 {
74 assert(!empty());
75 --_impl._size;
76 }
77
78 T& back()
79 {
80 assert(!empty());
81 return _impl._data[_impl._size-1];
82 }
83
84 const T& back() const
85 {
86 assert(!empty());
87 return _impl._data[_impl._size-1];
88 }
89
90 T& front()
91 {
92 assert(!empty());
93 return _impl._data[0];
94 }
95
96 const T& front() const
97 {
98 assert(!empty());
99 return _impl._data[0];
100 }
101
102 T& operator[](std::size_t k)
103 {
104 assert(k < _impl._size);
105 return _impl._data[k];
106 }
107
108 const T& operator[](std::size_t k) const
109 {
110 assert(k < _impl._size);
111 return _impl._data[k];
112 }
113
114 private:
115 Impl& _impl;
116
117 };
118
119
120 template<typename T, std::size_t capacity>
121 class FixedCapacityStack
122 : private std::array<T,capacity>
123 , private FixedCapacityStackView<T>::Impl
124 , public FixedCapacityStackView<T>
125 {
126
127 typedef FixedCapacityStackView<T> view_base;
128
129 public:
130
131 using view_base::back;
132 using view_base::front;
133 using view_base::size;
134 using view_base::operator[];
135
136 FixedCapacityStack()
137 : FixedCapacityStackView<T>::Impl(&(static_cast<std::array<T,capacity>&>(*this)[0]),capacity)
138 , FixedCapacityStackView<T>(static_cast<typename FixedCapacityStackView<T>::Impl&>(*this))
139 {}
140
141 private:
142
143 //FixedCapacityStack(const FixedCapacityStack&);
144 FixedCapacityStack& operator=(const FixedCapacityStack&);
145
146 };
147
149
150 } // namespace TypeTree
151} //namespace Dune
152
153#endif // DUNE_TYPETREE_FIXEDCAPACITYSTACK_HH
constexpr auto back(const HybridTreePath< T... > &tp) -> decltype(tp.back())
Returns a copy of the last element of the HybridTreePath.
Definition: treepath.hh:392
constexpr auto pop_back(const HybridTreePath< T... > &tp)
Removes last index on a HybridTreePath.
Definition: treepath.hh:542
constexpr auto front(const HybridTreePath< T... > &tp) -> decltype(tp.front())
Returns a copy of the first element of the HybridTreePath.
Definition: treepath.hh:405
Dune namespace.
Definition: alignedallocator.hh:13
constexpr std::integer_sequence< T, II..., T(IN)> push_back(std::integer_sequence< T, II... >, std::integral_constant< T, IN >={})
Append an index IN to the back of the sequence.
Definition: integersequence.hh:69
constexpr std::integral_constant< std::size_t, sizeof...(II)> size(std::integer_sequence< T, II... >)
Return the size of the sequence.
Definition: integersequence.hh:75
constexpr std::bool_constant<(sizeof...(II)==0)> empty(std::integer_sequence< T, II... >)
Checks whether the sequence is empty.
Definition: integersequence.hh:80
constexpr std::integral_constant< T, I0 > front(std::integer_sequence< T, I0, II... >)
Return the first entry of the sequence.
Definition: integersequence.hh:39
constexpr auto back(std::integer_sequence< T, II... > seq)
Return the last entry of the sequence.
Definition: integersequence.hh:44
STL namespace.
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 24, 23:30, 2024)