139 lines
3.6 KiB
C++
139 lines
3.6 KiB
C++
// =====================================================================================================================
|
|
// fennec, a free and open source game engine
|
|
// Copyright © 2025 Medusa Slockbower
|
|
//
|
|
// This program is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// This program is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU General Public License
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
// =====================================================================================================================
|
|
|
|
///
|
|
/// \file array.h
|
|
/// \brief statically allocated array wrapper
|
|
///
|
|
///
|
|
/// \details
|
|
/// \author Medusa Slockbower
|
|
///
|
|
/// \copyright Copyright © 2025 Medusa Slockbower ([GPLv3](https://www.gnu.org/licenses/gpl-3.0.en.html))
|
|
///
|
|
///
|
|
|
|
|
|
#ifndef FENNEC_CONTAINERS_ARRAY_H
|
|
#define FENNEC_CONTAINERS_ARRAY_H
|
|
|
|
#include <fennec/lang/types.h>
|
|
#include <fennec/lang/assert.h>
|
|
|
|
namespace fennec
|
|
{
|
|
|
|
///
|
|
///
|
|
/// \brief wrapper for fixed size arrays
|
|
///
|
|
/// \details
|
|
/// | Property | Value |
|
|
/// |:--------:|:-----------------------:|
|
|
/// | stable | ✔ |
|
|
/// | access | \f$O(1)\f$ |
|
|
/// | space | \f$O(N)\f$ |
|
|
///
|
|
/// \tparam ValueT value type
|
|
/// \tparam ElemV number of elements
|
|
template<typename ValueT, size_t ElemV>
|
|
struct array
|
|
{
|
|
///
|
|
/// \brief backing c-style array handle
|
|
ValueT elements[ElemV];
|
|
|
|
/// \name Element Access
|
|
/// @{
|
|
|
|
///
|
|
/// \copydetails array::operator[](size_t) const
|
|
constexpr ValueT& operator[](size_t i) {
|
|
assertd(i < ElemV, "Array Out of Bounds");
|
|
return elements[i];
|
|
}
|
|
|
|
///
|
|
/// \brief access specified element
|
|
/// \details Returns a reference to the element at \c i
|
|
/// \param i index of the element to return
|
|
/// \return reference to the requested element
|
|
///
|
|
/// \par Time-Complexity
|
|
/// Constant
|
|
///
|
|
/// \par Space-Complexity
|
|
/// Constant
|
|
constexpr const ValueT& operator[](size_t i) const {
|
|
assertd(i < ElemV, "Array Out of Bounds");
|
|
return elements[i];
|
|
}
|
|
|
|
constexpr ValueT* begin() { return elements; }
|
|
constexpr ValueT* end() { return elements + ElemV; }
|
|
|
|
constexpr const ValueT* begin() const { return elements; }
|
|
constexpr const ValueT* end() const { return elements + ElemV; }
|
|
|
|
/// @}
|
|
|
|
|
|
|
|
/// \name Capacity
|
|
/// @{
|
|
|
|
///
|
|
/// \brief returns the number of elements in the array
|
|
/// \returns the number of elements in the array
|
|
[[nodiscard]] constexpr size_t size() const { return ElemV; }
|
|
|
|
///
|
|
/// \brief returns **true** when the array is empty
|
|
/// \return \f$ElemV == 0\f$
|
|
[[nodiscard]] constexpr bool_t empty() const { return ElemV == 0; }
|
|
|
|
/// @}
|
|
|
|
|
|
|
|
/// \name Comparison Operators
|
|
/// @{
|
|
|
|
///
|
|
/// \brief
|
|
friend constexpr bool_t operator==(const array& lhs, const array& rhs) {
|
|
return array::_compare(lhs, rhs, make_index_sequence<ElemV>{});
|
|
}
|
|
|
|
friend constexpr bool_t operator!=(const array& lhs, const array& rhs) {
|
|
return not array::_compare(lhs, rhs, make_index_sequence<ElemV>{});
|
|
}
|
|
|
|
/// @}
|
|
|
|
private:
|
|
template<size_t...i>
|
|
static bool _compare(const array& lhs, const array& rhs, index_sequence<i...>) {
|
|
return ((lhs[i] == rhs[i]) && ...);
|
|
}
|
|
};
|
|
|
|
}
|
|
|
|
#endif // FENNEC_CONTAINERS_ARRAY_H
|