blob: 4e62e208f6bca222378b2c877246b6c40f41dcf2 [file] [log] [blame]
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001/*
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
22template<typename T>
23struct LinkedListEntry {
24 LinkedListEntry<T>* next;
25 T* element;
26};
27
28/*
29 * Represents linked list of objects of type T
30 */
31template<typename T, typename Allocator>
32class LinkedList {
33 public:
Dmitriy Ivanovdb7a17d2014-08-04 23:39:22 +000034 LinkedList() : head_(nullptr), tail_(nullptr) {}
Dmitriy Ivanov93c3f422014-08-26 14:16:52 -070035 ~LinkedList() {
36 clear();
37 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -070038
39 void push_front(T* const element) {
40 LinkedListEntry<T>* new_entry = Allocator::alloc();
41 new_entry->next = head_;
42 new_entry->element = element;
43 head_ = new_entry;
Dmitriy Ivanovdb7a17d2014-08-04 23:39:22 +000044 if (tail_ == nullptr) {
45 tail_ = new_entry;
46 }
47 }
48
49 void push_back(T* const element) {
50 LinkedListEntry<T>* new_entry = Allocator::alloc();
51 new_entry->next = nullptr;
52 new_entry->element = element;
53 if (tail_ == nullptr) {
54 tail_ = head_ = new_entry;
55 } else {
56 tail_->next = new_entry;
57 tail_ = new_entry;
58 }
59 }
60
61 T* pop_front() {
62 if (head_ == nullptr) {
63 return nullptr;
64 }
65
66 LinkedListEntry<T>* entry = head_;
67 T* element = entry->element;
68 head_ = entry->next;
69 Allocator::free(entry);
70
71 if (head_ == nullptr) {
72 tail_ = nullptr;
73 }
74
75 return element;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -070076 }
77
78 void clear() {
79 while (head_ != nullptr) {
80 LinkedListEntry<T>* p = head_;
81 head_ = head_->next;
82 Allocator::free(p);
83 }
Dmitriy Ivanovdb7a17d2014-08-04 23:39:22 +000084
85 tail_ = nullptr;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -070086 }
87
88 template<typename F>
Dmitriy Ivanov00dce522014-11-03 21:15:25 -080089 void for_each(F action) {
Dmitriy Ivanov4466bd72014-09-02 09:45:40 -070090 visit([&] (T* si) {
91 action(si);
92 return true;
93 });
94 }
95
96 template<typename F>
Dmitriy Ivanov00dce522014-11-03 21:15:25 -080097 bool visit(F action) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -070098 for (LinkedListEntry<T>* e = head_; e != nullptr; e = e->next) {
Dmitriy Ivanov4466bd72014-09-02 09:45:40 -070099 if (!action(e->element)) {
100 return false;
101 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700102 }
Dmitriy Ivanov4466bd72014-09-02 09:45:40 -0700103 return true;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700104 }
105
106 template<typename F>
Dmitriy Ivanov4d01d082014-08-29 14:01:48 -0700107 void remove_if(F predicate) {
108 for (LinkedListEntry<T>* e = head_, *p = nullptr; e != nullptr;) {
109 if (predicate(e->element)) {
110 LinkedListEntry<T>* next = e->next;
111 if (p == nullptr) {
112 head_ = next;
113 } else {
114 p->next = next;
115 }
116 Allocator::free(e);
117 e = next;
118 } else {
119 p = e;
120 e = e->next;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700121 }
122 }
123 }
124
Dmitriy Ivanov4d01d082014-08-29 14:01:48 -0700125 size_t copy_to_array(T* array[], size_t array_length) const {
126 size_t sz = 0;
127 for (LinkedListEntry<T>* e = head_; sz < array_length && e != nullptr; e = e->next) {
128 array[sz++] = e->element;
129 }
130
131 return sz;
132 }
133
134 bool contains(const T* el) const {
135 for (LinkedListEntry<T>* e = head_; e != nullptr; e = e->next) {
136 if (e->element == el) {
Dmitriy Ivanov8a84d382014-08-12 21:02:13 -0700137 return true;
138 }
139 }
140 return false;
141 }
142
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700143 private:
144 LinkedListEntry<T>* head_;
Dmitriy Ivanovdb7a17d2014-08-04 23:39:22 +0000145 LinkedListEntry<T>* tail_;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700146 DISALLOW_COPY_AND_ASSIGN(LinkedList);
147};
148
149#endif // __LINKED_LIST_H