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-2006 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_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 // $Log: sc_vector.h,v $
00038 // Revision 1.2  2007/01/12 21:04:58  acg
00039 //  Andy Goodrich: fix for Microsoft compiler.
00040 //
00041 // Revision 1.3  2006/01/13 18:53:11  acg
00042 // Andy Goodrich: Added $Log command so that CVS comments are reproduced in
00043 // the source.
00044 //
00045 
00046 #ifndef SC_VECTOR_H
00047 #define SC_VECTOR_H
00048 
00049 #include <vector>
00050 
00051 namespace sc_core {
00052 
00053 extern "C" {
00054   typedef int (*CFT)( const void*, const void* );
00055 } 
00056 
00057 
00058 // #define ACCESS(I) m_vector.at(I) // index checking
00059 #define ACCESS(I) m_vector[I]
00060 
00061 // ----------------------------------------------------------------------------
00062 //  CLASS : sc_pvector<T>
00063 //
00064 //  Simple vector class.
00065 // ----------------------------------------------------------------------------
00066 
00067 template< class T >
00068 class sc_pvector
00069 {
00070 public:
00071 
00072     typedef const T* const_iterator;
00073     typedef       T* iterator;
00074     // typedef typename ::std::vector<T>::const_iterator const_iterator;
00075     // typedef typename ::std::vector<T>::iterator       iterator;
00076 
00077     sc_pvector( int alloc_n = 0 )
00078     {
00079     }
00080 
00081     sc_pvector( const sc_pvector<T>& rhs )
00082     : m_vector( rhs.m_vector )
00083     {}
00084 
00085     ~sc_pvector()
00086     {}
00087 
00088 
00089     int size() const
00090     { return m_vector.size(); }
00091 
00092 
00093     iterator begin()
00094         { return (iterator) &ACCESS(0); }
00095 
00096     const_iterator begin() const
00097         { return (const_iterator) &ACCESS(0); }
00098 
00099     iterator end()
00100         { return static_cast<iterator> (&ACCESS(m_vector.size()-1)+1); }
00101 
00102     const_iterator end() const
00103         { return static_cast<const_iterator> (&ACCESS(m_vector.size()-1)+1); }
00104 
00105 
00106     sc_pvector<T>& operator = ( const sc_pvector<T>& rhs )
00107     { m_vector = rhs.m_vector; return *this; }
00108 
00109 
00110     T& operator [] ( unsigned int i )
00111     { 
00112         if ( i >= m_vector.size() ) m_vector.resize(i+1);
00113         return (T&) m_vector.operator [] ( i );
00114     }
00115 
00116     const T& operator [] ( unsigned int i ) const
00117     { 
00118         if ( i >= m_vector.size() ) m_vector.resize(i+1);
00119         return (const T&) m_vector.operator [] ( i );
00120     }
00121 
00122     T& fetch( int i )
00123     { return ACCESS(i); }
00124 
00125     const T& fetch( int i ) const
00126     { return (const T&) ACCESS(i); }
00127 
00128 
00129     T* raw_data()
00130     { return (T*) &ACCESS(0); }
00131 
00132     const T* raw_data() const
00133     { return (const T*) &ACCESS(0); }
00134 
00135 
00136     operator const ::std::vector<T>& () const
00137         { return m_vector; }
00138 
00139     void push_back( T item )
00140     { m_vector.push_back( item ); }
00141 
00142 
00143     void erase_all()
00144     { m_vector.resize(0); }
00145 
00146     void sort( CFT compar )
00147     {qsort( (void*)&m_vector[0], m_vector.size(), sizeof(void*), compar );}
00148 
00149     /* These methods have been added from Ptr_Array */
00150     
00151     void put( T item, int i )
00152     { ACCESS(i) = item; }
00153 
00154     void decr_count()
00155     { m_vector.resize(m_vector.size()-1); }
00156 
00157     void decr_count( int k )
00158     { m_vector.resize(m_vector.size()-k); }
00159 
00160 
00161 
00162   protected:
00163     mutable ::std::vector<T> m_vector;    // Actual vector of pointers.
00164 };
00165 
00166 #undef ACCESS
00167 
00168 } // namespace sc_core
00169 
00170 #endif

Generated on Wed Jan 21 15:32:10 2009 for SystemC by  doxygen 1.5.5