blob: ed2b1508cdbcfb9bd443e27398480b843be98700 [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
29#ifndef __LINKED_LIST_H
30#define __LINKED_LIST_H
31
32#include "private/bionic_macros.h"
33
34template<typename T>
35struct LinkedListEntry {
36 LinkedListEntry<T>* next;
37 T* element;
38};
39
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -070040// ForwardInputIterator
41template<typename T>
42class LinkedListIterator {
43 public:
44 LinkedListIterator() : entry_(nullptr) {}
45 LinkedListIterator(const LinkedListIterator<T>& that) : entry_(that.entry_) {}
46 explicit LinkedListIterator(LinkedListEntry<T>* entry) : entry_(entry) {}
47
48 LinkedListIterator<T>& operator=(const LinkedListIterator<T>& that) {
49 entry_ = that.entry_;
50 return *this;
51 }
52
53 LinkedListIterator<T>& operator++() {
54 entry_ = entry_->next;
55 return *this;
56 }
57
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -070058 T* const operator*() {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -070059 return entry_->element;
60 }
61
62 bool operator==(const LinkedListIterator<T>& that) const {
63 return entry_ == that.entry_;
64 }
65
66 bool operator!=(const LinkedListIterator<T>& that) const {
67 return entry_ != that.entry_;
68 }
69
70 private:
71 LinkedListEntry<T> *entry_;
72};
73
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -070074/*
75 * Represents linked list of objects of type T
76 */
77template<typename T, typename Allocator>
78class LinkedList {
79 public:
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -070080 typedef LinkedListIterator<T> iterator;
81 typedef T* value_type;
82
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -070083 LinkedList() : head_(nullptr), tail_(nullptr) {}
Dmitriy Ivanov14241402014-08-26 14:16:52 -070084 ~LinkedList() {
85 clear();
86 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -070087
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -070088 LinkedList(LinkedList&& that) {
89 this->head_ = that.head_;
90 this->tail_ = that.tail_;
91 that.head_ = that.tail_ = nullptr;
92 }
93
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -070094 void push_front(T* const element) {
95 LinkedListEntry<T>* new_entry = Allocator::alloc();
96 new_entry->next = head_;
97 new_entry->element = element;
98 head_ = new_entry;
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -070099 if (tail_ == nullptr) {
100 tail_ = new_entry;
101 }
102 }
103
104 void push_back(T* const element) {
105 LinkedListEntry<T>* new_entry = Allocator::alloc();
106 new_entry->next = nullptr;
107 new_entry->element = element;
108 if (tail_ == nullptr) {
109 tail_ = head_ = new_entry;
110 } else {
111 tail_->next = new_entry;
112 tail_ = new_entry;
113 }
114 }
115
116 T* pop_front() {
117 if (head_ == nullptr) {
118 return nullptr;
119 }
120
121 LinkedListEntry<T>* entry = head_;
122 T* element = entry->element;
123 head_ = entry->next;
124 Allocator::free(entry);
125
126 if (head_ == nullptr) {
127 tail_ = nullptr;
128 }
129
130 return element;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700131 }
132
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800133 T* front() const {
134 if (head_ == nullptr) {
135 return nullptr;
136 }
137
138 return head_->element;
139 }
140
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700141 void clear() {
142 while (head_ != nullptr) {
143 LinkedListEntry<T>* p = head_;
144 head_ = head_->next;
145 Allocator::free(p);
146 }
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700147
148 tail_ = nullptr;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700149 }
150
Dimitry Ivanovec90e242017-02-10 11:04:20 -0800151 bool empty() {
152 return (head_ == nullptr);
153 }
154
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700155 template<typename F>
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700156 void for_each(F action) const {
Dmitriy Ivanova4926052014-09-02 09:45:40 -0700157 visit([&] (T* si) {
158 action(si);
159 return true;
160 });
161 }
162
163 template<typename F>
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700164 bool visit(F action) const {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700165 for (LinkedListEntry<T>* e = head_; e != nullptr; e = e->next) {
Dmitriy Ivanova4926052014-09-02 09:45:40 -0700166 if (!action(e->element)) {
167 return false;
168 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700169 }
Dmitriy Ivanova4926052014-09-02 09:45:40 -0700170 return true;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700171 }
172
173 template<typename F>
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700174 void remove_if(F predicate) {
175 for (LinkedListEntry<T>* e = head_, *p = nullptr; e != nullptr;) {
176 if (predicate(e->element)) {
177 LinkedListEntry<T>* next = e->next;
178 if (p == nullptr) {
179 head_ = next;
180 } else {
181 p->next = next;
182 }
Dmitriy Ivanov7a9311f2015-11-05 17:41:05 -0800183
184 if (tail_ == e) {
185 tail_ = p;
186 }
187
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700188 Allocator::free(e);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700189
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700190 e = next;
191 } else {
192 p = e;
193 e = e->next;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700194 }
195 }
196 }
197
Dimitry Ivanovec90e242017-02-10 11:04:20 -0800198 void remove(T* element) {
199 remove_if([&](T* e) {
200 return e == element;
201 });
202 }
203
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700204 template<typename F>
205 T* find_if(F predicate) const {
206 for (LinkedListEntry<T>* e = head_; e != nullptr; e = e->next) {
207 if (predicate(e->element)) {
208 return e->element;
209 }
210 }
211
212 return nullptr;
213 }
214
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -0700215 iterator begin() const {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700216 return iterator(head_);
217 }
218
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -0700219 iterator end() const {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700220 return iterator(nullptr);
221 }
222
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -0700223 iterator find(T* value) const {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700224 for (LinkedListEntry<T>* e = head_; e != nullptr; e = e->next) {
225 if (e->element == value) {
226 return iterator(e);
227 }
228 }
229
230 return end();
231 }
232
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700233 size_t copy_to_array(T* array[], size_t array_length) const {
234 size_t sz = 0;
235 for (LinkedListEntry<T>* e = head_; sz < array_length && e != nullptr; e = e->next) {
236 array[sz++] = e->element;
237 }
238
239 return sz;
240 }
241
242 bool contains(const T* el) const {
243 for (LinkedListEntry<T>* e = head_; e != nullptr; e = e->next) {
244 if (e->element == el) {
Dmitriy Ivanov042426b2014-08-12 21:02:13 -0700245 return true;
246 }
247 }
248 return false;
249 }
250
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700251 static LinkedList make_list(T* const element) {
252 LinkedList<T, Allocator> one_element_list;
253 one_element_list.push_back(element);
254 return one_element_list;
255 }
256
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700257 private:
258 LinkedListEntry<T>* head_;
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700259 LinkedListEntry<T>* tail_;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700260 DISALLOW_COPY_AND_ASSIGN(LinkedList);
261};
262
263#endif // __LINKED_LIST_H