blob: 36fe9fd1ba20cf057d03a2771493519bbc98ee70 [file] [log] [blame]
Colin Crossbcb4ed32016-01-14 15:35:40 -08001/*
2 * Copyright (C) 2016 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 LIBMEMUNREACHABLE_LINKED_LIST_H_
18#define LIBMEMUNREACHABLE_LINKED_LIST_H_
19
Colin Crossa9939e92017-06-21 13:13:00 -070020namespace android {
21
Colin Crossa83881e2017-06-22 10:50:05 -070022template <class T>
Colin Crossbcb4ed32016-01-14 15:35:40 -080023class LinkedList {
Colin Crossa83881e2017-06-22 10:50:05 -070024 public:
25 LinkedList() : next_(this), prev_(this), data_() {}
26 explicit LinkedList(T data) : LinkedList() { data_ = data; }
27 ~LinkedList() {}
28 void insert(LinkedList<T>& node) {
29 assert(node.empty());
30 node.next_ = this->next_;
31 node.next_->prev_ = &node;
32 this->next_ = &node;
33 node.prev_ = this;
34 }
35 void remove() {
36 this->next_->prev_ = this->prev_;
37 this->prev_->next_ = this->next_;
38 this->next_ = this;
39 this->prev_ = this;
40 }
41 T data() { return data_; }
42 bool empty() { return next_ == this && prev_ == this; }
43 LinkedList<T>* next() { return next_; }
44
45 private:
46 LinkedList<T>* next_;
47 LinkedList<T>* prev_;
48 T data_;
Colin Crossbcb4ed32016-01-14 15:35:40 -080049};
50
Colin Crossa83881e2017-06-22 10:50:05 -070051template <class T>
Colin Crossbcb4ed32016-01-14 15:35:40 -080052class LinkedListHead {
Colin Crossa83881e2017-06-22 10:50:05 -070053 public:
54 LinkedListHead() : node_() {}
55 ~LinkedListHead() {}
Colin Crossbcb4ed32016-01-14 15:35:40 -080056
Colin Crossa83881e2017-06-22 10:50:05 -070057 private:
58 LinkedList<T> node_;
Colin Crossbcb4ed32016-01-14 15:35:40 -080059};
60
Colin Crossa9939e92017-06-21 13:13:00 -070061} // namespace android
62
Colin Crossbcb4ed32016-01-14 15:35:40 -080063#endif