This commit is contained in:
ninemine
2025-06-13 21:56:11 +08:00
parent 7c7e337e1b
commit cbf0dc51d1
2 changed files with 130 additions and 98 deletions

View File

@@ -150,7 +150,21 @@ class ICompare
{ {
public: public:
virtual ~ICompare() {} virtual ~ICompare() {}
virtual int Compare(T left, T right) const noexcept abstract; virtual int Compare(T left, T right) const noexcept
{
if constexpr (std::is_arithmetic_v<T>)
{
return left - right;
}
else
{
if (left < right)
return -1;
else if (right < left)
return 1;
return 0;
}
}
}; };
template<> template<>
@@ -159,6 +173,12 @@ class ICompare<void>
public: public:
template<typename T> template<typename T>
int Compare(const T& left, const T& right) const noexcept int Compare(const T& left, const T& right) const noexcept
{
if constexpr (std::is_arithmetic_v<T>)
{
return left - right;
}
else
{ {
if (left < right) if (left < right)
return -1; return -1;
@@ -166,6 +186,34 @@ public:
return 1; return 1;
return 0; return 0;
} }
}
};
template<typename T, typename Comparer = ICompare<T>>
class IComparable
{
public:
virtual ~IComparable() {}
bool operator>(const T& other) const
{
return Comparer().Compare(*this, other) > 0;
}
bool operator<(const T& other) const
{
return Comparer().Compare(*this, other) < 0;
}
bool operator==(const T& other) const
{
return Comparer().Compare(*this, other) == 0;
}
bool operator<=(const T& other) const
{
return Comparer().Compare(*this, other) <= 0;
}
bool operator>=(const T& other) const
{
return Comparer().Compare(*this, other) >= 0;
}
}; };
constexpr bool ConstexprStrEqual( constexpr bool ConstexprStrEqual(

View File

@@ -8,33 +8,21 @@ namespace Convention
{ {
namespace Generics namespace Generics
{ {
template<typename Element, typename Index = int, typename ReadValueType = Element&>
struct ISequence
{
virtual ~ISequence() {}
virtual ReadValueType operator[](Index index) abstract;
virtual Index size();
virtual Index GetBeginIndex() const abstract;
virtual Index GetEndIndex() const abstract;
virtual Index GetHeadIndex() const
{
return GetBeginIndex();
}
virtual Index GetTailIndex() const
{
return GetEndIndex() - 1;
}
};
namespace Iterator namespace Iterator
{ {
template<typename Element, typename ReadValueType = Element&>
/**
* @brief 序列迭代器接口
* @tparam ReadValueType 读出元素类型
* @version BS 0.0.1
*/
template<typename ReadValueType>
struct ISequenceIterator struct ISequenceIterator
{ {
virtual ~ISequenceIterator() {} virtual ~ISequenceIterator() {}
virtual void Next() abstract; virtual void Next() abstract;
virtual ReadValueType ReadValue() const abstract; virtual ReadValueType ReadValue() const abstract;
virtual ISequenceIterator& operator++() ISequenceIterator& operator++()
{ {
this->Next(); this->Next();
return *this; return *this;
@@ -50,89 +38,85 @@ namespace Convention
} }
}; };
template<typename Element>
class DefaultSequenceIterator:public ISequenceIterator<Element>
{
public:
DefaultSequenceIterator() {}
};
}
/** /**
* @brief 序列容器枚举 * @brief 序列迭代器的默认实现
* @tparam Sequence 序列类型 * @tparam Sequence 序列类型
* @tparam _Index 索引类型 * @tparam Index 索引类型, 必须为具有operator++()的类型
* @tparam ReadValueType 读出元素类型
* @version BS 0.0.1 * @version BS 0.0.1
*/ */
template<typename Sequence, typename _Index = int> template<
class SequenceIterator typename Sequence,
typename Index,
typename ReadValueType = decltype(std::declval<Sequence>()[std::declval<Index>()])
>
class DefaultSequenceIterator
: public ISequenceIterator<ReadValueType>,
public IComparable<DefaultSequenceIterator<Sequence, Index, ReadValueType>, ICompare<Index>>
{ {
private: private:
const Sequence& target; Sequence& target;
_Index index; Index index;
public: public:
SequenceIterator(const Sequence& target, _Index index) :__init(target), __init(index) {} operator const Index&()
SequenceIterator(const SequenceIterator& other) : target(other.target), index(other.index) {} {
SequenceIterator& operator=(const SequenceIterator& other) return index;
}
DefaultSequenceIterator(Sequence& target, Index index)
: __init(target), __init(index)
{
}
DefaultSequenceIterator(const DefaultSequenceIterator& other) noexcept
: target(other.target), index(other.index)
{
}
DefaultSequenceIterator& operator=(const DefaultSequenceIterator& other) noexcept
{ {
target = other.target; target = other.target;
index = other.index; index = other.index;
return *this; return *this;
} }
bool operator==(const SequenceIterator& other) noexcept virtual ~DefaultSequenceIterator() {}
void Next()
{ {
return other.index == index; index++;
} }
bool operator!=(const SequenceIterator& other) noexcept ReadValueType ReadValue() const
{ {
return other.index != index; return target[index];
} }
SequenceIterator& operator++() noexcept bool operator==(const ISequenceIterator<ReadValueType>& other) const noexcept
{ {
++index; auto ptr = dynamic_cast<const DefaultSequenceIterator* const>(&other);
return *this; if (ptr != nullptr)
} return index == ptr->index;
SequenceIterator operator++(int) noexcept return false;
{
++index;
return SequenceIterator(target, index - 1);
}
SequenceIterator& operator--() noexcept
{
--index;
return *this;
}
SequenceIterator operator--(int) noexcept
{
--index;
return SequenceIterator(target, index + 1);
} }
}; };
}
/** /**
* @brief 序列容器可枚举类型 * @brief 序列接口
* @tparam Sequence 序列类型 * @tparam Element 元素
* @tparam _Index 索引类型 * @tparam Index 索引类型, 必须为具有operator++()的类型
* @tparam ReadValueType 读出元素类型
* @version BS 0.0.1 * @version BS 0.0.1
*/ */
template<typename Sequence, typename _Index = int> template<
struct SequenceRange typename Element,
typename Index = int,
typename ReadValueType = Element&
>
struct ISequence
{ {
using iterator = SequenceIterator<Sequence, _Index>; virtual ~ISequence() {}
iterator mbegin, mend; virtual ReadValueType operator[](Index index) abstract;
SequenceRange(const Sequence& target, _Index begin = 0, _Index end = 0ll - 1) :mbegin(target, begin), mend(target, end) {} virtual size_t size() const noexcept abstract;
Iterator::ISequenceIterator<ReadValueType> begin() abstract;
constexpr iterator begin() Iterator::ISequenceIterator<ReadValueType> end() abstract;
{
return mbegin;
}
constexpr iterator end()
{
return mend;
}
}; };
/** /**
* @brief 栈上静态数组 * @brief 栈上静态数组
* @tparam Element 内容物类型 * @tparam Element 内容物类型