#ifndef XING_LIST_H
#define XING_LIST_H
namespace xing
{
template <class _Ty>
class list
{
private:
struct _Node;
typedef struct _Node* _Nodeptr;
struct _Node
{
_Nodeptr _Prev, _Next;
_Ty _Value;
};
struct _Acc;
struct _Acc
{
typedef _Ty& _Vref;
typedef struct _Node*& _Nodepref;
static _Vref _Value(_Nodeptr _P)
{
return (*_P)._Value;
}
static _Nodepref _Prev(_Nodeptr _P)
{
return (*_P)._Prev;
}
static _Nodepref _Next(_Nodeptr _P)
{
return (*_P)._Next;
}
};
public:
typedef _Ty value_type;
typedef _Ty& reference;
typedef const _Ty& const_reference;
typedef _Ty* pointer;
typedef const _Ty const_pointer;
typedef size_t size_type;
typedef int difference_type;
class iterator;
class const_iterator;
class const_iterator
{
protected:
_Nodeptr _Ptr;
public:
const_iterator(_Nodeptr _P) :_Ptr(_P) {}
const_reference operator *()const
{
return _Acc::_Value(_Ptr);
}
const_pointer operator->()const
{
return &**this;
}
const_iterator& operator++()
{
return _Acc::_Next(_Ptr);
}
const_iterator operator++(int)
{
iterator old = *this;
++* this;
return old;
}
const_iterator& operator--()const
{
return _Acc::_Prev(_Ptr);
}
const_iterator operator--(int)
{
iterator old = *this;
--* this;
return old;
}
bool operator == (const const_iterator& _X)const
{
return _Ptr == _X._Ptr;
}
bool operator != (const const_iterator& _X)const
{
return !(*this == _X);
}
_Nodeptr _Mynode()const
{
return _Ptr;
}
};
class iterator :public const_iterator
{
public:
iterator(_Nodeptr _P = NULL) :const_iterator(_P) {};
typedef const_iterator Base;
reference operator *()const
{
return _Acc::_Value(Base::_Ptr);
}
pointer operator->()const
{
return &**this;
}
iterator& operator++()
{
Base::_Ptr = _Acc::_Next(Base::_Ptr);
return *this;
}
iterator operator++(int)
{
iterator old = *this;
++* this;
return old;
}
iterator& operator--()
{
Base::_Ptr = _Acc::_Prev(Base::_Ptr);
return *this;
}
iterator operator--(int)
{
iterator old = *this;
--* this;
return old;
}
bool operator == (const iterator& _X)const
{
return Base::_Ptr == _X.Base::_Ptr;
}
bool operator != (const iterator& _X)const
{
return !(*this == _X);
}
};
iterator begin()
{
return iterator(_Acc::_Next(_Head));
}
iterator end()
{
return iterator(_Head);
}
const_iterator begin()const
{
return const_iterator(_Acc::_Next(_Head));
}
const_iterator end()const
{
return const_iterator(_Head);
}
public:
list() :_Head(_BuyNode()), _Size(0) {};
~list()
{
clear();
_Freenode(_Head);
}
void push_front(const _Ty& val)
{
insert(begin(), val);
}
void push_back(const _Ty& val)
{
insert(end(), val);
}
iterator insert(iterator _P, const _Ty& val)
{
_Nodeptr _S = _P._Mynode();
_Acc::_Prev(_S) = _BuyNode(_Acc::_Prev(_S), _S);
_S = _Acc::_Prev(_S);
_Acc::_Next(_Acc::_Prev(_S)) = _S;
//_Acc::_Value(_S) = val;
new(&_Acc::_Value(_S))_Ty(val);
_Size += 1;
return _S;
}
iterator erase(iterator _P)
{
_Nodeptr _S = _P++._Mynode();
_Acc::_Prev(_Acc::_Next(_S)) = _Acc::_Prev(_S);
_Acc::_Next(_Acc::_Prev(_S)) = _Acc::_Next(_S);
(&_Acc::_Value(_S))->~_Ty();
_Freenode(_S);
_Size -= 1;
return _P;
}
void clear()
{
while (begin() != end())
{
erase(begin()++);
}
}
void remove()
{
}
private:
_Nodeptr _Head;
size_type _Size;
private:
_Nodeptr _BuyNode(_Nodeptr Narg = NULL, _Nodeptr Parg = NULL)
{
_Nodeptr _S = (_Nodeptr)malloc(sizeof(_Node));
_S->_Prev = Narg == NULL ? _S : Narg;
_S->_Next = Parg == NULL ? _S : Parg;
return _S;
}
void _Freenode(_Nodeptr _P)
{
free(_P);
}
};
}
#endif
迭代器实现
最新推荐文章于 2022-07-03 17:44:46 发布