zengine-old/engine/3rdparty/zlib/include/zstd/svector.h

76 lines
2.2 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#pragma once
#include <array>
#include <concepts>
namespace zstd {
//栈容器大小可变只能从std::array安全构造
template<typename T>
class svector {
protected:
T* m_ptr;
int m_count;
int m_capicty;
public:
constexpr svector() :m_ptr(nullptr), m_count(0), m_capicty(0) {}
template<auto N>
constexpr svector(std::array<T, N>& arr, int count) : m_ptr(arr.data()), m_capicty(N), m_count(count) {}
constexpr T* front()const noexcept {
return m_ptr;
}
constexpr T* back()const noexcept {
return m_ptr + m_count;
}
constexpr void push_back(const T& t) {
if (m_count < m_capicty) {
*(m_ptr + m_count) = t;
m_count++;
}
}
constexpr bool IsValid() 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;
}
};
};
}