算了,还是不 typedef 了,类型真烦
如果要 typedef,搞出来的都是些很恶心的名字,自己看了也生气,还是不搞好了,就 size_t 和 int 玩到底吧。
把 Array 的接口又改得一塌糊涂了,重新贴一下:
namespace xl
{
template <typename ValueType>
class Array
{
public:
Array(size_t nSize = 0);
Array(size_t nSize, const ValueType &tValue);
Array(const Array<ValueType> &that);
~Array();
public:
class Iterator
{
public:
Iterator();
Iterator(ValueType *pValue);
Iterator(ValueType *pValue, ValueType *pStart, ValueType *pEof);
Iterator(const Iterator &that);
private:
ValueType *m_pStart;
ValueType *m_pEof;
ValueType *m_pCurrent;
public:
ValueType &operator * ();
ValueType *operator -> ();
public:
Iterator &operator = (const Iterator &that);
bool operator == (const Iterator &that) const;
bool operator != (const Iterator &that) const;
public:
Iterator &operator ++ ();
Iterator operator ++ (int);
Iterator &operator -- ();
Iterator operator -- (int);
public:
Iterator operator +(int nDistance) const;
Iterator operator -(int nDistance) const;
Iterator &operator +=(int nDistance);
Iterator &operator -=(int nDistance);
};
class ReverseIterator : public Iterator
{
public:
ReverseIterator &operator ++ ();
ReverseIterator operator ++ (int);
ReverseIterator &operator -- ();
ReverseIterator operator -- (int);
};
public:
Iterator Begin() const;
Iterator End() const;
ReverseIterator RBegin() const;
ReverseIterator REnd() const;
public:
Array<ValueType> &operator=(const Array<ValueType> &that);
bool operator==(const Array<ValueType> &that) const;
bool operator!=(const Array<ValueType> &that) const;
public:
ValueType &operator[](size_t nIndex);
const ValueType &operator[](size_t nIndex) const;
public:
bool Empty();
size_t Size() const;
void SetSize(size_t nSize);
public:
void Insert(const Iterator &itBeforeWhich, const ValueType &tValue);
void Insert(const ReverseIterator &itBeforeWhich, const ValueType &tValue);
void PushFront(const ValueType &tValue);
void PushBack(const ValueType &tValue);
template <typename IteratorType>
void Insert(const Iterator &itBeforeWhich, const IteratorType &itFirstToInsert, const IteratorType &itAfterLastToInsert);
template <typename IteratorType>
void Insert(const ReverseIterator &itBeforeWhich, const IteratorType &itFirstToInsert, const IteratorType &itAfterLastToInsert);
Iterator Delete(const Iterator &itWhich);
ReverseIterator Delete(const ReverseIterator &itWhich);
void PopFront();
void PopBack();
Iterator Delete(const Iterator &itFirstToInsert, const Iterator &itAfterLastToDelete);
Iterator Delete(const ReverseIterator &itFirstToInsert, const ReverseIterator &itAfterLastToDelete);
void Clear();
void SetValue(const Iterator &itWhich, const ValueType &tValue);
void SetValue(const ReverseIterator &itWhich, const ValueType &tValue);
void SetValue(const Iterator &itFirstToSet, const Iterator &itAfterLastToSet, const ValueType &tValue);
void SetValue(const ReverseIterator &itFirstToSet, const ReverseIterator &itAfterLastToSet, const ValueType &tValue);
private:
ValueType *m_pData;
size_t m_nSize;
size_t m_nStart;
size_t m_nEof;
private:
void Release();
size_t GetWellSize(size_t nSize) const;
void MoveData(size_t nIndex, size_t nCount, int nDistance);
void CopyData(size_t nIndex, size_t nCount, ValueType *pNewMem) const;
};
}
主要的考虑,还是想实现“跨容器的 iterator”(抱歉,我还是觉得这么称呼挺符合我的预期的)。只是,在没有语言层面的 concepts 支持的情况下,如何显式地让用户知道模板参数要符合哪些条件呢?
在这里再次感谢一下 OwnWaterloo 同学,上篇评论里的东西我会继续慢慢琢磨的。^_^