PGSStas

Untitled

Nov 13th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.62 KB | None | 0 0
  1. template<typename T>
  2. class BiDirectionalList {
  3.  public:
  4.   // Node
  5.   // It have pointers on next and previous nodes
  6.   // Also, it is have two constructors: lvalue and rvalue
  7.   struct Node {
  8.    public:
  9.     T value;
  10.    private:
  11.     explicit Node(const T& val, Node* prev_node, Node* next_node);
  12.     explicit Node(T&& val, Node* prev_node, Node* next_node);
  13.     Node* next_node_;
  14.     Node* prev_node_;
  15.  
  16.     friend BiDirectionalList;
  17.   };
  18.   // Constructors and assignment operators
  19.   BiDirectionalList() = default;  // Default constructor
  20.   BiDirectionalList(const std::initializer_list<T>& init);  // InitList
  21.   BiDirectionalList(const BiDirectionalList& second_list);  // Copy constructor
  22.   BiDirectionalList& operator=(const BiDirectionalList& second_list);  // Copy
  23.   BiDirectionalList(BiDirectionalList&& second_list);  // Move constructor
  24.   BiDirectionalList& operator=(BiDirectionalList&& second_list);  // Move
  25.   // Destructor
  26.   ~BiDirectionalList();
  27.   // Size methods
  28.   int Size() const;  // returns size of list
  29.   bool IsEmpty() const;  // returns true if list is empty
  30.   // Push values
  31.   void PushFront(const T& value);  // push lvalue to front of list
  32.   void PushFront(T&& value);  // push rvalue to front of list
  33.   void PushBack(const T& value);  // push lvalue to back of list
  34.   void PushBack(T&& value);  // push rvalue to back of list
  35.   // Get Node non constant methods/operators
  36.   Node* Front();
  37.   Node* Back();
  38.   Node* operator[](int index);
  39.   // Get Node constant methods/operators
  40.   const Node* Front() const;
  41.   const Node* Back() const;
  42.   const Node* operator[](int index) const;
  43.   // Pop
  44.   void PopFront();
  45.   void PopBack();
  46.   // Convert to vector
  47.   std::vector<T> ToVector() const;
  48.   // Get first entry of Node
  49.   int Find(const T& value) const;
  50.   // Get all entries of Node
  51.   std::vector<int> FindAll(const T& value) const;
  52.   // Insert Node
  53.   void InsertBefore(Node* element, const T& value);  // insert lvalue before
  54.   void InsertBefore(Node* element, T&& value);  // insert rvalue before
  55.   void InsertAfter(Node* element, const T& value);  // insert lvalue after
  56.   void InsertAfter(Node* element, T&& value);  // insert rvalue after
  57.  
  58.   // Erase Node
  59.   void Erase(Node* element);
  60.   // Compare
  61.   bool operator==(const BiDirectionalList<T>& second_list) const;
  62.   bool operator!=(const BiDirectionalList<T>& second_list) const;
  63.  
  64.  private:
  65.   void Clear();
  66.  
  67.   int size_ = 0;
  68.   Node* front_node_ = nullptr;
  69.   Node* back_node_ = nullptr;
  70. };
  71.  
  72. template<typename T>
  73. class SharedPtr {
  74.  public:
  75.   // Constructors
  76.   SharedPtr();
  77.   explicit SharedPtr(T* pointer);
  78.   SharedPtr<T>& operator=(T* pointer);
  79.   // Copy
  80.   SharedPtr(const SharedPtr& shared_pointer);
  81.   SharedPtr<T>& operator=(const SharedPtr& shared_pointer);
  82.   // Move
  83.   SharedPtr(SharedPtr&& shared_pointer);
  84.   SharedPtr<T>& operator=(SharedPtr&& shared_pointer);
  85.   // Destructor
  86.   ~SharedPtr();
  87.   // Get pointer
  88.   T* Get();
  89.   const T* Get() const;
  90.   // Release object
  91.   void Release();
  92.   // Operators
  93.   T& operator*();
  94.   T* operator->();
  95.  
  96.   const T& operator*() const;
  97.   const T* operator->() const;
  98.   // Equal / not equal
  99.   bool operator==(const SharedPtr<T>& second_ptr) const;
  100.   bool operator!=(const SharedPtr<T>& second_ptr) const;
  101.  
  102.   bool operator==(T* second_ptr) const;
  103.   bool operator!=(T* second_ptr) const;
  104.  
  105.   template<typename S>
  106.   friend bool operator==(S* first_ptr, const SharedPtr<S>& second_ptr);
  107.   template<typename S>
  108.   friend bool operator!=(S* first_ptr, const SharedPtr<S>& second_ptr);
  109.  
  110.  private:
  111.   // Clear shared pointer
  112.   void Destroy();
  113.  
  114.   T* pointer_;
  115.   int* counter_;
  116.   bool* is_correct_;
  117. };
Advertisement
Add Comment
Please, Sign In to add comment