1 /* 2 * Copyright 2019 The libgav1 Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef LIBGAV1_SRC_UTILS_STACK_H_ 18 #define LIBGAV1_SRC_UTILS_STACK_H_ 19 20 #include <cassert> 21 #include <utility> 22 23 namespace libgav1 { 24 25 // A LIFO stack of a fixed capacity. The elements are moved using std::move, so 26 // the element type T has to be movable. 27 // 28 // WARNING: No error checking is performed. 29 template <typename T, int capacity> 30 class Stack { 31 public: 32 // Pushes the element |value| to the top of the stack. It is an error to call 33 // Push() when the stack is full. Push(T value)34 void Push(T value) { 35 ++top_; 36 assert(top_ < capacity); 37 elements_[top_] = std::move(value); 38 } 39 40 // Returns the element at the top of the stack and removes it from the stack. 41 // It is an error to call Pop() when the stack is empty. Pop()42 T Pop() { 43 assert(top_ >= 0); 44 return std::move(elements_[top_--]); 45 } 46 47 // Returns true if the stack is empty. Empty()48 bool Empty() const { return top_ < 0; } 49 50 private: 51 static_assert(capacity > 0, ""); 52 T elements_[capacity]; 53 // The array index of the top of the stack. The stack is empty if top_ is -1. 54 int top_ = -1; 55 }; 56 57 } // namespace libgav1 58 59 #endif // LIBGAV1_SRC_UTILS_STACK_H_ 60