2021-05-11 14:47:48 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, Jesse Buhagiar <jooster669@gmail.com>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Vector.h>
|
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
template<typename T, size_t stack_size>
|
|
|
|
class Stack {
|
|
|
|
public:
|
|
|
|
Stack() = default;
|
|
|
|
~Stack() = default;
|
|
|
|
|
2022-10-17 01:06:11 +03:00
|
|
|
bool push(T const& item)
|
2021-05-11 14:47:48 +03:00
|
|
|
{
|
|
|
|
if (m_stack.size() >= stack_size)
|
|
|
|
return false;
|
|
|
|
|
2022-01-03 14:11:45 +03:00
|
|
|
m_stack.unchecked_append(item);
|
2021-05-11 14:47:48 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool push(T&& item)
|
|
|
|
{
|
|
|
|
if (m_stack.size() >= stack_size)
|
|
|
|
return false;
|
|
|
|
|
2022-01-03 14:11:45 +03:00
|
|
|
m_stack.unchecked_append(move(item));
|
2021-05-11 14:47:48 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_empty() const
|
|
|
|
{
|
|
|
|
return m_stack.is_empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t size() const
|
|
|
|
{
|
|
|
|
return m_stack.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool pop()
|
|
|
|
{
|
|
|
|
if (is_empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
m_stack.resize_and_keep_capacity(m_stack.size() - 1);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
T& top()
|
|
|
|
{
|
|
|
|
return m_stack.last();
|
|
|
|
}
|
|
|
|
|
2022-10-17 01:06:11 +03:00
|
|
|
T const& top() const
|
2021-05-11 14:47:48 +03:00
|
|
|
{
|
|
|
|
return m_stack.last();
|
|
|
|
}
|
|
|
|
|
2022-11-18 20:12:45 +03:00
|
|
|
bool contains_slow(T const& value) const
|
|
|
|
{
|
|
|
|
return m_stack.contains_slow(value);
|
|
|
|
}
|
|
|
|
|
2021-05-11 14:47:48 +03:00
|
|
|
private:
|
|
|
|
Vector<T, stack_size> m_stack;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-11-26 14:18:30 +03:00
|
|
|
#if USING_AK_GLOBALLY
|
2021-05-11 14:47:48 +03:00
|
|
|
using AK::Stack;
|
2022-11-26 14:18:30 +03:00
|
|
|
#endif
|