pstore2
array_stack.hpp
Go to the documentation of this file.
1 //===- include/pstore/core/array_stack.hpp ----------------*- mode: C++ -*-===//
2 //* _ _ *
3 //* __ _ _ __ _ __ __ _ _ _ ___| |_ __ _ ___| | __ *
4 //* / _` | '__| '__/ _` | | | | / __| __/ _` |/ __| |/ / *
5 //* | (_| | | | | | (_| | |_| | \__ \ || (_| | (__| < *
6 //* \__,_|_| |_| \__,_|\__, | |___/\__\__,_|\___|_|\_\ *
7 //* |___/ *
8 //===----------------------------------------------------------------------===//
9 //
10 // Part of the pstore project, under the Apache License v2.0 with LLVM Exceptions.
11 // See https://github.com/SNSystems/pstore/blob/master/LICENSE.txt for license
12 // information.
13 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
14 //
15 //===----------------------------------------------------------------------===//
19 
20 #ifndef PSTORE_CORE_ARRAY_STACK_HPP
21 #define PSTORE_CORE_ARRAY_STACK_HPP
22 
23 #include <array>
24 #include <cstdlib>
25 
27 
28 namespace pstore {
29 
37 
38  template <class Ty, std::size_t Size>
39  class array_stack {
40  public:
41  using container_type = std::array<Ty, Size>;
42  using value_type = typename container_type::value_type;
43  using reference = typename container_type::reference;
44  using const_reference = typename container_type::const_reference;
45  using size_type = typename container_type::size_type;
46 
47  array_stack () = default;
48 
49  bool operator== (array_stack const & other) const {
50  return elements_ == other.elements_ && std::equal (begin (), end (), other.begin ());
51  }
52  bool operator!= (array_stack const & other) const { return !operator== (other); }
53 
55  typename container_type::const_iterator begin () const { return std::begin (c_); }
57  typename container_type::const_iterator end () const { return std::begin (c_) + elements_; }
58 
61 
63  constexpr bool empty () const noexcept { return elements_ == 0; }
64 
66  constexpr size_type size () const noexcept { return elements_; }
67 
70  constexpr size_t max_size () const noexcept { return Size; }
72 
73 
76 
78  reference top () noexcept {
79  PSTORE_ASSERT (elements_ > 0);
80  return c_[elements_ - 1];
81  }
83  const_reference top () const noexcept {
84  PSTORE_ASSERT (elements_ > 0);
85  return c_[elements_ - 1];
86  }
87 
90 
93  void push (value_type const & value) {
94  PSTORE_ASSERT (elements_ < Size);
95  c_[elements_++] = value;
96  }
99  void push (value_type && value) {
100  PSTORE_ASSERT (elements_ < Size);
101  c_[elements_++] = std::move (value);
102  }
103 
105  void pop () {
106  PSTORE_ASSERT (elements_ > 0);
107  --elements_;
108  }
110 
111  private:
113  container_type c_;
115  std::size_t elements_{0};
116  };
117 } // namespace pstore
118 
119 #endif // PSTORE_CORE_ARRAY_STACK_HPP
void pop()
Remove the top element from the stack.
Definition: array_stack.hpp:105
const_reference top() const noexcept
Acceses the top element.
Definition: array_stack.hpp:83
constexpr size_type size() const noexcept
Returns the number of elements stored on the stack.
Definition: array_stack.hpp:66
container_type::const_iterator begin() const
Returns an iterator pointing to the first element in the stack.
Definition: array_stack.hpp:55
A simple wrapper for std::array which provides the functionality of a stack, specifically a FILO (fir...
Definition: array_stack.hpp:39
Definition: nonpod2.cpp:40
constexpr size_t max_size() const noexcept
Returns the maximum number of elements that the stack is able to hold.
Definition: array_stack.hpp:70
An implementation of the standard assert() macro with the exception that it will, on failure...
constexpr bool empty() const noexcept
Checks whether the stack is empty.
Definition: array_stack.hpp:63
void push(value_type const &value)
Inserts an element at the top of the container.
Definition: array_stack.hpp:93
container_type::const_iterator end() const
Returns an iterator pointing to the past-the-end element in the array container.
Definition: array_stack.hpp:57
reference top() noexcept
Acceses the top element.
Definition: array_stack.hpp:78
void push(value_type &&value)
Inserts an element at the top of the container.
Definition: array_stack.hpp:99