| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2014 The Android Open Source Project | 
| Dimitry Ivanov | bcc4da9 | 2017-02-15 15:31:13 -0800 | [diff] [blame] | 3 | * All rights reserved. | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 4 | * | 
| Dimitry Ivanov | bcc4da9 | 2017-02-15 15:31:13 -0800 | [diff] [blame] | 5 | * 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 Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 14 | * | 
| Dimitry Ivanov | bcc4da9 | 2017-02-15 15:31:13 -0800 | [diff] [blame] | 15 | * 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 Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 27 | */ | 
|  | 28 |  | 
| Elliott Hughes | cbc80ba | 2018-02-13 14:26:29 -0800 | [diff] [blame] | 29 | #pragma once | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 30 |  | 
| Elliott Hughes | 5e62b34 | 2018-10-25 11:00:00 -0700 | [diff] [blame] | 31 | #include <android-base/macros.h> | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 32 |  | 
|  | 33 | template<typename T> | 
|  | 34 | struct LinkedListEntry { | 
|  | 35 | LinkedListEntry<T>* next; | 
|  | 36 | T* element; | 
|  | 37 | }; | 
|  | 38 |  | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 39 | // ForwardInputIterator | 
|  | 40 | template<typename T> | 
|  | 41 | class 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 Ivanov | d88e1f3 | 2016-03-24 15:30:30 -0700 | [diff] [blame] | 57 | T* const operator*() { | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 58 | 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 Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 73 | /* | 
|  | 74 | * Represents linked list of objects of type T | 
|  | 75 | */ | 
|  | 76 | template<typename T, typename Allocator> | 
|  | 77 | class LinkedList { | 
|  | 78 | public: | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 79 | typedef LinkedListIterator<T> iterator; | 
|  | 80 | typedef T* value_type; | 
|  | 81 |  | 
| Dmitriy Ivanov | aa0f2bd | 2014-07-28 17:32:20 -0700 | [diff] [blame] | 82 | LinkedList() : head_(nullptr), tail_(nullptr) {} | 
| Dmitriy Ivanov | 1424140 | 2014-08-26 14:16:52 -0700 | [diff] [blame] | 83 | ~LinkedList() { | 
|  | 84 | clear(); | 
|  | 85 | } | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 86 |  | 
| Chih-Hung Hsieh | 9a64b56 | 2018-09-25 14:00:44 -0700 | [diff] [blame] | 87 | LinkedList(LinkedList&& that) noexcept { | 
| Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 88 | this->head_ = that.head_; | 
|  | 89 | this->tail_ = that.tail_; | 
|  | 90 | that.head_ = that.tail_ = nullptr; | 
|  | 91 | } | 
|  | 92 |  | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 93 | 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 Ivanov | aa0f2bd | 2014-07-28 17:32:20 -0700 | [diff] [blame] | 98 | 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 Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 130 | } | 
|  | 131 |  | 
| Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 132 | T* front() const { | 
|  | 133 | if (head_ == nullptr) { | 
|  | 134 | return nullptr; | 
|  | 135 | } | 
|  | 136 |  | 
|  | 137 | return head_->element; | 
|  | 138 | } | 
|  | 139 |  | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 140 | void clear() { | 
|  | 141 | while (head_ != nullptr) { | 
|  | 142 | LinkedListEntry<T>* p = head_; | 
|  | 143 | head_ = head_->next; | 
|  | 144 | Allocator::free(p); | 
|  | 145 | } | 
| Dmitriy Ivanov | aa0f2bd | 2014-07-28 17:32:20 -0700 | [diff] [blame] | 146 |  | 
|  | 147 | tail_ = nullptr; | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 148 | } | 
|  | 149 |  | 
| Dimitry Ivanov | ec90e24 | 2017-02-10 11:04:20 -0800 | [diff] [blame] | 150 | bool empty() { | 
|  | 151 | return (head_ == nullptr); | 
|  | 152 | } | 
|  | 153 |  | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 154 | template<typename F> | 
| Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 155 | void for_each(F action) const { | 
| Dmitriy Ivanov | a492605 | 2014-09-02 09:45:40 -0700 | [diff] [blame] | 156 | visit([&] (T* si) { | 
|  | 157 | action(si); | 
|  | 158 | return true; | 
|  | 159 | }); | 
|  | 160 | } | 
|  | 161 |  | 
|  | 162 | template<typename F> | 
| Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 163 | bool visit(F action) const { | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 164 | for (LinkedListEntry<T>* e = head_; e != nullptr; e = e->next) { | 
| Dmitriy Ivanov | a492605 | 2014-09-02 09:45:40 -0700 | [diff] [blame] | 165 | if (!action(e->element)) { | 
|  | 166 | return false; | 
|  | 167 | } | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 168 | } | 
| Dmitriy Ivanov | a492605 | 2014-09-02 09:45:40 -0700 | [diff] [blame] | 169 | return true; | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 170 | } | 
|  | 171 |  | 
|  | 172 | template<typename F> | 
| Dmitriy Ivanov | 4bea498 | 2014-08-29 14:01:48 -0700 | [diff] [blame] | 173 | 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 Ivanov | 7a9311f | 2015-11-05 17:41:05 -0800 | [diff] [blame] | 182 |  | 
|  | 183 | if (tail_ == e) { | 
|  | 184 | tail_ = p; | 
|  | 185 | } | 
|  | 186 |  | 
| Dmitriy Ivanov | 4bea498 | 2014-08-29 14:01:48 -0700 | [diff] [blame] | 187 | Allocator::free(e); | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 188 |  | 
| Dmitriy Ivanov | 4bea498 | 2014-08-29 14:01:48 -0700 | [diff] [blame] | 189 | e = next; | 
|  | 190 | } else { | 
|  | 191 | p = e; | 
|  | 192 | e = e->next; | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 193 | } | 
|  | 194 | } | 
|  | 195 | } | 
|  | 196 |  | 
| Dimitry Ivanov | ec90e24 | 2017-02-10 11:04:20 -0800 | [diff] [blame] | 197 | void remove(T* element) { | 
|  | 198 | remove_if([&](T* e) { | 
|  | 199 | return e == element; | 
|  | 200 | }); | 
|  | 201 | } | 
|  | 202 |  | 
| Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 203 | 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 Ivanov | d88e1f3 | 2016-03-24 15:30:30 -0700 | [diff] [blame] | 214 | iterator begin() const { | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 215 | return iterator(head_); | 
|  | 216 | } | 
|  | 217 |  | 
| Dimitry Ivanov | d88e1f3 | 2016-03-24 15:30:30 -0700 | [diff] [blame] | 218 | iterator end() const { | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 219 | return iterator(nullptr); | 
|  | 220 | } | 
|  | 221 |  | 
| Dimitry Ivanov | d88e1f3 | 2016-03-24 15:30:30 -0700 | [diff] [blame] | 222 | iterator find(T* value) const { | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 223 | 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 Ivanov | 4bea498 | 2014-08-29 14:01:48 -0700 | [diff] [blame] | 232 | 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 Ivanov | 042426b | 2014-08-12 21:02:13 -0700 | [diff] [blame] | 244 | return true; | 
|  | 245 | } | 
|  | 246 | } | 
|  | 247 | return false; | 
|  | 248 | } | 
|  | 249 |  | 
| Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 250 | 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 |  | 
| Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 256 | size_t size() const { | 
|  | 257 | size_t result = 0; | 
|  | 258 | for_each([&](T*) { ++result; }); | 
|  | 259 | return result; | 
|  | 260 | } | 
|  | 261 |  | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 262 | private: | 
|  | 263 | LinkedListEntry<T>* head_; | 
| Dmitriy Ivanov | aa0f2bd | 2014-07-28 17:32:20 -0700 | [diff] [blame] | 264 | LinkedListEntry<T>* tail_; | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 265 | DISALLOW_COPY_AND_ASSIGN(LinkedList); | 
|  | 266 | }; |