00001 //* Módulo : xylist.h 00002 // _Desc_ : Declara templates _list_elem, XYListIterator e XYList. 00003 // _Autores_ : Renato Borges e Paulo Mattos. 00004 // _Data_ : 15 fev 96. 00005 00006 00007 #ifndef __XYLIST_H 00008 #define __XYLIST_H 00009 00010 template <class T> class XYListIterator; 00011 00012 //* _list_elem 00013 // Elemento da lista 00014 template <class T> struct _list_elem 00015 { 00016 //* Atributos 00017 _list_elem<T>* next; 00018 T* data; 00019 00020 //* Construtor 00021 _list_elem (T* d, _list_elem<T>* n) 00022 { 00023 data = d; 00024 next = n; 00025 } 00026 }; 00027 00028 //* XYList 00029 // Cria uma lista de tipos template 00030 template <class T> class XYList 00031 { 00032 public: 00033 00034 //* Construtores e Destrutor 00035 //* Cria lista vazia 00036 XYList () 00037 { 00038 _first = 0; 00039 } 00040 00041 //* Destrutor desaloca lista 00042 ~XYList () 00043 { 00044 removeAll(); 00045 } 00046 00047 //* Manipulação da Lista 00048 //* Reseta lista 00049 void Reset () 00050 { 00051 _first = 0; 00052 } 00053 00054 //* Insere elemento no inicio 00055 int insert (T* d) 00056 { 00057 _list_elem<T>* e = new _list_elem<T> (d, _first); 00058 00059 if (!e) return 0; 00060 _first = e; 00061 return 1; 00062 } 00063 00064 //* Insere elemento no inicio 00065 int insertBegin (T* d) 00066 { 00067 return insert(d); 00068 } 00069 00070 //* Insere elemento no final 00071 int insertEnd (T* d) 00072 { 00073 _list_elem<T>* e = new _list_elem<T> (d, 0); 00074 if (!e) return 0; 00075 00076 _list_elem<T>* p = _first; 00077 00078 while (p && p -> next) 00079 p = p -> next; 00080 00081 if (p) 00082 p -> next = e; 00083 else 00084 _first = e; 00085 00086 return 1; 00087 } 00088 00089 //* Remove elemento da lista 00090 int remove(T* d) 00091 { 00092 _list_elem<T> *ant = 0; 00093 _list_elem<T> *p = _first; 00094 00095 while (p) 00096 { 00097 if (p -> data == d) 00098 { 00099 if (!ant) 00100 _first = p -> next; 00101 else 00102 ant -> next = p -> next; 00103 00104 delete p; 00105 return 1; 00106 } 00107 00108 ant = p; 00109 p = p -> next; 00110 } 00111 00112 return 0; 00113 } 00114 00115 //* Remove todos os elementos da lista 00116 void removeAll(void) 00117 { 00118 _list_elem<T> *p = _first; 00119 00120 while (p) 00121 { 00122 _list_elem<T> *prox = p -> next; 00123 delete p; 00124 p = prox; 00125 } 00126 00127 _first = 0; 00128 } 00129 00130 private: 00131 00132 friend class XYListIterator<T>; 00133 _list_elem<T>* _first; 00134 }; 00135 00136 //* XYListIterator 00137 // Iterator p/ lista tipo template 00138 template <class T> class XYListIterator 00139 { 00140 public: 00141 00142 //* Construtor 00143 //* Constroi iterator baseado em uma lista 00144 XYListIterator(const XYList<T>* l) 00145 { 00146 _list = l; 00147 _current = l -> _first; 00148 } 00149 00150 //* Manipulação da Lista 00151 //* Volta ao inicio da lista 00152 void Reset () 00153 { 00154 _current = _list -> _first; 00155 } 00156 00157 //* Próximo elemento da lista 00158 T* Next () 00159 { 00160 T* result = _current -> data; 00161 _current = _current -> next; 00162 return result; 00163 } 00164 00165 //* Testa se lista chegou ao fim 00166 int End () 00167 { 00168 return _current == 0; 00169 } 00170 00171 //* Retorna elemento i da lista [0 a N-1] 00172 T* operator [](int n) 00173 { 00174 _list_elem<T>* p = _list->_first; 00175 for (int i = 0;;p = p->next,i++) 00176 { 00177 if ( p == 0 ) // Acabou lista ? 00178 return 0; 00179 if ( i == n ) // Chegou no elemento ? 00180 return p->data; 00181 } 00182 } 00183 00184 //* Verifica se elemento pertence a lista 00185 xybool Contains (const T *d) 00186 { 00187 _list_elem<T>* p = _list->_first; 00188 for (;p;p = p->next) 00189 { 00190 if ( d == p->data ) 00191 return xytrue; 00192 } 00193 00194 return xyfalse; 00195 } 00196 00197 private: 00198 00199 const XYList<T>* _list; 00200 _list_elem<T>* _current; 00201 }; 00202 00203 // Tipos utilizados em lua 00204 class XYMask; 00205 class XYText; 00206 class XYAxis; 00207 class XYMarker; 00208 class XYSlice; 00209 class XYObject; 00210 typedef XYList<XYMask> XYList_XYMask; 00211 typedef XYListIterator<XYMask> XYListIterator_XYMask; 00212 typedef XYList<XYText> XYList_XYText; 00213 typedef XYListIterator<XYText> XYListIterator_XYText; 00214 typedef XYList<XYAxis> XYList_XYAxis; 00215 typedef XYListIterator<XYAxis> XYListIterator_XYAxis; 00216 typedef XYList<XYMarker> XYList_XYMarker; 00217 typedef XYListIterator<XYMarker> XYListIterator_XYMarker; 00218 typedef XYList<XYSlice> XYList_XYSlice; 00219 typedef XYListIterator<XYSlice> XYListIterator_XYSlice; 00220 typedef XYList<XYObject> XYList_XYObject; 00221 typedef XYListIterator<XYObject> XYListIterator_XYObject; 00222 00223 00224 #endif 00225