您的位置:首页 > 其它

MyList 双链表

2016-03-13 17:33 344 查看
1.改进完善迭代器

2.了解xmonery这个类,改进自己内存管理。

#define ZB_MYLIST_
#ifdef ZB_MYLIST_
#include "debug.h"
#include <xstddef>

class AllocMyList
{
public:
AllocMyList(){}
~AllocMyList(){}
void* Allocator(size_t len) { return new char[len]; }
void Free(void *ptr, size_t len = 0) { delete[] ptr; }
void Swip(){}

template<class _Objty,
class... _Types>
void construct(_Objty *_Ptr, _Types&&... _Args)
{	// construct _Objty(_Types...) at _Ptr
::new ((void *)_Ptr) _Objty(_STD forward<_Types>(_Args)...);
}
};

template<typename T, typename Alloc = AllocMyList>
class MyList;

template<typename T>
struct Node
{
typedef T value_type;
typedef Node<T>* node_printer;
public:
Node()
:Data(), pPrev(nullptr), pNext(nullptr){}

Node(value_type data)
:Data(data), pPrev(nullptr), pNext(nullptr){}

Node(value_type data, node_printer prev, node_printer next)
:Data(data), pPrev(prev), pNext(next){}

bool operator==(Node& Rigth)
{
return (this->Data == Rigth.Data &&
this->pPrev == Rigth.pPrev &&
this->pNext == Rigth.pNext);
}

bool operator!=(Node& Right)
{
return !(*this == Right);
}

value_type Data;
node_printer pPrev;
node_printer pNext;
};

template<typename T>
class TIterator
{
typedef T value_type;
typedef Node<T> node_type;
typedef Node<T>* node_printer;
typedef MyList<T>* hash_printer;
typedef TIterator<T> iterator;
public:
TIterator():m_pNode(nullptr), m_pSelf(nullptr) {}

TIterator(node_printer pNode, hash_printer pList)
:m_pNode(pNode), m_pSelf(pList){}

node_printer GetNode()
{
return m_pNode;
}

value_type GetData()
{
return m_pNode->Data;
}

iterator& operator++()
{
ASSERT (m_pSelf->GetCount == 0 ||
m_pNode->pNext == nullptr);

this->m_pNode = this->m_pNode->pNext;
return *this;
}

iterator operator++(int)
{
TIterator temp = this;
++this;
return temp;
}

iterator& operator--()
{
ASSERT(m_pSelf->size() == 0 ||
m_pNode->pNext == nullptr);

return *this;
}

iterator operator--(int)
{
iterator temp = this;
--this;
return temp;
}
bool operator==(TIterator& Right)
{
return (this->m_pNode == Right.m_pNode);
}

bool operator!=(TIterator& Right)
{
return !(*this == Right);
}
private:
node_printer m_pNode;
hash_printer m_pSelf;
};

template<typename T, typename Alloc = AllocMyList>
class MyList
{
typedef T value_type;
typedef T* value_printer;
typedef T& reference;
typedef Node<T> node_type;
typedef Node<T>* node_printer;
typedef TIterator<T> iterator;
typedef size_t size_type;
public:
MyList()
{
MyHead = Buy();
MySize = 0;
}

MyList(size_type nCount)
{

}

MyList(size_type nCount, value_type Val)
{
Construct_n(nCount, Val);
}

// 返回当前对象个数
size_type size() const { return (this->MySize); }
size_type max_size() const { return ((size_type)(-1) / sizeof(value_type)); }//*
// 提供访问函数
reference front(){ return (*begin()); }
reference back() { return (*(end() - 1)); }
// 获取几种迭代器
iterator begin() const
{
return iterator(this->MyHead->pNext, this);
}

iterator end() const
{
return iterator(this->MyHead, this);
}

protected:
static node_printer& Nextnode(node_printer pNode)
{
return ((node_printer&)pNode->pNext);
}

static node_printer& Prevnode(node_printer pNode)
{
return ((node_printer&)pNode->pPrev);
}

static reference Myval(node_printer pNode)
{
return ((reference)pNode->Data);
}

private:
void Construct_n(size_type nCount, value_type Val)
{
Insert_n(begin(), nCount, Val);
}

void Insert_n(iterator Where, size_type nCount, value_type Val)
{
size_type nSaveCount = nCount;
_TRY_BEGIN
for (; nCount > 0; --nCount)
{
Inser(Where, Val);
}
_CATCH_ALL
for (;nCount < nSaveCount; ++nCount)
{
iterator Before = Where;
uncheck_erase(--Before);
}
_RERAISE;
_CATCH_END
}

void uncheck_erase(iterator Where)
{
node_printer pNode = Where.GetNode();
this->Nextnode(this->Prevnode(pNode)) = this->Nextnode(pNode);
this->Prevnode(this->Nextnode(pNode)) = this->Prevnode(pNode);
this->MySize--;
}

void Inser(iterator Where, value_type Val)
{
node_printer pNode = Where.GetNode();
node_printer new_node = BuyNode(Prevnode(pNode), pNode, Val);
IncSize(1);
this->Prevnode(pNode) = new_node;
this->Nextnode(this->Prevnode(new_node)) = new_node;
}

Alloc& Getal()
{
return (this->alloc);
}

Alloc& Getal() const
{
return (this->alloc);
}

node_printer BuyNode(node_printer prev, node_printer next, value_type data)
{
node_printer temp = static_cast<node_type*>(this->Getal().Allocator(sizeof(data)));
if (*next == node_type())
{
prev = temp;
next = temp;
}
_TRY_BEGIN
this->Getal().construct(std::addressof(Prevnode(temp)), prev);
this->Getal().construct(std::addressof(Nextnode(temp)), next);
_CATCH_ALL
this->Getal().Free(temp);
_RERAISE;
_CATCH_END
return temp;
}

void IncSize(size_type nCount)
{
if (max_size() - this->MySize - 1 < nCount)
ASSERT("Mylist<T> is too long");
this->MySize += nCount;
}
private:
Alloc alloc;
node_printer MyHead;
size_type MySize;
};

#endif//ZB_MYLIST_
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: