Merge zizzer.eecs.umich.edu:/bk/m5
[gem5.git] / base / dbl_list.hh
1 /*
2 * Copyright (c) 2000-2001, 2003 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #ifndef __DBL_LIST_HH__
30 #define __DBL_LIST_HH__
31
32 class DblListEl {
33 DblListEl *next;
34 DblListEl *prev;
35
36 // remove this from list
37 void remove() {
38 prev->next = next;
39 next->prev = prev;
40 }
41
42 // insert this before old_el
43 void insertBefore(DblListEl *old_el) {
44 prev = old_el->prev;
45 next = old_el;
46 prev->next = this;
47 next->prev = this;
48 }
49
50 // insert this after old_el
51 void insertAfter(DblListEl *old_el) {
52 next = old_el->next;
53 prev = old_el;
54 next->prev = this;
55 prev->next = this;
56 }
57
58 friend class DblListBase;
59 };
60
61
62 //
63 // doubly-linked list of DblListEl objects
64 //
65 class DblListBase {
66 // dummy list head element: dummy.next is head, dummy.prev is tail
67 DblListEl dummy;
68
69 // length counter
70 unsigned length;
71
72 DblListEl *valid_or_null(DblListEl *el) {
73 // make sure users never see the dummy element
74 return (el == &dummy) ? NULL : el;
75 }
76
77 public:
78
79 DblListEl *head() {
80 return valid_or_null(dummy.next);
81 }
82
83 DblListEl *tail() {
84 return valid_or_null(dummy.prev);
85 }
86
87 DblListEl *next(DblListEl *el) {
88 return valid_or_null(el->next);
89 }
90
91 DblListEl *prev(DblListEl *el) {
92 return valid_or_null(el->prev);
93 }
94
95 bool is_empty() {
96 return (dummy.next == &dummy);
97 }
98
99 void remove(DblListEl *el) {
100 el->remove();
101 --length;
102 }
103
104 void insertBefore(DblListEl *new_el, DblListEl *old_el) {
105 new_el->insertBefore(old_el);
106 ++length;
107 }
108
109 void insertAfter(DblListEl *new_el, DblListEl *old_el) {
110 new_el->insertAfter(old_el);
111 ++length;
112 }
113
114 // append to end of list, i.e. as dummy.prev
115 void append(DblListEl *el) {
116 insertBefore(el, &dummy);
117 }
118
119 // prepend to front of list (push), i.e. as dummy.next
120 void prepend(DblListEl *el) {
121 insertAfter(el, &dummy);
122 }
123
124 DblListEl *pop() {
125 DblListEl *hd = head();
126 if (hd != NULL)
127 remove(hd);
128 return hd;
129 }
130
131 // constructor
132 DblListBase() {
133 dummy.next = dummy.prev = &dummy;
134 length = 0;
135 }
136 };
137
138
139 //
140 // Template class serves solely to cast args & return values
141 // to appropriate type (T *)
142 //
143 template<class T> class DblList : private DblListBase {
144
145 public:
146
147 T *head() { return (T *)DblListBase::head(); }
148 T *tail() { return (T *)DblListBase::tail(); }
149
150 T *next(T *el) { return (T *)DblListBase::next(el); }
151 T *prev(T *el) { return (T *)DblListBase::prev(el); }
152
153 bool is_empty() { return DblListBase::is_empty(); }
154
155 void remove(T *el) { DblListBase::remove(el); }
156
157 void append(T *el) { DblListBase::append(el); }
158 void prepend(T *el) { DblListBase::prepend(el); }
159
160 T *pop() { return (T *)DblListBase::pop(); }
161
162 DblList<T>() { }
163 };
164
165 #endif // __DBL_LIST_HH__