• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         if (head_ == nullptr) {
39             return;
40         }
41         head_->next_ = head_;
42         head_->prev_ = head_;
43     }
44 
~List()45     ~List()
46     {
47         RemoveAll();
48         delete head_;
49         head_ = nullptr;
50     }
51 
Front()52     const T Front() const
53     {
54         if (count_ == 0) {
55             return head_->value_;
56         }
57 
58         return head_->next_->value_;
59     }
60 
PushFront(T value)61     void PushFront(T value)
62     {
63         auto node = new (std::nothrow) Node<T>(value);
64         if (node == nullptr) {
65             return;
66         }
67 
68         node->prev_ = head_;
69         node->next_ = head_->next_;
70         head_->next_->prev_ = node;
71         head_->next_ = node;
72         count_++;
73     }
74 
PopFront()75     void PopFront()
76     {
77         if (count_ == 0) {
78             return;
79         }
80 
81         Node<T> *node = head_->next_;
82         node->next_->prev_ = head_;
83         head_->next_ = node->next_;
84         delete node;
85         count_--;
86     }
87 
Back()88     const T Back() const
89     {
90         if (count_ == 0) {
91             return head_->value_;
92         }
93 
94         return head_->prev_->value_;
95     }
96 
PushBack(T value)97     void PushBack(T value)
98     {
99         auto node = new (std::nothrow) Node<T>(value);
100         if (node == nullptr) {
101             return;
102         }
103 
104         node->next_ = head_;
105         node->prev_ = head_->prev_;
106         head_->prev_->next_ = node;
107         head_->prev_ = node;
108         count_++;
109     }
110 
PopBack()111     void PopBack()
112     {
113         if (count_ == 0) {
114             return;
115         }
116 
117         Node<T> *node = head_->prev_;
118         node->prev_->next_ = head_;
119         head_->prev_ = node->prev_;
120         delete node;
121         count_--;
122     }
123 
Remove(Node<T> * node)124     void Remove(Node<T> *node)
125     {
126         if ((count_ == 0) || (node == nullptr)) {
127             return;
128         }
129 
130         node->prev_->next_ = node->next_;
131         node->next_->prev_ = node->prev_;
132 
133         delete node;
134         count_--;
135     }
136 
RemoveAll()137     void RemoveAll()
138     {
139         Node<T> *node = head_->next_;
140         while (node != head_) {
141             Node<T> *temp = node;
142             node = node->next_;
143             delete temp;
144         }
145     }
146 
Begin()147     Node<T> *Begin() const
148     {
149         return head_->next_;
150     }
151 
End()152     const Node<T> *End() const
153     {
154         return head_;
155     }
156 
Size()157     uint32_t Size() const
158     {
159         return count_;
160     }
161 
IsEmpty()162     bool IsEmpty() const
163     {
164         return count_ == 0;
165     }
166 
167 private:
168     Node<T> *head_;
169     int count_;
170 };
171 } // namespace OHOS
172 #endif  // OHOS_UTILS_LIST_H
173