Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | /* |
2 | * ========== Copyright Header Begin ========================================== | |
3 | * | |
4 | * OpenSPARC T2 Processor File: pList.h | |
5 | * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved. | |
6 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES. | |
7 | * | |
8 | * The above named program is free software; you can redistribute it and/or | |
9 | * modify it under the terms of the GNU General Public | |
10 | * License version 2 as published by the Free Software Foundation. | |
11 | * | |
12 | * The above named program is distributed in the hope that it will be | |
13 | * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | * General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public | |
18 | * License along with this work; if not, write to the Free Software | |
19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. | |
20 | * | |
21 | * ========== Copyright Header End ============================================ | |
22 | */ | |
23 | #ifndef __pList_h | |
24 | #define __pList_h | |
25 | ||
26 | #include <assert.h> | |
27 | #include "List.h" | |
28 | using namespace std; | |
29 | ||
30 | /** @file pList.h | |
31 | * The file pList.h contains "private" code for methods relating to the | |
32 | * List class. | |
33 | * This file is considered private to the class implementation and not | |
34 | * part of its interface. This code is in a header file just to | |
35 | * allow the template class to be packaged up into a library. | |
36 | */ | |
37 | ||
38 | template<class T> List<T>::List() | |
39 | { | |
40 | head = NULL; | |
41 | tail = NULL; | |
42 | length = 0; | |
43 | } | |
44 | ||
45 | template<class T> List<T>::~List() | |
46 | { | |
47 | while (removeHead() != NULL) | |
48 | ; | |
49 | assert(head == NULL); | |
50 | assert(tail == NULL); | |
51 | assert(length == 0); | |
52 | } | |
53 | ||
54 | template<class T> inline Link<T> *List<T>::addHead(T *newItem) | |
55 | { | |
56 | assert(newItem != NULL); | |
57 | Link<T> *link = new Link<T>(newItem, head, NULL); | |
58 | if (head) | |
59 | head->prev = link; | |
60 | else | |
61 | tail = link; | |
62 | head = link; | |
63 | length++; | |
64 | return link; | |
65 | } | |
66 | ||
67 | template<class T> inline Link<T> *List<T>::addTail(T *newItem) | |
68 | { | |
69 | assert(newItem != NULL); | |
70 | Link<T> *link = new Link<T>(newItem, NULL, tail); | |
71 | if (tail) | |
72 | tail->next = link; | |
73 | else | |
74 | head = link; | |
75 | tail = link; | |
76 | length++; | |
77 | return link; | |
78 | } | |
79 | ||
80 | template<class T> inline Link<T> *List<T>::addBefore (Link<T> *link, | |
81 | T *newItem) | |
82 | { | |
83 | assert(link != NULL); | |
84 | assert(newItem != NULL); | |
85 | assert(length > 0); | |
86 | Link<T> *newLink = new Link<T>(newItem, link, link->prev); | |
87 | link->prev = newLink; | |
88 | if (head == link) | |
89 | head = newLink; | |
90 | else | |
91 | newLink->prev->next = newLink; | |
92 | length++; | |
93 | return newLink; | |
94 | } | |
95 | ||
96 | template<class T> inline Link<T> *List<T>::addAfter (Link<T> *link, | |
97 | T *newItem) | |
98 | { | |
99 | assert(link != NULL); | |
100 | assert(newItem != NULL); | |
101 | assert(length > 0); | |
102 | Link<T> *newLink = new Link<T>(newItem, link->next, link); | |
103 | link->next = newLink; | |
104 | if (tail == link) | |
105 | tail = newLink; | |
106 | else | |
107 | newLink->next->prev = newLink; | |
108 | length++; | |
109 | return newLink; | |
110 | } | |
111 | ||
112 | template<class T> inline T *List<T>::removeHead() | |
113 | { | |
114 | if (head) { | |
115 | assert(length > 0); | |
116 | Link<T> *link = head; | |
117 | T *item = head->item; | |
118 | head = head->next; | |
119 | if (head) | |
120 | head->prev = NULL; | |
121 | else | |
122 | tail = NULL; | |
123 | delete link; | |
124 | length--; | |
125 | return item; | |
126 | } | |
127 | else | |
128 | return NULL; | |
129 | } | |
130 | ||
131 | template<class T> inline T *List<T>::removeTail() | |
132 | { | |
133 | if (tail) { | |
134 | assert(length > 0); | |
135 | Link<T> *link = tail; | |
136 | T *item = tail->item; | |
137 | tail = tail->prev; | |
138 | if (tail) | |
139 | tail->next = NULL; | |
140 | else | |
141 | head = NULL; | |
142 | delete link; | |
143 | length--; | |
144 | return item; | |
145 | } | |
146 | else | |
147 | return NULL; | |
148 | } | |
149 | ||
150 | template<class T> inline T *List<T>::removeItem (Link<T> *link) | |
151 | { | |
152 | assert(link != NULL); | |
153 | ||
154 | if (link->prev) | |
155 | link->prev->next = link->next; | |
156 | else | |
157 | head = link->next; | |
158 | ||
159 | if (link->next) | |
160 | link->next->prev = link->prev; | |
161 | else | |
162 | tail = link->prev; | |
163 | ||
164 | T *item = link->item; | |
165 | delete link; | |
166 | assert(length > 0); | |
167 | length--; | |
168 | ||
169 | return item; | |
170 | } | |
171 | ||
172 | template<class T> void List<T>::printThis (ostream &os) | |
173 | { | |
174 | os << "List has length " << length << "\n"; | |
175 | ||
176 | // ListIterator<T> iter(this); | |
177 | // for (Link<T> *link = iter.getHeadLink(); | |
178 | // link != NULL; | |
179 | // link = iter.getNextLink()) { | |
180 | // cout << " "; | |
181 | // link->printThis(cout); | |
182 | // } | |
183 | } | |
184 | ||
185 | template<class T> inline T *ListIterator<T>::getNextItem () | |
186 | { | |
187 | if (link && link->next) { | |
188 | link = link->next; | |
189 | return link->item; | |
190 | } | |
191 | else | |
192 | return NULL; | |
193 | } | |
194 | ||
195 | template<class T> inline T *ListIterator<T>::getPrevItem () | |
196 | { | |
197 | if (link && link->prev) { | |
198 | link = link->prev; | |
199 | return link->item; | |
200 | } | |
201 | else | |
202 | return NULL; | |
203 | } | |
204 | ||
205 | template<class T> inline Link<T> *ListIterator<T>::getNextLink () | |
206 | { | |
207 | if (link && link->next) { | |
208 | link = link->next; | |
209 | return link; | |
210 | } | |
211 | else | |
212 | return NULL; | |
213 | } | |
214 | ||
215 | template<class T> inline Link<T> *ListIterator<T>::getPrevLink () | |
216 | { | |
217 | if (link && link->prev) { | |
218 | link = link->prev; | |
219 | return link; | |
220 | } | |
221 | else | |
222 | return NULL; | |
223 | } | |
224 | ||
225 | #endif |