67 lines
1.9 KiB
C++
67 lines
1.9 KiB
C++
#pragma once
|
|
#include <array>
|
|
#include <concepts>
|
|
namespace refl {
|
|
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){}
|
|
constexpr svector(T* ptr, int size, int count) : m_ptr(ptr), m_capicty(size), m_count(count){}
|
|
T* front()const noexcept {
|
|
return m_ptr;
|
|
}
|
|
T* back()const noexcept {
|
|
return m_ptr + m_count;
|
|
}
|
|
void push_back(const T& t) {
|
|
if (m_count < m_capicty) {
|
|
*(m_ptr + m_count) = t;
|
|
m_count++;
|
|
}
|
|
}
|
|
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;
|
|
}
|
|
};
|
|
};
|
|
} |