From 1b40329be038434a83db258b377bbf804567a799 Mon Sep 17 00:00:00 2001 From: Baptiste Wicht Date: Wed, 29 Jan 2014 14:43:13 +0100 Subject: [PATCH] Implement a simple doubly linked list --- kernel/include/stl/list.hpp | 140 ++++++++++++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 kernel/include/stl/list.hpp diff --git a/kernel/include/stl/list.hpp b/kernel/include/stl/list.hpp new file mode 100644 index 00000000..9288ab52 --- /dev/null +++ b/kernel/include/stl/list.hpp @@ -0,0 +1,140 @@ +//======================================================================= +// Copyright Baptiste Wicht 2013-2014. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +//======================================================================= + +#ifndef LIST_H +#define LIST_H + +#include "stl/types.hpp" + +namespace std { + +template +struct list_node; + +template +class list { +public: + typedef T value_type; + typedef value_type* pointer_type; + typedef size_t size_type; + typedef list_node node_type; + +private: + size_t _size; + node_type* head; + node_type* tail; + +public: + list() : _size(0), head(nullptr), tail(nullptr) { + //Nothing else to init + } + + // Disable copy for now + list(const list& rhs) = delete; + list& operator=(const list& rhs) = delete; + + // Disable move for now + list(list&& rhs) = delete; + list& operator=(list&& rhs) = delete; + + size_t size() const { + return _size; + } + + bool empty() const { + return _size; + } + + void push_front(const value_type& value){ + if(_size == 0){ + head = new node_type(value, nullptr, nullptr); + tail = head; + } else { + auto node = new node_type(value, head, nullptr); + head->prev = node; + head = node; + } + + ++_size; + } + + void push_back(const value_type& value){ + if(_size == 0){ + head = new node_type(value, nullptr, nullptr); + tail = head; + } else { + auto node = new node_type(value, nullptr, tail); + tail->next = node; + tail = node; + } + + ++_size; + } + + void pop_front(){ + auto old = head; + + if(_size == 1){ + tail = head = nullptr; + } else { + head = head->next; + head->prev = nullptr; + } + + delete old; + + --_size; + } + + void pop_back(){ + auto old = tail; + + if(_size == 1){ + tail = head = nullptr; + } else { + tail = tail->prev; + tail->next = nullptr; + } + + delete old; + + --_size; + } + + const T& front() const { + return head->value; + } + + T& front(){ + return head->value; + } + + const T& back() const { + return tail->value; + } + + T& back(){ + return tail->value; + } + + //TODO +}; + +template +struct list_node { + T value; + list_node* next; + list_node* prev; + + list_node(const T& v, list_node* n, list_node* p) : value(v), next(n), prev(p) { + //Nothing else to init + } +}; + +} //end of namespace std + +#endif