lxg

导航

 

/*****************************************************************************************
** Copyright(C) 2018-2022 北京世冠金洋科技发展有限公司
** All rights reserved
**
** filename : GContainer.h
** description :
**
** created on : 2021-03-01
** http://www.globalcrown.com.cn/
******************************************************************************************/

ifndef __UContainer_H

define __UContainer_H

include "GAssert.h"

ifndef NULL

define NULL 0

endif

/**************************************************
Class UVectContainer
***************************************************/
template
class UVectContainer
{
protected:
T *Pdata;
int Size, Count;
public:
UVectContainer():Pdata(0),Size(0),Count(0){};
UVectContainer(int aSize):Pdata(0),Count(0){ Init(aSize); };
virtual ~UVectContainer(){ delete[] Pdata; };

void Init(int aSize);
int GetSize() const { return Size;};
int GetCount() const { return Count; };
virtual void Put(const T& item)=0;
virtual bool Get(T& item)=0;
virtual bool Empty() const =0;
virtual bool Full() const =0;

bool operator!(){ return !Pdata; }; //!UVector means !Pdata
T& operator[](int i);// const;

void forEach( void(f)(T&, void), void args );
void antiforEach( void(
f)(T&, void), void args );
T firstThat( bool(cond)(const T&, void
), void
args ) const;
T lastThat( bool(cond)(const T&, void), void ) const;

protected:
void PutAt(int i, const T &item);
void GetAt(int i, T &item);
};

template
void UVectContainer::Init(int aSize)
{
if(Pdata) //???
{
delete[] Pdata;
Pdata = 0;
}
if(aSize != 0)
{
Pdata = new T[aSize];
GASSERT(0 != Pdata);
}
Size = aSize;
}

template
void UVectContainer::PutAt(int i, const T &item)
{
(*this)[i] = item;
}

template
void UVectContainer::GetAt(int i, T &item)
{
item = (*this)[i];
}

template
void UVectContainer::forEach( void (f)(T&, void),void* args )
{
for( int cur = 0; cur < Size; cur++ )
f( Pdata[cur], args );
}

template
void UVectContainer::antiforEach( void (f)(T&, void),void* args )
{
for( int cur = Size-1; cur >= 0; cur-- )
f( Pdata[cur], args );
}

template
T UVectContainer::firstThat( bool(cond)(const T&, void),void args ) const
{
for(int cur=0; cur<Size; cur++ )
if( cond( Pdata[cur], args ) )
return &Pdata[cur];
return 0;
}

template
T UVectContainer::lastThat( bool(cond)(const T&, void),void args ) const
{
for(int cur=Size-1; cur>=0; cur-- )
if( cond( Pdata[cur], args ) )
return &Pdata[cur];
return 0;
}

//UVectContainer[i] means Pdata[i]
template
T& UVectContainer::operator[](int i)// const
{
GASSERT(i >= 0 && i < Size);

return Pdata[i];                

}

/*******************************************************************
Class UQueue
********************************************************************/
template
class UQueue:public UVectContainer
{
int Head, Tail;

public:
UQueue():Head(0),Tail(0),UVectContainer(){};
UQueue( int aSize ):Head(0),Tail(0),UVectContainer(aSize){};

int GetTrueLen() const {
if(Head < Tail)
return Head + this->Size - Tail;
else
return Head - Tail;
}
virtual void Put(const T& Pitem);
virtual void PutAtTail(const T& Pitem);

virtual bool PeekAtTail(T& item);
virtual bool Get(T& adata);

virtual bool PeekAtHead(T& adata);
virtual bool Full() const { return GetTrueLen()this->Size-1; };
virtual bool Empty() const { return GetTrueLen()
0; };
virtual void Flush(){ this->Count = Head = Tail = 0; };

void forEach( void(f)(T&, void), void args );
void antiforEach( void(
f)(T&, void), void args );
T firstThat( bool(cond)(const T&, void
), void
args ) const;
T lastThat( bool(cond)(const T&, void), void ) const;

};

/*** Copy an Item to the head of the Queue ***/
template
void UQueue::Put(const T& item)
{
if( !Full() )
{
this->PutAt( Head, item );
Head++;
Head %= this->Size;
}
}

/*** Copy an Item to the tail of the Queue ***/
template
void UQueue::PutAtTail(const T& item)
{
if( !Full() )
{
if(Tail == 0)
Tail = this->Size-1;
else
Tail--;
this->PutAt( Tail, item );
}
}

