71 lines
2.0 KiB
C++
71 lines
2.0 KiB
C++
#pragma once
|
|
#include "svector.h"
|
|
#include <concepts>
|
|
namespace refl {
|
|
template<typename T>
|
|
class sarray {
|
|
protected:
|
|
const T* m_ptr;
|
|
int m_count;
|
|
public:
|
|
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()) {}
|
|
const T* front() const noexcept {
|
|
return m_ptr;
|
|
}
|
|
const T* back()const noexcept {
|
|
return m_ptr + m_count;
|
|
}
|
|
const T* at(int i) const noexcept {
|
|
if (i < m_count) {
|
|
return m_ptr + i;
|
|
}
|
|
return nullptr;
|
|
}
|
|
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;
|
|
}
|
|
};
|
|
};
|
|
} |