#pragma once
#include "svector.h"
#include <concepts>
namespace zstd {
    template<typename T>
    class sarray {
    protected:
        const T* m_ptr;
        int m_count;
    public:
        constexpr sarray(const T& ptr) : m_ptr(&ptr), m_count(1) {}
        constexpr sarray(const T* ptr, int count) : m_ptr(ptr), m_count(count) {}
        constexpr sarray(const svector<T>& vec) : m_ptr(vec.front()), m_count(vec.size()) {}
        constexpr sarray() : m_ptr(nullptr), m_count(0) {}
        constexpr sarray(std::initializer_list<T> list) : m_ptr(list.begin()), m_count(list.size()) {}
        constexpr const T* front() const noexcept {
            return m_ptr;
        }
        constexpr const T* back()const noexcept {
            return m_ptr + m_count;
        }
        constexpr const T* at(int i) const noexcept {
            if (i < m_count) {
                return m_ptr + i;
            }
            return nullptr;
        }
        constexpr const T* operator[](int i) const noexcept {
            if (i < m_count) {
                return m_ptr + i;
            }
            return nullptr;
        }
        constexpr bool valid() const noexcept {
            return m_count > 0;
        }
        constexpr bool empty() const noexcept {
            return m_count == 0;
        }
        constexpr int size() const noexcept {
            return m_count;
        }
        constexpr auto begin()const noexcept {
            return iterator{ m_ptr };
        }
        constexpr auto end() const noexcept {
            return iterator{ m_ptr + m_count };
        }
        // Iterator class
        class iterator {
        private:
            const T* ptr;

        public:
            constexpr iterator(const T* p) : ptr(p) {}

            // Overload ++ to move to next element
            constexpr iterator& operator++() noexcept {
                ++ptr;
                return *this;
            }
            //这里其实是--it,而不是it--
            constexpr iterator& operator--(int) noexcept {
                --ptr;
                return *this;
            }
            constexpr const T* operator->() const noexcept {
                return ptr;
            }
            // Overload * to dereference iterator
            constexpr const T& operator*() const noexcept {
                return *ptr;
            }

            // Overload != to compare iterators
            constexpr bool operator!=(const iterator& other) const noexcept {
                return ptr != other.ptr;
            }
        };
    };
}