Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

/magnus/back_end/Todd-Coxeter/include/EasyList.h

Go to the documentation of this file.
00001 /*
00002  *   $Id:
00003  */
00004 
00005 // Copyright (C) 1997 The New York Group Theory Cooperative
00006 // See magnus/doc/COPYRIGHT for the full notice.
00007 
00008 // Contents: Definition and implementation of EasyList and EasyCell
00009 // template classes.
00010 //
00011 // Principal Authors: Alexei Myasnikov
00012 //
00013 // Status: Useable.
00014 //
00015 // Revision History:
00016 //
00017 // Special Notes:
00018 //
00019 
00020 #ifndef _EYASY_LIST_H_
00021 #define _EYASY_LIST_H_
00022 
00023 #include "Cell.h"
00024 
00025 template <class T> class EasyList;
00026 
00027 
00028 //---------------------- class EasyCell ----------------------------------
00029 
00030 template <class T> class EasyCell {
00031 public:
00032   // Constructors :
00033   EasyCell(const T& t): theContent(t),theNext(NULL){}
00034   EasyCell(const T& t, EasyCell* c): theContent(t),theNext(c){}
00035   
00036   EasyCell* next() const {return theNext;}
00037   // Returns next element, or NULL.
00038   const T& content()const {return theContent;}
00039   //Returns the value of content
00040   void setContent(const T& t) { theContent = t;}
00041   //Sets the value of content
00042   void setNext(EasyCell* n) { theNext = n;}
00043   // Sets pointer to next cell. 
00044 private:
00045   EasyCell(const EasyCell&);
00046   // Copy constructor is prohibited
00047 
00048   friend class EasyList<T>;
00049   T theContent;
00050   T& ref() {return theContent;}
00051   EasyCell* theNext;
00052 };
00053 
00054 //---------------------- class EasyList ----------------------------------
00055 
00056 template <class T> class EasyList {
00057 
00058 public:
00059   //Constructors:
00060   EasyList( ) : root(NULL), last(NULL), current(NULL),end_was_red(false) { }
00061 
00062   EasyList( const T& t ) {
00063     current = root = last = new EasyCell<T>(t);
00064   }
00065 
00066   // copy constructor 
00067   EasyList( const EasyList& lr );
00068   
00069   ~EasyList( ) {
00070     EasyCell<T>* r;
00071     while ( root ) {
00072       r = root->next();
00073       delete root;
00074       root = r;
00075     }
00076   }
00077   //--------------------------------------------------
00078   // Fast methods :
00079   //--------------------------------------------------
00080   bool isFirst() const { 
00081     if (!root) return false;
00082     return root == current;
00083   }
00084   // True if current points to the first element.
00085   // false, if empty list
00086 
00087   bool isLast() const { 
00088     if (!last) return false;
00089     return last == current;
00090   }
00091   // True if current points to the last element.
00092   // false, if empty list
00093   
00094   bool gotEnd() const { 
00095     return end_was_red || current == NULL;
00096   }
00097   // True if list is empty, or if last element was red by 
00098   // getCurrent() or value() methods.
00099   // After changing of current position gotEnd() will be reseted to false
00100 
00101   EasyCell<T>* getLast() const { 
00102     if (!last)
00103       error ("List is empty.");
00104     return last;
00105   }
00106   // Returns a pointer on the last cell in the list
00107 
00108   EasyCell<T>* getFirst() const { 
00109     if (!root)
00110       error ("List is empty.");
00111     return root;
00112   }
00113   // Returns a pointer on the first cell in the list
00114   
00115   EasyCell<T>* getCurrent()const;
00116   // Returns a pointer on the current cell in the list
00117   // Pseudo constant. Actualy it can change the return value of 
00118   // gotEnd() method
00119 
00120   const T& value()const { return getCurrent()->content();}
00121   // Returns a value of the contents of current cell  
00122 
00123   bool setCurrent(EasyCell<T>* setpoint);
00124   // Returns true and sets current position in the list, pointing
00125   // to the setpoint. False if empty list  
00126 
00127   void setContent(T setcont){ getCurrent()->ref() = setcont; }
00128   // Sets value ov content of the current cell
00129   
00130   bool setToFirst() {return setCurrent(root);}
00131   // False if empty list, if not
00132   // sets current to the first cell
00133 
00134   bool setToLast() {return setCurrent(last);}
00135   // False if empty list, if not
00136   // sets current to the last cell
00137   
00138   bool isEmpty() const {return !root;}
00139   // True if empty list
00140 
00141   bool next();
00142   // false, if at the end of list. Moves current to the next cell.
00143 
00144   //--------------------------------------------------
00145   // Methods, adding elements
00146   //--------------------------------------------------
00147   void prepend(const T&);
00148   // add a new element at the begining of the list
00149   void append(const T&);
00150   // add a new element to the end of the list
00151    
00152   void insertAfter(const T&);
00153   // Inserts element after current, if list is emty
00154   // just adds this element
00155   void insertBefore(const T&);
00156   // Inserts element before current, if list is emty
00157   // just adds this element
00158   
00159   //-----------------------------------------------
00160   // Methods deleting elements
00161   //-----------------------------------------------
00162   bool deleteFirst();
00163   // Deletes first element of the list
00164   bool deleteCurrent();
00165   // Deletes current element of the list
00166   // If current == last, then it will be time consuming
00167   bool deleteAfter();
00168   // Deletes after current
00169 
00170   //---------------------------------------
00171   // Time consuming methods
00172   //---------------------------------------
00173   bool setCurrentToIndex(int i);
00174   // set current to the ith cell in the list
00175   // If i > then set to the end, and returns false.
00176 
00177   bool search(const T&,EasyCell<T>** ) const;
00178   // Set the second argument to the pointer on a cell with data equal to
00179   // first argument. NULL if can`t find.
00180   bool deleteLast();
00181   // Delete the last cell in the list
00182 
00183   //---------------------------------------
00184   // operators
00185   //---------------------------------------
00186   bool operator == ( const EasyList& lr ) const;
00187 
00188   friend ostream& operator << ( ostream& ostr, const EasyList& ls ) 
00189   {
00190     ls.printOn(ostr);
00191     return ostr;
00192   }
00193 
00194   friend ostream& operator < ( ostream& ostr, const EasyList& cr )
00195   {
00196     error("IPC tools are not emplemented for EasyList");
00197     return ostr;
00198   } 
00199 
00200   friend istream& operator > ( istream& istr, EasyList& cr )
00201   {
00202     error("IPC tools are not emplemented for EasyList");
00203     return istr;
00204   } 
00205   
00206 private:
00207   void printOn(ostream& ostr=cout)const;
00208   // Data members:
00209 
00210   EasyCell<T>* root;
00211   // pointer to the first cell
00212   EasyCell<T>* last;
00213   // pointer to the last cell
00214   EasyCell<T>* current;
00215   // pointer to the current cell
00216   bool end_was_red;
00217 
00218 };
00219 
00220 #endif

Generated at Tue Jun 19 09:49:44 2001 for Magnus Classes by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001