dune-typetree  2.8-git
fixedcapacitystack.hh
Go to the documentation of this file.
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 
10 namespace Dune {
11  namespace TypeTree {
12 
13 
17 
18  template<typename T>
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 
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>
120  : private std::array<T,capacity>
121  , private FixedCapacityStackView<T>::Impl
122  , public FixedCapacityStackView<T>
123  {
124 
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 
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
Definition: accumulate_static.hh:13
Definition: fixedcapacitystack.hh:20
bool empty() const
Definition: fixedcapacitystack.hh:54
bool full() const
Definition: fixedcapacitystack.hh:59
void pop_back()
Definition: fixedcapacitystack.hh:70
T & operator[](std::size_t k)
Definition: fixedcapacitystack.hh:100
const T & back() const
Definition: fixedcapacitystack.hh:82
const T & front() const
Definition: fixedcapacitystack.hh:94
std::size_t capacity() const
Definition: fixedcapacitystack.hh:49
T & front()
Definition: fixedcapacitystack.hh:88
void push_back(const T &t)
Definition: fixedcapacitystack.hh:64
const T & operator[](std::size_t k) const
Definition: fixedcapacitystack.hh:106
std::size_t size() const
Definition: fixedcapacitystack.hh:44
FixedCapacityStackView(Impl &impl)
Definition: fixedcapacitystack.hh:38
T & back()
Definition: fixedcapacitystack.hh:76
Definition: fixedcapacitystack.hh:123
FixedCapacityStack()
Definition: fixedcapacitystack.hh:134