5 #ifndef V8_BASE_LIST_H_ 6 #define V8_BASE_LIST_H_ 10 #include "src/base/logging.h" 18 List() : front_(
nullptr), back_(
nullptr) {}
20 void PushBack(
T* element) {
21 DCHECK(!element->list_node().next());
22 DCHECK(!element->list_node().prev());
25 InsertAfter(element, back_);
27 AddFirstElement(element);
31 void PushFront(
T* element) {
32 DCHECK(!element->list_node().next());
33 DCHECK(!element->list_node().prev());
36 InsertBefore(element, front_);
38 AddFirstElement(element);
42 void Remove(
T* element) {
43 DCHECK(Contains(element));
44 if (back_ == element) {
45 back_ = element->list_node().prev();
47 if (front_ == element) {
48 front_ = element->list_node().next();
50 T* next = element->list_node().next();
51 T* prev = element->list_node().prev();
52 if (next) next->list_node().set_prev(prev);
53 if (prev) prev->list_node().set_next(next);
54 element->list_node().set_prev(
nullptr);
55 element->list_node().set_next(
nullptr);
58 bool Contains(
T* element) {
61 if (it == element)
return true;
62 it = it->list_node().next();
67 bool Empty() {
return !front_ && !back_; }
69 T* front() {
return front_; }
70 T* back() {
return back_; }
73 void AddFirstElement(
T* element) {
76 DCHECK(!element->list_node().next());
77 DCHECK(!element->list_node().prev());
78 element->list_node().set_prev(
nullptr);
79 element->list_node().set_next(
nullptr);
84 void InsertAfter(
T* element,
T* other) {
85 T* other_next = other->list_node().next();
86 element->list_node().set_next(other_next);
87 element->list_node().set_prev(other);
88 other->list_node().set_next(element);
90 other_next->list_node().set_prev(element);
95 void InsertBefore(
T* element,
T* other) {
96 T* other_prev = other->list_node().prev();
97 element->list_node().set_next(other);
98 element->list_node().set_prev(other_prev);
99 other->list_node().set_prev(element);
101 other_prev->list_node().set_next(element);
116 T* next() {
return next_; }
117 T* prev() {
return prev_; }
125 void set_next(
T* next) { next_ = next; }
126 void set_prev(
T* prev) { prev_ = prev; }
131 friend class List<
T>;
136 #endif // V8_BASE_LIST_H_