您的位置:首页 > 其它

STL-list实现

2016-03-01 20:05 211 查看
#include<stdlib.h>
#include<new>
template <class T1, class T2>
inline void construct(T1* p, const T2& value)
{
new (p)T1(value);
}

template <class T>
inline void destroy(T * pointer)
{
pointer->~T();//显式的调用T的析构
}
template <class T>
inline void destroy(T * first, T * last)
{
for (; first < last; ++first)
{
destroy(&*first);
}
}
template <class T>
class simple_alloc
{
private:
static void * allocate_malloc(size_t n)
{
void * result = malloc(n);
return result;
}
static void deallocate(void * p, size_t)
{
//
free(p);
}
public:
static T * allocate(size_t n)
{
if (n == 0)
return 0;
else
{
return (T*)allocate_malloc(n * sizeof(T));
}
}
static T * allocate(void)
{
return (T*)allocate_malloc(sizeof(T));
}
static void deallocate(T *p, size_t n)
{
if (0 != n)
{
dellocate_free(p, n*sizeof(T));
}

}
static void deallocate(T *p)
{
dellocate_free(p, sizeof(T));
}
};

template <class I>
inline size_t distance(I first, I last)
{
size_t n = 0;
while (first != last)
++first; ++n;
return n;
}

template <class T>
struct list_node
{
typedef list_node<T>* pointer;
pointer prev;
pointer next;
T data;
};

template <class T , class Ref , class Ptr>
struct list_iterator
{
typedef list_iterator<T, T&, T*>    iterator;
typedef list_iterator<T, Ref, Ptr> self;

typedef T value_type;
typedef Ptr pointer;
typedef Ref reference;
typedef list_node<T>* link_type;
typedef size_t size_type;
typedef ptrdiff_t difference_type;

link_type node;

list_iterator(link_type x) :node(x) {}
list_iterator() {}
list_iterator(const iterator & x) :node(x.node) {}

bool operator==(const self& x) const
{
return node == x.node;
}
bool operator!=(const self& x) const
{
return node != x.node;
}
reference operator*() const
{
return (*node).data;
}
pointer operator->() const
{
return &(operator*());
}
self& operator++()
{
node = (link_type)((*node).next);
}
self operator++(int)
{
self tmp = *this;
++*this;
return tmp;
}
self& operator--()
{
node = (link_type)((*node).prev);
}
self operator--(int)
{
self tmp = *this;
--*this;
return tmp;
}
};

template <class T >
class list
{

protected:
typedef list_node<T> list_node;
typedef simple_alloc<list_node> list_node_allocator;
public:
typedef list_node* link_type;
typedef list_iterator<T , T& , T*> iterator;
typedef link_type& reference;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
protected:

link_type node;//双向循环链表的最后一个节点

link_type get_node()
{
return list_node_allocator::allocate();
}
void put_node(link_type p)
{
list_node_allocator::deallocate(p);
}

link_type create_node(const T& x)
{
link_type p = get_node();
construct(&p->data, x);
return p;
}

void destroy_node(link_type p)
{
destroy(&p->data);
put_node(p);
}

void empty_initialize()
{
node = get_node();
node->next = node;
node->prev = node;
}

iterator insert(iterator position, const T& x)
{
link_type tmp = create_node(x);
tmp->next = position.node;
tmp->prev = position.node->prev;
(link_type(position.node->prev))->next = tmp;
position.node->prev = tmp;
return tmp;
}
public:
list()
{
empty_initialize();
}
iterator begin()
{
return (link_type)((*node).next);
}
iterator end()
{
return node;
}
bool empty() const
{
return node->next == node;
}
size_type size() const
{
size_type result = 0;
result = distance(begin(), end(), result);
return result;
}
reference front()
{
return *begin();
}
reference back()
{
return *(--end());
}

void push_front(const T& x)
{
insert(begin(), x);
}
void push_back(const T& x)
{
insert(end(), x);
}

iterator erase(iterator position)
{
link_type next_node = link_type(position.node->next);
link_type prev_node = link_type(position.node->prev);
prev_node->next = next_node;
next_node->prev = prev_node;
destroy_node(position.node);
return iterator(next_node);
}

void pop_front()
{
erase(begin());
}
void pop_back()
{
iterator tmp = end();
erase(--tmp);
}

void clear()
{
link_type cur = (link_type)node->next;
while (cur != node)
{
link_type tmp = cur;
cur = (link_type)cur->next;
destroy_node(tmp);
}
node->next = node;
node->prev = node;
}

void remove(const T& value)
{
iterator first = begin();
iterator last = end();
while (first != last)
{
iterator next = first;
++next;
if (*first == value)
erase(first);
first = next;
}
}
};


参考书籍:《STL源码剖析》
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: