/*****************************************************************************************
** 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
{
if(Pdata) //???
{
delete[] Pdata;
Pdata = 0;
}
if(aSize != 0)
{
Pdata = new T[aSize];
GASSERT(0 != Pdata);
}
Size = aSize;
}
template
void UVectContainer
{
(*this)[i] = item;
}
template
void UVectContainer
{
item = (*this)[i];
}
template
void UVectContainer
{
for( int cur = 0; cur < Size; cur++ )
f( Pdata[cur], args );
}
template
void UVectContainer
{
for( int cur = Size-1; cur >= 0; cur-- )
f( Pdata[cur], args );
}
template
T UVectContainer
{
for(int cur=0; cur<Size; cur++ )
if( cond( Pdata[cur], args ) )
return &Pdata[cur];
return 0;
}
template
T UVectContainer
{
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
{
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
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
{
if( !Full() )
{
this->PutAt( Head, item );
Head++;
Head %= this->Size;
}
}
/*** Copy an Item to the tail of the Queue ***/
template
void UQueue
{
if( !Full() )
{
if(Tail == 0)
Tail = this->Size-1;
else
Tail--;
this->PutAt( Tail, item );
}
}
template
bool UQueue
{
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
{
if( !Empty() )
{
this->GetAt( Tail, item );
Tail++;
Tail %= this->Size;
return true;
}
else return false;
}
template
bool UQueue
{
if(!Empty())
{
this->GetAt(Head, item);
return true;
}
else
return false;
}
// end;
template
void UQueue
{
for( int cur = 0; cur < GetTrueLen(); cur++ )
f( this->Pdata[(Head + cur) % this->Size], args );
}
template
void UQueue
{
for( int cur = GetTrueLen()-1; cur >= 0; cur-- )
f( this->Pdata[(Head + cur) % this->Size], args );
}
template
T UQueue
{
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
{
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
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
{
return firstThat(ItemEqual, &item);
}
template
void UArray
{
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
{
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
{
if( Full() ) ReSize();
PutAt( this->Count, item );
this->Count++;
}
//Read the item at the tail of the array
template
bool UArray
{
if( Empty() )
return false;
GetAt( this->Count-1, item );
return true;
}
//Remove the last item from the Array
template
void UArray
{
this->Count--;
}
//Remove the last num items from the Array
template
void UArray
{
int i;
for( i=0; i<num; i++ )
RemoveLast();
}
template
void UArray
{
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
{
for( int cur=0; cur
f( this->Pdata[cur], args );
}
template
void UArray
{
for( int cur=this->Count-1; cur>=0; cur-- )
f( this->Pdata[cur], args );
}
template
T UArray
{
for(int cur=0; cur
if( cond( this->Pdata[cur], args ) )
return &this->Pdata[cur];
return 0;
}
template
T UArray
{
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
void Push(const T& item);
bool Pop(T& item);
};
template
void UStack
{
Put(item);
}
template
bool UStack
{
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
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
void LinkPrev(UListNode
UListNode
};
template
UListNode
{
pitem = new T();
}
template
UListNode
{
pitem = new T(*item);
}
template
UListNode
{
if(pitem)
{
delete pitem;
pitem = 0;
}
}
template
void UListNode
{
p->next = next;
if(next != NULL)
next->prev = p;
p->prev = this;
next = p;
}
template
void UListNode
{
p->prev = prev;
if(prev != NULL)
prev->next = p;
p->next = this;
prev = p;
}
template
UListNode
{
if(prev != NULL)
prev->next = next;
if(next != NULL)
next->prev = prev;
return this;
}
/***************************************************************
class UList
***********************************************************/
template
class UList
{
protected:
UListNode
UListNode
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
UListNode
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
virtual void LinkTail(UListNode
virtual UListNode
virtual UListNode
//Iterate
virtual void forEach( void(f)(UListNode
virtual UListNode
virtual UListNode
};
/***************************************************************
class UListIterator
***************************************************************/
template
class UListIterator
{
const UList
UListNode
int CurPosi;
public:
UListIterator(const UList
UListIterator(const UList
bool MoveToHead();
bool MoveToTail();
bool MoveToNext();
bool MoveToPrev();
bool FindFirst(const T& item);
bool FindNext(const T& item);
UListNode
int GetCurPosi() { return CurPosi; };
void Iterate(void(f)(UListNode
};
template
UListIterator
{
pList = lp;
MoveToHead();
}
template
UListIterator
{
pList = &lr;
MoveToHead();
}
//move the current to head of the list, if success return true
template
bool UListIterator
{
if( pList )
curNode = pList->GetHead();
else
curNode = 0;
if( curNode )
{
CurPosi = 0;
return true;
}
else
{
CurPosi = -1;
return false;
}
}
template
bool UListIterator
{
if( pList )
curNode = pList->GetTail();
else
curNode = 0;
if( curNode )
{
CurPosi = pList->GetCount()-1;
return true;
}
else
{
CurPosi = -1;
return false;
}
}
template
bool UListIterator
{
if( curNode )
{
if(curNode->next)
{
curNode = curNode->next;
CurPosi++;
return true;
}
}
return false;
}
template
bool UListIterator
{
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
{
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
{
if(!MoveToNext())
return false;
do{
if(curNode->pitem && *(curNode->pitem) == item)
return true;
} while(MoveToNext());
return false;
}
template
UListNode
{
if(curNode) return curNode;
return 0;
}
template
void UListIterator
{
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
void LinkAfter(UListNode
void LinkHead(UListNode
void LinkTail(UListNode
public:
void LinkBefore(const T* pitem);
void LinkAfter(const T* pitem);
void LinkHead(const T* pitem);
void LinkTail(const T* pitem);
//Unlink node
UListNode
UListNode
UListNode
void UnlinkAll();
//move
bool Reset(int pos=0);
bool Next();
bool Prev();
//Iterate //运行以下函数后currPtr和CurPosition会发生改变;
void forEach( void(f)(UListNode
UListNode
UListNode
//sort
void ListSort( int(f)(T, T));
private:
void quicksort(int(f)(T, T),T** item,int left,int right);
};
template
UDList
{
}
template
UDList
{
UnlinkAll();
}
template
void UDList
{
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
{
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
{
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
{
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
{
UListNode
LinkBefore(pnode);
}
template
void UDList
{
UListNode
LinkAfter(pnode);
}
template
void UDList
{
UListNode
LinkHead(pnode);
}
template
void UDList
{
UListNode
LinkTail(pnode);
}
template
UListNode
{
if(this->currPtr == this->head)
return UnlinkHead();
else if(this->currPtr == this->tail)
return UnlinkTail();
else
{
UListNode
UListNode
this->currPtr = pTempNode1;
this->Count--;
if(pTempNode2) //added to free the node;
{
delete pTempNode2;
pTempNode2 = NULL;
}
return pTempNode2;
}
}
template
UListNode
{
if(this->head == NULL)
return NULL;
else
{
UListNode
UListNode
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
{
if(this->tail == NULL)
return NULL;
else
{
UListNode
UListNode
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
{
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
{
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
{
if(this->currPtr == NULL || this->currPtr->next == NULL)
return false;
else
{
this->currPtr = this->currPtr->next;
this->CurPosition++;
return true;
}
}
template
bool UDList
{
if(this->currPtr == NULL || this->currPtr->prev == NULL)
return false;
else
{
this->currPtr = this->currPtr->prev;
this->CurPosition--;
return true;
}
}
template
void UDList
{
UListIterator
iter.Iterate(f, args);
}
template
UListNode
{
UListIterator
// 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
{
UListIterator
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
{
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
{
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
UListNode
UListNode
private:
void LinkTail(UListNode
virtual UListNode
void LinkHead(UListNode
UListNode
UListNode
void DeleteAll();
};
template
USubList
{
}
template
USubList
{
DeleteAll();
}
template
void USubList
{
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
{
if(!this->head)
return 0;
UListNode
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
{
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
{
if(!this->head)
return 0;
UListNode
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
{
while(this->Count)
UnlinkTail();
}
template
static bool doFindNode(const UListNode
{
if(&aNode == (UListNode
return true;
else
return false;
}
template
UListNode
{
if(!this->head)
return 0;
if(item == this->head)
return USubList
else if(item == this->tail)
return USubList
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
{
this->head = this->tail = 0;
this->Count = 0;
}
template
void USubList
{
UListIterator
iter.Iterate(f, args);
this->currPtr = iter.CurrentItem();
this->CurPosition = iter.GetCurPosi();
}
template
UListNode
{
UListIterator
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
{
UListIterator
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
UArray< UListNode
public:
UStaticList(){};
UStaticList(int aSize, int aDelta);
UStaticList& operator=(const UStaticList& other) = delete;
~UStaticList();
virtual void LinkTail(UListNode
virtual void LinkTail(T* item);
virtual UListNode
virtual void LinkHead(UListNode
virtual void LinkHead(T* item);
virtual UListNode
virtual UListNode
};
template
UStaticList
{
NodeArray = new UArray< UListNode
list2 = new USubList
for(int i=0; i<aSize; i++)
list2->LinkTail( &(*NodeArray)[i] );
}
template
UStaticList
{
this->UnlinkAll();
list2->UnlinkAll();
delete list2;
delete NodeArray;
}
//add an item at tail
template
void UStaticList
{
if(!item)
return; //Add a null item is inhibited
UListNode
if(pitem)
{
//*pitem = *item;//error!!!
(pitem->pitem) =(item->pitem);
USubList
}
}
template
void UStaticList
{
if(!item)
return;
UListNode
if(pitem)
{
*(pitem->pitem) = *item;
USubList
}
}
template
UListNode
{
if(!this->head)
return 0;
UListNode
list2->LinkTail(pitem);
return pitem;
}
template
void UStaticList
{
if(!item)
return; //Add a null item is inhibited
UListNode
if(pitem)
{
(pitem->pitem) =(item->pitem);
USubList
}
}
template
void UStaticList
{
if(!item)
return;
UListNode
if(pitem)
{
*(pitem->pitem) = *item;
USubList
}
}
template
UListNode
{
if(!this->head)
return 0;
UListNode
list2->LinkTail(pitem);
return pitem;
}
template
UListNode
{
if(!this->head)
return 0;
UListNode
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].ptrv && 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( mMaxSizemFactor );
--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 mLength0; };
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;
};