Horus Doc || C++ Reference || Class Overview   Pixels   Images   Detector   Geometry   Registry || Doxygen's quick Index  

cdlist.h

00001 // cdlist.h : interface of a double list 
00002 //
00003 // Although some compliers (such as Visual C++) support List data type, 
00004 // this class is still been developed to made the whole system can be used
00005 // under those compliers which don't have such kind of class.
00006 // Furthermore, this class has been developped to meet our specific requirement.
00007 // It is faster than those versions designed for general purpose;
00008 //
00009 //*****************************************************************************
00010 // 
00011 //                  MPEG Developing Classes
00012 //
00013 // Copyright (C) 1998, Vision and Neural Networks Laboratory, Computer Science 
00014 // Department, Wayne State University, All Rights Reserved; 
00015 //
00016 //                  Disclaimer of Warranty
00017 //
00018 //
00019 // MPEG Developing Classes, both binary and source (hereafter, Software)
00020 // is copyrighted by Vision and Neural Networks Laboratory, Computer Science 
00021 // Department, Wayne State University (WSU), and ownership remains with WSU.
00022 //
00023 // Permission is hereby granted, free of charge, to any person obtaining
00024 // a copy of this software and associated documentation files to use, copy,
00025 // and distribute the software, provided that no charge is associated with 
00026 // such copies and that the copyright notice and this statement appears on 
00027 // all copies.
00028 //
00029 // THE SOFTWARE IS AVAILABLE TO THE USER ON AN "AS IS" BASIS, WITHOUT WARRANTY
00030 // OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING, WITHOUT LIMITATION, THE 
00031 // IMPLIED WARRANTIES OR MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
00032 // IN NO EVENT SHALL THE COPYRIGHT-HOLDER BE LIABLE FOR ANY CLAIMS, DAMAGES, OR
00033 // OTHER LIABILITIES OF ANY KIND WHATSOEVER ARISING FROM, OUT OF OR IN CONNECTION
00034 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. THIS 
00035 // DISCLAIMAER OF WARRANTY EXTENDS TO THE USER OF THIS SOFTWARE. ALSO THE WSU 
00036 // DOES NOT REPRESENT OR WARRANT THAT THE SOFTWARE FURNISHED HEREUNDER ARE FREE OF
00037 // INFRINGMENT OF ANY THIRD-PARTY PATENTS.
00038 //
00039 // Many patents of MPEG-1 and MPEG-2 are general enough such that commercial
00040 // implementations, including shareware, are unavoidable subject to royalty fees to
00041 // patent holders, regardless of implementation design.
00042 //
00043 //
00044 // Vision and Neural Networks Laboratory,
00045 // Computer Science Department,
00046 // Wayne State University,
00047 // Detroit, MI 48202.
00048 // Dongge Li(dil@cs.wayne.edu) and Ishwar K. Sethi(sethi@cs.wayne.edu).
00049 //
00051 
00052 #if !defined CDLIST_H
00053 #define CDLIST_H
00054 
00055 #include <stdio.h>
00056 #include "datatype.h"
00057 
00058 template <class CItem, int iMaxItemNum> 
00059 class CDList
00060 {
00061 // constructor and destructor
00062 public:
00063     CDList(void);
00064     ~CDList();
00065 
00066 // attributes
00067 protected:
00068     int iHeadPointer;
00069     int iTailPointer;
00070     int iFreePointer;
00071     CItem *pItemList;
00072     int *piPrevious;
00073     int *piNext;
00074     int iCurPointer;
00075 
00076 // operation
00077 public:
00078     int iStatus; //the value of iStatus has no effect on CDList, only used to indicate current status of CDList;
00079     void AddHead(const CItem &NewItem);
00080     void AddTail(const CItem &NewItem);
00081     CItem RemoveHead();
00082     CItem RemoveTail();
00083     CItem ShowHead() {return pItemList[iHeadPointer];};
00084     CItem ShowTail() {return pItemList[iTailPointer];};
00085     bool IsEmpty() { return (iHeadPointer==iFreePointer)? true:false;};
00086     int GetItemNum();
00087     void RemoveAll();
00088 // must ensure the list is not empty before use the following functions;
00089     void ToHead() {iCurPointer=iHeadPointer;}; // initialize iCurPointer;
00090     void ToTail() {iCurPointer=iTailPointer;}; // initialize iCurPointer;
00091     inline bool ToNext();
00092     inline bool ToPrevious();
00093     CItem ShowItem() {return pItemList[iCurPointer];};
00094 };
00095 
00096 // constructor
00097 template <class CItem, int iMaxItemNum>
00098 CDList<CItem, iMaxItemNum>::CDList(void)
00099 {
00100     pItemList=new CItem [iMaxItemNum];
00101     if (pItemList==NULL)
00102     {
00103         iStatus=MEM_ERROR;
00104         return;
00105     }
00106     piPrevious=new int [iMaxItemNum];
00107     if (piPrevious==NULL)
00108     {
00109         iStatus=MEM_ERROR;
00110         delete [] pItemList;
00111         return;
00112     }
00113     piNext=new int [iMaxItemNum];
00114     if (piNext==NULL)
00115     {
00116         iStatus=MEM_ERROR;
00117         delete [] piPrevious;
00118         delete [] pItemList;
00119         return;
00120     }
00121     iHeadPointer=iTailPointer=iFreePointer=0;  //point to the same place
00122     for (int i=0; i<iMaxItemNum-1; i++)
00123         piNext[i]=i+1;
00124     piNext[iMaxItemNum-1]=0;  // form a circle
00125     iStatus=MDC_SUCCESS;
00126 }
00127 
00128 // destructor
00129 template <class CItem, int iMaxItemNum>
00130 CDList<CItem, iMaxItemNum>::~CDList()
00131 {
00132     if (pItemList!=NULL)
00133         delete [] pItemList;
00134     if (piPrevious!=NULL)
00135         delete [] piPrevious;
00136     if (piNext!=NULL)
00137         delete [] piNext;
00138 }
00139 
00140 // operations
00141 
00142 template <class CItem, int iMaxItemNum>
00143 void CDList<CItem, iMaxItemNum>::AddHead(const CItem &NewItem)
00144 {
00145     int iPosition;
00146 
00147     //change free list
00148     iPosition=iFreePointer;   // get the free position
00149     iFreePointer=piNext[iFreePointer];  // change the free list
00150     //set this new item 
00151     pItemList[iPosition]=NewItem;  
00152     piPrevious[iPosition]=iPosition;  // point to itself  
00153     piNext[iPosition]=iHeadPointer;
00154     //change head pointer
00155     piPrevious[iHeadPointer]=iPosition;
00156     iHeadPointer=iPosition;
00157 }
00158 
00159 template <class CItem, int iMaxItemNum>
00160 void CDList<CItem, iMaxItemNum>::AddTail(const CItem &NewItem)
00161 {
00162     int iPosition;
00163 
00164     iPosition=iFreePointer;   // get the free position
00165     iFreePointer=piNext[iFreePointer];  // change the free list
00166     pItemList[iPosition]=NewItem;  
00167     piPrevious[iPosition]=iTailPointer;  
00168     piNext[iPosition]=iPosition;  // point to itself
00169     piNext[iTailPointer]=iPosition;
00170     iTailPointer=iPosition;
00171 }
00172 
00173 template <class CItem, int iMaxItemNum>
00174 CItem CDList<CItem, iMaxItemNum>::RemoveHead()
00175 {
00176     int iPosition;
00177 
00178     iPosition=iHeadPointer;
00179     iHeadPointer=piNext[iPosition];
00180     piPrevious[iHeadPointer]=iHeadPointer;  // point to itself
00181     piNext[iPosition]=iFreePointer;
00182     iFreePointer=iPosition;
00183     return pItemList[iPosition];
00184 }
00185 
00186 template <class CItem, int iMaxItemNum>
00187 CItem CDList<CItem, iMaxItemNum>::RemoveTail()
00188 {
00189     int iPosition;
00190 
00191     iPosition=iTailPointer;
00192     iTailPointer=piPrevious[iPosition];
00193     piNext[iTailPointer]=iTailPointer; // point to itself
00194     piNext[iPosition]=iFreePointer;
00195     iFreePointer=iPosition;
00196     return pItemList[iPosition];
00197 }
00198 
00199 template <class CItem, int iMaxItemNum>
00200 int CDList<CItem, iMaxItemNum>::GetItemNum()
00201 {
00202     int iPosition;
00203     int iCount;
00204 
00205     if (IsEmpty())
00206         return 0;
00207     iPosition=iHeadPointer;
00208     for (iCount=1; iCount<=iMaxItemNum+1; iCount++)
00209     {
00210         if (piNext[iPosition]==iPosition)
00211             break;
00212         iPosition=piNext[iPosition];
00213     }
00214     return iCount;  // if i=iMaxItemNum+1, there is an error;
00215 }
00216 
00217 template <class CItem, int iMaxItemNum>
00218 void CDList<CItem, iMaxItemNum>::RemoveAll()
00219 {
00220     int iCount;
00221 
00222     iHeadPointer=iTailPointer=iFreePointer=0;  //point to the same place
00223     for (iCount=0; iCount<iMaxItemNum-1; iCount++)
00224         piNext[iCount]=iCount+1;
00225     piNext[iMaxItemNum-1]=0;  // form a circle
00226 }
00227 
00228 template <class CItem, int iMaxItemNum>
00229 inline bool CDList<CItem, iMaxItemNum>::ToNext()
00230 {
00231     if (piNext[iCurPointer]==iCurPointer)  //tail of the list;
00232         return false;   
00233     iCurPointer=piNext[iCurPointer];
00234     return true;
00235 };
00236 
00237 template <class CItem, int iMaxItemNum>
00238 inline bool CDList<CItem, iMaxItemNum>::ToPrevious()
00239 {
00240     if (piPrevious[iCurPointer]==iCurPointer)//head of the list;
00241         return false;   
00242     iCurPointer=piPrevious[iCurPointer];
00243     return true;
00244 };
00245 
00246 
00247 #endif // CDLIST_H

Generated on Mon Jan 27 15:48:40 2003 for C++Reference by doxygen1.2.12 written by Dimitri van Heesch, © 1997-2001