2014-05-09 18:10:14 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 The Android Open Source Project
|
2017-02-16 00:31:13 +01:00
|
|
|
* All rights reserved.
|
2014-05-09 18:10:14 +02:00
|
|
|
*
|
2017-02-16 00:31:13 +01:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
2014-05-09 18:10:14 +02:00
|
|
|
*
|
2017-02-16 00:31:13 +01:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
|
|
|
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
2014-05-09 18:10:14 +02:00
|
|
|
*/
|
|
|
|
|
2018-02-13 23:26:29 +01:00
|
|
|
#pragma once
|
2014-05-09 18:10:14 +02:00
|
|
|
|
2018-10-25 20:00:00 +02:00
|
|
|
#include <android-base/macros.h>
|
2014-05-09 18:10:14 +02:00
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct LinkedListEntry {
|
|
|
|
LinkedListEntry<T>* next;
|
|
|
|
T* element;
|
|
|
|
};
|
|
|
|
|
2015-10-30 01:01:24 +01:00
|
|
|
// ForwardInputIterator
|
|
|
|
template<typename T>
|
|
|
|
class LinkedListIterator {
|
|
|
|
public:
|
|
|
|
LinkedListIterator() : entry_(nullptr) {}
|
|
|
|
LinkedListIterator(const LinkedListIterator<T>& that) : entry_(that.entry_) {}
|
|
|
|
explicit LinkedListIterator(LinkedListEntry<T>* entry) : entry_(entry) {}
|
|
|
|
|
|
|
|
LinkedListIterator<T>& operator=(const LinkedListIterator<T>& that) {
|
|
|
|
entry_ = that.entry_;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
LinkedListIterator<T>& operator++() {
|
|
|
|
entry_ = entry_->next;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2016-03-24 23:30:30 +01:00
|
|
|
T* const operator*() {
|
2015-10-30 01:01:24 +01:00
|
|
|
return entry_->element;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(const LinkedListIterator<T>& that) const {
|
|
|
|
return entry_ == that.entry_;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const LinkedListIterator<T>& that) const {
|
|
|
|
return entry_ != that.entry_;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
LinkedListEntry<T> *entry_;
|
|
|
|
};
|
|
|
|
|
2014-05-09 18:10:14 +02:00
|
|
|
/*
|
|
|
|
* Represents linked list of objects of type T
|
|
|
|
*/
|
|
|
|
template<typename T, typename Allocator>
|
|
|
|
class LinkedList {
|
|
|
|
public:
|
2015-10-30 01:01:24 +01:00
|
|
|
typedef LinkedListIterator<T> iterator;
|
|
|
|
typedef T* value_type;
|
|
|
|
|
2021-11-23 18:48:10 +01:00
|
|
|
// Allocating the head/tail fields separately from the LinkedList struct saves memory in the
|
|
|
|
// Zygote (e.g. because adding an soinfo to a namespace doesn't dirty the page containing the
|
|
|
|
// soinfo).
|
|
|
|
struct LinkedListHeader {
|
|
|
|
LinkedListEntry<T>* head;
|
|
|
|
LinkedListEntry<T>* tail;
|
|
|
|
};
|
|
|
|
|
|
|
|
// The allocator returns a LinkedListEntry<T>* but we want to treat it as a LinkedListHeader
|
|
|
|
// struct instead.
|
|
|
|
static_assert(sizeof(LinkedListHeader) == sizeof(LinkedListEntry<T>));
|
|
|
|
static_assert(alignof(LinkedListHeader) == alignof(LinkedListEntry<T>));
|
|
|
|
|
|
|
|
constexpr LinkedList() : header_(nullptr) {}
|
2014-08-26 23:16:52 +02:00
|
|
|
~LinkedList() {
|
|
|
|
clear();
|
2021-11-23 18:48:10 +01:00
|
|
|
if (header_ != nullptr) {
|
|
|
|
Allocator::free(reinterpret_cast<LinkedListEntry<T>*>(header_));
|
|
|
|
}
|
2014-08-26 23:16:52 +02:00
|
|
|
}
|
2014-05-09 18:10:14 +02:00
|
|
|
|
2018-09-25 23:00:44 +02:00
|
|
|
LinkedList(LinkedList&& that) noexcept {
|
2021-11-23 18:48:10 +01:00
|
|
|
this->header_ = that.header_;
|
|
|
|
that.header_ = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool empty() const {
|
|
|
|
return header_ == nullptr || header_->head == nullptr;
|
2014-08-28 23:12:12 +02:00
|
|
|
}
|
|
|
|
|
2014-05-09 18:10:14 +02:00
|
|
|
void push_front(T* const element) {
|
2021-11-23 18:48:10 +01:00
|
|
|
alloc_header();
|
2014-05-09 18:10:14 +02:00
|
|
|
LinkedListEntry<T>* new_entry = Allocator::alloc();
|
2021-11-23 18:48:10 +01:00
|
|
|
new_entry->next = header_->head;
|
2014-05-09 18:10:14 +02:00
|
|
|
new_entry->element = element;
|
2021-11-23 18:48:10 +01:00
|
|
|
header_->head = new_entry;
|
|
|
|
if (header_->tail == nullptr) {
|
|
|
|
header_->tail = new_entry;
|
2014-07-29 02:32:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void push_back(T* const element) {
|
2021-11-23 18:48:10 +01:00
|
|
|
alloc_header();
|
2014-07-29 02:32:20 +02:00
|
|
|
LinkedListEntry<T>* new_entry = Allocator::alloc();
|
|
|
|
new_entry->next = nullptr;
|
|
|
|
new_entry->element = element;
|
2021-11-23 18:48:10 +01:00
|
|
|
if (header_->tail == nullptr) {
|
|
|
|
header_->tail = header_->head = new_entry;
|
2014-07-29 02:32:20 +02:00
|
|
|
} else {
|
2021-11-23 18:48:10 +01:00
|
|
|
header_->tail->next = new_entry;
|
|
|
|
header_->tail = new_entry;
|
2014-07-29 02:32:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
T* pop_front() {
|
2021-11-23 18:48:10 +01:00
|
|
|
if (empty()) return nullptr;
|
2014-07-29 02:32:20 +02:00
|
|
|
|
2021-11-23 18:48:10 +01:00
|
|
|
LinkedListEntry<T>* entry = header_->head;
|
2014-07-29 02:32:20 +02:00
|
|
|
T* element = entry->element;
|
2021-11-23 18:48:10 +01:00
|
|
|
header_->head = entry->next;
|
2014-07-29 02:32:20 +02:00
|
|
|
Allocator::free(entry);
|
|
|
|
|
2021-11-23 18:48:10 +01:00
|
|
|
if (header_->head == nullptr) {
|
|
|
|
header_->tail = nullptr;
|
2014-07-29 02:32:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return element;
|
2014-05-09 18:10:14 +02:00
|
|
|
}
|
|
|
|
|
2014-11-29 22:57:41 +01:00
|
|
|
T* front() const {
|
2021-11-23 18:48:10 +01:00
|
|
|
return empty() ? nullptr : header_->head->element;
|
2014-11-29 22:57:41 +01:00
|
|
|
}
|
|
|
|
|
2014-05-09 18:10:14 +02:00
|
|
|
void clear() {
|
2021-11-23 18:48:10 +01:00
|
|
|
if (empty()) return;
|
|
|
|
|
|
|
|
while (header_->head != nullptr) {
|
|
|
|
LinkedListEntry<T>* p = header_->head;
|
|
|
|
header_->head = header_->head->next;
|
2014-05-09 18:10:14 +02:00
|
|
|
Allocator::free(p);
|
|
|
|
}
|
2014-07-29 02:32:20 +02:00
|
|
|
|
2021-11-23 18:48:10 +01:00
|
|
|
header_->tail = nullptr;
|
2017-02-10 20:04:20 +01:00
|
|
|
}
|
|
|
|
|
2014-05-09 18:10:14 +02:00
|
|
|
template<typename F>
|
2014-10-21 18:23:18 +02:00
|
|
|
void for_each(F action) const {
|
2014-09-02 18:45:40 +02:00
|
|
|
visit([&] (T* si) {
|
|
|
|
action(si);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename F>
|
2014-10-21 18:23:18 +02:00
|
|
|
bool visit(F action) const {
|
2021-11-23 18:48:10 +01:00
|
|
|
for (LinkedListEntry<T>* e = head(); e != nullptr; e = e->next) {
|
2014-09-02 18:45:40 +02:00
|
|
|
if (!action(e->element)) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-05-09 18:10:14 +02:00
|
|
|
}
|
2014-09-02 18:45:40 +02:00
|
|
|
return true;
|
2014-05-09 18:10:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename F>
|
2014-08-29 23:01:48 +02:00
|
|
|
void remove_if(F predicate) {
|
2021-11-23 18:48:10 +01:00
|
|
|
if (empty()) return;
|
|
|
|
for (LinkedListEntry<T>* e = header_->head, *p = nullptr; e != nullptr;) {
|
2014-08-29 23:01:48 +02:00
|
|
|
if (predicate(e->element)) {
|
|
|
|
LinkedListEntry<T>* next = e->next;
|
|
|
|
if (p == nullptr) {
|
2021-11-23 18:48:10 +01:00
|
|
|
header_->head = next;
|
2014-08-29 23:01:48 +02:00
|
|
|
} else {
|
|
|
|
p->next = next;
|
|
|
|
}
|
2015-11-06 02:41:05 +01:00
|
|
|
|
2021-11-23 18:48:10 +01:00
|
|
|
if (header_->tail == e) {
|
|
|
|
header_->tail = p;
|
2015-11-06 02:41:05 +01:00
|
|
|
}
|
|
|
|
|
2014-08-29 23:01:48 +02:00
|
|
|
Allocator::free(e);
|
2015-10-30 01:01:24 +01:00
|
|
|
|
2014-08-29 23:01:48 +02:00
|
|
|
e = next;
|
|
|
|
} else {
|
|
|
|
p = e;
|
|
|
|
e = e->next;
|
2014-05-09 18:10:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-10 20:04:20 +01:00
|
|
|
void remove(T* element) {
|
|
|
|
remove_if([&](T* e) {
|
|
|
|
return e == element;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-04-09 22:42:33 +02:00
|
|
|
template<typename F>
|
|
|
|
T* find_if(F predicate) const {
|
2021-11-23 18:48:10 +01:00
|
|
|
for (LinkedListEntry<T>* e = head(); e != nullptr; e = e->next) {
|
2015-04-09 22:42:33 +02:00
|
|
|
if (predicate(e->element)) {
|
|
|
|
return e->element;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-03-24 23:30:30 +01:00
|
|
|
iterator begin() const {
|
2021-11-23 18:48:10 +01:00
|
|
|
return iterator(head());
|
2015-10-30 01:01:24 +01:00
|
|
|
}
|
|
|
|
|
2016-03-24 23:30:30 +01:00
|
|
|
iterator end() const {
|
2015-10-30 01:01:24 +01:00
|
|
|
return iterator(nullptr);
|
|
|
|
}
|
|
|
|
|
2016-03-24 23:30:30 +01:00
|
|
|
iterator find(T* value) const {
|
2021-11-23 18:48:10 +01:00
|
|
|
for (LinkedListEntry<T>* e = head(); e != nullptr; e = e->next) {
|
2015-10-30 01:01:24 +01:00
|
|
|
if (e->element == value) {
|
|
|
|
return iterator(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return end();
|
|
|
|
}
|
|
|
|
|
2014-08-29 23:01:48 +02:00
|
|
|
size_t copy_to_array(T* array[], size_t array_length) const {
|
|
|
|
size_t sz = 0;
|
2021-11-23 18:48:10 +01:00
|
|
|
for (LinkedListEntry<T>* e = head(); sz < array_length && e != nullptr; e = e->next) {
|
2014-08-29 23:01:48 +02:00
|
|
|
array[sz++] = e->element;
|
|
|
|
}
|
|
|
|
|
|
|
|
return sz;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool contains(const T* el) const {
|
2021-11-23 18:48:10 +01:00
|
|
|
for (LinkedListEntry<T>* e = head(); e != nullptr; e = e->next) {
|
2014-08-29 23:01:48 +02:00
|
|
|
if (e->element == el) {
|
2014-08-13 06:02:13 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-08-28 23:12:12 +02:00
|
|
|
static LinkedList make_list(T* const element) {
|
|
|
|
LinkedList<T, Allocator> one_element_list;
|
|
|
|
one_element_list.push_back(element);
|
|
|
|
return one_element_list;
|
|
|
|
}
|
|
|
|
|
2020-01-03 01:36:06 +01:00
|
|
|
size_t size() const {
|
|
|
|
size_t result = 0;
|
|
|
|
for_each([&](T*) { ++result; });
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-05-09 18:10:14 +02:00
|
|
|
private:
|
2021-11-23 18:48:10 +01:00
|
|
|
void alloc_header() {
|
|
|
|
if (header_ == nullptr) {
|
|
|
|
header_ = reinterpret_cast<LinkedListHeader*>(Allocator::alloc());
|
|
|
|
header_->head = header_->tail = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LinkedListEntry<T>* head() const {
|
|
|
|
return header_ != nullptr ? header_->head : nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
LinkedListHeader* header_;
|
2014-05-09 18:10:14 +02:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(LinkedList);
|
|
|
|
};
|