Dune Core Modules (2.9.0)

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
4#ifndef DUNE_TYPETREE_FIXEDCAPACITYSTACK_HH
5#define DUNE_TYPETREE_FIXEDCAPACITYSTACK_HH
6
7#include <array>
8#include <cassert>
9
10namespace Dune {
11 namespace TypeTree {
12
13
17
18 template<typename T>
19 class FixedCapacityStackView
20 {
21
22 public:
23
24 struct Impl
25 {
26
27 Impl(T* data, std::size_t capacity)
28 : _data(data)
29 , _size(0)
30 , _capacity(capacity)
31 {}
32
33 T * const _data;
34 std::size_t _size;
35 const std::size_t _capacity;
36 };
37
38 FixedCapacityStackView(Impl& impl)
39 : _impl(impl)
40 {}
41
42 public:
43
44 std::size_t size() const
45 {
46 return _impl._size;
47 }
48
49 std::size_t capacity() const
50 {
51 return _impl._capacity;
52 }
53
54 bool empty() const
55 {
56 return _impl._size == 0;
57 }
58
59 bool full() const
60 {
61 return _impl._size == _impl._capacity;
62 }
63
64 void push_back(const T& t)
65 {
66 assert(!full());
67 _impl._data[_impl._size++] = t;
68 }
69
70 void pop_back()
71 {
72 assert(!empty());
73 --_impl._size;
74 }
75
76 T& back()
77 {
78 assert(!empty());
79 return _impl._data[_impl._size-1];
80 }
81
82 const T& back() const
83 {
84 assert(!empty());
85 return _impl._data[_impl._size-1];
86 }
87
88 T& front()
89 {
90 assert(!empty());
91 return _impl._data[0];
92 }
93
94 const T& front() const
95 {
96 assert(!empty());
97 return _impl._data[0];
98 }
99
100 T& operator[](std::size_t k)
101 {
102 assert(k < _impl._size);
103 return _impl._data[k];
104 }
105
106 const T& operator[](std::size_t k) const
107 {
108 assert(k < _impl._size);
109 return _impl._data[k];
110 }
111
112 private:
113 Impl& _impl;
114
115 };
116
117
118 template<typename T, std::size_t capacity>
119 class FixedCapacityStack
120 : private std::array<T,capacity>
121 , private FixedCapacityStackView<T>::Impl
122 , public FixedCapacityStackView<T>
123 {
124
125 typedef FixedCapacityStackView<T> view_base;
126
127 public:
128
129 using view_base::back;
130 using view_base::front;
131 using view_base::size;
132 using view_base::operator[];
133
134 FixedCapacityStack()
135 : FixedCapacityStackView<T>::Impl(&(static_cast<std::array<T,capacity>&>(*this)[0]),capacity)
136 , FixedCapacityStackView<T>(static_cast<typename FixedCapacityStackView<T>::Impl&>(*this))
137 {}
138
139 private:
140
141 //FixedCapacityStack(const FixedCapacityStack&);
142 FixedCapacityStack& operator=(const FixedCapacityStack&);
143
144 };
145
147
148 } // namespace TypeTree
149} //namespace Dune
150
151#endif // DUNE_TYPETREE_FIXEDCAPACITYSTACK_HH
constexpr HybridTreePath< T..., std::size_t > push_back(const HybridTreePath< T... > &tp, std::size_t i)
Appends a run time index to a HybridTreePath.
Definition: treepath.hh:281
constexpr auto back(const HybridTreePath< T... > &tp) -> decltype(treePathEntry< sizeof...(T) -1 >(tp))
Returns a copy of the last element of the HybridTreePath.
Definition: treepath.hh:257
constexpr auto front(const HybridTreePath< T... > &tp) -> decltype(treePathEntry< 0 >(tp))
Returns a copy of the first element of the HybridTreePath.
Definition: treepath.hh:270
constexpr auto pop_back(const HybridTreePath< T... > &tp)
Removes last index on a HybridTreePath.
Definition: treepath.hh:356
Dune namespace.
Definition: alignedallocator.hh:13
STL namespace.
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Jul 15, 22:36, 2024)