template
bool UQueue::PeekAtTail(T& item)
{
if(!Empty())
{
this->GetAt(Tail, item);
return true;
}
else
return false;
}
// end;

/*** Get a copy of the head Item from the Queue, check the boundary
and return TRUE if the operation is success ***/
template
bool UQueue::Get(T& item)
{
if( !Empty() )
{
this->GetAt( Tail, item );
Tail++;
Tail %= this->Size;
return true;
}
else return false;
}

template
bool UQueue::PeekAtHead(T& item)
{
if(!Empty())
{
this->GetAt(Head, item);
return true;
}
else
return false;
}
// end;

template
void UQueue::forEach( void (f)(T&, void),void* args )
{
for( int cur = 0; cur < GetTrueLen(); cur++ )
f( this->Pdata[(Head + cur) % this->Size], args );
}

template
void UQueue::antiforEach( void (f)(T&, void),void* args )
{
for( int cur = GetTrueLen()-1; cur >= 0; cur-- )
f( this->Pdata[(Head + cur) % this->Size], args );
}

template
T UQueue::firstThat( bool(cond)(const T&, void),void args ) const
{
for(int cur=1; cur<=GetTrueLen(); cur++ )
if( cond( this->Pdata[(Head + cur) % this->Size], args ) )
return &this->Pdata[(Head + cur) % this->Size];
return 0;
}

template
T UQueue::lastThat( bool(cond)(const T&, void),void args ) const
{
for(int cur=GetTrueLen()-1; cur>=0; cur-- )
if( cond( this->Pdata[(Head + cur) % this->Size], args ) )
return &this->Pdata[(Head + cur) % this->Size];
return 0;
}

/***************************************************************
Class UArray
****************************************************************/
template
class UArray:public UVectContainer
{
protected:
int Delta;
public:
UArray():Delta(0),UVectContainer(){};
UArray(int aSize, int aDelta):Delta(aDelta),UVectContainer(aSize){};

virtual void Put(const T& item);
virtual bool Get(T& item);
virtual void RemoveLast();
void Remove(int num);
void RemoveAll(){ Remove(this->Count); };
virtual void ReSize();
virtual bool Full()const { return this->Count >= this->Size; };
virtual bool Empty()const{ return this->Count==0; };
int CurrentIndex()const{ return this->Count-1; };

void forEach( void(f)(T&, void), void args );
void antiforEach( void(
f)(T&, void), void args );
T firstThat( bool(cond)(const T&, void
),void
args ) const;
T lastThat( bool(cond)(const T&, void),void args ) const;

virtual void Delete(int index);
virtual void Insert(int index, const T& item);
const T* FindItem(T& item) const;
};

template
static bool ItemEqual(const T& item, void* args)
{
T* p = (T*)args;
if(item== *p)
return true;
else
return false;
}

template
const T* UArray::FindItem(T& item) const
{
return firstThat(ItemEqual, &item);
}

template
void UArray::Delete(int index)
{
if(index<0 || index>this->GetCount())
return;
int last_item=this->GetCount()-1;
for(int i=index; i < last_item; i++)
(this)[i]=(this)[i+1];

this->Count--;
}

template
void UArray::Insert(int index, const T& item)
{
if(index<0 || index>this->GetCount())
return;
if(this->Count>=this->Size)
ReSize();
for(int i= this->GetCount(); i>index; i--)
(this)[i]=(this)[i-1];
(*this)[index]=item;
this->Count++;
}

//Add an item at the tail of the array
template
void UArray::Put(const T& item)
{
if( Full() ) ReSize();
PutAt( this->Count, item );
this->Count++;
}

//Read the item at the tail of the array
template
bool UArray::Get(T& item)
{
if( Empty() )
return false;
GetAt( this->Count-1, item );
return true;
}

//Remove the last item from the Array
template
void UArray::RemoveLast()
{
this->Count--;
}

//Remove the last num items from the Array
template
void UArray::Remove(int num)
{
int i;
for( i=0; i<num; i++ )
RemoveLast();
}

template
void UArray::ReSize()
{
GASSERT(Delta > 0);
T* newPdata = new T[this->Size+Delta];

GASSERT( 0 != newPdata );

for (int i = 0; i < this->Size; i++)
    newPdata[i] = this->Pdata[i];

delete [] this->Pdata;
this->Pdata = newPdata;
this->Size = this->Size + Delta;

}

template
void UArray::forEach( void (f)(T&, void),void* args ) //OK
{
for( int cur=0; curCount; cur++ )
f( this->Pdata[cur], args );
}

template
void UArray::antiforEach( void (f)(T&, void),void* args )
{
for( int cur=this->Count-1; cur>=0; cur-- )
f( this->Pdata[cur], args );
}

template
T UArray::firstThat( bool(cond)(const T&, void),void args ) const //OK
{
for(int cur=0; curCount; cur++ )
if( cond( this->Pdata[cur], args ) )
return &this->Pdata[cur];
return 0;
}

template
T UArray::lastThat( bool(cond)(const T&, void),void args ) const //OK
{
for(int cur=this->Count-1; cur>=0; cur-- )
if( cond( this->Pdata[cur], args ) )
return &this->Pdata[cur];
return 0;
}

/***************************************************************
Class UStack
****************************************************************/
template
class UStack:public UArray
{
public:
UStack():UArray(){};
UStack(int aSize, int aDelta):UArray(aSize, aDelta){};

void Push(const T& item);
bool Pop(T& item);
};

template
void UStack::Push(const T& item)
{
Put(item);
}

template
bool UStack::Pop(T& item)
{
if(this->Empty() )
return false;
GetAt( this->Count-1, item );
this->RemoveLast();
return true;
}

//#ifdef ULIST_IS_ALLRIGHT
/***************************************************************
class UListNode
***************************************************************/
template
class UListNode
{
public:
UListNode *next, prev;
T pitem;
// T item;
public:
UListNode();
UListNode(const T item);
UListNode& operator=(const UListNode& other) = delete;
~UListNode();
//operations about changing the list
void LinkNext(UListNode
p);
void LinkPrev(UListNode
p);
UListNode
UnlinkNode();
};

template
UListNode::UListNode():next(0),prev(0)
{
pitem = new T();
}

template
UListNode::UListNode(const T* item):next(0),prev(0)
{
pitem = new T(*item);
}

template
UListNode::~UListNode()
{
if(pitem)
{
delete pitem;
pitem = 0;
}

}

template
void UListNode::LinkNext(UListNode* p)
{
p->next = next;
if(next != NULL)
next->prev = p;
p->prev = this;
next = p;
}

template
void UListNode::LinkPrev(UListNode* p)
{
p->prev = prev;
if(prev != NULL)
prev->next = p;
p->next = this;
prev = p;
}

template
UListNode* UListNode::UnlinkNode()
{
if(prev != NULL)
prev->next = next;
if(next != NULL)
next->prev = prev;
return this;
}

/***************************************************************
class UList the base class of list
***********************************************************/
template
class UList
{
protected:
UListNode head,tail;
UListNode currPtr;
int Count;
int CurPosition;
public:
UList()
{
head = 0;
tail = 0;
Count = 0;
CurPosition = -1;
currPtr = 0;
};
virtual ~UList(){};
//status
bool IsEmpty() const { return (Count==0); };
int GetCount() const { return Count; };
int GetCurPosition() { return CurPosition; };
UListNode
GetHead() const { return head; };
UListNode
GetTail() const { return tail; };
T
GetHeadData() const
{
if(head)
return head->pitem;
else
return NULL;
};
T
GetTailData() const
{
if(tail)
return tail->pitem;
else
return NULL;
};
T
GetCurData()
{
if(currPtr)
return currPtr->pitem;
else
return NULL;
};

virtual void LinkHead(UListNode* item)=0;
virtual void LinkTail(UListNode* item)=0;

virtual UListNode* UnlinkHead()=0;
virtual UListNode* UnlinkTail()=0;

//Iterate
virtual void forEach( void(f)(UListNode&, void), void args )=0;
virtual UListNode firstThat( bool(cond)(const UListNode&, void
), void* args )=0;
virtual UListNode lastThat( bool(cond)(const UListNode&, void), void )=0;
};

/***************************************************************
class UListIterator
***************************************************************/
template
class UListIterator
{
const UList *pList; //point to the list be iterated
UListNode *curNode; //point to the current iterated listnode
int CurPosi;
public:
UListIterator(const UList *lptr);
UListIterator(const UList &lref);

bool MoveToHead();
bool MoveToTail();
bool MoveToNext();
bool MoveToPrev();
bool FindFirst(const T& item);
bool FindNext(const T& item);
UListNode* CurrentItem();
int GetCurPosi() { return CurPosi; };
void Iterate(void(f)(UListNode& iref, void), void* arg);
};

