| 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 |  | 
|  | 28 | /* | 
|  | 29 | * Represents linked list of objects of type T | 
|  | 30 | */ | 
|  | 31 | template<typename T, typename Allocator> | 
|  | 32 | class LinkedList { | 
|  | 33 | public: | 
| Dmitriy Ivanov | aa0f2bd | 2014-07-28 17:32:20 -0700 | [diff] [blame] | 34 | LinkedList() : head_(nullptr), tail_(nullptr) {} | 
| Dmitriy Ivanov | 1424140 | 2014-08-26 14:16:52 -0700 | [diff] [blame] | 35 | ~LinkedList() { | 
|  | 36 | clear(); | 
|  | 37 | } | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 38 |  | 
| Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 39 | LinkedList(LinkedList&& that) { | 
|  | 40 | this->head_ = that.head_; | 
|  | 41 | this->tail_ = that.tail_; | 
|  | 42 | that.head_ = that.tail_ = nullptr; | 
|  | 43 | } | 
|  | 44 |  | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 45 | 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 Ivanov | aa0f2bd | 2014-07-28 17:32:20 -0700 | [diff] [blame] | 50 | 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 Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 82 | } | 
|  | 83 |  | 
|  | 84 | void clear() { | 
|  | 85 | while (head_ != nullptr) { | 
|  | 86 | LinkedListEntry<T>* p = head_; | 
|  | 87 | head_ = head_->next; | 
|  | 88 | Allocator::free(p); | 
|  | 89 | } | 
| Dmitriy Ivanov | aa0f2bd | 2014-07-28 17:32:20 -0700 | [diff] [blame] | 90 |  | 
|  | 91 | tail_ = nullptr; | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 92 | } | 
|  | 93 |  | 
|  | 94 | template<typename F> | 
| Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 95 | void for_each(F action) const { | 
| Dmitriy Ivanov | a492605 | 2014-09-02 09:45:40 -0700 | [diff] [blame] | 96 | visit([&] (T* si) { | 
|  | 97 | action(si); | 
|  | 98 | return true; | 
|  | 99 | }); | 
|  | 100 | } | 
|  | 101 |  | 
|  | 102 | template<typename F> | 
| Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 103 | bool visit(F action) const { | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 104 | for (LinkedListEntry<T>* e = head_; e != nullptr; e = e->next) { | 
| Dmitriy Ivanov | a492605 | 2014-09-02 09:45:40 -0700 | [diff] [blame] | 105 | if (!action(e->element)) { | 
|  | 106 | return false; | 
|  | 107 | } | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 108 | } | 
| Dmitriy Ivanov | a492605 | 2014-09-02 09:45:40 -0700 | [diff] [blame] | 109 | return true; | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 110 | } | 
|  | 111 |  | 
|  | 112 | template<typename F> | 
| Dmitriy Ivanov | 4bea498 | 2014-08-29 14:01:48 -0700 | [diff] [blame] | 113 | void remove_if(F predicate) { | 
|  | 114 | for (LinkedListEntry<T>* e = head_, *p = nullptr; e != nullptr;) { | 
|  | 115 | if (predicate(e->element)) { | 
|  | 116 | LinkedListEntry<T>* next = e->next; | 
|  | 117 | if (p == nullptr) { | 
|  | 118 | head_ = next; | 
|  | 119 | } else { | 
|  | 120 | p->next = next; | 
|  | 121 | } | 
|  | 122 | Allocator::free(e); | 
|  | 123 | e = next; | 
|  | 124 | } else { | 
|  | 125 | p = e; | 
|  | 126 | e = e->next; | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 127 | } | 
|  | 128 | } | 
|  | 129 | } | 
|  | 130 |  | 
| Dmitriy Ivanov | 4bea498 | 2014-08-29 14:01:48 -0700 | [diff] [blame] | 131 | size_t copy_to_array(T* array[], size_t array_length) const { | 
|  | 132 | size_t sz = 0; | 
|  | 133 | for (LinkedListEntry<T>* e = head_; sz < array_length && e != nullptr; e = e->next) { | 
|  | 134 | array[sz++] = e->element; | 
|  | 135 | } | 
|  | 136 |  | 
|  | 137 | return sz; | 
|  | 138 | } | 
|  | 139 |  | 
|  | 140 | bool contains(const T* el) const { | 
|  | 141 | for (LinkedListEntry<T>* e = head_; e != nullptr; e = e->next) { | 
|  | 142 | if (e->element == el) { | 
| Dmitriy Ivanov | 042426b | 2014-08-12 21:02:13 -0700 | [diff] [blame] | 143 | return true; | 
|  | 144 | } | 
|  | 145 | } | 
|  | 146 | return false; | 
|  | 147 | } | 
|  | 148 |  | 
| Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 149 | static LinkedList make_list(T* const element) { | 
|  | 150 | LinkedList<T, Allocator> one_element_list; | 
|  | 151 | one_element_list.push_back(element); | 
|  | 152 | return one_element_list; | 
|  | 153 | } | 
|  | 154 |  | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 155 | private: | 
|  | 156 | LinkedListEntry<T>* head_; | 
| Dmitriy Ivanov | aa0f2bd | 2014-07-28 17:32:20 -0700 | [diff] [blame] | 157 | LinkedListEntry<T>* tail_; | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 158 | DISALLOW_COPY_AND_ASSIGN(LinkedList); | 
|  | 159 | }; | 
|  | 160 |  | 
|  | 161 | #endif // __LINKED_LIST_H |