blob: 092e831db9ddbcfb5404256d00e0e2288f71fbd9 [file] [log] [blame]
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef __LINKED_LIST_H
18#define __LINKED_LIST_H
19
20#include "private/bionic_macros.h"
21
22template<typename T>
23struct LinkedListEntry {
24 LinkedListEntry<T>* next;
25 T* element;
26};
27
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -070028// ForwardInputIterator
29template<typename T>
30class LinkedListIterator {
31 public:
32 LinkedListIterator() : entry_(nullptr) {}
33 LinkedListIterator(const LinkedListIterator<T>& that) : entry_(that.entry_) {}
34 explicit LinkedListIterator(LinkedListEntry<T>* entry) : entry_(entry) {}
35
36 LinkedListIterator<T>& operator=(const LinkedListIterator<T>& that) {
37 entry_ = that.entry_;
38 return *this;
39 }
40
41 LinkedListIterator<T>& operator++() {
42 entry_ = entry_->next;
43 return *this;
44 }
45
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -070046 T* const operator*() {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -070047 return entry_->element;
48 }
49
50 bool operator==(const LinkedListIterator<T>& that) const {
51 return entry_ == that.entry_;
52 }
53
54 bool operator!=(const LinkedListIterator<T>& that) const {
55 return entry_ != that.entry_;
56 }
57
58 private:
59 LinkedListEntry<T> *entry_;
60};
61
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -070062/*
63 * Represents linked list of objects of type T
64 */
65template<typename T, typename Allocator>
66class LinkedList {
67 public:
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -070068 typedef LinkedListIterator<T> iterator;
69 typedef T* value_type;
70
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -070071 LinkedList() : head_(nullptr), tail_(nullptr) {}
Dmitriy Ivanov14241402014-08-26 14:16:52 -070072 ~LinkedList() {
73 clear();
74 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -070075
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -070076 LinkedList(LinkedList&& that) {
77 this->head_ = that.head_;
78 this->tail_ = that.tail_;
79 that.head_ = that.tail_ = nullptr;
80 }
81
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -070082 void push_front(T* const element) {
83 LinkedListEntry<T>* new_entry = Allocator::alloc();
84 new_entry->next = head_;
85 new_entry->element = element;
86 head_ = new_entry;
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -070087 if (tail_ == nullptr) {
88 tail_ = new_entry;
89 }
90 }
91
92 void push_back(T* const element) {
93 LinkedListEntry<T>* new_entry = Allocator::alloc();
94 new_entry->next = nullptr;
95 new_entry->element = element;
96 if (tail_ == nullptr) {
97 tail_ = head_ = new_entry;
98 } else {
99 tail_->next = new_entry;
100 tail_ = new_entry;
101 }
102 }
103
104 T* pop_front() {
105 if (head_ == nullptr) {
106 return nullptr;
107 }
108
109 LinkedListEntry<T>* entry = head_;
110 T* element = entry->element;
111 head_ = entry->next;
112 Allocator::free(entry);
113
114 if (head_ == nullptr) {
115 tail_ = nullptr;
116 }
117
118 return element;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700119 }
120
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800121 T* front() const {
122 if (head_ == nullptr) {
123 return nullptr;
124 }
125
126 return head_->element;
127 }
128
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700129 void clear() {
130 while (head_ != nullptr) {
131 LinkedListEntry<T>* p = head_;
132 head_ = head_->next;
133 Allocator::free(p);
134 }
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700135
136 tail_ = nullptr;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700137 }
138
139 template<typename F>
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700140 void for_each(F action) const {
Dmitriy Ivanova4926052014-09-02 09:45:40 -0700141 visit([&] (T* si) {
142 action(si);
143 return true;
144 });
145 }
146
147 template<typename F>
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700148 bool visit(F action) const {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700149 for (LinkedListEntry<T>* e = head_; e != nullptr; e = e->next) {
Dmitriy Ivanova4926052014-09-02 09:45:40 -0700150 if (!action(e->element)) {
151 return false;
152 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700153 }
Dmitriy Ivanova4926052014-09-02 09:45:40 -0700154 return true;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700155 }
156
157 template<typename F>
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700158 void remove_if(F predicate) {
159 for (LinkedListEntry<T>* e = head_, *p = nullptr; e != nullptr;) {
160 if (predicate(e->element)) {
161 LinkedListEntry<T>* next = e->next;
162 if (p == nullptr) {
163 head_ = next;
164 } else {
165 p->next = next;
166 }
Dmitriy Ivanov7a9311f2015-11-05 17:41:05 -0800167
168 if (tail_ == e) {
169 tail_ = p;
170 }
171
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700172 Allocator::free(e);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700173
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700174 e = next;
175 } else {
176 p = e;
177 e = e->next;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700178 }
179 }
180 }
181
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700182 template<typename F>
183 T* find_if(F predicate) const {
184 for (LinkedListEntry<T>* e = head_; e != nullptr; e = e->next) {
185 if (predicate(e->element)) {
186 return e->element;
187 }
188 }
189
190 return nullptr;
191 }
192
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -0700193 iterator begin() const {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700194 return iterator(head_);
195 }
196
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -0700197 iterator end() const {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700198 return iterator(nullptr);
199 }
200
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -0700201 iterator find(T* value) const {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700202 for (LinkedListEntry<T>* e = head_; e != nullptr; e = e->next) {
203 if (e->element == value) {
204 return iterator(e);
205 }
206 }
207
208 return end();
209 }
210
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700211 size_t copy_to_array(T* array[], size_t array_length) const {
212 size_t sz = 0;
213 for (LinkedListEntry<T>* e = head_; sz < array_length && e != nullptr; e = e->next) {
214 array[sz++] = e->element;
215 }
216
217 return sz;
218 }
219
220 bool contains(const T* el) const {
221 for (LinkedListEntry<T>* e = head_; e != nullptr; e = e->next) {
222 if (e->element == el) {
Dmitriy Ivanov042426b2014-08-12 21:02:13 -0700223 return true;
224 }
225 }
226 return false;
227 }
228
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700229 static LinkedList make_list(T* const element) {
230 LinkedList<T, Allocator> one_element_list;
231 one_element_list.push_back(element);
232 return one_element_list;
233 }
234
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700235 private:
236 LinkedListEntry<T>* head_;
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700237 LinkedListEntry<T>* tail_;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700238 DISALLOW_COPY_AND_ASSIGN(LinkedList);
239};
240
241#endif // __LINKED_LIST_H