2 * Copyright (c) 2000-2001, 2003 The Regents of The University of Michigan
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.
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.
29 #ifndef __DBL_LIST_HH__
30 #define __DBL_LIST_HH__
36 // remove this from list
42 // insert this before old_el
43 void insertBefore(DblListEl *old_el) {
50 // insert this after old_el
51 void insertAfter(DblListEl *old_el) {
58 friend class DblListBase;
63 // doubly-linked list of DblListEl objects
66 // dummy list head element: dummy.next is head, dummy.prev is tail
72 DblListEl *valid_or_null(DblListEl *el) {
73 // make sure users never see the dummy element
74 return (el == &dummy) ? NULL : el;
80 return valid_or_null(dummy.next);
84 return valid_or_null(dummy.prev);
87 DblListEl *next(DblListEl *el) {
88 return valid_or_null(el->next);
91 DblListEl *prev(DblListEl *el) {
92 return valid_or_null(el->prev);
96 return (dummy.next == &dummy);
99 void remove(DblListEl *el) {
104 void insertBefore(DblListEl *new_el, DblListEl *old_el) {
105 new_el->insertBefore(old_el);
109 void insertAfter(DblListEl *new_el, DblListEl *old_el) {
110 new_el->insertAfter(old_el);
114 // append to end of list, i.e. as dummy.prev
115 void append(DblListEl *el) {
116 insertBefore(el, &dummy);
119 // prepend to front of list (push), i.e. as dummy.next
120 void prepend(DblListEl *el) {
121 insertAfter(el, &dummy);
125 DblListEl *hd = head();
133 dummy.next = dummy.prev = &dummy;
140 // Template class serves solely to cast args & return values
141 // to appropriate type (T *)
143 template<class T> class DblList : private DblListBase {
147 T *head() { return (T *)DblListBase::head(); }
148 T *tail() { return (T *)DblListBase::tail(); }
150 T *next(T *el) { return (T *)DblListBase::next(el); }
151 T *prev(T *el) { return (T *)DblListBase::prev(el); }
153 bool is_empty() { return DblListBase::is_empty(); }
155 void remove(T *el) { DblListBase::remove(el); }
157 void append(T *el) { DblListBase::append(el); }
158 void prepend(T *el) { DblListBase::prepend(el); }
160 T *pop() { return (T *)DblListBase::pop(); }
165 #endif // __DBL_LIST_HH__