#ifndef MSVC_LIST_H #define MSVC_LIST_H #include #include #include #include #include "allocator.h" namespace msvc { template struct list { class node { public: msvc::list::node *next; msvc::list::node *prev; T data; node() { this->next = nullptr; this->prev = nullptr; } node( msvc::list::node *prev, msvc::list::node *next, T data) { this->next = next; this->prev = prev; this->data = data; } }; public: typedef size_t size_type; typedef T value_type; typedef T& reference; typedef T* pointer; // forward iterator class iterator { public: typedef iterator self_type; typedef int difference_type; typedef std::forward_iterator_tag iterator_category; iterator() {} iterator(msvc::list::node* ptr) : list_node(ptr) { } self_type operator=(const self_type& other) { list_node = other.list_node; return *this; } self_type operator++() { list_node = list_node->next; return *this; } // ++i self_type operator++(int junk) { self_type i = *this; list_node = list_node->next; return i; } // i++ self_type operator--() { list_node = list_node->prev; return *this; } self_type operator--(int junk) { self_type i = *this; list_node = list_node->prev; return i; } reference operator*() { return list_node->data; } pointer operator->() { return &list_node->data; } bool operator==(const self_type& rhs) const { return list_node == rhs.list_node; } bool operator!=(const self_type& rhs) const { return list_node != rhs.list_node; } msvc::list::node* list_node; }; private: msvc::list::node *head; size_type _size; public: reference front() { return head->next->data; } reference back() { return head->prev->data; } iterator begin() { return iterator(head->next); } iterator end() { return iterator(head); } size_type size() { return _size; } size_type max_size() const noexcept { // Silly, theoretical max container size. return UINTPTR_MAX / sizeof(T); } iterator insert( iterator pos, const T& value ) { list::node* current_node = pos.list_node; list::node* previous_node = current_node->prev; list::node* new_node = new node(previous_node, current_node, value); current_node->prev = new_node; previous_node->next = new_node; _size++; return iterator(new_node); } iterator erase( iterator pos ) { if (!size()) { return end(); } list::node* current_node = pos.list_node; list::node* previous_node = current_node->prev; list::node* next_node = current_node->next; next_node->prev = previous_node; previous_node->next = next_node; delete current_node; return iterator(next_node); } }; } #endif // MSVC_LIST_H