1 | // -*- C++ -*- |
---|
2 | |
---|
3 | /* |
---|
4 | |
---|
5 | Heap Layers: An Extensible Memory Allocation Infrastructure |
---|
6 | |
---|
7 | Copyright (C) 2000-2003 by Emery Berger |
---|
8 | http://www.cs.umass.edu/~emery |
---|
9 | emery@cs.umass.edu |
---|
10 | |
---|
11 | This program is free software; you can redistribute it and/or modify |
---|
12 | it under the terms of the GNU General Public License as published by |
---|
13 | the Free Software Foundation; either version 2 of the License, or |
---|
14 | (at your option) any later version. |
---|
15 | |
---|
16 | This program is distributed in the hope that it will be useful, |
---|
17 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
19 | GNU General Public License for more details. |
---|
20 | |
---|
21 | You should have received a copy of the GNU General Public License |
---|
22 | along with this program; if not, write to the Free Software |
---|
23 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
24 | |
---|
25 | */ |
---|
26 | |
---|
27 | #ifndef _DLLIST_H_ |
---|
28 | #define _DLLIST_H_ |
---|
29 | |
---|
30 | #include <assert.h> |
---|
31 | |
---|
32 | /** |
---|
33 | * |
---|
34 | * @class DLList |
---|
35 | * @brief A "memory neutral" doubly-linked list. |
---|
36 | * @author Emery Berger |
---|
37 | */ |
---|
38 | |
---|
39 | namespace HL { |
---|
40 | |
---|
41 | class DLList { |
---|
42 | public: |
---|
43 | |
---|
44 | inline DLList (void) { |
---|
45 | clear(); |
---|
46 | } |
---|
47 | |
---|
48 | class Entry; |
---|
49 | |
---|
50 | /// Clear the list. |
---|
51 | inline void clear (void) { |
---|
52 | head.setPrev (&head); |
---|
53 | head.setNext (&head); |
---|
54 | } |
---|
55 | |
---|
56 | /// Is the list empty? |
---|
57 | inline bool isEmpty (void) const { |
---|
58 | return (head.getNext() == &head); |
---|
59 | } |
---|
60 | |
---|
61 | /// Get the head of the list. |
---|
62 | inline Entry * get (void) { |
---|
63 | const Entry * e = head.next; |
---|
64 | if (e == &head) { |
---|
65 | return NULL; |
---|
66 | } |
---|
67 | head.next = e->next; |
---|
68 | head.next->prev = &head; |
---|
69 | return (Entry *) e; |
---|
70 | } |
---|
71 | |
---|
72 | /// Remove one item from the list. |
---|
73 | inline void remove (Entry * e) { |
---|
74 | e->remove(); |
---|
75 | } |
---|
76 | |
---|
77 | /// Insert an entry into the head of the list. |
---|
78 | inline void insert (Entry * e) { |
---|
79 | e->insert (&head, head.next); |
---|
80 | } |
---|
81 | |
---|
82 | /// An entry in the list. |
---|
83 | class Entry { |
---|
84 | public: |
---|
85 | // private: |
---|
86 | inline void setPrev (Entry * p) { assert (p != NULL); prev = p; } |
---|
87 | inline void setNext (Entry * p) { assert (p != NULL); next = p; } |
---|
88 | inline Entry * getPrev (void) const { return prev; } |
---|
89 | inline Entry * getNext (void) const { return next; } |
---|
90 | inline void remove (void) const { |
---|
91 | prev->setNext(next); |
---|
92 | next->setPrev(prev); |
---|
93 | } |
---|
94 | inline void insert (Entry * p, Entry * n) { |
---|
95 | prev = p; |
---|
96 | next = n; |
---|
97 | p->setNext (this); |
---|
98 | n->setPrev (this); |
---|
99 | } |
---|
100 | Entry * prev; |
---|
101 | Entry * next; |
---|
102 | }; |
---|
103 | |
---|
104 | |
---|
105 | private: |
---|
106 | |
---|
107 | /// The head of the list. |
---|
108 | Entry head; |
---|
109 | |
---|
110 | }; |
---|
111 | |
---|
112 | }; |
---|
113 | |
---|
114 | #endif |
---|