Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / sam / devices / common / regdef / include / pList.h
/*
* ========== Copyright Header Begin ==========================================
*
* OpenSPARC T2 Processor File: pList.h
* Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES.
*
* The above named program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License version 2 as published by the Free Software Foundation.
*
* The above named program is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this work; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*
* ========== Copyright Header End ============================================
*/
#ifndef __pList_h
#define __pList_h
#include <assert.h>
#include "List.h"
using namespace std;
/** @file pList.h
* The file pList.h contains "private" code for methods relating to the
* List class.
* This file is considered private to the class implementation and not
* part of its interface. This code is in a header file just to
* allow the template class to be packaged up into a library.
*/
template<class T> List<T>::List()
{
head = NULL;
tail = NULL;
length = 0;
}
template<class T> List<T>::~List()
{
while (removeHead() != NULL)
;
assert(head == NULL);
assert(tail == NULL);
assert(length == 0);
}
template<class T> inline Link<T> *List<T>::addHead(T *newItem)
{
assert(newItem != NULL);
Link<T> *link = new Link<T>(newItem, head, NULL);
if (head)
head->prev = link;
else
tail = link;
head = link;
length++;
return link;
}
template<class T> inline Link<T> *List<T>::addTail(T *newItem)
{
assert(newItem != NULL);
Link<T> *link = new Link<T>(newItem, NULL, tail);
if (tail)
tail->next = link;
else
head = link;
tail = link;
length++;
return link;
}
template<class T> inline Link<T> *List<T>::addBefore (Link<T> *link,
T *newItem)
{
assert(link != NULL);
assert(newItem != NULL);
assert(length > 0);
Link<T> *newLink = new Link<T>(newItem, link, link->prev);
link->prev = newLink;
if (head == link)
head = newLink;
else
newLink->prev->next = newLink;
length++;
return newLink;
}
template<class T> inline Link<T> *List<T>::addAfter (Link<T> *link,
T *newItem)
{
assert(link != NULL);
assert(newItem != NULL);
assert(length > 0);
Link<T> *newLink = new Link<T>(newItem, link->next, link);
link->next = newLink;
if (tail == link)
tail = newLink;
else
newLink->next->prev = newLink;
length++;
return newLink;
}
template<class T> inline T *List<T>::removeHead()
{
if (head) {
assert(length > 0);
Link<T> *link = head;
T *item = head->item;
head = head->next;
if (head)
head->prev = NULL;
else
tail = NULL;
delete link;
length--;
return item;
}
else
return NULL;
}
template<class T> inline T *List<T>::removeTail()
{
if (tail) {
assert(length > 0);
Link<T> *link = tail;
T *item = tail->item;
tail = tail->prev;
if (tail)
tail->next = NULL;
else
head = NULL;
delete link;
length--;
return item;
}
else
return NULL;
}
template<class T> inline T *List<T>::removeItem (Link<T> *link)
{
assert(link != NULL);
if (link->prev)
link->prev->next = link->next;
else
head = link->next;
if (link->next)
link->next->prev = link->prev;
else
tail = link->prev;
T *item = link->item;
delete link;
assert(length > 0);
length--;
return item;
}
template<class T> void List<T>::printThis (ostream &os)
{
os << "List has length " << length << "\n";
// ListIterator<T> iter(this);
// for (Link<T> *link = iter.getHeadLink();
// link != NULL;
// link = iter.getNextLink()) {
// cout << " ";
// link->printThis(cout);
// }
}
template<class T> inline T *ListIterator<T>::getNextItem ()
{
if (link && link->next) {
link = link->next;
return link->item;
}
else
return NULL;
}
template<class T> inline T *ListIterator<T>::getPrevItem ()
{
if (link && link->prev) {
link = link->prev;
return link->item;
}
else
return NULL;
}
template<class T> inline Link<T> *ListIterator<T>::getNextLink ()
{
if (link && link->next) {
link = link->next;
return link;
}
else
return NULL;
}
template<class T> inline Link<T> *ListIterator<T>::getPrevLink ()
{
if (link && link->prev) {
link = link->prev;
return link;
}
else
return NULL;
}
#endif