// =====================================================================================================================
//  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 A header containing the definition for a static/stack allocated array
///
///
/// \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 Data Structure that defines a compile-time allocated array
///
/// \details
/// | Property   | Value      |
/// |:----------:|:----------:|
/// | stable     |      ✅     |
/// | dynamic    |      ⛔     |
/// | homogenous |      ✅     |
/// | distinct   |      ⛔     |
/// | ordered    |      ⛔     |
/// | space      | \f$O(N)\f$ |
/// | linear     |      ✅     |
/// | access     | \f$O(1)\f$ |
/// | find       | \f$O(N)\f$ |
/// | insertion  |      ⛔     |
/// | deletion   |      ⛔     |
///
/// \tparam ValueT value type
/// \tparam ElemV number of elements
template<typename ValueT, size_t ElemV>
struct array
{
// Public Members ======================================================================================================

	/// \name Public Members
	/// @{

	///
	/// \brief backing c-style array handle
	ValueT elements[ElemV];

	/// @}



// Properties ==========================================================================================================

	/// \name Properties
	/// @{

	///
	/// \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; }

	/// @}



// Access ==============================================================================================================

	/// \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];
	}

	/// @}



// Comparison ==========================================================================================================

	/// \name Comparison Operators
	/// @{

	///
	/// \brief Checks if all elements in the arrays are equal
	friend constexpr bool_t operator==(const array& lhs, const array& rhs) {
		return array::_compare(lhs, rhs, make_index_sequence<ElemV>{});
	}

	///
	/// \brief Checks if any element in the arrays is not equal
	friend constexpr bool_t operator!=(const array& lhs, const array& rhs) {
		return not array::_compare(lhs, rhs, make_index_sequence<ElemV>{});
	}

	/// @}


// Iteration ===========================================================================================================

	/// \name Iteration
	/// @{

	///
	/// \brief C++ Iterator Specification `begin()`
	/// \returns A pointer to the first element of the array
	constexpr ValueT* begin() {
		return elements;
	}

	///
	/// \brief C++ Iterator Specification `end()`
	/// \returns A pointer to one after the end of the array
	constexpr ValueT* end() {
		return elements + ElemV;
	}



	///
	/// \brief Const C++ Iterator Specification `begin()`
	/// \returns A const-qualified pointer to the first element of the array
	constexpr const ValueT* begin() const {
		return elements;
	}

	///
	/// \brief Const C++ Iterator Specification `end()`
	/// \returns A const-qualified pointer to one after the end of the array
	constexpr const ValueT* end() const {
		return elements + ElemV;
	}

	/// @}

private:
	template<size_t...i>
	static bool _compare(const array& lhs, const array& rhs, const_index_sequence<i...>) {
		return ((lhs[i] == rhs[i]) && ...);
	}
};

}

#endif // FENNEC_CONTAINERS_ARRAY_H
