1 /* 2 * Copyright (c) 2020 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef OHOS_UTILS_LIST_H 17 #define OHOS_UTILS_LIST_H 18 19 #include <new> 20 21 namespace OHOS { 22 template<class T> 23 struct Node { 24 Node() = default; NodeNode25 explicit Node(T value) : value_(value), next_(nullptr), prev_(nullptr) {} 26 27 T value_; 28 Node<T> *next_; 29 Node<T> *prev_; 30 }; 31 32 template<class T> 33 class List { 34 public: List()35 List() : count_(0) 36 { 37 head_ = new (std::nothrow) Node<T>(); 38 head_->next_ = head_; 39 head_->prev_ = head_; 40 } 41 ~List()42 ~List() 43 { 44 RemoveAll(); 45 delete head_; 46 head_ = nullptr; 47 } 48 Front()49 const T Front() const 50 { 51 if (count_ == 0) { 52 return head_->value_; 53 } 54 55 return head_->next_->value_; 56 } 57 PushFront(T value)58 void PushFront(T value) 59 { 60 auto node = new (std::nothrow) Node<T>(value); 61 if (node == nullptr) { 62 return; 63 } 64 65 node->prev_ = head_; 66 node->next_ = head_->next_; 67 head_->next_->prev_ = node; 68 head_->next_ = node; 69 count_++; 70 } 71 PopFront()72 void PopFront() 73 { 74 if (count_ == 0) { 75 return; 76 } 77 78 Node<T> *node = head_->next_; 79 node->next_->prev_ = head_; 80 head_->next_ = node->next_; 81 delete node; 82 count_--; 83 } 84 Back()85 const T Back() const 86 { 87 if (count_ == 0) { 88 return head_->value_; 89 } 90 91 return head_->prev_->value_; 92 } 93 PushBack(T value)94 void PushBack(T value) 95 { 96 auto node = new (std::nothrow) Node<T>(value); 97 98 node->next_ = head_; 99 node->prev_ = head_->prev_; 100 head_->prev_->next_ = node; 101 head_->prev_ = node; 102 count_++; 103 } 104 PopBack()105 void PopBack() 106 { 107 if (count_ == 0) { 108 return; 109 } 110 111 Node<T> *node = head_->prev_; 112 node->prev_->next_ = head_; 113 head_->prev_ = node->prev_; 114 delete node; 115 count_--; 116 } 117 Remove(Node<T> * node)118 void Remove(Node<T> *node) 119 { 120 if ((count_ == 0) || (node == nullptr)) { 121 return; 122 } 123 124 node->prev_->next_ = node->next_; 125 node->next_->prev_ = node->prev_; 126 127 delete node; 128 count_--; 129 } 130 RemoveAll()131 void RemoveAll() 132 { 133 Node<T> *node = head_->next_; 134 while (node != head_) { 135 Node<T> *temp = node; 136 node = node->next_; 137 delete temp; 138 } 139 } 140 Begin()141 Node<T> *Begin() const 142 { 143 return head_->next_; 144 } 145 End()146 const Node<T> *End() const 147 { 148 return head_; 149 } 150 Size()151 uint32_t Size() const 152 { 153 return count_; 154 } 155 IsEmpty()156 bool IsEmpty() const 157 { 158 return count_ == 0; 159 } 160 161 private: 162 Node<T> *head_; 163 int count_; 164 }; 165 } // namespace OHOS 166 #endif // OHOS_UTILS_LIST_H 167