SystemC  2.3.2
Accellera SystemC proof-of-concept library
sc_pvector.h
Go to the documentation of this file.
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_vector.h -- Simple implementation of a vector class.
23 */
33 #ifndef SC_VECTOR_H
34 #define SC_VECTOR_H
35 
36 #include <vector>
37 
38 namespace sc_core {
39 
40 extern "C" {
41  typedef int (*CFT)( const void*, const void* );
42 }
43 
44 
45 // #define ACCESS(I) m_vector.at(I) // index checking
46 #define ACCESS(I) m_vector[I]
47 #define ADDR_ACCESS(I) (m_vector.size() != 0 ? &m_vector[I] : 0 )
48 
55 template< class T >
57 {
58 public:
59 
60  typedef const T* const_iterator;
61  typedef T* iterator;
62  // typedef typename ::std::vector<T>::const_iterator const_iterator;
63  // typedef typename ::std::vector<T>::iterator iterator;
64 
65  sc_pvector( int alloc_n = 0 )
66  {
67  }
68 
69  sc_pvector( const sc_pvector<T>& rhs )
70  : m_vector( rhs.m_vector )
71  {}
72 
74  {}
75 
76 
77  std::size_t size() const
78  { return m_vector.size(); }
79 
80 
81  iterator begin()
82  { return (iterator) ADDR_ACCESS(0); }
83 
84  const_iterator begin() const
85  { return (const_iterator) ADDR_ACCESS(0); }
86 
87  iterator end()
88  { return static_cast<iterator> (ADDR_ACCESS(m_vector.size())); }
89 
90  const_iterator end() const
91  {
92  return static_cast<const_iterator> (ADDR_ACCESS(m_vector.size()));
93  }
94 
95 
97  { m_vector = rhs.m_vector; return *this; }
98 
99 
100  T& operator [] ( unsigned int i )
101  {
102  if ( i >= m_vector.size() ) m_vector.resize(i+1);
103  return (T&) m_vector.operator [] ( i );
104  }
105 
106  const T& operator [] ( unsigned int i ) const
107  {
108  if ( i >= m_vector.size() ) m_vector.resize(i+1);
109  return (const T&) m_vector.operator [] ( i );
110  }
111 
112  T& fetch( int i )
113  { return ACCESS(i); }
114 
115  const T& fetch( int i ) const
116  { return (const T&) ACCESS(i); }
117 
118 
119  T* raw_data()
120  { return (T*) &ACCESS(0); }
121 
122  const T* raw_data() const
123  { return (const T*) &ACCESS(0); }
124 
125 
126  operator const ::std::vector<T>& () const
127  { return m_vector; }
128 
129  void push_back( T item )
130  { m_vector.push_back( item ); }
131 
132 
133  void erase_all()
134  { m_vector.resize(0); }
135 
136  void sort( CFT compar )
137  {qsort( (void*)&m_vector[0], m_vector.size(), sizeof(void*), compar );}
138 
139  /* These methods have been added from Ptr_Array */
140 
141  void put( T item, int i )
142  { ACCESS(i) = item; }
143 
144  void decr_count()
145  { m_vector.resize(m_vector.size()-1); }
146 
147  void decr_count( int k )
148  { m_vector.resize(m_vector.size()-k); }
149 
150 
151 
152  protected:
153  mutable ::std::vector<T> m_vector; // Actual vector of pointers.
154 };
155 
156 #undef ACCESS
157 #undef ADDR_ACCESS
158 
159 } // namespace sc_core
160 
161 // $Log: sc_pvector.h,v $
162 // Revision 1.4 2011/08/26 20:46:19 acg
163 // Andy Goodrich: moved the modification log to the end of the file to
164 // eliminate source line number skew when check-ins are done.
165 //
166 // Revision 1.3 2011/02/18 20:38:44 acg
167 // Andy Goodrich: Updated Copyright notice.
168 //
169 // Revision 1.2 2011/01/20 16:52:21 acg
170 // Andy Goodrich: changes for IEEE 1666 2011.
171 //
172 // Revision 1.1 2010/12/07 20:11:45 acg
173 // Andy Goodrich: moved sc_pvector class to new header file to allow the
174 // use of sc_vector.h for Philipp Hartmann's new sc_vector class.
175 //
176 // Revision 1.4 2010/08/03 17:52:15 acg
177 // Andy Goodrich: fix signature for size() method of sc_pvector.
178 //
179 // Revision 1.3 2008/10/09 21:20:33 acg
180 // Andy Goodrich: fixed the way the end() methods calculate their results.
181 // I had incorrectly cut and pasted code from the begin() method.
182 //
183 // Revision 1.2 2007/01/17 22:44:34 acg
184 // Andy Goodrich: fix for Microsoft compiler.
185 //
186 // Revision 1.3 2006/01/13 18:53:11 acg
187 // Andy Goodrich: Added $Log command so that CVS comments are reproduced in
188 // the source.
189 //
190 
191 #endif
void put(T item, int i)
Definition: sc_pvector.h:141
const T & fetch(int i) const
Definition: sc_pvector.h:115
void decr_count(int k)
Definition: sc_pvector.h:147
const_iterator end() const
Definition: sc_pvector.h:90
#define ACCESS(I)
Definition: sc_pvector.h:46
sc_pvector< T > & operator=(const sc_pvector< T > &rhs)
Definition: sc_pvector.h:96
T & fetch(int i)
Definition: sc_pvector.h:112
const_iterator begin() const
Definition: sc_pvector.h:84
#define ADDR_ACCESS(I)
Definition: sc_pvector.h:47
iterator end()
Definition: sc_pvector.h:87
mutable ::std::vector< T > m_vector
Definition: sc_pvector.h:153
Simple vector class.
void sort(CFT compar)
Definition: sc_pvector.h:136
sc_pvector(const sc_pvector< T > &rhs)
Definition: sc_pvector.h:69
sc_pvector(int alloc_n=0)
Definition: sc_pvector.h:65
int(* CFT)(const void *, const void *)
Definition: sc_pvector.h:41
const T * const_iterator
Definition: sc_pvector.h:60
iterator begin()
Definition: sc_pvector.h:81
std::size_t size() const
Definition: sc_pvector.h:77
const T * raw_data() const
Definition: sc_pvector.h:122
void push_back(T item)
Definition: sc_pvector.h:129
T & operator[](unsigned int i)
Definition: sc_pvector.h:100