diff options
Diffstat (limited to 'euler/include/std/list.hpp')
-rw-r--r-- | euler/include/std/list.hpp | 128 |
1 files changed, 119 insertions, 9 deletions
diff --git a/euler/include/std/list.hpp b/euler/include/std/list.hpp index 030eccc..cd777b2 100644 --- a/euler/include/std/list.hpp +++ b/euler/include/std/list.hpp @@ -1,10 +1,11 @@ #pragma once +#include <iterator> #include <memory> namespace std { - template <class T, class Allocator = std::allocator<T>> + template <class T> class list { public: @@ -18,6 +19,13 @@ namespace std { struct generic_iterator { node *the_node; + node *last_node; + + using iterator_category = std::bidirectional_iterator_tag; + using reference = V &; + using pointer = V *; + using value_type = V; + using difference_type = size_t; bool operator ==(const generic_iterator &other) const { return the_node == other.the_node; @@ -40,11 +48,29 @@ namespace std { return *this; } + generic_iterator &operator --() { + if (the_node == 0) + the_node = last_node; + else + the_node = the_node->prev; + return *this; + } + + generic_iterator operator -(size_t count) const { + generic_iterator it = { .the_node = the_node }; + for (size_t i = 0; i < count; ++i) + --it; + return it; + } + }; - using iterator = generic_iterator<T>; + using iterator = generic_iterator< T>; using const_iterator = generic_iterator<const T>; + using reverse_iterator = std::reverse_iterator< iterator>; + using const_reverse_iterator = std::reverse_iterator<const_iterator>; + private: node *first_node; node *last_node; @@ -79,17 +105,101 @@ namespace std { if (n->next) n->next->prev = n->prev; else last_node = n->prev; delete n; - return iterator { .the_node = r }; + return iterator { + .the_node = r, + .last_node = last_node + }; } - iterator begin() noexcept { return iterator { .the_node = first_node }; } - iterator end() noexcept { return iterator { .the_node = 0 }; } + iterator begin() noexcept { + return iterator { + .the_node = first_node, + .last_node = last_node + }; + } + + iterator end() noexcept { + return iterator { + .the_node = 0, + .last_node = last_node + }; + } - const_iterator begin() const noexcept { return iterator { .the_node = first_node }; } - const_iterator end() const noexcept { return iterator { .the_node = 0 }; } + const_iterator begin() const noexcept { + return iterator { + .the_node = first_node, + .last_node = last_node + }; + } - const_iterator cbegin() const noexcept { return iterator { .the_node = first_node }; } - const_iterator cend() const noexcept { return iterator { .the_node = 0 }; } + const_iterator end() const noexcept { + return iterator { + .the_node = 0, + .last_node = last_node + }; + } + + const_iterator cbegin() const noexcept { + return iterator { + .the_node = first_node, + .last_node = last_node + }; + } + + const_iterator cend() const noexcept { + return iterator { + .the_node = 0, + .last_node = last_node + }; + } + + reverse_iterator rbegin() noexcept { + return reverse_iterator( + iterator { + .the_node = 0, + .last_node = last_node + }); + } + + reverse_iterator rend() noexcept { + return reverse_iterator( + iterator { + .the_node = first_node, + .last_node = last_node + }); + } + + const_reverse_iterator rbegin() const noexcept { + return const_reverse_iterator( + iterator { + .the_node = 0, + .last_node = last_node + }); + } + + const_reverse_iterator rend() const noexcept { + return const_reverse_iterator( + iterator { + .the_node = first_node, + .last_node = last_node + }); + } + + const_reverse_iterator crbegin() const noexcept { + return const_reverse_iterator( + iterator { + .the_node = 0, + .last_node = last_node + }); + } + + const_reverse_iterator crend() const noexcept { + return const_reverse_iterator( + iterator { + .the_node = first_node, + .last_node = last_node + }); + } size_t remove(const T &value) { size_t removed = 0; |