Sagar2018

List_H

Nov 11th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.34 KB | None | 0 0
  1. #ifndef LIST_H
  2. #define LIST_H value
  3.  
  4. #include <memory>
  5. #include <iostream>
  6.  
  7. template <bool flag, class IsTrue, class IsFalse>
  8. struct choose;
  9.  
  10. template <class IsTrue, class IsFalse>
  11. struct choose<true, IsTrue, IsFalse> {
  12.    typedef IsTrue type;
  13. };
  14.  
  15. template <class IsTrue, class IsFalse>
  16. struct choose<false, IsTrue, IsFalse> {
  17.    typedef IsFalse type;
  18. };
  19.  
  20. /* using shared_ptr = std::shared_ptr; */
  21. using namespace std;
  22.  
  23. /**
  24.  * Singly linked list
  25.  */
  26. template<typename D>
  27. class List {
  28.     struct Node {
  29.         shared_ptr<D> data;
  30.         Node* next;
  31.         Node* prev;
  32.         Node(shared_ptr<D> d, Node* p, Node* n) : data(d), prev(p), next(n) {}
  33.         ~Node() {
  34.             data.reset();
  35.             delete next;
  36.         }
  37.     };
  38.  
  39.     /**
  40.      * Iterator structure for this list
  41.      */
  42.     template <bool isconst = false>
  43.     struct iterator : public std::iterator<std::forward_iterator_tag, shared_ptr<D>> {
  44.         typedef std::forward_iterator_tag iterator_category;
  45.         typedef shared_ptr<D> value_type;
  46.         typedef std::ptrdiff_t Distance;
  47.         typedef typename choose<isconst, const value_type&, value_type&>::type
  48.                 Reference;
  49.         typedef typename choose<isconst, const value_type*, value_type*>::type
  50.                 Pointer;
  51.  
  52.         typedef typename choose<isconst, const Node*, Node*>::type
  53.                 nodeptr;
  54.  
  55.         iterator(nodeptr x = nullptr) : curr_node(x) {}
  56.  
  57.         iterator(const iterator<false>& i) : curr_node(i.curr_node) {}
  58.  
  59.         Reference operator*() const { return curr_node->data; }
  60.  
  61.         Pointer operator->() const { return &(curr_node->data); }
  62.  
  63.         template<bool A>
  64.         friend bool operator==(const iterator<A>& a, const iterator<A>& b) {
  65.             return a.curr_node == b.curr_node;
  66.         }
  67.  
  68.         template<bool A>
  69.         friend bool operator!=(const iterator<A>& a, const iterator<A>& b) {
  70.             return !(a.curr_node == b.curr_node);
  71.         }
  72.  
  73.         friend class List<D>;
  74.  
  75.         iterator& operator++() {
  76.             curr_node = curr_node->next;
  77.             return *this;
  78.         }
  79.  
  80.         private:
  81.             nodeptr curr_node;
  82.     };
  83.  
  84.     public:
  85.         /**
  86.          * Create a new empty list
  87.          */
  88.         List();
  89.  
  90.         int len() const;
  91.  
  92.         ~List();
  93.  
  94.         /**
  95.          * allows us to stream the list
  96.          */
  97.         std::ostream& dump(std::ostream &strm) const {
  98.             for (const auto s : *this) {
  99.                 strm << *s << std::endl;
  100.             }
  101.             return strm;
  102.         }
  103.  
  104.         /**
  105.          * iterator methods
  106.          */
  107.         iterator<false> begin() {
  108.             return iterator<false>(head);
  109.         }
  110.  
  111.         iterator<false> end() {
  112.             return iterator<false>(nullptr);
  113.         }
  114.  
  115.         iterator<true> begin() const {
  116.             return iterator<true>(head);
  117.         }
  118.  
  119.         iterator<true> end() const {
  120.             return iterator<true>(nullptr);
  121.         }
  122.  
  123.     private:
  124.  
  125.         Node* head;
  126. };
  127.  
  128. template<typename D>
  129. List<D>::List() {
  130.     head = nullptr;
  131. }
  132.  
  133. template<typename D>
  134. int List<D>::len() const {
  135.     int ret = 0;
  136.     for (const auto& n : *this) {
  137.         ret++;
  138.     }
  139.     return ret;
  140. }
  141.  
  142. template<typename D>
  143. List<D>::~List() {
  144.     delete this->head;
  145. }
  146.  
  147.  
  148. #endif /* ifndef LIST_H */
Add Comment
Please, Sign In to add comment