template< class C >class TComList : public std::list< C >{public: typedef typename std::list<C>::iterator TComIterator; TComList& operator += ( const TComList& rcTComList) { if( ! rcTComList.empty() ) { insert( this->end(), rcTComList.begin(), rcTComList.end()); } return *this; } // leszek C popBack() { C cT = this->back(); this->pop_back(); return cT; } C popFront() { C cT = this->front(); this->pop_front(); return cT; } Void pushBack( const C& rcT ) { /*assert( sizeof(C) == 4);*/ if( rcT != NULL ) { this->push_back( rcT); } } Void pushFront( const C& rcT ) { /*assert( sizeof(C) == 4);*/ if( rcT != NULL ) { this->push_front( rcT); } } TComIterator find( const C& rcT ) // leszek { return find( this->begin(), this->end(), rcT ); }};
list就是資料結構中的雙向鏈表(根據sgi stl原始碼),因此它的記憶體空間可以是不連續的,通過指標來進行資料的訪問。
class C 的取值有TComList<TComPicYuv*> , TComList<TComPic*> 。及TComList<TComPic*>::iterator,TComList<TComPicYuv*>::iterator。
TComList<TComPicYuv*> m_cListPicYuvRec; ///< list of reconstruction YUV files
TComList<TComPic*> m_cListPic; ///< dynamic list of pictures
m_cListPic 用到的函數有size(), begin(),end(),erase(),insert()
m_cListPicYuvRec size(), begin(),end()
list<C>的結點結構,是一個環狀雙向鏈表。轉換成C後,ilist是一個空結點(head),只需要這樣衣個環中的空結點便可表示出整個
鏈表。對於迭代器類型,轉換為TComList_pTComPicYuv*結點類型,指向某個結點,而迭代器類型不但知道了結點的位置,還直接
指向了結點的資料(data)。
typedef struct _TComList_pTComPicYuv{ struct _TComList_pTComPicYuv* next; struct _TComList_pTComPicYuv* prev; TComPicYuv* data;}TComList_pTComPicYuv;
一個根據stl_list.h中的create_node函數改為對應的C來實現:
TComList_pTComPicYuv* TComList_pTComPicYuv_create_node( TComPicYuv* x) { TComList_pTComPicYuv* this_ptr; /* Allocate memory of struct size. */ this_ptr = (TComList_pTComPicYuv *) malloc(sizeof(TComList_pTComPicYuv)); if(this_ptr == NULL) { printf("Alloc memory failed!\n"); exit(0); } else { this_ptr->data = x; this_ptr->prev = NULL; this_ptr->next = NULL; } return this_ptr;}
problem:
1.error C4430: 缺少類型說明符 - 假定為 int。注意: C++ 不支援預設 int d:\hm\source\lib\tlibcommon\tcompicsym_c.h 72
#include "TComPic_C.h" 在有些檔案中不能被包含,一包含就出現很多錯誤。由於TComPic 類的成員關係中存在一個迴圈結構,需要TComPic 聲明的地方,只使用struct TComPic; 來聲明TComPic即可,而不使用包含"TComPic_C.h" 的方式。
2.
Void TComSlice::sortPicList (TComList<TComPic*>& rcListPic){ ... iterPicExtract_1 = iterPicExtract; iterPicExtract_1++; // swap iterPicExtract and iterPicInsert, iterPicExtract = curr. / iterPicInsert = insertion position rcListPic.insert (iterPicInsert, iterPicExtract, iterPicExtract_1); rcListPic.erase (iterPicExtract);}
改寫為:
{iterPicExtract_1 = iterPicExtract; iterPicExtract_1 =iterPicExtract_1->next; // swap iterPicExtract and iterPicInsert, iterPicExtract = curr. / iterPicInsert = insertion position //for ( ; iterPicExtract != iterPicExtract_1; iterPicExtract=iterPicExtract->next) TComList_pTComPic_insert( iterPicInsert,iterPicExtract->data); //iterPicExtract=iterPicExtract->next; TComList_pTComPic_erase(rcListPic,iterPicExtract); }
該函數中這段語句的改寫,該段語句實際上是在iterPicInsert之前再插入一個iterPicExtract指向的元素,然後再erase 原來的iterPicExtract ,即實現了iterPicExtract and iterPicInsert 的調換。
insert函數的調用為(檔案<list>):
View Code
template<class _Iter>
void insert(const_iterator _Where, _Iter _First, _Iter _Last)
{ // insert [_First, _Last) at _Where _Insert(_Where, _First, _Last, _Iter_cat(_First));//_Iter_cat(_First)返回迭代器類型,決定重載調用哪個函數
}
template<class _Iter>
void _Insert(const_iterator _Where,
_Iter _First, _Iter _Last, forward_iterator_tag)
{ // insert [_First, _Last) at _Where, forward iterators
_DEBUG_RANGE(_First, _Last);
_Iter _Next = _First;
_TRY_BEGIN
for (; _First != _Last; ++_First)//展開實際上是這兩句,_First = iterPicExtract,_Last=iterPicExtract_1
_Insert(_Where, *_First);
_CATCH_ALL
for (; _Next != _First; ++_Next)
{ // undo inserts
const_iterator _Before = _Where;
erase(--_Before);
}
_RERAISE;
_CATCH_END
}
由於iterator 的範圍[begin,end)是一個前閉後開的區間,改成C後head->next等於begin,head等於end,但是在判斷是否達到末尾時
還會有與迭代器的判斷有差別。
View Code
// find current position
TComList<TComPic*>::iterator iterPic = rcListPic.begin();
TComPic* pcRefPic = NULL;
TComPic* pcPic = *(iterPic);
while ( (TComPic_getPOC(pcPic) != (Int)uiPOCCurr) && (iterPic != rcListPic.end()) )
{
iterPic++;
pcPic = *(iterPic);//
} 改寫為:
// find current position
TComList_pTComPic* iterPic = rcListPic->next;
TComPic* pcRefPic = NULL;
TComPic* pcPic = iterPic->data;
while ( (TComPic_getPOC(pcPic) != (Int)uiPOCCurr) && (iterPic != rcListPic) )
{
iterPic=iterPic->next;
pcPic = iterPic->data;
}
3.erase函數實現問題
//destroy_node也銷毀了資料void TComList_pTComPic_destroy_node(TComList_pTComPic* position){if(position->data){free(position->data);position->data = NULL;}if(position){free(position);position = NULL;}}
erase函數調用destroy_node,也銷毀了資料,但是前面的iterPicExtract 交換位置,data指向了同一對象,然後的erase把這個對象也釋放了。
而原本的rcListPic.erase (iterPicExtract)函數並沒有釋放對象。所以對data(為指標類型)的銷毀,只因是銷毀指標本身(=NULL),而不是銷毀所指向的對象。應把free(position->data); 注釋掉。