99 lines
2.7 KiB
C++
99 lines
2.7 KiB
C++
#pragma once
|
|
#include <concepts>
|
|
#include <vector>
|
|
namespace zstd {
|
|
template<typename T>
|
|
void malloc_list(T*& ptr, int count) {
|
|
ptr = (T*)malloc(count * sizeof(T));
|
|
for (size_t i = 0; i < count; i++)
|
|
{
|
|
std::construct_at(ptr + i);
|
|
}
|
|
}
|
|
template<typename T>
|
|
class harray {
|
|
protected:
|
|
T* m_ptr{nullptr};
|
|
int m_count{0};
|
|
public:
|
|
constexpr harray() {}
|
|
harray(const std::vector<T>& vec)noexcept {
|
|
m_count = vec.size();
|
|
if (m_count > 0) {
|
|
malloc_list(m_ptr, m_count);
|
|
std::move(vec.begin(), vec.end(), m_ptr);
|
|
}
|
|
}
|
|
~harray() {
|
|
for (int i = 0; i < m_count; i++) {
|
|
std::destroy_at(m_ptr + i);
|
|
}
|
|
m_ptr = nullptr;
|
|
m_count = 0;
|
|
}
|
|
constexpr void reset() noexcept {
|
|
m_ptr = nullptr;
|
|
m_count = 0;
|
|
}
|
|
constexpr T* data() noexcept {
|
|
return m_ptr;
|
|
}
|
|
constexpr T* front() noexcept {
|
|
return m_ptr;
|
|
}
|
|
constexpr T* back() noexcept {
|
|
return m_ptr + m_count;
|
|
}
|
|
constexpr T* at(int i) noexcept {
|
|
if (i < m_count) {
|
|
return m_ptr + i;
|
|
}
|
|
return nullptr;
|
|
}
|
|
constexpr operator bool() 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() noexcept {
|
|
return iterator{ m_ptr };
|
|
}
|
|
constexpr auto end() 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;
|
|
}
|
|
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;
|
|
}
|
|
};
|
|
};
|
|
} |