Clean and doc

This commit is contained in:
Baptiste Wicht 2016-09-27 17:45:04 +02:00
parent 74fbd907a7
commit d6734a0ce7
No known key found for this signature in database
GPG Key ID: C5566B6C7F884532

View File

@ -78,21 +78,33 @@ private:
node_type* current; node_type* current;
}; };
/*!
* \brief A doubly-linked list implementation.
*
* This container should almost never be preferred over vector. The only time when list is better than vector
* is when the data is very expensive to copy or it is necessary to have stable references.
*/
template<typename T> template<typename T>
struct list { struct list {
using value_type = T; using value_type = T; ///7< The value type of the container
using pointer_type = value_type*; using pointer_type = value_type*; ///< The pointer type of the container
using size_type = size_t; using size_type = size_t; ///< The size type of the container
using node_type = list_node<T>; using node_type = list_node<T>; ///< The type of nodes
using iterator = list_iterator<T, T>; using iterator = list_iterator<T, T>; ///< The iterator type
using const_iterator = list_iterator<T, std::add_const_t<T>>; using const_iterator = list_iterator<T, std::add_const_t<T>>; ///< The const iterator type
using reverse_iterator = std::reverse_iterator<list_iterator<T, T>>; using reverse_iterator = std::reverse_iterator<list_iterator<T, T>>; ///< The reverse iterator type
using const_reverse_iterator = std::reverse_iterator<list_iterator<T, std::add_const_t<T>>>; using const_reverse_iterator = std::reverse_iterator<list_iterator<T, std::add_const_t<T>>>; ///< The const reverse iterator type
/*!
* \brief Construct a ne empty list
*/
list() : _size(0), head(nullptr), tail(nullptr) { list() : _size(0), head(nullptr), tail(nullptr) {
//Nothing else to init //Nothing else to init
} }
/*!
* \brief Destructs a list
*/
~list(){ ~list(){
clear(); clear();
} }
@ -130,20 +142,32 @@ struct list {
return *this; return *this;
} }
/*!
* \brief Returns the size of the container
*/
size_t size() const { size_t size() const {
return _size; return _size;
} }
/*!
* \brief Indicates if the list is empty
*/
bool empty() const { bool empty() const {
return _size; return _size;
} }
/*!
* \brief Empty the list
*/
void clear(){ void clear(){
while(!empty()){ while(!empty()){
pop_back(); pop_back();
} }
} }
/*!
* \brief Add a new element at the front of the list
*/
void push_front(const value_type& value){ void push_front(const value_type& value){
if(_size == 0){ if(_size == 0){
head = new node_type(value, nullptr, nullptr); head = new node_type(value, nullptr, nullptr);
@ -157,6 +181,9 @@ struct list {
++_size; ++_size;
} }
/*!
* \brief Add a new element at the back of the list
*/
void push_back(const value_type& value){ void push_back(const value_type& value){
if(_size == 0){ if(_size == 0){
head = new node_type(value, nullptr, nullptr); head = new node_type(value, nullptr, nullptr);
@ -202,6 +229,9 @@ struct list {
return tail->value; return tail->value;
} }
/*!
* \brief Removes the element at the front of the list
*/
void pop_front(){ void pop_front(){
auto old = head; auto old = head;
@ -217,6 +247,9 @@ struct list {
--_size; --_size;
} }
/*!
* \brief Removes the element at the back of the list
*/
void pop_back(){ void pop_back(){
auto old = tail; auto old = tail;
@ -290,36 +323,60 @@ public:
// Element access // Element access
/*!
* \brief Returns a reference to the element at the front of the list
*/
T& front(){ T& front(){
return head->value; return head->value;
} }
/*!
* \brief Returns a const reference to the element at the front of the list
*/
const T& front() const { const T& front() const {
return head->value; return head->value;
} }
/*!
* \brief Returns a reference to the element at the back of the list
*/
T& back(){ T& back(){
return tail->value; return tail->value;
} }
/*!
* \brief Returns a const reference to the element at the back of the list
*/
const T& back() const { const T& back() const {
return tail->value; return tail->value;
} }
// Iterators // Iterators
/*!
* \brief Returns an iterator pointing to the first element of the list
*/
iterator begin(){ iterator begin(){
return iterator(head); return iterator(head);
} }
/*!
* \brief Returns a const iterator pointing to the first element of the list
*/
const iterator begin() const { const iterator begin() const {
return const_iterator(head); return const_iterator(head);
} }
/*!
* \brief Returns an iterator pointing to the past-the-end element of the list
*/
iterator end(){ iterator end(){
return iterator(nullptr); return iterator(nullptr);
} }
/*!
* \brief Returns a const iterator pointing to the past-the-end element of the list
*/
const_iterator end() const { const_iterator end() const {
return const_iterator(nullptr); return const_iterator(nullptr);
} }
@ -341,17 +398,13 @@ public:
} }
private: private:
size_t _size; size_t _size; ///< The size of the list
node_type* head; node_type* head; ///< The front node of the list
node_type* tail; node_type* tail; ///< The tail node of the list
}; };
template<typename T> template<typename T>
struct list_node { struct list_node {
T value;
list_node<T>* next;
list_node<T>* prev;
list_node(const T& v, list_node<T>* n, list_node<T>* p) : value(v), next(n), prev(p) { list_node(const T& v, list_node<T>* n, list_node<T>* p) : value(v), next(n), prev(p) {
//Nothing else to init //Nothing else to init
} }
@ -360,6 +413,15 @@ struct list_node {
list_node(list_node<T>* n, list_node<T>* p, Args&&... args) : value(std::forward<Args>(args)...), next(n), prev(p) { list_node(list_node<T>* n, list_node<T>* p, Args&&... args) : value(std::forward<Args>(args)...), next(n), prev(p) {
//Nothing else to init //Nothing else to init
} }
friend struct list<T>;
friend struct list_iterator<T, T>;
friend struct list_iterator<T, std::add_const_t<T>>;
private:
T value;
list_node<T>* next;
list_node<T>* prev;
}; };
} //end of namespace std } //end of namespace std