blob: 7f70a2c7be6783ae176df74b63e45c30f83e683b [file] [log] [blame]
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
Dimitry Ivanovbcc4da92017-02-15 15:31:13 -08003 * All rights reserved.
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07004 *
Dimitry Ivanovbcc4da92017-02-15 15:31:13 -08005 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -070014 *
Dimitry Ivanovbcc4da92017-02-15 15:31:13 -080015 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -070027 */
28
Elliott Hughescbc80ba2018-02-13 14:26:29 -080029#pragma once
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -070030
31#include "private/bionic_macros.h"
32
33template<typename T>
34struct LinkedListEntry {
35 LinkedListEntry<T>* next;
36 T* element;
37};
38
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -070039// ForwardInputIterator
40template<typename T>
41class LinkedListIterator {
42 public:
43 LinkedListIterator() : entry_(nullptr) {}
44 LinkedListIterator(const LinkedListIterator<T>& that) : entry_(that.entry_) {}
45 explicit LinkedListIterator(LinkedListEntry<T>* entry) : entry_(entry) {}
46
47 LinkedListIterator<T>& operator=(const LinkedListIterator<T>& that) {
48 entry_ = that.entry_;
49 return *this;
50 }
51
52 LinkedListIterator<T>& operator++() {
53 entry_ = entry_->next;
54 return *this;
55 }
56
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -070057 T* const operator*() {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -070058 return entry_->element;
59 }
60
61 bool operator==(const LinkedListIterator<T>& that) const {
62 return entry_ == that.entry_;
63 }
64
65 bool operator!=(const LinkedListIterator<T>& that) const {
66 return entry_ != that.entry_;
67 }
68
69 private:
70 LinkedListEntry<T> *entry_;
71};
72
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -070073/*
74 * Represents linked list of objects of type T
75 */
76template<typename T, typename Allocator>
77class LinkedList {
78 public:
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -070079 typedef LinkedListIterator<T> iterator;
80 typedef T* value_type;
81
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -070082 LinkedList() : head_(nullptr), tail_(nullptr) {}
Dmitriy Ivanov14241402014-08-26 14:16:52 -070083 ~LinkedList() {
84 clear();
85 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -070086
Chih-Hung Hsieh9a64b562018-09-25 14:00:44 -070087 LinkedList(LinkedList&& that) noexcept {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -070088 this->head_ = that.head_;
89 this->tail_ = that.tail_;
90 that.head_ = that.tail_ = nullptr;
91 }
92
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -070093 void push_front(T* const element) {
94 LinkedListEntry<T>* new_entry = Allocator::alloc();
95 new_entry->next = head_;
96 new_entry->element = element;
97 head_ = new_entry;
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -070098 if (tail_ == nullptr) {
99 tail_ = new_entry;
100 }
101 }
102
103 void push_back(T* const element) {
104 LinkedListEntry<T>* new_entry = Allocator::alloc();
105 new_entry->next = nullptr;
106 new_entry->element = element;
107 if (tail_ == nullptr) {
108 tail_ = head_ = new_entry;
109 } else {
110 tail_->next = new_entry;
111 tail_ = new_entry;
112 }
113 }
114
115 T* pop_front() {
116 if (head_ == nullptr) {
117 return nullptr;
118 }
119
120 LinkedListEntry<T>* entry = head_;
121 T* element = entry->element;
122 head_ = entry->next;
123 Allocator::free(entry);
124
125 if (head_ == nullptr) {
126 tail_ = nullptr;
127 }
128
129 return element;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700130 }
131
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800132 T* front() const {
133 if (head_ == nullptr) {
134 return nullptr;
135 }
136
137 return head_->element;
138 }
139
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700140 void clear() {
141 while (head_ != nullptr) {
142 LinkedListEntry<T>* p = head_;
143 head_ = head_->next;
144 Allocator::free(p);
145 }
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700146
147 tail_ = nullptr;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700148 }
149
Dimitry Ivanovec90e242017-02-10 11:04:20 -0800150 bool empty() {
151 return (head_ == nullptr);
152 }
153
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700154 template<typename F>
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700155 void for_each(F action) const {
Dmitriy Ivanova4926052014-09-02 09:45:40 -0700156 visit([&] (T* si) {
157 action(si);
158 return true;
159 });
160 }
161
162 template<typename F>
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700163 bool visit(F action) const {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700164 for (LinkedListEntry<T>* e = head_; e != nullptr; e = e->next) {
Dmitriy Ivanova4926052014-09-02 09:45:40 -0700165 if (!action(e->element)) {
166 return false;
167 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700168 }
Dmitriy Ivanova4926052014-09-02 09:45:40 -0700169 return true;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700170 }
171
172 template<typename F>
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700173 void remove_if(F predicate) {
174 for (LinkedListEntry<T>* e = head_, *p = nullptr; e != nullptr;) {
175 if (predicate(e->element)) {
176 LinkedListEntry<T>* next = e->next;
177 if (p == nullptr) {
178 head_ = next;
179 } else {
180 p->next = next;
181 }
Dmitriy Ivanov7a9311f2015-11-05 17:41:05 -0800182
183 if (tail_ == e) {
184 tail_ = p;
185 }
186
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700187 Allocator::free(e);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700188
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700189 e = next;
190 } else {
191 p = e;
192 e = e->next;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700193 }
194 }
195 }
196
Dimitry Ivanovec90e242017-02-10 11:04:20 -0800197 void remove(T* element) {
198 remove_if([&](T* e) {
199 return e == element;
200 });
201 }
202
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700203 template<typename F>
204 T* find_if(F predicate) const {
205 for (LinkedListEntry<T>* e = head_; e != nullptr; e = e->next) {
206 if (predicate(e->element)) {
207 return e->element;
208 }
209 }
210
211 return nullptr;
212 }
213
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -0700214 iterator begin() const {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700215 return iterator(head_);
216 }
217
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -0700218 iterator end() const {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700219 return iterator(nullptr);
220 }
221
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -0700222 iterator find(T* value) const {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700223 for (LinkedListEntry<T>* e = head_; e != nullptr; e = e->next) {
224 if (e->element == value) {
225 return iterator(e);
226 }
227 }
228
229 return end();
230 }
231
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700232 size_t copy_to_array(T* array[], size_t array_length) const {
233 size_t sz = 0;
234 for (LinkedListEntry<T>* e = head_; sz < array_length && e != nullptr; e = e->next) {
235 array[sz++] = e->element;
236 }
237
238 return sz;
239 }
240
241 bool contains(const T* el) const {
242 for (LinkedListEntry<T>* e = head_; e != nullptr; e = e->next) {
243 if (e->element == el) {
Dmitriy Ivanov042426b2014-08-12 21:02:13 -0700244 return true;
245 }
246 }
247 return false;
248 }
249
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700250 static LinkedList make_list(T* const element) {
251 LinkedList<T, Allocator> one_element_list;
252 one_element_list.push_back(element);
253 return one_element_list;
254 }
255
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700256 private:
257 LinkedListEntry<T>* head_;
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700258 LinkedListEntry<T>* tail_;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700259 DISALLOW_COPY_AND_ASSIGN(LinkedList);
260};