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