BS 0.0.1
This commit is contained in:
@@ -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<>
|
||||||
@@ -160,11 +174,45 @@ 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 (left < right)
|
if constexpr (std::is_arithmetic_v<T>)
|
||||||
return -1;
|
{
|
||||||
else if (right < left)
|
return left - right;
|
||||||
return 1;
|
}
|
||||||
return 0;
|
else
|
||||||
|
{
|
||||||
|
if (left < right)
|
||||||
|
return -1;
|
||||||
|
else if (right < left)
|
||||||
|
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;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@@ -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>
|
* @brief 序列迭代器的默认实现
|
||||||
|
* @tparam Sequence 序列类型
|
||||||
|
* @tparam Index 索引类型, 必须为具有operator++()的类型
|
||||||
|
* @tparam ReadValueType 读出元素类型
|
||||||
|
* @version BS 0.0.1
|
||||||
|
*/
|
||||||
|
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:
|
||||||
|
Sequence& target;
|
||||||
|
Index index;
|
||||||
public:
|
public:
|
||||||
DefaultSequenceIterator() {}
|
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;
|
||||||
|
}
|
||||||
|
virtual ~DefaultSequenceIterator() {}
|
||||||
|
void Next()
|
||||||
|
{
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
ReadValueType ReadValue() const
|
||||||
|
{
|
||||||
|
return target[index];
|
||||||
|
}
|
||||||
|
bool operator==(const ISequenceIterator<ReadValueType>& other) const noexcept
|
||||||
|
{
|
||||||
|
auto ptr = dynamic_cast<const DefaultSequenceIterator* const>(&other);
|
||||||
|
if (ptr != nullptr)
|
||||||
|
return index == ptr->index;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @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<
|
||||||
class SequenceIterator
|
typename Element,
|
||||||
|
typename Index = int,
|
||||||
|
typename ReadValueType = Element&
|
||||||
|
>
|
||||||
|
struct ISequence
|
||||||
{
|
{
|
||||||
private:
|
virtual ~ISequence() {}
|
||||||
const Sequence& target;
|
virtual ReadValueType operator[](Index index) abstract;
|
||||||
_Index index;
|
virtual size_t size() const noexcept abstract;
|
||||||
public:
|
Iterator::ISequenceIterator<ReadValueType> begin() abstract;
|
||||||
SequenceIterator(const Sequence& target, _Index index) :__init(target), __init(index) {}
|
Iterator::ISequenceIterator<ReadValueType> end() abstract;
|
||||||
SequenceIterator(const SequenceIterator& other) : target(other.target), index(other.index) {}
|
|
||||||
SequenceIterator& operator=(const SequenceIterator& other)
|
|
||||||
{
|
|
||||||
target = other.target;
|
|
||||||
index = other.index;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
bool operator==(const SequenceIterator& other) noexcept
|
|
||||||
{
|
|
||||||
return other.index == index;
|
|
||||||
}
|
|
||||||
bool operator!=(const SequenceIterator& other) noexcept
|
|
||||||
{
|
|
||||||
return other.index != index;
|
|
||||||
}
|
|
||||||
SequenceIterator& operator++() 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);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief 序列容器可枚举类型
|
|
||||||
* @tparam Sequence 序列类型
|
|
||||||
* @tparam _Index 索引类型
|
|
||||||
* @version BS 0.0.1
|
|
||||||
*/
|
|
||||||
template<typename Sequence, typename _Index = int>
|
|
||||||
struct SequenceRange
|
|
||||||
{
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief 栈上静态数组
|
* @brief 栈上静态数组
|
||||||
* @tparam Element 内容物类型
|
* @tparam Element 内容物类型
|
||||||
|
Reference in New Issue
Block a user