src/sysc/utils/sc_pq.h

Go to the documentation of this file.
00001 /*****************************************************************************
00002 
00003   The following code is derived, directly or indirectly, from the SystemC
00004   source code Copyright (c) 1996-2005 by all Contributors.
00005   All Rights reserved.
00006 
00007   The contents of this file are subject to the restrictions and limitations
00008   set forth in the SystemC Open Source License Version 2.4 (the "License");
00009   You may not use this file except in compliance with such restrictions and
00010   limitations. You may obtain instructions on how to receive a copy of the
00011   License at http://www.systemc.org/. Software distributed by Contributors
00012   under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF
00013   ANY KIND, either express or implied. See the License for the specific
00014   language governing rights and limitations under the License.
00015 
00016  *****************************************************************************/
00017 
00018 /*****************************************************************************
00019 
00020   sc_pq.h -- A simple priority queue (which can be used to model multiple 
00021              clocks). From Cormen-Leiserson-Rivest, Ch.7.
00022 
00023   Original Author: Stan Y. Liao, Synopsys, Inc.
00024 
00025  *****************************************************************************/
00026 
00027 /*****************************************************************************
00028 
00029   MODIFICATION LOG - modifiers, enter your name, affiliation, date and
00030   changes you are making here.
00031 
00032       Name, Affiliation, Date:
00033   Description of Modification:
00034 
00035  *****************************************************************************/
00036 
00037 #ifndef SC_PQ_H
00038 #define SC_PQ_H
00039 
00040 
00041 #include <cassert>
00042 
00043 namespace sc_core {
00044 
00045 // ----------------------------------------------------------------------------
00046 //  CLASS : sc_ppq_base
00047 //
00048 //  Priority queue base class.
00049 // ----------------------------------------------------------------------------
00050 
00051 class sc_ppq_base
00052 {
00053 public:
00054 
00055     typedef int (*compare_fn_t)( const void*, const void* );
00056 
00057     sc_ppq_base( int sz, compare_fn_t cmp );
00058 
00059     ~sc_ppq_base();
00060 
00061     void* top() const
00062         { return m_heap[1]; }
00063 
00064     void* extract_top();
00065 
00066     void insert( void* elem );
00067 
00068     int size() const
00069         { return m_heap_size; }
00070 
00071     bool empty() const
00072         { return (m_heap_size == 0); }
00073 
00074 protected:
00075 
00076     int parent( int i ) const
00077         { return i >> 1; }
00078 
00079     int left( int i ) const
00080         { return i << 1; }
00081 
00082     int right( int i ) const
00083         { return (i << 1) + 1; }
00084 
00085     void heapify( int i );
00086 
00087 private:
00088 
00089     void**       m_heap;
00090     int          m_size_alloc;
00091     int          m_heap_size;
00092     compare_fn_t m_compar;
00093 };
00094 
00095 
00096 // ----------------------------------------------------------------------------
00097 //  CLASS TEMPLATE : sc_ppq<T>
00098 //
00099 //  This class is a simple implementation of a priority queue based on
00100 //  binary heaps. The class is templatized on its data type. A comparison
00101 //  function needs to be supplied.
00102 // ----------------------------------------------------------------------------
00103 
00104 template <class T>
00105 class sc_ppq
00106     : public sc_ppq_base
00107 {
00108 public:
00109 
00110     // constructor - specify the maximum size of the queue and
00111     // give a comparison function.
00112 
00113     sc_ppq( int sz, compare_fn_t cmp )
00114         : sc_ppq_base( sz, cmp )
00115         {}
00116 
00117     ~sc_ppq()
00118         {}
00119 
00120     // returns the value of the top element in the priority queue.
00121     T top() const
00122         { return (T) sc_ppq_base::top(); }
00123 
00124     // pops the first element of the priority queue.
00125 
00126     T extract_top()
00127         { return (T) sc_ppq_base::extract_top(); }
00128 
00129     // insert a new element to the priority queue.
00130 
00131     void insert( T elem )
00132         { sc_ppq_base::insert( (void*) elem ); }
00133 
00134     // size() and empty() are inherited.
00135 };
00136 
00137 } // namespace sc_core
00138 
00139 #endif

Generated on Wed Apr 25 13:53:28 2007 for SystemC by  doxygen 1.5.1