• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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 DISTRIBUTEDDATAMGR_PASTEBOARD_LINKED_LIST_H
17 #define DISTRIBUTEDDATAMGR_PASTEBOARD_LINKED_LIST_H
18 
19 #include <functional>
20 #include <mutex>
21 #include "pasteboard_hilog.h"
22 
23 namespace OHOS {
24 namespace MiscServices {
25 
26 template <typename T>
27 struct ListNode {
valueListNode28     explicit ListNode(const T &val = {}) : value(val), next(nullptr)
29     {
30     }
31 
32     T value;
33     ListNode<T> *next;
34 };
35 
36 template <typename T>
37 class LinkedList {
38 public:
LinkedList()39     LinkedList()
40     {
41         head_ = new ListNode<T>();
42     }
43 
~LinkedList()44     ~LinkedList()
45     {
46         Clear();
47         if (head_) {
48             delete head_;
49             head_ = nullptr;
50         }
51     }
52 
Clear()53     void Clear() noexcept
54     {
55         std::lock_guard<std::mutex> lock(mutex_);
56         if (head_ == nullptr) {
57             return;
58         }
59 
60         ListNode<T> *iter = head_->next;
61         ListNode<T> *next = nullptr;
62         while (iter) {
63             next = iter->next;
64             delete iter;
65             iter = next;
66         }
67         head_->next = nullptr;
68     }
69 
InsertFront(const T & value)70     void InsertFront(const T &value) noexcept
71     {
72         std::lock_guard<std::mutex> lock(mutex_);
73         if (head_ == nullptr) {
74             return;
75         }
76 
77         ListNode<T> *newNode = new ListNode<T>(value);
78         PASTEBOARD_CHECK_AND_RETURN_LOGE(newNode != nullptr, PASTEBOARD_MODULE_SERVICE, "newNode is null");
79         newNode->next = head_->next;
80         head_->next = newNode;
81     }
82 
InsertTail(const T & value)83     void InsertTail(const T &value) noexcept
84     {
85         std::lock_guard<std::mutex> lock(mutex_);
86         if (head_ == nullptr) {
87             return;
88         }
89 
90         ListNode<T> *newNode = new ListNode<T>(value);
91         ListNode<T> *iter = head_;
92         while (iter->next) {
93             iter = iter->next;
94         }
95         iter->next = newNode;
96     }
97 
RemoveIf(std::function<bool (const T &)> func)98     void RemoveIf(std::function<bool(const T&)> func) noexcept
99     {
100         std::lock_guard<std::mutex> lock(mutex_);
101         if (head_ == nullptr) {
102             return;
103         }
104 
105         ListNode<T> *iter = head_->next;
106         ListNode<T> *prev = head_;
107         while (iter) {
108             if (func(iter->value)) {
109                 prev->next = iter->next;
110                 delete iter;
111                 iter = prev->next;
112             } else {
113                 prev = iter;
114                 iter = iter->next;
115             }
116         }
117     }
118 
FindExist(const T & value)119     bool FindExist(const T &value) noexcept
120     {
121         std::lock_guard<std::mutex> lock(mutex_);
122         if (head_ == nullptr) {
123             return false;
124         }
125 
126         ListNode<T> *iter = head_->next;
127         while (iter) {
128             if (iter->value == value) {
129                 return true;
130             }
131             iter = iter->next;
132         }
133         return false;
134     }
135 
FindExist(std::function<bool (const T &)> func)136     bool FindExist(std::function<bool(const T&)> func) noexcept
137     {
138         std::lock_guard<std::mutex> lock(mutex_);
139         if (head_ == nullptr) {
140             return false;
141         }
142 
143         ListNode<T> *iter = head_->next;
144         while (iter) {
145             if (func(iter->value)) {
146                 return true;
147             }
148             iter = iter->next;
149         }
150         return false;
151     }
152 
ForEach(std::function<void (const T &)> func)153     void ForEach(std::function<void(const T&)> func) noexcept
154     {
155         std::lock_guard<std::mutex> lock(mutex_);
156         if (head_ == nullptr) {
157             return;
158         }
159 
160         ListNode<T> *iter = head_->next;
161         while (iter) {
162             func(iter->value);
163             iter = iter->next;
164         }
165     }
166 
167 private:
168     ListNode<T> *head_;
169     std::mutex mutex_;
170 };
171 } // namespace MiscServices
172 } // namespace OHOS
173 
174 #endif // DISTRIBUTEDDATAMGR_PASTEBOARD_LINKED_LIST_H
175