template
UListIterator::UListIterator(const UList *lp)
{
pList = lp;
MoveToHead();
}

template
UListIterator::UListIterator(const UList &lr)
{
pList = &lr;
MoveToHead();
}

//move the current to head of the list, if success return true
template
bool UListIterator::MoveToHead()
{
if( pList )
curNode = pList->GetHead();
else
curNode = 0;
if( curNode )
{
CurPosi = 0;
return true;
}
else
{
CurPosi = -1;
return false;
}
}

template
bool UListIterator::MoveToTail()
{
if( pList )
curNode = pList->GetTail();
else
curNode = 0;
if( curNode )
{
CurPosi = pList->GetCount()-1;
return true;
}
else
{
CurPosi = -1;
return false;
}
}

template
bool UListIterator::MoveToNext()
{
if( curNode )
{
if(curNode->next)
{
curNode = curNode->next;
CurPosi++;
return true;
}
}
return false;
}

template
bool UListIterator::MoveToPrev()
{
if( curNode )
{
if(curNode->prev)
{
curNode = curNode->prev;
CurPosi--;
return true;
}
}
return false;
}

//move curNode to the first node that equals to item
//if success return true
template
bool UListIterator::FindFirst(const T& item)
{
if(!MoveToHead())
return false;
do{
if(curNode->pitem && *(curNode->pitem) == item)
return true;
} while(MoveToNext());
return false;
}

//move curNode to next node that equals to item
//if success return true
template
bool UListIterator::FindNext(const T& item)
{
if(!MoveToNext())
return false;
do{
if(curNode->pitem && *(curNode->pitem) == item)
return true;
} while(MoveToNext());
return false;
}

template
UListNode* UListIterator::CurrentItem()
{
if(curNode) return curNode;
return 0;
}

template
void UListIterator::Iterate(void(f)(UListNode& iref, void), void* arg)
{
if( !MoveToHead() ) return;
do{
f(*CurrentItem(), arg);
}while(MoveToNext());
}

/***************************************************************
class UDList
***************************************************************/
template
class UDList:public UList
{
public:
UDList();
virtual ~UDList();

//Link node
private:
void LinkBefore(UListNode* item);
void LinkAfter(UListNode* item);
void LinkHead(UListNode* item);
void LinkTail(UListNode* item);
public:
void LinkBefore(const T* pitem);
void LinkAfter(const T* pitem);
void LinkHead(const T* pitem);
void LinkTail(const T* pitem);

//Unlink node
UListNode* UnlinkAt();
UListNode* UnlinkHead();
UListNode* UnlinkTail();
void UnlinkAll();

//move
bool Reset(int pos=0);
bool Next();
bool Prev();

//Iterate //运行以下函数后currPtr和CurPosition会发生改变;
void forEach( void(f)(UListNode&, void), void args );
UListNode firstThat( bool(cond)(const UListNode&, void
), void* args );
UListNode lastThat( bool(cond)(const UListNode&, void), void );

//sort
void ListSort( int(f)(T, T));
private:
void quicksort(int(
f)(T, T),T** item,int left,int right);
};

template
UDList::UDList():UList()
{

}

template
UDList::~UDList()
{
UnlinkAll();
}

template
void UDList::LinkBefore(UListNode* item)
{
if(this->currPtr == NULL)
{
this->currPtr = this->head = this->tail = item;
this->Count = 1;
this->CurPosition = 0;
}
else if(this->currPtr == this->head)
LinkHead(item);
else
{
this->currPtr->LinkPrev(item);
this->Count++;
this->CurPosition++;
}
}

template
void UDList::LinkAfter(UListNode* item)
{
if(this->currPtr == NULL)
{
this->currPtr = this->head = this->tail = item;
this->Count = 1;
this->CurPosition = 0;
}
else if(this->currPtr == this->tail)
LinkTail(item);
else
{
this->currPtr->LinkNext(item);
this->Count++;
}
}

template
void UDList::LinkHead(UListNode* item)
{
if(this->head == NULL)
this->head = this->tail = this->currPtr = item;
else
{
this->head->LinkPrev(item);
this->head = item;
}
this->Count++;
this->CurPosition++;
}

template
void UDList::LinkTail(UListNode* item)
{
if(this->tail == NULL)
{
this->tail = this->head = this->currPtr = item;
this->CurPosition = 0;
}
else
{
this->tail->LinkNext(item);
this->tail = item;
}
this->Count++;
}

template
void UDList::LinkBefore(const T* pitem)
{
UListNode* pnode = new UListNode(pitem);
LinkBefore(pnode);
}

template
void UDList::LinkAfter(const T* pitem)
{
UListNode* pnode = new UListNode(pitem);
LinkAfter(pnode);
}

template
void UDList::LinkHead(const T* pitem)
{
UListNode* pnode = new UListNode(pitem);
LinkHead(pnode);
}

template
void UDList::LinkTail(const T* pitem)
{
UListNode* pnode = new UListNode(pitem);
LinkTail(pnode);
}

template
UListNode* UDList::UnlinkAt()
{
if(this->currPtr == this->head)
return UnlinkHead();
else if(this->currPtr == this->tail)
return UnlinkTail();
else
{
UListNode* pTempNode1 = this->currPtr->next;
UListNode* pTempNode2 = this->currPtr->UnlinkNode();
this->currPtr = pTempNode1;
this->Count--;
if(pTempNode2) //added to free the node;
{
delete pTempNode2;
pTempNode2 = NULL;
}
return pTempNode2;
}
}

template
UListNode* UDList::UnlinkHead()
{
if(this->head == NULL)
return NULL;
else
{
UListNode* pTempNode1 = this->head->next;
UListNode* pTempNode2 = this->head->UnlinkNode();
this->head = pTempNode1;
this->Count--;
if(this->currPtr != pTempNode2)
this->CurPosition--;
else
{
this->currPtr = this->head;
if(this->Count == 0) //when only have one node;
{
this->tail = NULL;
this->CurPosition = -1;
}
}
if(pTempNode2) //added to free the node;
{
delete pTempNode2;
pTempNode2 = NULL;
}
return pTempNode2;
}
}

template
UListNode* UDList::UnlinkTail()
{
if(this->tail == NULL)
return NULL;
else
{
UListNode* pTempNode1 = this->tail->prev;
UListNode* pTempNode2 = this->tail->UnlinkNode();
this->tail = pTempNode1;
this->Count--;
if(this->currPtr == pTempNode2)
{
this->currPtr = this->tail;
this->CurPosition--;
if(this->Count == 0) //when only have one node;
{
this->head = NULL;
this->CurPosition = -1;
}
}
if(pTempNode2) //added to free the node;
{
delete pTempNode2;
pTempNode2 = NULL;
}
return pTempNode2;
}
}

template
void UDList::UnlinkAll()
{
while(this->tail)
{
this->currPtr = this->tail;
this->tail = this->tail->prev;
delete this->currPtr;
}
this->currPtr = this->head = this->tail = NULL;
this->Count = 0;
this->CurPosition = -1;
}

template
bool UDList::Reset(int pos)
{
if(this->Count == 0 || pos > this->Count-1 || pos < 0)
return false;
else if(pos == this->CurPosition)
return true;
else if(pos == 0)
{
this->currPtr = this->head;
this->CurPosition = 0;
}
else if(pos > this->CurPosition)
{
while(pos != this->CurPosition)
Next();
}
else
{
while(pos != this->CurPosition)
Prev();
}
return true;
}

template
bool UDList::Next()
{
if(this->currPtr == NULL || this->currPtr->next == NULL)
return false;
else
{
this->currPtr = this->currPtr->next;
this->CurPosition++;
return true;
}
}

template
bool UDList::Prev()
{
if(this->currPtr == NULL || this->currPtr->prev == NULL)
return false;
else
{
this->currPtr = this->currPtr->prev;
this->CurPosition--;
return true;
}
}

template
void UDList::forEach( void(f)(UListNode&, void), void *args )
{
UListIterator iter(this);
iter.Iterate(f, args);
}

template
UListNode* UDList::firstThat( bool(cond)(const UListNode&, void), void* args)
{
UListIterator iter(this);
// iter.MoveToHead();
if(iter.CurrentItem() == 0)
return 0;
do{
if( cond( *(iter.CurrentItem()), args) )
{
this->currPtr = iter.CurrentItem();
this->CurPosition = iter.GetCurPosi();
return iter.CurrentItem();
}
}while(iter.MoveToNext());

return 0;
}

template
UListNode* UDList::lastThat( bool(cond)(const UListNode&, void), void* args)
{
UListIterator iter(this);
iter.MoveToTail();
if(iter.CurrentItem() == 0)
return 0;
do{
if( cond( *(iter.CurrentItem()), args) )
{
this->currPtr = iter.CurrentItem();
this->CurPosition = iter.GetCurPosi();
return iter.CurrentItem();
}
}while(iter.MoveToPrev());

return 0;
}

