Stack.hpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #ifndef CORE_STACK_HPP
  2. #define CORE_STACK_HPP
  3. #include "data/ArrayList.hpp"
  4. #include "data/List.hpp"
  5. namespace Core {
  6. namespace Internal {
  7. template<typename T, typename S>
  8. class BaseStack final {
  9. S data{};
  10. public:
  11. template<typename... Args>
  12. check_return Error push(Args&&... args) {
  13. return data.add(Core::forward<Args>(args)...);
  14. }
  15. void clear() {
  16. data.clear();
  17. }
  18. check_return Error pop() {
  19. return data.removeBySwap(data.getLength() - 1);
  20. }
  21. bool isEmpty() const {
  22. return data.getLength() == 0;
  23. }
  24. T& peek() {
  25. return data[data.getLength() - 1];
  26. }
  27. const T& peek() const {
  28. return data[data.getLength() - 1];
  29. }
  30. template<typename String>
  31. check_return Error toString(String& s) const {
  32. return s.append(data);
  33. }
  34. };
  35. }
  36. template<typename T>
  37. using ListStack = Internal::BaseStack<T, List<T>>;
  38. template<typename T, int N>
  39. using ArrayStack = Internal::BaseStack<T, ArrayList<T, N>>;
  40. }
  41. #endif