IT 취미생활.  
Front Page
Tag | Location | Media | Guestbook | Admin   
 
'우선순위 큐'에 해당하는 글(1)
2008.10.17   [알고리즘] Priority Queue (우선 순위큐)


[알고리즘] Priority Queue (우선 순위큐)

Project에 적용하기 위해서 작성한 Heap을 기반으로한 Priority Queue Class 입니다.
MultiThread-Safe 하긴한데 performance가 따라 줄지는 모르겟습니다.
성능 개선을 위한 부분 있다면 언제든지 코멘트 부탁 드립니다 ㅜㅜ;;;;

딱 보면 아시겠지만, 생각하는 프로그래머에 Code를 그대로 옮겨 놓은 듯 합니다.
^0^ 그럼 고운하루 되세요.

template< class T >
class CPriQueue
{
private:
 int m_nCurIndex, m_nMaxsize;
 CRITICAL_SECTION m_cs;
 T *m_pTemplateArray;

 void swap( int i, int j )
 {  
  T t = m_pTemplateArray[i];
  m_pTemplateArray[i] = m_pTemplateArray[j];
  m_pTemplateArray[j] = t;
 }

public:
 CPriQueue()
 {
  m_pTemplateArray = NULL;
  InitializeCriticalSection( &m_cs );
 }

 ~CPriQueue()
 {
  FlushQueue();
  delete []m_pTemplateArray;
  DeleteCriticalSection( &m_cs );
  m_pTemplateArray = NULL;
 }

 void SetQueueSize( unsigned int QueueSize )
 {
  if( QueueSize <= 0 )
   QueueSize = 1;

  m_pTemplateArray = new T[ QueueSize +1 ];
  if( m_pTemplateArray )
  {
   m_nMaxsize = QueueSize;
   m_nCurIndex = 0;
  }
 }

 void Push( T t )
 {
  if( m_nCurIndex >= m_nMaxsize   )
  {
   FlushQueue();
   printf("******FULL Queue ******* ");
   return;
  }

  int nParentIndex;
  EnterCriticalSection( &m_cs );
  m_pTemplateArray[ ++m_nCurIndex ] = t;


  if(  m_nCurIndex >  1 && m_pTemplateArray[ nParentIndex = m_nCurIndex >> 1  ] > m_pTemplateArray[m_nCurIndex] )
  { 
   for ( int nCurrentIndex = m_nCurIndex;
    nCurrentIndex > 1 && m_pTemplateArray[ nParentIndex = nCurrentIndex >> 1  ] > m_pTemplateArray[nCurrentIndex];
    nCurrentIndex = nParentIndex )
   {
    swap( nParentIndex, nCurrentIndex );
   }
  }
  LeaveCriticalSection( &m_cs );
 }


 T Pop()
 {
  if ( m_nCurIndex == 0 )
  {
   return 0;
  }

  int nChild = 0;
  EnterCriticalSection( &m_cs );
  T  t = m_pTemplateArray[ 1 ];
  m_pTemplateArray[ 1 ] = m_pTemplateArray[ m_nCurIndex-- ];

  for(  int i = 1; ( nChild =  i << 1 ) <= m_nCurIndex ; i = nChild )
  {
   if( nChild + 1 <= m_nCurIndex && m_pTemplateArray[ nChild + 1 ] < m_pTemplateArray[ nChild ] )
   {
    nChild++;
   }

   if( m_pTemplateArray[ i ] <= m_pTemplateArray[ nChild ]  )
   {
    break;
   }
   swap( nChild, i );
  }
  LeaveCriticalSection( &m_cs);
  return t;
 }
 
 void FlushQueue()
 {
  UINT nfreameCount = 0;
  T tFrame  = NULL;
  while( tFrame = Pop() )
  {
   delete tFrame;
   tFrame  = NULL;
   nfreameCount++;
  }
  m_nCurIndex = 0;
  printf("******Flush Queue *******  :: %d \n", nfreameCount );
 }
};



BLOG main image
취미생활
 Notice
 Category
분류 전체보기 (191)
매일매일갱생 (83)
서버개발 (1)
임베디드개발 (12)
Programming (80)
Personal Projects (6)
유용한 프로그램 (0)
 TAGS
퇴사 M480 isdbt Error Case It 개발자 벨소리 변경 출장 티스토리초대 미라지폰 spam mail Linux debugging english email English Wince5.0 warning 군대 티스토리 초대장 Java VC++ 영어 이메일 DVB Debug Dshow project Algorithm 서태지 Brazil 음식 MP3 ISDB-T C Windows Mobile6.0 DirectShow 1seg C++ 알고리즘 DVB-T
 Calendar
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
 Recent Entries
 Recent Comments
 Recent Trackbacks
 Archive
 Link Site
zextor
괴짜 프로그래머의 일상사~@@
Gag & Peace, and more..
Kazakhstan Almaty.......
Min-A
Sadgarret
Steve Yoon's log
가슴 뛰는 삶을 살아라
오스틴 파워
GUI sin
melanie parker_Lady
제레미의 TV 2.0 이야기..
 Visitor Statistics
Total :
Today :
Yesterday :
rss