📄 list.cpp
字号:
/******************************************************************************** list.cpp: Lists manipulation*-------------------------------------------------------------------------------* (c)1999-2001 VideoLAN* $Id: list.cpp,v 1.1 2001/10/06 21:23:36 bozo Exp $** Authors: Benoit Steiner <benny@via.ecp.fr>** This program is free software; you can redistribute it and/or* modify it under the terms of the GNU General Public License* as published by the Free Software Foundation; either version 2* of the License, or (at your option) any later version.** This program is distributed in the hope that it will be useful,* but WITHOUT ANY WARRANTY; without even the implied warranty of* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the* GNU General Public License for more details.** You should have received a copy of the GNU General Public License* along with this program; if not, write to the Free Software* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.**-------------------------------------------------------------------------------* Notice: This file must be included in the source file with its header* TO DO:* Warning: This class is not thread safe********************************************************************************///------------------------------------------------------------------------------// Preamble//------------------------------------------------------------------------------// There is no preamble since this file is to be included in the files which// use the template: look at list.h for further explanations//******************************************************************************// class C_ListNode//******************************************************************************// Simple class used to store the data that must be kept by the list and their// relations with the other nodes of the list. A C_ListNode should never be// empty, so the default constructor is not public. Howewer, it is provided as a// protected memeber for C_List optimisations. //******************************************************************************//------------------------------------------------------------------------------// //------------------------------------------------------------------------------// //------------------------------------------------------------------------------template <class T> C_ListNode<T>::C_ListNode(T* pObject){ pData = pObject; ZERO(pPrevious); ZERO(pNext);}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------// //------------------------------------------------------------------------------template <class T> C_ListNode<T>::C_ListNode(const C_ListNode<T>& cNode){ ASSERT(false); // Shouldn't be used, the job is done by the C_List /* if(cNode.pData) { // We cannot call the copy constructor for it wouldn't work with virtual // classes: We use the Clone() method instead pData = cNode.pData->Clone(); } else pData = NULL; ZERO(pPrevious); ZERO(pNext); */ }//------------------------------------------------------------------------------// //------------------------------------------------------------------------------////------------------------------------------------------------------------------template <class T> C_ListNode<T>::C_ListNode(){ pData = NULL; ZERO(pPrevious); ZERO(pNext);}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------// pData can be NULL, so test this case//------------------------------------------------------------------------------template <class T> C_ListNode<T>::~C_ListNode(){ if (pData) delete pData; ZERO(pPrevious); ZERO(pNext); ZERO(pData);}//******************************************************************************// class C_List//******************************************************************************// The C_List class is basically a container used to store some data in a doubly// linked list. It also provides some iterations methods, but an real iterator// should be written in the future.//******************************************************************************//------------------------------------------------------------------------------// Default constructor: create an empty list//------------------------------------------------------------------------------template <class T> C_List<T>::C_List(byte bAutoClean){ m_bAutoClean = bAutoClean; // Create the dummy elements pFirst = new C_ListNode<T>(); pLast = new C_ListNode<T>(); // Init the dummy elements pFirst->pPrevious = NULL; pFirst->pNext = pLast; pLast->pPrevious = pFirst; pLast->pNext = NULL; // No real node is stored in the list at that time iNodeNumber = 0;}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------template <class T> C_List<T>::~C_List(){ Empty(); // Delete the 2 dummy elements delete pFirst; delete pLast;}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------template <class T> C_List<T>::C_List(const C_List<T>& cList){ // Create the dummy elements pFirst = new C_ListNode<T>; pLast = new C_ListNode<T>; // Init the dummy elements pFirst->pPrevious = NULL; pFirst->pNext = pLast; pLast->pPrevious = pFirst; pLast->pNext = NULL; // No real node is stored in the list at that time iNodeNumber = 0; m_bAutoClean = cList.m_bAutoClean; // Now copy the elements T* pData; for(unsigned int iIndex = 0; iIndex < cList.iNodeNumber; iIndex++) { // We cannot call the copy constructor for it wouldn't work with virtual // classes: We use the Clone() method pData = cList[iIndex].Clone(); ASSERT(pData); PushEnd(pData); }}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------template <class T> C_List<T>& C_List<T>::operator = (const C_List<T>& cSrc){ // To avoid pbs if the dst = scr operation is tried if(this != &cSrc) { // Delete old list excepted the dummy first and last elements C_ListNode<T>* pNode = pFirst->pNext; while (pNode->pNext != NULL) { pNode = pNode->pNext; delete pNode->pPrevious; } // Reinit the dummy elements pFirst->pNext = pLast; pLast->pPrevious = pFirst; // No real node is stored in the list at that time iNodeNumber = 0; // Now copy the elements of the src list in the new list T* pData; for(unsigned int iIndex = 0; iIndex < cSrc.iNodeNumber; iIndex++) { // We cannot call the copy constructor for it wouldn't work with virtual // classes: We use the Clone() method pData = cSrc[iIndex].Clone(); ASSERT(pData); PushEnd(pData); } } return *this;}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------template <class T> void C_List<T>::PushStart(T* pData){ C_ListNode<T>* pNewNode = new C_ListNode<T>(pData); C_ListNode<T>* pFirstNode = pFirst->pNext; pFirstNode->pPrevious = pNewNode; pFirst->pNext = pNewNode; pNewNode->pNext = pFirstNode; pNewNode->pPrevious = pFirst; iNodeNumber++;}//------------------------------------------------------------------------------// //------------------------------------------------------------------------------template <class T> void C_List<T>::PushEnd(T* pData){ C_ListNode<T>* pNewNode = new C_ListNode<T>(pData); C_ListNode<T>* pLastNode = pLast->pPrevious; pLastNode->pNext = pNewNode; pLast->pPrevious = pNewNode; pNewNode->pPrevious = pLastNode; pNewNode->pNext = pLast; iNodeNumber++;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -