/*------------------------------------------------------------------------* * programm : C O N T A I N . C P P * * date : 12.06.1992 * * created by : (fa) * * Projekt MEE * * * *------------------------------------------------------------------------*/ // // Contents ------------------------------------------------- // // Implementation for class Container // // Memeber functions: // // Container::Container // Container::~Container // Container::Add // Container::Insert // Container::Remove // Container::Delete // Container::Clear // Container::First // Container::Next // Container::Current // Container::Previous // Container::isEmpty // // End ------------------------------------------------------ // // Implementation Dependencies ------------------------------ #include #include "contain.hpp" // End ------------------------------------------------------ // Implementation ------------------------------------------- Container::Container(){ current=first=(Container_element*)NULL; itemsInContainer = NULL; } //----------------------------------------------------------- void Container::Insert(Element &q){ if(first==(Container_element*)NULL){ // 0 ... 1 element current=first=new Container_element(&q); // current->next = current; // current->previous = current; } else{ if((current->previous==current) & (current->next==current)){ Container_element *p=new Container_element(&q,current,current); current->next = p; // 1 ... 2 elements current->previous = p; // first = current = p; } else{ Container_element *p=new Container_element(&q,current->previous, current); current->previous->next = p; // 2 ... more elements current->previous = p; if(first==current) first=current=p; else current=p; } } itemsInContainer++; } //------------------------------------------------------------ void Container::Add( Element &q){ if(first != (Container_element *)NULL) First(); Insert(q); first=current->next; } //------------------------------------------------------------ Element* Container::Remove(){ Element *elm_tmp; if(itemsInContainer){ // ? container empty if(first==current) first=current->next; // shift right for first if((current->previous==current) & (current->next==current)){ elm_tmp=current->element; delete current; // removing last element current=first=(Container_element*)NULL; } else{ current->previous->next=current->next; current->next->previous=current->previous; Container_element *tmp=current; current=current->next; elm_tmp=tmp->element; delete tmp; } itemsInContainer--; return elm_tmp; } else return (Element*)NULL; } //------------------------------------------------------------ void Container::Delete(){ delete Remove(); } //------------------------------------------------------------ void Container::Clear(){ while(itemsInContainer) Delete(); } //------------------------------------------------------------ int Container::itemsNumber(){ return itemsInContainer; } //------------------------------------------------------------ Element * Container::First(){ current=first; return current->element; } //------------------------------------------------------------ Element * Container::Next(){ current=current->next; return current->element; } //------------------------------------------------------------ Element * Container::Current(){ return current->element; } //------------------------------------------------------------ Element * Container::Previous(){ current=current->previous; return current->element; } //------------------------------------------------------------ int Container::isEmpty(){ return ((itemsInContainer>0)?0:1); } //------------------------------------------------------------ int Container::isFirst(){ return ((first==current)?1:0); } //------------------------------------------------------------ Container::~Container(){ while(itemsInContainer) Remove(); } // End of implementation -------------------------------------