Page principale | Hiérarchie des classes | Liste par ordre alphabétique | Liste des composants | Liste des fichiers | Composants | Déclarations | Pages associées

MyList.h

Aller à la documentation de ce fichier.
00001 /* ***********************************************************************************
00002         Writer:         Sebastien Bloc
00003         Copyright:      2004
00004         eMail:          sebastien.bloc@free.fr
00005         URL:            http://mignonsoft.free.fr
00006 
00007         This program is free software; you can redistribute it and/or
00008         modify it under the terms of the GNU General Public License
00009         as published by the Free Software Foundation; either version 2
00010         of the License, or (at your option) any later version.
00011 
00012         This program is distributed in the hope that it will be useful,
00013         but WITHOUT ANY WARRANTY; without even the implied warranty of
00014         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015         GNU General Public License for more details.
00016         http://www.gnu.org/copyleft/gpl.html
00017 *********************************************************************************** */
00018 
00023 #ifndef INCLUDE_LISTE_GENERIQUE_H
00024 #define INCLUDE_LISTE_GENERIQUE_H
00025 /*
00026 #ifndef BOOL // gestion des cross-platform
00027         #define BOOL    short
00028         #define TRUE    1
00029         #define FALSE   0
00030 #endif
00031 */
00032 
00033 #include <stdio.h>
00034 #include <math.h>
00035 
00036 typedef enum _ETypeStart
00037 {
00038         current = 1,first,last,befor,next
00039 } ETypeStart; // pour le param1 de GoIndex
00040 
00041 #include "MyException.h"
00042 
00066 template <class T> class MyList
00067 {
00068         typedef struct List_
00069         {
00070           T *elem;                                                      // element de la liste 
00071           struct List_ *next,*befor;       // sucesseur 
00072           BOOL selfAlloc;                                       // le ptr doit il etre des alouées a la fin
00073         } *List, ElemList;
00074 
00075         private:
00076                 List    m_first;
00077                 List    m_last;
00078                 List    m_current;
00079                 int             m_nbElem;
00080                 int             m_numLastElem;
00081 //              int             (* FoncCompare)(T elem1,T elem2);       // fonction de comparaison
00082 
00083         public:
00084                 T *             m_elemNull;
00085 
00089                 MyList ()
00090                 {
00091                         m_first = NULL;
00092                         m_current = NULL;
00093                         m_last = NULL;
00094                         m_nbElem = 0;
00095                         m_numLastElem = -1;
00096                         //FoncCompare = DefaultFoncCompare;
00097                 }
00098 
00099 /*
00100                 MyList (int(*_FoncCompare)(T,T))        // fonction de comparaison
00101                 {
00102                         m_first = NULL;
00103                         m_current = NULL;
00104                         m_last = NULL;
00105                         m_nbElem = 0;
00106                         m_numLastElem = -1;
00107 
00108                         FoncNew = _FoncNew ? _FoncNew : DefaultFoncNew;
00109                 }
00110 */
00114                 ~MyList ()
00115                 {
00116                         SuprAll();
00117                 }
00118 
00122                 List NewElem (T *elem,BOOL selfAlloc)
00123                 {
00124                         List liste = new (ElemList);
00125                         if (!liste) throw MyExceptionMemory("MyList","NewElem",sizeof(ElemList));
00126                         liste->selfAlloc = selfAlloc;
00127                         if (selfAlloc) 
00128                         {
00129                                 liste->elem = new T;
00130                                 *liste->elem=*elem; //operator=(T &source) de la class appelé, pourais etre aussi le contructeur de copie
00131                         }
00132                         else liste->elem = elem;
00133                         if (!liste->elem) throw MyExceptionMemory("MyList","NewElem",sizeof(T));
00134                         liste->next=NULL;
00135                         liste->befor=NULL;
00136                         m_numLastElem = -1;
00137                         return liste;
00138                 }
00139 
00140                 List NewElem (T *elem) { return NewElem(elem,FALSE); }
00141 
00142                 List NewElem (T &elem) { return NewElem(&elem,TRUE); }
00143 /*
00144                         List liste = new (ElemList);
00145                         if (!liste) throw CMyExceptionMemory("MyList","NewElem",sizeof(ElemList));
00146                         liste->selfAlloc = TRUE;
00147                         liste->elem = new T;
00148                         if (!liste->elem) throw CMyExceptionMemory("MyList","NewElem",sizeof(T));
00149                         liste->next=NULL;
00150                         liste->befor=NULL;
00151                         *liste->elem=elem; //operator=(T &source) de la class appelé, pourais etre aussi le contructeur de copie
00152                         m_numLastElem = -1;
00153                         return liste;
00154                 }
00155 */
00159                 BOOL DelElem (List l)
00160                 {
00161                         if (l == NULL) return FALSE;
00162                         if (l->elem&&l->selfAlloc) delete l->elem;
00163                         delete l;
00164                         m_numLastElem = -1;
00165                         return TRUE;
00166                 }
00167 
00171                 BOOL AddFirst (T *elem) { return AddFirst(elem,FALSE); }
00172                 BOOL AddFirst (T &elem) { return AddFirst(&elem,TRUE); }
00173                 BOOL AddFirst (T *elem,BOOL selfAlloc)
00174                 {
00175                         List lNew;
00176                         lNew = NewElem (elem,selfAlloc);
00177                         if (lNew == NULL) return FALSE;
00178                         if (m_first == NULL) m_current = m_first = m_last = lNew;  
00179                         else 
00180                         {
00181                                 m_first->befor = lNew;
00182                                 lNew->next = m_first;
00183                                 m_first = lNew;
00184                                 m_current = lNew;
00185                         }
00186                         m_nbElem ++;
00187                         return TRUE;
00188                 }
00189 
00190 
00194                 BOOL AddLast (T *elem) { return AddLast(elem,FALSE); }
00195                 BOOL AddLast (T &elem) { return AddLast(&elem,TRUE); }
00196                 BOOL AddLast (T *elem,BOOL selfAlloc)
00197                 {
00198                         List lNew;
00199                         lNew = NewElem (elem,selfAlloc);
00200                         if (lNew == NULL) return FALSE;
00201                         if (m_last == NULL) m_current = m_first = m_last = lNew;  
00202                         else 
00203                         {
00204                                 m_last->next = lNew;
00205                                 lNew->befor = m_last;
00206                                 m_last = lNew;
00207                                 m_current = lNew;
00208                         }
00209                         m_nbElem ++;
00210                         return TRUE;
00211                 }
00212 
00216                 BOOL AddBeforCurrent (T *elem) { return AddBeforCurrent(elem,FALSE); }
00217                 BOOL AddBeforCurrent (T &elem) { return AddBeforCurrent(&elem,TRUE); }
00218                 BOOL AddBeforCurrent (T *elem,BOOL selfAlloc)
00219                 {
00220                         List lNew;
00221                         lNew = NewElem (elem,selfAlloc);
00222                         if (lNew == NULL) return FALSE;
00223                         if (m_current == NULL)  
00224                         {
00225                                 if (m_first == NULL && m_last == NULL) m_first = m_last = m_current = lNew;
00226                                 else return FALSE;
00227                         }
00228                         else 
00229                         {
00230                                 if (m_current == m_first)
00231                                 {
00232                                         m_first->befor = lNew;
00233                                         lNew->next = m_first;
00234                                         m_first = m_current = lNew;
00235                                 }
00236                                 else
00237                                 {
00238                                         lNew->befor = m_current->befor;
00239                                         m_current->befor->next = lNew;
00240                                         m_current->befor = lNew;
00241                                         lNew->next = m_current;
00242                                         m_current = lNew;
00243                                 }
00244                         }
00245                         m_nbElem ++;
00246                         return TRUE;
00247                 }
00248 
00252                 BOOL AddAfterCurrent (T *elem) { return AddAfterCurrent(elem,FALSE); }
00253                 BOOL AddAfterCurrent (T &elem) { return AddAfterCurrent(&elem,TRUE); }
00254                 BOOL AddAfterCurrent (T *elem,BOOL selfAlloc)
00255                 {
00256                         List lNew;
00257                         lNew = NewElem (elem,selfAlloc);
00258                         if (lNew == NULL) return FALSE;
00259                         if (m_current == NULL) 
00260                         {
00261                                 if (m_first == NULL && m_last == NULL) m_first = m_last = m_current = lNew;
00262                                 else return FALSE;
00263                         }
00264                         else 
00265                         {
00266                                 if (m_current == m_last)
00267                                 {
00268                                         m_last->next = lNew;
00269                                         lNew->befor  = m_last;
00270                                         m_last = m_current = lNew;
00271                                 }
00272                                 else
00273                                 {
00274                                         lNew->next = m_current->next;
00275                                         m_current->next->befor = lNew;
00276                                         m_current->next = lNew;
00277                                         lNew->befor = m_current;
00278                                         m_current = lNew;
00279                                 }
00280                         }
00281                         m_nbElem ++;
00282                         return TRUE;
00283                 }
00284 
00288                 BOOL SuprFirst ()
00289                 {
00290                         if (m_first == NULL) return FALSE;
00291                         List temp;
00292 
00293                         temp = m_first->next;
00294                         DelElem(m_first);
00295                         if (temp == NULL) m_current = m_first = m_last = NULL;
00296                         else
00297                         {
00298                                 temp->befor = NULL;
00299                                 m_current = m_first = temp;
00300                         }
00301                         m_nbElem --;
00302                         return TRUE;
00303                 }
00304 
00308                 BOOL SuprLast ()
00309                 {
00310                         if (m_last == NULL) return FALSE;
00311                         List temp;
00312 
00313                         temp = m_last->befor;
00314                         DelElem(m_last);
00315                         if (temp == NULL) m_current = m_first = m_last = NULL;
00316                         else
00317                         {
00318                                 temp->next = NULL;
00319                                 m_current = m_last = temp;
00320                         }
00321                         m_nbElem --;
00322                         return TRUE;            
00323                 }
00324 
00328                 BOOL SuprCurrent ()
00329                 {
00330                         if (m_current == NULL) return FALSE;
00331                         List lNext,lBefor,lOld;
00332 
00333                         lNext = m_current->next;
00334                         lBefor = m_current->befor;
00335                         lOld = m_current;
00336 
00337                         if (lBefor == NULL && lNext == NULL)
00338                         {
00339                                 m_first = m_last = m_current = NULL;    
00340                         }
00341                         else
00342                         {
00343                                 if (lBefor != NULL) 
00344                                 {
00345                                         lBefor->next=lNext;
00346                                         if (lNext != NULL) 
00347                                         {
00348                                                 lNext->befor = lBefor;
00349                                                 m_current = lNext;
00350                                         }
00351                                         else m_last = m_current = lBefor;
00352                                 }
00353                                 else 
00354                                 {
00355                                         m_first = lNext;
00356                                         lNext->befor = NULL;
00357                                 }
00358                         }
00359                         DelElem(lOld);
00360                         m_nbElem --;
00361                         m_current = NULL;
00362                         return TRUE;
00363                 }
00364 
00368                 BOOL SuprAll ()
00369                 {
00370                         List temp;
00371                         for (GoFirst();More();m_current = temp) 
00372                         {
00373                                 temp = m_current->next;
00374                                 DelElem(m_current);
00375                         }
00376                         m_current = NULL;
00377                         m_first = NULL;
00378                         m_last = NULL;
00379                         m_nbElem = 0;
00380                         return TRUE;
00381                 }
00382 
00386                 BOOL Supr (int index)
00387                 {
00388                         if (!GoIndex(first,index)) return FALSE;
00389                         return SuprCurrent();
00390                 }
00391 
00395                 int GetNbElem()
00396                 {
00397                         return m_nbElem;
00398                 }
00399 
00403                 T * GetNewElem()
00404                 {
00405                         T elem;
00406                         AddLast(elem);
00407                         return GetElemPtr();
00408                 }
00409 
00417                 int GetIndex()
00418                 {
00419                         return m_numLastElem;
00420                 }
00421 
00425                 BOOL GoNext ()
00426                 {
00427                         if (m_current != NULL) m_current = m_current->next; 
00428                         if (m_current != NULL)
00429                         { 
00430                                 m_numLastElem = m_numLastElem == -1 ? -1 : m_numLastElem+1;
00431                                 return TRUE ;
00432                         }
00433                         else 
00434                         {
00435                                 m_numLastElem = -1;
00436                                 return FALSE;
00437                         }
00438                 }
00439 
00443                 BOOL GoBefor ()
00444                 {
00445                         if (m_current != NULL) m_current = m_current->befor;
00446                         if (m_current != NULL) 
00447                         {
00448                                 m_numLastElem = m_numLastElem == -1 ? -1 : m_numLastElem-1;
00449                                 return TRUE;
00450                         } 
00451                         else 
00452                         {
00453                                 m_numLastElem = -1;
00454                                 return FALSE;
00455                         }
00456                 }
00457 
00461                 BOOL GoFirst ()
00462                 {
00463                         m_current = m_first;
00464                         m_numLastElem = m_current != NULL ? 0 : -1;
00465                         return m_current != NULL ? TRUE : FALSE;                                
00466                 }
00467 
00468 
00472                 BOOL GoIndex (ETypeStart start,int nb)
00473                 {
00474                         BOOL isUp;
00475                         isUp = nb<0 ? FALSE : TRUE ; 
00476                         //char temp[128];
00477                         //sprintf(temp,"isUp: %d @nb:%d\n",isUp,nb);
00478                         //OutputDebugString(temp);
00479 
00480                         nb = labs(nb);
00481 
00482                         //sprintf(temp,"nb:%d\n",nb);
00483                         //OutputDebugString(temp);
00484 
00485                         switch (start)
00486                         {
00487                                 case current: break;
00488                                 case first: 
00489                                         if (m_numLastElem != -1) 
00490                                         {
00491                                                 nb = m_numLastElem-nb; 
00492                                                 isUp = nb<=0;
00493                                                 nb = labs(nb);
00494                                         }
00495                                         else 
00496                                         {
00497                                                 isUp = TRUE;  
00498                                                 GoFirst(); 
00499                                         }
00500                                         break;
00501                                 case last:
00502                                         if (m_numLastElem != -1) 
00503                                         {
00504                                                 nb = m_nbElem-m_numLastElem-nb; 
00505                                                 isUp = nb>=0;
00506                                                 nb = labs(nb);
00507                                         }
00508                                         else 
00509                                         {
00510                                                 isUp = FALSE;  
00511                                                 GoLast(); 
00512                                         }
00513                                         break;
00514                                 case befor: isUp = FALSE; break;
00515                                 case next:  isUp = TRUE;  break;
00516                                 default:
00517                                         return FALSE;
00518                         }
00519                         for (int i=0;i<nb;i++)
00520                         {
00521                                 if (More())
00522                                 {
00523                                         if (isUp) GoNext();
00524                                         else GoBefor();
00525                                 } else return FALSE;
00526                         }
00527                         return TRUE;
00528                 }
00529         
00533                 BOOL GoLast ()
00534                 {
00535                         m_current = m_last;
00536                         m_numLastElem = m_current != NULL ? m_nbElem -1 : -1;
00537                         return m_current ? TRUE : FALSE;
00538                 }
00539 
00540                 // ********************************************************************************
00541 
00542                 //inline operator T() const { return GetElem(); } marche pas
00543                 //inline operator T *() const { return GetElemPtr(); }
00544 
00556                 T & GetElem (int index=-1)
00557                 {
00558                         if (index!=-1) GoIndex(first,index);
00559                         if (!m_current) return *m_elemNull;
00560                         return *m_current->elem;
00561                 }
00562 
00574                 T * GetElemPtr (int index=-1)
00575                 {
00576                         if (index!=-1) GoIndex(first,index);
00577                         if (!m_current) return m_elemNull;
00578                         return m_current->elem;
00579                 }
00580 
00584                 BOOL More ()
00585                 {
00586                         return m_current ? TRUE : FALSE;
00587                 }               
00588 
00592                 BOOL IsLastOne()
00593                 {
00594                         return (m_current==m_last)? TRUE : FALSE;
00595                 }               
00596 
00600                 BOOL IsFirstOne()
00601                 {
00602                         return (m_current==m_first)? TRUE : FALSE;
00603                 }               
00604 
00610                 char * TestSubString(char *text)
00611                 {
00612                         return strstr(value,text); 
00613                 }
00614 
00618                 BOOL SaveData (FILE *fich)
00619                 {
00620                         if (fich == NULL) return FALSE;
00621 
00622                         fprintf(fich,"%d",m_nbElem);
00623                         for (GoFirst();More();GoNext())
00624                         {
00625                                 if (m_current->elem != NULL)
00626                                         if (!fwrite ((void *) m_current->elem,sizeof(T),1,fich)) return FALSE;
00627                         }
00628                         return TRUE;
00629                 }
00630 
00634                 BOOL LoadData (FILE *fich)
00635                 {
00636                         if (fich == NULL) return FALSE;
00637                         
00638                         int i,nb;
00639                         T elem;
00640                         fscanf(fich,"%d",&nb);
00641                         for (i=0;i<nb;i++)
00642                         {
00643                                 if (!fread ((void *) &elem,sizeof(T),1,fich))return FALSE;
00644                                 if (!AddAfterCurrent(elem)) return FALSE;
00645                         }
00646                         return TRUE;
00647                 }
00648 
00654                 void SetCompareFunction (int (* fonction)(T * elem1,T * elem2))
00655                 {
00656                         FoncCompare = fonction;
00657                 }
00658 /*
00659                 void SetNewFunction (T * (* fonction)(T * elem))
00660                 {
00661                         FoncNew = fonction;
00662                 }
00663 
00664                 void SetDeleteFunction (BOOL (* fonction)(T *elem))
00665                 {
00666                         FoncDelete = fonction;
00667                 }
00668 */
00669                 /* **********************************************************************************
00670                         list += elem;                   ==> AddLast
00671                         listDest = listSource;  ==> Copy d'une liste dans une autre
00672                         listDest += listSource; ==> Ajout d'une liste dans une autre
00673                         elem = list[12];                ==> Acced a l'elem 12 (13) vu que ca commance a 0
00674                         elem = list[-1];                ==> Acced au dernier elem
00675                         list = 0;                               ==> Se place a l'element indexé 0
00676                         list ++;                                ==> Se place a l'element suivant;
00677                         list --;                                ==> Se place a l'element d'avant;
00678                         lisr += 2;                              ==> Se place a 2 element plus loin
00679                         lisr -= 2;                              ==> Se place a 2 element avant
00680                 ********************************************************************************** */
00681 /*
00682                 void operator += (T &elem)
00683                 {
00684                         AddLast(elem);
00685                 }
00686 */
00687                 BOOL operator += (T *elem)
00688                 {
00689                         return AddLast(elem);
00690                 }
00691 
00692                 BOOL operator += (T elem)
00693                 {
00694                         return AddLast(elem);
00695                 }
00696         
00697                 BOOL operator = (int index)
00698                 {
00699                         if (index>=0) return GoIndex(first,index);
00700                         else return GoIndex(last,-index+1);
00701                 }
00702 
00703                 MyList<T> & operator = (MyList<T> &source)
00704                 {
00705                         SuprAll();
00706                         *this+= source;
00707                         return *this;
00708                 }
00709 
00710                 MyList<T> & operator += (MyList<T> &source)
00711                 {
00712                         T elem;
00713                         for (source=0;source.More();source++)
00714                         {
00715                                 elem = source.GetElem();
00716                                 *this += elem;
00717                         }
00718                         return *this;
00719                 }
00720 
00721                 BOOL operator++ (int)
00722                 {
00723                         return GoNext();
00724                 }
00725 
00726                 BOOL operator-- (int)
00727                 {
00728                         return GoBefor();
00729                 }
00730 
00731                 T * operator () (int num=-1)
00732                 {
00733                         return GetElemPtr(num);
00734                 }
00735 
00736                 T & operator [] (int num)
00737                 {
00738                         if (num<0)
00739                         {
00740                                 if (!GoIndex(last,-num)) return *m_elemNull;
00741                         }
00742                         else
00743                         {                       
00744                                 if (!GoIndex(first,num)) return *m_elemNull;
00745                         }
00746                         return GetElem();
00747                 }               
00748 };
00749 
00753 template <class T> class MyStack : public MyList<T>
00754 {
00755 public:
00756         MyStack() : MyList<T>() {}
00757 
00758         BOOL Push(T elem)
00759         {
00760                 return AddFirst(elem);
00761         }
00762 
00763         T Pop()
00764         {
00765                 if (GoFirst())
00766                 {
00767                         T elem = GetElem();
00768                         SuprFirst();
00769                         return elem;
00770                 }
00771                 return *m_elemNull;
00772         }
00773 
00774         T Peek()
00775         {
00776                 if (GoFirst()) return GetElem();
00777                 return *m_elemNull;
00778         }
00779 
00780         BOOL FrontPush(T elem)
00781         {
00782                 return AddLast(elem);
00783         }
00784 
00785         T FrontPop()
00786         {
00787                 if (GoLast())
00788                 {
00789                         T elem = GetElem();
00790                         SuprLast();
00791                         return elem;
00792                 }
00793                 return *m_elemNull;
00794         }
00795 
00796         T FrontPeek()
00797         {
00798                 if (GoLast()) return GetElem();
00799                 return *m_elemNull;
00800         }
00801 };
00802 
00803 // definition des fonctions d'allocation et des alloc par Default
00804 
00805 template <class T> int DefaultFoncCompare (T elem1,T elem2)
00806 {
00807         return memcmp((void *)elem1,(void *)elem2,sizeof(T));
00808 }
00809 
00810 /*
00811 template <class T> T * DefaultFoncNew (T *elemSource)
00812 {
00813         //T * elemDest = elemSource? new T(elemSource) : new T;
00814         T * elemDest = new T;
00815         *elemDest = elemSource;
00816         return elemDest;
00817 
00818         T * elemDest = new T;
00819         if (elemSource) memcpy((void *)elemDest,(void *)elemSource,sizeof(T));
00820         return elemDest;
00821 }
00822 
00823 template <class T> BOOL DefaultFoncDelete (T * elem)
00824 {
00825         delete elem;
00826         return TRUE;
00827 }
00828 
00829 // definition des fonctions d'allocation et des alloc dans les MyString
00830 
00831 #include "myString.h"
00832 
00833 MyString * MyString_FoncNew (MyString *elemSource);
00834 int MyString_FoncCompare (MyString * elem1,MyString * elem2);
00835 #define MyListString(name) MyList<MyString> name(MyString_FoncCompare,MyString_FoncNew,NULL);
00836 */
00837 
00838 #endif

Généré le Thu Apr 8 18:58:42 2004 pour SFC par doxygen 1.3.6