BS 0.0.1
This commit is contained in:
@@ -150,7 +150,21 @@ class ICompare
|
||||
{
|
||||
public:
|
||||
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<>
|
||||
@@ -159,6 +173,12 @@ class ICompare<void>
|
||||
public:
|
||||
template<typename T>
|
||||
int Compare(const T& left, const T& right) const noexcept
|
||||
{
|
||||
if constexpr (std::is_arithmetic_v<T>)
|
||||
{
|
||||
return left - right;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (left < right)
|
||||
return -1;
|
||||
@@ -166,6 +186,34 @@ public:
|
||||
return 1;
|
||||
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(
|
||||
|
@@ -8,33 +8,21 @@ namespace Convention
|
||||
{
|
||||
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
|
||||
{
|
||||
template<typename Element, typename ReadValueType = Element&>
|
||||
|
||||
/**
|
||||
* @brief 序列迭代器接口
|
||||
* @tparam ReadValueType 读出元素类型
|
||||
* @version BS 0.0.1
|
||||
*/
|
||||
template<typename ReadValueType>
|
||||
struct ISequenceIterator
|
||||
{
|
||||
virtual ~ISequenceIterator() {}
|
||||
virtual void Next() abstract;
|
||||
virtual ReadValueType ReadValue() const abstract;
|
||||
virtual ISequenceIterator& operator++()
|
||||
ISequenceIterator& operator++()
|
||||
{
|
||||
this->Next();
|
||||
return *this;
|
||||
@@ -50,89 +38,85 @@ namespace Convention
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Element>
|
||||
class DefaultSequenceIterator:public ISequenceIterator<Element>
|
||||
{
|
||||
public:
|
||||
DefaultSequenceIterator() {}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 序列容器枚举
|
||||
* @brief 序列迭代器的默认实现
|
||||
* @tparam Sequence 序列类型
|
||||
* @tparam _Index 索引类型
|
||||
* @tparam Index 索引类型, 必须为具有operator++()的类型
|
||||
* @tparam ReadValueType 读出元素类型
|
||||
* @version BS 0.0.1
|
||||
*/
|
||||
template<typename Sequence, typename _Index = int>
|
||||
class SequenceIterator
|
||||
template<
|
||||
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:
|
||||
const Sequence& target;
|
||||
_Index index;
|
||||
Sequence& target;
|
||||
Index index;
|
||||
public:
|
||||
SequenceIterator(const Sequence& target, _Index index) :__init(target), __init(index) {}
|
||||
SequenceIterator(const SequenceIterator& other) : target(other.target), index(other.index) {}
|
||||
SequenceIterator& operator=(const SequenceIterator& other)
|
||||
operator const Index&()
|
||||
{
|
||||
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;
|
||||
index = other.index;
|
||||
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;
|
||||
return *this;
|
||||
}
|
||||
SequenceIterator operator++(int) noexcept
|
||||
{
|
||||
++index;
|
||||
return SequenceIterator(target, index - 1);
|
||||
}
|
||||
SequenceIterator& operator--() noexcept
|
||||
{
|
||||
--index;
|
||||
return *this;
|
||||
}
|
||||
SequenceIterator operator--(int) noexcept
|
||||
{
|
||||
--index;
|
||||
return SequenceIterator(target, index + 1);
|
||||
auto ptr = dynamic_cast<const DefaultSequenceIterator* const>(&other);
|
||||
if (ptr != nullptr)
|
||||
return index == ptr->index;
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 序列容器可枚举类型
|
||||
* @tparam Sequence 序列类型
|
||||
* @tparam _Index 索引类型
|
||||
* @brief 序列接口
|
||||
* @tparam Element 元素
|
||||
* @tparam Index 索引类型, 必须为具有operator++()的类型
|
||||
* @tparam ReadValueType 读出元素类型
|
||||
* @version BS 0.0.1
|
||||
*/
|
||||
template<typename Sequence, typename _Index = int>
|
||||
struct SequenceRange
|
||||
template<
|
||||
typename Element,
|
||||
typename Index = int,
|
||||
typename ReadValueType = Element&
|
||||
>
|
||||
struct ISequence
|
||||
{
|
||||
using iterator = SequenceIterator<Sequence, _Index>;
|
||||
iterator mbegin, mend;
|
||||
SequenceRange(const Sequence& target, _Index begin = 0, _Index end = 0ll - 1) :mbegin(target, begin), mend(target, end) {}
|
||||
|
||||
constexpr iterator begin()
|
||||
{
|
||||
return mbegin;
|
||||
}
|
||||
constexpr iterator end()
|
||||
{
|
||||
return mend;
|
||||
}
|
||||
virtual ~ISequence() {}
|
||||
virtual ReadValueType operator[](Index index) abstract;
|
||||
virtual size_t size() const noexcept abstract;
|
||||
Iterator::ISequenceIterator<ReadValueType> begin() abstract;
|
||||
Iterator::ISequenceIterator<ReadValueType> end() abstract;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @brief 栈上静态数组
|
||||
* @tparam Element 内容物类型
|
||||
|
Reference in New Issue
Block a user