/* *_________________________________________________________________________* * POEMS: PARALLELIZABLE OPEN SOURCE EFFICIENT MULTIBODY SOFTWARE * * DESCRIPTION: SEE READ-ME * * FILE NAME: poemslist.h * * AUTHORS: See Author List * * GRANTS: See Grants List * * COPYRIGHT: (C) 2005 by Authors as listed in Author's List * * LICENSE: Please see License Agreement * * DOWNLOAD: Free at www.rpi.edu/~anderk5 * * ADMINISTRATOR: Prof. Kurt Anderson * * Computational Dynamics Lab * * Rensselaer Polytechnic Institute * * 110 8th St. Troy NY 12180 * * CONTACT: anderk5@rpi.edu * *_________________________________________________________________________*/ #ifndef _POEMSLIST_H_ #define _POEMSLIST_H_ #include #include using namespace std; template class ListElement{ public: ListElement* prev; ListElement* next; T* value; ListElement(); ListElement(T* v); ~ListElement(); }; template class List { int numelements; ListElement* head; ListElement* tail; public: List(); ~List(); int GetNumElements(); ListElement* GetHeadElement(); ListElement* GetTailElement(); void Remove(ListElement* ele); ListElement* Append(S* v); ListElement* Prepend(S* v); S** CreateArray(); S* operator()(int id); void Append(List * listToAppend); void DeleteValues(); void RemoveElementAndDeleteValue(ListElement* ele); void PrintList(); }; // // ListElement // template ListElement::ListElement(){ next = prev = 0; value = 0; } template ListElement::ListElement(T* v){ next = prev = 0; value = v; } template ListElement::~ListElement(){ } // // List // template List::List(){ head = tail = 0; numelements = 0; } template List::~List(){ // delete all list elements while(numelements) Remove(tail); } template void List::Append(List * listToAppend) { tail->next = listToAppend->head; listToAppend->head->prev = tail; tail = listToAppend->tail; } template int List::GetNumElements(){ return numelements; } template ListElement* List::GetHeadElement(){ return head; } template ListElement* List::GetTailElement(){ return tail; } template void List::Remove(ListElement* ele){ if(!ele){ cerr << "ERROR: ListElement to be removed not defined" << endl; exit(0); } if(!numelements){ cerr << "ERROR: List is empty" << endl; exit(0); } if(ele != head) ele->prev->next = ele->next; else head = ele->next; if(ele != tail) ele->next->prev = ele->prev; else tail = ele->prev; numelements--; if(ele) delete ele; } template ListElement* List::Append(S* v){ if(!v){ cerr << "ERROR: cannot add null Body to list" << endl; exit(0); } numelements++; ListElement* ele = new ListElement(v); if(numelements==1) head = tail = ele; else{ /* tail->next = ele; ele->prev = tail; tail = ele;*/ ele->prev = tail; tail = ele; ele->prev->next = ele; } return ele; } template ListElement* List::Prepend(S* v){ if(!v){ cerr << "ERROR: cannot add null Body to list" << endl; exit(0); } numelements++; ListElement* ele = new ListElement(v); if(numelements==1) head = tail = ele; else{ ele->next = head; head = ele; ele->next->prev = ele; } return ele; } template S** List::CreateArray(){ S** array = new S* [numelements]; ListElement* ele = head; for(int i=0;ele != 0;i++){ array[i] = ele->value; ele = ele->next; } return array; } template S* List::operator()(int id){ if(id >= numelements){ cerr << "ERROR: subscript out of bounds" << endl; exit(0); } ListElement* ele = head; for(int i=0;inext; } return ele->value; } template void List::DeleteValues(){ while(numelements) RemoveElementAndDeleteValue(tail); } template void List::RemoveElementAndDeleteValue(ListElement* ele){ S* v = ele->value; Remove(ele); delete v; } template void List::PrintList(){ cout<<"Printing List "<* ele = head; cout<<*(ele->value)<<" "; ele = ele->next; for(int k =2; kvalue)<<" "; ele = ele->next; } cout<<*(ele->value)<