blob: 0572e635bf7bae60c0d366182d0b5bc62ae97bb7 [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
28/*
29 * Represents linked list of objects of type T
30 */
31template<typename T, typename Allocator>
32class LinkedList {
33 public:
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -070034 LinkedList() : head_(nullptr), tail_(nullptr) {}
Dmitriy Ivanov14241402014-08-26 14:16:52 -070035 ~LinkedList() {
36 clear();
37 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -070038
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -070039 LinkedList(LinkedList&& that) {
40 this->head_ = that.head_;
41 this->tail_ = that.tail_;
42 that.head_ = that.tail_ = nullptr;
43 }
44
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -070045 void push_front(T* const element) {
46 LinkedListEntry<T>* new_entry = Allocator::alloc();
47 new_entry->next = head_;
48 new_entry->element = element;
49 head_ = new_entry;
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -070050 if (tail_ == nullptr) {
51 tail_ = new_entry;
52 }
53 }
54
55 void push_back(T* const element) {
56 LinkedListEntry<T>* new_entry = Allocator::alloc();
57 new_entry->next = nullptr;
58 new_entry->element = element;
59 if (tail_ == nullptr) {
60 tail_ = head_ = new_entry;
61 } else {
62 tail_->next = new_entry;
63 tail_ = new_entry;
64 }
65 }
66
67 T* pop_front() {
68 if (head_ == nullptr) {
69 return nullptr;
70 }
71
72 LinkedListEntry<T>* entry = head_;
73 T* element = entry->element;
74 head_ = entry->next;
75 Allocator::free(entry);
76
77 if (head_ == nullptr) {
78 tail_ = nullptr;
79 }
80
81 return element;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -070082 }
83
Dmitriy Ivanovab972b92014-11-29 13:57:41 -080084 T* front() const {
85 if (head_ == nullptr) {
86 return nullptr;
87 }
88
89 return head_->element;
90 }
91
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -070092 void clear() {
93 while (head_ != nullptr) {
94 LinkedListEntry<T>* p = head_;
95 head_ = head_->next;
96 Allocator::free(p);
97 }
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -070098
99 tail_ = nullptr;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700100 }
101
102 template<typename F>
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700103 void for_each(F action) const {
Dmitriy Ivanova4926052014-09-02 09:45:40 -0700104 visit([&] (T* si) {
105 action(si);
106 return true;
107 });
108 }
109
110 template<typename F>
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700111 bool visit(F action) const {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700112 for (LinkedListEntry<T>* e = head_; e != nullptr; e = e->next) {
Dmitriy Ivanova4926052014-09-02 09:45:40 -0700113 if (!action(e->element)) {
114 return false;
115 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700116 }
Dmitriy Ivanova4926052014-09-02 09:45:40 -0700117 return true;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700118 }
119
120 template<typename F>
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700121 void remove_if(F predicate) {
122 for (LinkedListEntry<T>* e = head_, *p = nullptr; e != nullptr;) {
123 if (predicate(e->element)) {
124 LinkedListEntry<T>* next = e->next;
125 if (p == nullptr) {
126 head_ = next;
127 } else {
128 p->next = next;
129 }
Dimitry Ivanov585f2bf2017-04-19 11:28:16 -0700130
131 if (tail_ == e) {
132 tail_ = p;
133 }
134
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700135 Allocator::free(e);
Dimitry Ivanov585f2bf2017-04-19 11:28:16 -0700136
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700137 e = next;
138 } else {
139 p = e;
140 e = e->next;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700141 }
142 }
143 }
144
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700145 template<typename F>
146 T* find_if(F predicate) const {
147 for (LinkedListEntry<T>* e = head_; e != nullptr; e = e->next) {
148 if (predicate(e->element)) {
149 return e->element;
150 }
151 }
152
153 return nullptr;
154 }
155
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700156 size_t copy_to_array(T* array[], size_t array_length) const {
157 size_t sz = 0;
158 for (LinkedListEntry<T>* e = head_; sz < array_length && e != nullptr; e = e->next) {
159 array[sz++] = e->element;
160 }
161
162 return sz;
163 }
164
165 bool contains(const T* el) const {
166 for (LinkedListEntry<T>* e = head_; e != nullptr; e = e->next) {
167 if (e->element == el) {
Dmitriy Ivanov042426b2014-08-12 21:02:13 -0700168 return true;
169 }
170 }
171 return false;
172 }
173
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700174 static LinkedList make_list(T* const element) {
175 LinkedList<T, Allocator> one_element_list;
176 one_element_list.push_back(element);
177 return one_element_list;
178 }
179
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700180 private:
181 LinkedListEntry<T>* head_;
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700182 LinkedListEntry<T>* tail_;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700183 DISALLOW_COPY_AND_ASSIGN(LinkedList);
184};
185
186#endif // __LINKED_LIST_H