template
void UDList::ListSort( int(f)(T, T)) //bubble
{
const int NUM = this->Count;
if(NUM < 2)
return;
int i;
T** tempArray = new T
[NUM];
Reset();
for(i=0;i<NUM;i++)
{
tempArray[i] = this->currPtr->pitem;
if(!Next())
break;
}
quicksort(f,tempArray, 0, NUM-1);
Reset();
for(i=0;i<NUM;i++)
{
this->currPtr->pitem = tempArray[i];
if(!Next())
break;
}
delete[] tempArray;
}

template
void UDList::quicksort(int(f)(T, T*),T** item,int left,int right)
{
int i, j;
T *m = NULL, *t = NULL;
i = left;
j = right;
if(j-i<=0)
return;
else
if(j-i==1)
{
if(f(item[j],item[i]) < 0)
{
t = item[i];
item[i] = item[j];
item[j] = t;
}
return;
}

m = item[(left+right)/2];
do
{
while((f(item[i],m)<0) && i<right)
i++;
while((f(m,item[j])<0) && j>left)
j--;
if(i<=j)
{
t = item[i];
item[i] = item[j];
item[j] = t;
i++;j--;
}
}while(i<=j);
if(left<j)
quicksort(f,item,left,j);
if(i<right)
quicksort(f,item,i,right);
}

template
class UStaticList;
/***************************************************************
class USubList
***************************************************************/
template
class USubList:public UList
{
friend class UStaticList;
friend class UListIterator;
public:
USubList();
~USubList();

virtual void UnlinkAll();
void forEach( void(f)(UListNode&, void), void *args );

UListNode firstThat( bool(cond)(const UListNode&, void), void args );
UListNode lastThat( bool(cond)(const UListNode&, void), void );

private:
void LinkTail(UListNode* item);
virtual UListNode* UnlinkTail();
void LinkHead(UListNode* item);
UListNode* UnlinkHead();
UListNode* UnlinkThe(UListNode* item); //item must be one node of the list;
void DeleteAll();
};

template
USubList::USubList():UList()
{
}

template
USubList::~USubList()
{
DeleteAll();
}

template
void USubList::LinkTail(UListNode* item)
{
if(!item) //disable to link a null item
return;

if(this->Count == 0)
{
item->prev = item->next = 0;
this->head = this->tail = item;
}
else
{
this->tail -> next = item;
item -> prev = this->tail;
this->tail = item;
this->tail->next = 0;
}
this->Count++;
}

template
UListNode* USubList::UnlinkTail()
{
if(!this->head)
return 0;

UListNode* pitem = this->tail;
if(this->Count == 1)
this->head = this->tail = 0;
else
{
this->tail = this->tail->prev;
this->tail->next = 0;
}
this->Count--;
pitem->prev = 0;
pitem->next = 0;
return pitem;
}

template
void USubList::LinkHead(UListNode* item)
{
if(!item) //disable to link a null item
return;

if(this->Count == 0)
{
item->prev = item->next = 0;
this->head = this->tail = item;
}
else
{
this->head -> prev = item;
item -> next = this->head;
this->head = item;
this->head->prev = 0; //???
}
this->Count++;
}

template
UListNode* USubList::UnlinkHead()
{
if(!this->head)
return 0;

UListNode* pitem = this->head;
if(this->Count == 1)
this->head = this->tail = 0;
else
{
this->head = this->head->next;
this->head->prev = 0;
}
this->Count--;
pitem->prev = 0;
pitem->next = 0;
return pitem;
}

template
void USubList::UnlinkAll()
{
while(this->Count)
UnlinkTail();
}
template
static bool doFindNode(const UListNode& aNode, void* p)
{
if(&aNode == (UListNode*)p)
return true;
else
return false;
}

template
UListNode* USubList::UnlinkThe(UListNode* item)
{
if(!this->head)
return 0;

if(item == this->head)
return USubList::UnlinkHead();
else if(item == this->tail)
return USubList::UnlinkTail();
else if(firstThat(doFindNode, item) != 0)
{
(item->prev)->next = item->next;
(item->next)->prev = item->prev;
item->prev = item->next = 0;
this->Count--;
return item;
}
else
return 0;
}

template
void USubList::DeleteAll()
{

this->head = this->tail = 0;
this->Count = 0;
}

