Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 22 | template<typename T> |
| 23 | struct LinkedListEntry { |
| 24 | LinkedListEntry<T>* next; |
| 25 | T* element; |
| 26 | }; |
| 27 | |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 28 | // ForwardInputIterator |
| 29 | template<typename T> |
| 30 | class 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 Ivanov | d88e1f3 | 2016-03-24 15:30:30 -0700 | [diff] [blame] | 46 | T* const operator*() { |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 47 | 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 Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 62 | /* |
| 63 | * Represents linked list of objects of type T |
| 64 | */ |
| 65 | template<typename T, typename Allocator> |
| 66 | class LinkedList { |
| 67 | public: |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 68 | typedef LinkedListIterator<T> iterator; |
| 69 | typedef T* value_type; |
| 70 | |
Dmitriy Ivanov | aa0f2bd | 2014-07-28 17:32:20 -0700 | [diff] [blame] | 71 | LinkedList() : head_(nullptr), tail_(nullptr) {} |
Dmitriy Ivanov | 1424140 | 2014-08-26 14:16:52 -0700 | [diff] [blame] | 72 | ~LinkedList() { |
| 73 | clear(); |
| 74 | } |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 75 | |
Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 76 | LinkedList(LinkedList&& that) { |
| 77 | this->head_ = that.head_; |
| 78 | this->tail_ = that.tail_; |
| 79 | that.head_ = that.tail_ = nullptr; |
| 80 | } |
| 81 | |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 82 | 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 Ivanov | aa0f2bd | 2014-07-28 17:32:20 -0700 | [diff] [blame] | 87 | 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 Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 121 | T* front() const { |
| 122 | if (head_ == nullptr) { |
| 123 | return nullptr; |
| 124 | } |
| 125 | |
| 126 | return head_->element; |
| 127 | } |
| 128 | |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 129 | void clear() { |
| 130 | while (head_ != nullptr) { |
| 131 | LinkedListEntry<T>* p = head_; |
| 132 | head_ = head_->next; |
| 133 | Allocator::free(p); |
| 134 | } |
Dmitriy Ivanov | aa0f2bd | 2014-07-28 17:32:20 -0700 | [diff] [blame] | 135 | |
| 136 | tail_ = nullptr; |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Dimitry Ivanov | ec90e24 | 2017-02-10 11:04:20 -0800 | [diff] [blame] | 139 | bool empty() { |
| 140 | return (head_ == nullptr); |
| 141 | } |
| 142 | |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 143 | template<typename F> |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 144 | void for_each(F action) const { |
Dmitriy Ivanov | a492605 | 2014-09-02 09:45:40 -0700 | [diff] [blame] | 145 | visit([&] (T* si) { |
| 146 | action(si); |
| 147 | return true; |
| 148 | }); |
| 149 | } |
| 150 | |
| 151 | template<typename F> |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 152 | bool visit(F action) const { |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 153 | for (LinkedListEntry<T>* e = head_; e != nullptr; e = e->next) { |
Dmitriy Ivanov | a492605 | 2014-09-02 09:45:40 -0700 | [diff] [blame] | 154 | if (!action(e->element)) { |
| 155 | return false; |
| 156 | } |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 157 | } |
Dmitriy Ivanov | a492605 | 2014-09-02 09:45:40 -0700 | [diff] [blame] | 158 | return true; |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | template<typename F> |
Dmitriy Ivanov | 4bea498 | 2014-08-29 14:01:48 -0700 | [diff] [blame] | 162 | void remove_if(F predicate) { |
| 163 | for (LinkedListEntry<T>* e = head_, *p = nullptr; e != nullptr;) { |
| 164 | if (predicate(e->element)) { |
| 165 | LinkedListEntry<T>* next = e->next; |
| 166 | if (p == nullptr) { |
| 167 | head_ = next; |
| 168 | } else { |
| 169 | p->next = next; |
| 170 | } |
Dmitriy Ivanov | 7a9311f | 2015-11-05 17:41:05 -0800 | [diff] [blame] | 171 | |
| 172 | if (tail_ == e) { |
| 173 | tail_ = p; |
| 174 | } |
| 175 | |
Dmitriy Ivanov | 4bea498 | 2014-08-29 14:01:48 -0700 | [diff] [blame] | 176 | Allocator::free(e); |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 177 | |
Dmitriy Ivanov | 4bea498 | 2014-08-29 14:01:48 -0700 | [diff] [blame] | 178 | e = next; |
| 179 | } else { |
| 180 | p = e; |
| 181 | e = e->next; |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
Dimitry Ivanov | ec90e24 | 2017-02-10 11:04:20 -0800 | [diff] [blame] | 186 | void remove(T* element) { |
| 187 | remove_if([&](T* e) { |
| 188 | return e == element; |
| 189 | }); |
| 190 | } |
| 191 | |
Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 192 | template<typename F> |
| 193 | T* find_if(F predicate) const { |
| 194 | for (LinkedListEntry<T>* e = head_; e != nullptr; e = e->next) { |
| 195 | if (predicate(e->element)) { |
| 196 | return e->element; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | return nullptr; |
| 201 | } |
| 202 | |
Dimitry Ivanov | d88e1f3 | 2016-03-24 15:30:30 -0700 | [diff] [blame] | 203 | iterator begin() const { |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 204 | return iterator(head_); |
| 205 | } |
| 206 | |
Dimitry Ivanov | d88e1f3 | 2016-03-24 15:30:30 -0700 | [diff] [blame] | 207 | iterator end() const { |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 208 | return iterator(nullptr); |
| 209 | } |
| 210 | |
Dimitry Ivanov | d88e1f3 | 2016-03-24 15:30:30 -0700 | [diff] [blame] | 211 | iterator find(T* value) const { |
Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 212 | for (LinkedListEntry<T>* e = head_; e != nullptr; e = e->next) { |
| 213 | if (e->element == value) { |
| 214 | return iterator(e); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | return end(); |
| 219 | } |
| 220 | |
Dmitriy Ivanov | 4bea498 | 2014-08-29 14:01:48 -0700 | [diff] [blame] | 221 | size_t copy_to_array(T* array[], size_t array_length) const { |
| 222 | size_t sz = 0; |
| 223 | for (LinkedListEntry<T>* e = head_; sz < array_length && e != nullptr; e = e->next) { |
| 224 | array[sz++] = e->element; |
| 225 | } |
| 226 | |
| 227 | return sz; |
| 228 | } |
| 229 | |
| 230 | bool contains(const T* el) const { |
| 231 | for (LinkedListEntry<T>* e = head_; e != nullptr; e = e->next) { |
| 232 | if (e->element == el) { |
Dmitriy Ivanov | 042426b | 2014-08-12 21:02:13 -0700 | [diff] [blame] | 233 | return true; |
| 234 | } |
| 235 | } |
| 236 | return false; |
| 237 | } |
| 238 | |
Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 239 | static LinkedList make_list(T* const element) { |
| 240 | LinkedList<T, Allocator> one_element_list; |
| 241 | one_element_list.push_back(element); |
| 242 | return one_element_list; |
| 243 | } |
| 244 | |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 245 | private: |
| 246 | LinkedListEntry<T>* head_; |
Dmitriy Ivanov | aa0f2bd | 2014-07-28 17:32:20 -0700 | [diff] [blame] | 247 | LinkedListEntry<T>* tail_; |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 248 | DISALLOW_COPY_AND_ASSIGN(LinkedList); |
| 249 | }; |
| 250 | |
| 251 | #endif // __LINKED_LIST_H |