| 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 |  | 
|  | 29 | #ifndef __LINKED_LIST_H | 
|  | 30 | #define __LINKED_LIST_H | 
|  | 31 |  | 
|  | 32 | #include "private/bionic_macros.h" | 
|  | 33 |  | 
|  | 34 | template<typename T> | 
|  | 35 | struct LinkedListEntry { | 
|  | 36 | LinkedListEntry<T>* next; | 
|  | 37 | T* element; | 
|  | 38 | }; | 
|  | 39 |  | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 40 | // ForwardInputIterator | 
|  | 41 | template<typename T> | 
|  | 42 | class 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 Ivanov | d88e1f3 | 2016-03-24 15:30:30 -0700 | [diff] [blame] | 58 | T* const operator*() { | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 59 | 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 Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 74 | /* | 
|  | 75 | * Represents linked list of objects of type T | 
|  | 76 | */ | 
|  | 77 | template<typename T, typename Allocator> | 
|  | 78 | class LinkedList { | 
|  | 79 | public: | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 80 | typedef LinkedListIterator<T> iterator; | 
|  | 81 | typedef T* value_type; | 
|  | 82 |  | 
| Dmitriy Ivanov | aa0f2bd | 2014-07-28 17:32:20 -0700 | [diff] [blame] | 83 | LinkedList() : head_(nullptr), tail_(nullptr) {} | 
| Dmitriy Ivanov | 1424140 | 2014-08-26 14:16:52 -0700 | [diff] [blame] | 84 | ~LinkedList() { | 
|  | 85 | clear(); | 
|  | 86 | } | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 87 |  | 
| Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 88 | LinkedList(LinkedList&& that) { | 
|  | 89 | this->head_ = that.head_; | 
|  | 90 | this->tail_ = that.tail_; | 
|  | 91 | that.head_ = that.tail_ = nullptr; | 
|  | 92 | } | 
|  | 93 |  | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 94 | 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 Ivanov | aa0f2bd | 2014-07-28 17:32:20 -0700 | [diff] [blame] | 99 | 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 Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 131 | } | 
|  | 132 |  | 
| Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 133 | T* front() const { | 
|  | 134 | if (head_ == nullptr) { | 
|  | 135 | return nullptr; | 
|  | 136 | } | 
|  | 137 |  | 
|  | 138 | return head_->element; | 
|  | 139 | } | 
|  | 140 |  | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 141 | void clear() { | 
|  | 142 | while (head_ != nullptr) { | 
|  | 143 | LinkedListEntry<T>* p = head_; | 
|  | 144 | head_ = head_->next; | 
|  | 145 | Allocator::free(p); | 
|  | 146 | } | 
| Dmitriy Ivanov | aa0f2bd | 2014-07-28 17:32:20 -0700 | [diff] [blame] | 147 |  | 
|  | 148 | tail_ = nullptr; | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 149 | } | 
|  | 150 |  | 
| Dimitry Ivanov | ec90e24 | 2017-02-10 11:04:20 -0800 | [diff] [blame] | 151 | bool empty() { | 
|  | 152 | return (head_ == nullptr); | 
|  | 153 | } | 
|  | 154 |  | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 155 | template<typename F> | 
| Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 156 | void for_each(F action) const { | 
| Dmitriy Ivanov | a492605 | 2014-09-02 09:45:40 -0700 | [diff] [blame] | 157 | visit([&] (T* si) { | 
|  | 158 | action(si); | 
|  | 159 | return true; | 
|  | 160 | }); | 
|  | 161 | } | 
|  | 162 |  | 
|  | 163 | template<typename F> | 
| Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 164 | bool visit(F action) const { | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 165 | for (LinkedListEntry<T>* e = head_; e != nullptr; e = e->next) { | 
| Dmitriy Ivanov | a492605 | 2014-09-02 09:45:40 -0700 | [diff] [blame] | 166 | if (!action(e->element)) { | 
|  | 167 | return false; | 
|  | 168 | } | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 169 | } | 
| Dmitriy Ivanov | a492605 | 2014-09-02 09:45:40 -0700 | [diff] [blame] | 170 | return true; | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 171 | } | 
|  | 172 |  | 
|  | 173 | template<typename F> | 
| Dmitriy Ivanov | 4bea498 | 2014-08-29 14:01:48 -0700 | [diff] [blame] | 174 | 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 Ivanov | 7a9311f | 2015-11-05 17:41:05 -0800 | [diff] [blame] | 183 |  | 
|  | 184 | if (tail_ == e) { | 
|  | 185 | tail_ = p; | 
|  | 186 | } | 
|  | 187 |  | 
| Dmitriy Ivanov | 4bea498 | 2014-08-29 14:01:48 -0700 | [diff] [blame] | 188 | Allocator::free(e); | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 189 |  | 
| Dmitriy Ivanov | 4bea498 | 2014-08-29 14:01:48 -0700 | [diff] [blame] | 190 | e = next; | 
|  | 191 | } else { | 
|  | 192 | p = e; | 
|  | 193 | e = e->next; | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 194 | } | 
|  | 195 | } | 
|  | 196 | } | 
|  | 197 |  | 
| Dimitry Ivanov | ec90e24 | 2017-02-10 11:04:20 -0800 | [diff] [blame] | 198 | void remove(T* element) { | 
|  | 199 | remove_if([&](T* e) { | 
|  | 200 | return e == element; | 
|  | 201 | }); | 
|  | 202 | } | 
|  | 203 |  | 
| Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 204 | 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 Ivanov | d88e1f3 | 2016-03-24 15:30:30 -0700 | [diff] [blame] | 215 | iterator begin() const { | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 216 | return iterator(head_); | 
|  | 217 | } | 
|  | 218 |  | 
| Dimitry Ivanov | d88e1f3 | 2016-03-24 15:30:30 -0700 | [diff] [blame] | 219 | iterator end() const { | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 220 | return iterator(nullptr); | 
|  | 221 | } | 
|  | 222 |  | 
| Dimitry Ivanov | d88e1f3 | 2016-03-24 15:30:30 -0700 | [diff] [blame] | 223 | iterator find(T* value) const { | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 224 | 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 Ivanov | 4bea498 | 2014-08-29 14:01:48 -0700 | [diff] [blame] | 233 | 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 Ivanov | 042426b | 2014-08-12 21:02:13 -0700 | [diff] [blame] | 245 | return true; | 
|  | 246 | } | 
|  | 247 | } | 
|  | 248 | return false; | 
|  | 249 | } | 
|  | 250 |  | 
| Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 251 | 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 Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 257 | private: | 
|  | 258 | LinkedListEntry<T>* head_; | 
| Dmitriy Ivanov | aa0f2bd | 2014-07-28 17:32:20 -0700 | [diff] [blame] | 259 | LinkedListEntry<T>* tail_; | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 260 | DISALLOW_COPY_AND_ASSIGN(LinkedList); | 
|  | 261 | }; | 
|  | 262 |  | 
|  | 263 | #endif // __LINKED_LIST_H |