template
void USubList::forEach( void(f)(UListNode&, void), void *args )
{
UListIterator iter(this);
iter.Iterate(f, args);
this->currPtr = iter.CurrentItem();
this->CurPosition = iter.GetCurPosi();
}

template
UListNode* USubList::firstThat( bool(cond)(const UListNode&, void), void* args)
{
UListIterator iter(this);
iter.MoveToHead();
if(iter.CurrentItem() == 0)
return 0;
do{
if( cond( *(iter.CurrentItem()), args) )
{
this->currPtr = iter.CurrentItem();
this->CurPosition = iter.GetCurPosi();
return iter.CurrentItem();
}
}while(iter.MoveToNext());
this->currPtr = iter.CurrentItem();
this->CurPosition = iter.GetCurPosi();
return 0;
}

template
UListNode* USubList::lastThat( bool(cond)(const UListNode&, void), void* args)
{
UListIterator iter(this);
iter.MoveToTail();
if(iter.CurrentItem() == 0)
return 0;
do{
if( cond( *(iter.CurrentItem()), args) )
{
this->currPtr = iter.CurrentItem();
this->CurPosition = iter.GetCurPosi();
return iter.CurrentItem();
}
}while(iter.MoveToPrev());
this->currPtr = iter.CurrentItem();
this->CurPosition = iter.GetCurPosi();
return 0;
}

/***************************************************************
class UStaticList
*************************************************************/
template
class UStaticList:public USubList
{
private:
USubList
list2;
UArray< UListNode >
NodeArray;
public:
UStaticList(){};
UStaticList(int aSize, int aDelta);
UStaticList& operator=(const UStaticList& other) = delete;
~UStaticList();

virtual void LinkTail(UListNode* item);
virtual void LinkTail(T* item);
virtual UListNode* UnlinkTail();
virtual void LinkHead(UListNode* item);
virtual void LinkHead(T* item);
virtual UListNode* UnlinkHead();
virtual UListNode* UnlinkThe(UListNode* item); //item must be one node of the list;
};

template
UStaticList::UStaticList(int aSize, int aDelta):USubList()
{
NodeArray = new UArray< UListNode >(aSize, aDelta);

list2 = new USubList();
for(int i=0; i<aSize; i++)
list2->LinkTail( &(*NodeArray)[i] );
}

template
UStaticList::~UStaticList()
{
this->UnlinkAll();
list2->UnlinkAll();
delete list2;
delete NodeArray;
}

//add an item at tail
template
void UStaticList::LinkTail(UListNode* item)
{
if(!item)
return; //Add a null item is inhibited

UListNode* pitem = list2->UnlinkTail();
if(pitem)
{
//*pitem = *item;//error!!!
(pitem->pitem) =(item->pitem);
USubList::LinkTail(pitem);
}
}

template
void UStaticList::LinkTail(T* item)
{
if(!item)
return;
UListNode* pitem = list2->UnlinkTail();
if(pitem)
{
*(pitem->pitem) = *item;
USubList::LinkTail(pitem);
}
}

template
UListNode* UStaticList::UnlinkTail()
{
if(!this->head)
return 0;

UListNode* pitem = USubList::UnlinkTail();
list2->LinkTail(pitem);

return pitem;
}

template
void UStaticList::LinkHead(UListNode* item)
{
if(!item)
return; //Add a null item is inhibited

UListNode* pitem = list2->UnlinkTail();
if(pitem)
{
(pitem->pitem) =(item->pitem);
USubList::LinkHead(pitem);
}
}

template
void UStaticList::LinkHead(T* item)
{
if(!item)
return;
UListNode* pitem = list2->UnlinkTail();
if(pitem)
{
*(pitem->pitem) = *item;
USubList::LinkHead(pitem);
}
}

template
UListNode* UStaticList::UnlinkHead()
{
if(!this->head)
return 0;

UListNode* pitem = USubList::UnlinkHead();
list2->LinkTail(pitem);

return pitem;
}

template
UListNode* UStaticList::UnlinkThe(UListNode* item)
{
if(!this->head)
return 0;

UListNode* pitem = USubList::UnlinkThe(item);
list2->LinkTail(pitem);

return pitem;
}

//Value限为整数或指针(可以用type trait技术来进行改进)
template <typename Value,int MaxSize=0x2000>
class UMap
{
struct MapTable{
Value ptr;
bool used;
};
MapTable maptbl[MaxSize+1];
public:
UMap(){
memset(maptbl,0,(MaxSize+1)*sizeof(MapTable));
}
Value GetValue(const int& k) const;
int GetKey(const Value& v) const;
bool SetPair(int key,Value Ptr);
bool DelPair(int key);
};

