Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

sc_vector.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-2004 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.3 (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_vector.h -- Simple implementation of a vector class.
00021 
00022   Original Author: Stan Y. Liao, Synopsys, Inc.
00023 
00024  *****************************************************************************/
00025 
00026 /*****************************************************************************
00027 
00028   MODIFICATION LOG - modifiers, enter your name, affiliation, date and
00029   changes you are making here.
00030 
00031       Name, Affiliation, Date:
00032   Description of Modification:
00033 
00034  *****************************************************************************/
00035 
00036 
00037 #ifndef SC_VECTOR_H
00038 #define SC_VECTOR_H
00039 
00040 
00041 extern "C" {
00042   typedef int (*CFT)( const void*, const void* );
00043 } 
00044 
00045 
00046 // ----------------------------------------------------------------------------
00047 //  CLASS : sc_pvector_base
00048 //
00049 //  Simple vector base class.
00050 // ----------------------------------------------------------------------------
00051 
00052 class sc_pvector_base
00053 {
00054   public:
00055     enum { 
00056   default_alloc = 4,    // Default allocation (from default_data).
00057   max_alloc = (1 << 20) // Maximum allocation chunk.
00058     };
00059 
00060 protected:
00061 
00062     typedef void* item_type;
00063 
00064     typedef item_type* iterator;
00065     typedef const item_type* const_iterator;
00066 
00067     sc_pvector_base( int alloc = default_alloc );
00068     sc_pvector_base( const sc_pvector_base& );
00069     ~sc_pvector_base();
00070 
00071 
00072     int size() const
00073   { return m_sz; }
00074 
00075 
00076     iterator begin()
00077         { return m_data; }
00078 
00079     const_iterator begin() const
00080         { return m_data; }
00081 
00082     iterator end()
00083         { return m_data + m_sz; }
00084 
00085     const_iterator end() const
00086         { return m_data + m_sz; }
00087 
00088 
00089     sc_pvector_base& operator = ( const sc_pvector_base& );
00090 
00091 
00092     void** raw_data()
00093   { return m_data; }
00094 
00095     void* const* raw_data() const
00096   { return m_data; }
00097 
00098 
00099     void*& operator [] ( int i );
00100     const void*& operator [] ( int i ) const;
00101 
00102     void*& fetch( int i )
00103   { return m_data[i]; }
00104 
00105     const void*& fetch( int i ) const
00106   { return (const void*&) m_data[i]; }
00107 
00108 
00109     void push_back( void* item )
00110     {
00111   if( m_sz == m_alloc ) {
00112       resize( m_sz + 11 );
00113   }
00114   m_data[m_sz] = item;
00115   ++ m_sz;
00116     }
00117 
00118 
00119     void erase_all()
00120         { m_sz = 0; }
00121 
00122 
00123     void sort( CFT compar );
00124 
00125     
00126     /* Change the ith item to new_item */
00127     void put( void* new_item, int i )
00128   { m_data[i] = new_item; }
00129 
00130     void decr_count()
00131   { m_sz ? m_sz -- : 0; }
00132 
00133     void decr_count( int k )
00134   { m_sz -= k; if( m_sz < 0 ) m_sz = 0; }
00135 
00136 private:
00137 
00138     void resize( int new_sz );
00139 
00140     int    m_alloc;       // Size of current allocation.
00141     void** m_data;        // Vector containing pointers.
00142     void*  m_default_data[default_alloc]; // Default allocation storage.
00143     int    m_sz;        // Size used within current alloc.
00144 };
00145 
00146 
00147 // ----------------------------------------------------------------------------
00148 //  CLASS : sc_pvector<T>
00149 //
00150 //  Simple vector class.
00151 // ----------------------------------------------------------------------------
00152 
00153 template< class T >
00154 class sc_pvector
00155 : public sc_pvector_base
00156 {
00157 public:
00158 
00159     typedef T* iterator;
00160     typedef const T* const_iterator;
00161 
00162     sc_pvector( int alloc = sc_pvector_base::default_alloc )
00163   : sc_pvector_base( alloc )
00164   {}
00165 
00166     sc_pvector( const sc_pvector<T>& rhs )
00167   : sc_pvector_base( rhs )
00168   {}
00169 
00170     ~sc_pvector()
00171   {}
00172 
00173 
00174     int size() const
00175   { return sc_pvector_base::size(); }
00176 
00177 
00178     iterator begin()
00179         { return (iterator) sc_pvector_base::begin(); }
00180 
00181     const_iterator begin() const
00182         { return (const_iterator) sc_pvector_base::begin(); }
00183 
00184     iterator end()
00185         { return (iterator) sc_pvector_base::end(); }
00186 
00187     const_iterator end() const
00188         { return (const_iterator) sc_pvector_base::end(); }
00189 
00190 
00191     sc_pvector<T>& operator = ( const sc_pvector<T>& rhs )
00192   { return (sc_pvector<T>&) sc_pvector_base::operator = ( rhs ); }
00193 
00194 
00195     T& operator [] ( int i )
00196   { return (T&) sc_pvector_base::operator [] ( i ); }
00197 
00198     const T& operator [] ( int i ) const
00199   { return (const T&) sc_pvector_base::operator [] ( i ); }
00200 
00201     T& fetch( int i )
00202   { return (T&) sc_pvector_base::fetch( i ); }
00203 
00204     const T& fetch( int i ) const
00205   { return (const T&) sc_pvector_base::fetch( i ); }
00206 
00207 
00208     T* raw_data()
00209   { return (T*) sc_pvector_base::raw_data(); }
00210 
00211     const T* raw_data() const
00212   { return (const T*) sc_pvector_base::raw_data(); }
00213 
00214 
00215     void push_back( T item )
00216   { sc_pvector_base::push_back( (void*) item ); }
00217 
00218 
00219     void erase_all()
00220   { sc_pvector_base::erase_all(); }
00221 
00222     void sort( CFT compar )
00223   { sc_pvector_base::sort( compar ); }
00224     
00225 
00226     /* These methods have been added from Ptr_Array */
00227     
00228     void put( T item, int i )
00229   { sc_pvector_base::put( (void*) item, i ); }
00230 
00231     void decr_count()
00232   { sc_pvector_base::decr_count(); }
00233 
00234     void decr_count( int k )
00235   { sc_pvector_base::decr_count( k ); }
00236 };
00237 
00238 
00239 #endif

Generated on Fri Jan 14 08:29:09 2005 for SystemC2.1beta11(excludingMSLib)(IncludingSCV)\nProvidedby:www.openverificationfoundation.org by doxygen1.2.18