#ifndef _M_LIST_H
#define _M_LIST_H
/***************************************
* Description:list实现
* Create:2019/12/1
* Author:zhangfeng
* History:
* 2019-12-3 搭建初步框架和基本接口
* node是循环连接,end->next连接front, front-prev连接end
* *************************************/
#include <memory>
#include <iostream>
#include <algorithm>
#include <cstddef>
//节点
template <class T>
struct List_node {
T _data;
List_node* _next;
List_node* _prev;
List_node(T x) : _data(x){}
};
//迭代器
template <class T, class Ref, class Ptr>
struct List_iterator {
typedef List_iterator<T, T&, T*> iterator;
typedef List_iterator<T, const T&, const T*> const_iterator;
typedef List_iterator<T, Ref, Ptr> _Self;
typedef T value_type;
typedef Ptr pointer;
typedef Ref reference;
typedef List_node<T> Node;
Node* _node;
List_iterator(Node *_x) : _node(_x) {}
reference operator*() const { return _node->_data; }
bool operator==(const iterator& rhs) { return _node == rhs._node; }
bool operator!=(const iterator& rhs) { return _node != rhs._node; }
void incr() { _node = _node->_next; }
void decr() { _node = _node->_prev; }
//++it 前缀
_Self& operator++() {
this->incr();
return *this;
}
//it++ 后缀
_Self operator++(int) {
_Self tmp = *this;
this->incr();
return tmp;
}
//--it 前缀
_Self& operator--() {
this->decr();
return *this;
}
//it-- 后缀
_Self operator--(int) {
_Self tmp = *this;
this->decr();
return tmp;
}
};
template <class T, class Alloc=std::allocator<List_node<T>>>
class mList {
public:
typedef T value_type;
typedef value_type* pointer;
typedef const value_type* const_pointer;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef List_node<T> Node;
public:
typedef List_iterator<T, T&, T*> iterator;
typedef List_iterator<T, const T&, const T*> const_iterator;
protected:
Node* create_node(const T& _x){
Node* p = _alloc.allocate(1);
try{
_alloc.construct(p, _x);
}catch(...){
_alloc.deallocate(p, 1);
}
return p;
}
public:
explicit mList() {
_node = _alloc.allocate(1);
_node->_next = _node;
_node->_prev = _node;
}
~mList() {
std::cout << "~mList" << std::endl;
clear();
_alloc.deallocate(_node, 1);
}
//元素访问
iterator begin() { return (Node*)_node->_next; }
const_iterator begin() const { return (Node*)_node->_next; }
iterator end() { return _node; }
const_iterator end() const { return _node; }
reference front() { return *begin(); }
const_reference front() const { return *begin(); }
reference back() { return *(--end()); }
const_reference back() const { return *(--end()); }
//容量
bool empty() const { return _node->_next = _node; }
size_type size() const {
size_type result = 0;
std::distance(begin(), end(), result);
return result;
}
size_type max_size() const { return size_type(-1); }
//修改器
void clear();
void push_back( const T& _x ) { insert(end(), _x); }
void push_front( const T& _x ) { insert(begin(), _x); }
iterator insert(iterator _pos, const T& _x){
Node* tmp = create_node(_x);
tmp->_next = _pos._node;
tmp->_prev = _pos._node->_prev;
_pos._node->_prev->_next = tmp;
_pos._node->_prev = tmp;
return tmp;
}
private:
Node* _node;
Alloc _alloc;
};
template <class T, class Alloc>
void mList<T, Alloc>::clear()
{
mList::Node* _cur = (mList::Node*)_node->_next;
while(_cur != _node) {
mList::Node* _tmp = _cur;
_cur = (mList::Node*)_cur->_next;
_alloc.destroy(&_tmp->_data);
_alloc.deallocate(_tmp, 1);
}
_node->_next = _node;
_node->_prev = _node;
}
#endif