2014-05-09 18:10:14 +02:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 The Android Open Source Project
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __LINKED_LIST_H
|
|
|
|
#define __LINKED_LIST_H
|
|
|
|
|
|
|
|
#include "private/bionic_macros.h"
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
T* operator*() {
|
|
|
|
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;
|
|
|
|
|
2014-07-29 02:32:20 +02:00
|
|
|
LinkedList() : head_(nullptr), tail_(nullptr) {}
|
2014-08-26 23:16:52 +02:00
|
|
|
~LinkedList() {
|
|
|
|
clear();
|
|
|
|
}
|
2014-05-09 18:10:14 +02:00
|
|
|
|
2014-08-28 23:12:12 +02:00
|
|
|
LinkedList(LinkedList&& that) {
|
|
|
|
this->head_ = that.head_;
|
|
|
|
this->tail_ = that.tail_;
|
|
|
|
that.head_ = that.tail_ = nullptr;
|
|
|
|
}
|
|
|
|
|
2014-05-09 18:10:14 +02:00
|
|
|
void push_front(T* const element) {
|
|
|
|
LinkedListEntry<T>* new_entry = Allocator::alloc();
|
|
|
|
new_entry->next = head_;
|
|
|
|
new_entry->element = element;
|
|
|
|
head_ = new_entry;
|
2014-07-29 02:32:20 +02:00
|
|
|
if (tail_ == nullptr) {
|
|
|
|
tail_ = new_entry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void push_back(T* const element) {
|
|
|
|
LinkedListEntry<T>* new_entry = Allocator::alloc();
|
|
|
|
new_entry->next = nullptr;
|
|
|
|
new_entry->element = element;
|
|
|
|
if (tail_ == nullptr) {
|
|
|
|
tail_ = head_ = new_entry;
|
|
|
|
} else {
|
|
|
|
tail_->next = new_entry;
|
|
|
|
tail_ = new_entry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
T* pop_front() {
|
|
|
|
if (head_ == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
LinkedListEntry<T>* entry = head_;
|
|
|
|
T* element = entry->element;
|
|
|
|
head_ = entry->next;
|
|
|
|
Allocator::free(entry);
|
|
|
|
|
|
|
|
if (head_ == nullptr) {
|
|
|
|
tail_ = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return element;
|
2014-05-09 18:10:14 +02:00
|
|
|
}
|
|
|
|
|
2014-11-29 22:57:41 +01:00
|
|
|
T* front() const {
|
|
|
|
if (head_ == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return head_->element;
|
|
|
|
}
|
|
|
|
|
2014-05-09 18:10:14 +02:00
|
|
|
void clear() {
|
|
|
|
while (head_ != nullptr) {
|
|
|
|
LinkedListEntry<T>* p = head_;
|
|
|
|
head_ = head_->next;
|
|
|
|
Allocator::free(p);
|
|
|
|
}
|
2014-07-29 02:32:20 +02:00
|
|
|
|
|
|
|
tail_ = nullptr;
|
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 {
|
2014-05-09 18:10:14 +02: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) {
|
|
|
|
for (LinkedListEntry<T>* e = head_, *p = nullptr; e != nullptr;) {
|
|
|
|
if (predicate(e->element)) {
|
|
|
|
LinkedListEntry<T>* next = e->next;
|
|
|
|
if (p == nullptr) {
|
|
|
|
head_ = next;
|
|
|
|
} else {
|
|
|
|
p->next = next;
|
|
|
|
}
|
2015-11-06 02:41:05 +01:00
|
|
|
|
|
|
|
if (tail_ == e) {
|
|
|
|
tail_ = p;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-09 22:42:33 +02:00
|
|
|
template<typename F>
|
|
|
|
T* find_if(F predicate) const {
|
|
|
|
for (LinkedListEntry<T>* e = head_; e != nullptr; e = e->next) {
|
|
|
|
if (predicate(e->element)) {
|
|
|
|
return e->element;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-10-30 01:01:24 +01:00
|
|
|
iterator begin() {
|
|
|
|
return iterator(head_);
|
|
|
|
}
|
|
|
|
|
|
|
|
iterator end() {
|
|
|
|
return iterator(nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
iterator find(T* value) {
|
|
|
|
for (LinkedListEntry<T>* e = head_; e != nullptr; e = e->next) {
|
|
|
|
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;
|
|
|
|
for (LinkedListEntry<T>* e = head_; sz < array_length && e != nullptr; e = e->next) {
|
|
|
|
array[sz++] = e->element;
|
|
|
|
}
|
|
|
|
|
|
|
|
return sz;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool contains(const T* el) const {
|
2014-08-13 06:02:13 +02: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;
|
|
|
|
}
|
|
|
|
|
2014-05-09 18:10:14 +02:00
|
|
|
private:
|
|
|
|
LinkedListEntry<T>* head_;
|
2014-07-29 02:32:20 +02:00
|
|
|
LinkedListEntry<T>* tail_;
|
2014-05-09 18:10:14 +02:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(LinkedList);
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // __LINKED_LIST_H
|