template <typename Value,int MaxSize>
Value UMap<Value,MaxSize>::GetValue(const int& k) const
{
if (k > 0 && k <= MaxSize)
{
return maptbl[k].used ? maptbl[k].ptr : NULL;
}
return NULL; //NOTE:Value must be pointer or int
}

template <typename Value,int MaxSize>
int UMap<Value,MaxSize>::GetKey(const Value& v) const
{
if(vNULL)
return -1;
for(int i=1;i<=MaxSize;i++)
if(maptbl[i].ptr
v && maptbl[i].used)
return i;
return -1;
}

template <typename Value,int MaxSize>
bool UMap<Value,MaxSize>::SetPair(int k,Value ptr)
{
if(k<1 || k>MaxSize)
return true; //be invalid id
else if(maptbl[k].used)
{
return false;
}
else{
maptbl[k].used=true;
maptbl[k].ptr=ptr;
}
return true;
}

template <typename Value,int MaxSize>
bool UMap<Value,MaxSize>::DelPair(int k)
{
if(k<1 || k>MaxSize || !maptbl[k].used)
return false;
maptbl[k].ptr=0; //NOTE:Value必须为指针或整型
maptbl[k].used=false;
return true;
}

/*

  • 双向队列
  • 自动增长大小,允许随机访问(常数时间),允许在头、尾插入、删除(常数时间)
    */

template
class UDeque
{
public:
UDeque(int size) //size指实际元素个数
:mMaxSize(size), mFront(0), mRear(0), mLength(0)
{
mpData = new T[mMaxSize];
}
UDeque& operator=(UDeque& other) = delete;
~UDeque()
{
delete[] mpData;
}
void PushBack(const T& t)
{
if( Full() )
Resize( mMaxSizemFactor );
mpData[mRear]=t;
++mRear;
if (mRear>=mMaxSize)
mRear-=mMaxSize;
++mLength;
}
void PushFront(const T& t)
{
if( Full() )
Resize( mMaxSize
mFactor );
--mFront;
if (mFront<0) mFront+=mMaxSize;
mpData[mFront] = t;
++mLength;
}
void PopBack(int num=1)
{
if( mLength<num && num>0 )
{
GASSERT(false);
return;
}
mRear -= num;
if (mRear<0) mRear+=mMaxSize;
mLength-=num;
}
void PopFront(int num=1)
{
if( mLength<num && num>0 )
{
GASSERT(false);
return;
}
mFront +=num;
if (mFront>=mMaxSize) mFront-=mMaxSize;
mLength-=num;
}
const T& Front() const { return mpData[mFront]; }
const T& Back() const { return mpData[mRear?mRear-1:mMaxSize-1]; }
int Length() const { return mLength; }
void Clear() { mRear = mFront = mLength = 0; }
T& operator[](int index) const
{
GASSERT( 0<=index && index<mLength );
int i=mFront+index;
if (i>=mMaxSize) i-=mMaxSize;
return mpData[i];
}

protected:
void Resize(int newsize) //newsize指实际元素个数
{
GASSERT( newsize>mLength );
T* p_newdata = new T[newsize];
int len = mLength;
for( int i=0; i<len; ++i )
{
p_newdata[i] = (*this)[i];
}
mFront = 0;
mRear= len;
mMaxSize = newsize;
delete []mpData;
mpData = p_newdata;
}

private:
bool Full() const { return mLengthmMaxSize; };
bool Empty() const { return mLength
0; };

int mMaxSize;
static const int mFactor=2;
int mFront;
int mRear;
T* mpData;
int mLength;

};

endif //__ULADT_H

点击查看代码
#ifndef GGENERALOS_H_
#define GGENERALOS_H_

#include "arch_global.h"
#include <stdint.h>

#ifdef __cplusplus
extern "C"
{
#endif /* __cplusplus */

uint64_t ARCH_EXPORT OS_Current_Task_Id();

#ifdef __cplusplus
}
#endif /* __cplusplus */


#include <QThread>

uint64_t OS_Current_Task_Id()
{
    return reinterpret_cast<uint64_t>(QThread::currentThreadId());
}

#endif

class ARCH_EXPORT GGlobal
{
public:
    static void* Pipe_InfoToMainTask;
    static uint64_t MainTaskThreadID;
};

posted on 2025-09-26 16:54  lxg_7105  阅读(3)  评论(0)    收藏  举报