1/***************************************************************************** 2 3 Licensed to Accellera Systems Initiative Inc. (Accellera) under one or 4 more contributor license agreements. See the NOTICE file distributed 5 with this work for additional information regarding copyright ownership. 6 Accellera licenses this file to you under the Apache License, Version 2.0 7 (the "License"); you may not use this file except in compliance with the 8 License. You may obtain a copy of the License at 9 10 http://www.apache.org/licenses/LICENSE-2.0 11 12 Unless required by applicable law or agreed to in writing, software 13 distributed under the License is distributed on an "AS IS" BASIS, 14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 15 implied. See the License for the specific language governing 16 permissions and limitations under the License. 17 18 *****************************************************************************/ 19 20/***************************************************************************** 21 22 sc_pq.cpp - Simple heap implementation of priority queue. 23 24 Original Author: Stan Y. Liao, Synopsys, Inc. 25 26 CHANGE LOG AT END OF FILE 27 *****************************************************************************/ 28 29// $Log: sc_pq.cpp,v $ 30// Revision 1.4 2011/08/26 20:46:18 acg 31// Andy Goodrich: moved the modification log to the end of the file to 32// eliminate source line number skew when check-ins are done. 33// 34 35#include "sysc/utils/sc_pq.h" 36 37namespace sc_core { 38 39sc_ppq_base::sc_ppq_base( int sz, int (*cmp)( const void*, const void* ) ) 40 : m_heap(0), m_size_alloc( sz ), m_heap_size( 0 ), m_compar( cmp ) 41{ 42 // m_size_alloc must be at least 2, otherwise resizing doesn't work 43 if( m_size_alloc < 2 ) { 44 m_size_alloc = 2; 45 } 46 // allocate 47 m_heap = new void*[m_size_alloc + 1]; 48 // initialize 49 for( int i = 0; i < m_size_alloc; ++ i ) { 50 m_heap[i] = 0; 51 } 52} 53 54sc_ppq_base::~sc_ppq_base() 55{ 56 delete[] m_heap; 57} 58 59void* 60sc_ppq_base::extract_top() 61{ 62 assert( m_heap_size > 0 ); 63 void* topelem = m_heap[1]; 64 m_heap[1] = m_heap[m_heap_size]; 65 m_heap_size --; 66 heapify( 1 ); 67 return topelem; 68} 69 70void 71sc_ppq_base::insert( void* elem ) 72{ 73 m_heap_size ++; 74 int i = m_heap_size; 75 76 // resize the heap in case there's not enough memory 77 if( m_heap_size > m_size_alloc ) { 78 m_size_alloc += m_size_alloc / 2; 79 void** new_heap = new void*[m_size_alloc + 1]; 80 for( int j = 1; j < m_heap_size; ++ j ) { 81 new_heap[j] = m_heap[j]; 82 } 83 delete[] m_heap; 84 m_heap = new_heap; 85 } 86 87 while( (i > 1) && (m_compar( m_heap[parent( i )], elem ) < 0) ) { 88 m_heap[i] = m_heap[parent( i )]; 89 i = parent( i ); 90 } 91 m_heap[i] = elem; 92} 93 94void 95sc_ppq_base::heapify( int i ) 96{ 97 int l; 98 while( l = left( i ), l <= m_heap_size ) { 99 int largest = (m_compar( m_heap[l], m_heap[i] ) > 0) ? l : i; 100 101 int r = right( i ); 102 if( (r <= m_heap_size) && 103 (m_compar( m_heap[r], m_heap[largest] ) > 0) ) { 104 largest = r; 105 } 106 107 if( largest != i ) { 108 void* tmp = m_heap[i]; 109 m_heap[i] = m_heap[largest]; 110 m_heap[largest] = tmp; 111 i = largest; 112 } else { 113 break; 114 } 115 } 116} 117 118} // namespace sc_core 119 120// Revision 1.3 2011/08/24 22:05:56 acg 121// Torsten Maehne: initialization changes to remove warnings. 122// 123// Revision 1.2 2011/02/18 20:38:44 acg 124// Andy Goodrich: Updated Copyright notice. 125// 126// Revision 1.1.1.1 2006/12/15 20:20:06 acg 127// SystemC 2.3 128// 129// Revision 1.3 2006/01/13 18:53:11 acg 130// Andy Goodrich: Added $Log command so that CVS comments are reproduced in 131// the source. 132 133// taf 134