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

_scv_associative_array.h

Go to the documentation of this file.
00001 //  -*- C++ -*- <this line is for emacs to recognize it as C++ code>
00002 /*****************************************************************************
00003 
00004   The following code is derived, directly or indirectly, from the SystemC
00005   source code Copyright (c) 1996-2002 by all Contributors.
00006   All Rights reserved.
00007 
00008   The contents of this file are subject to the restrictions and limitations
00009   set forth in the SystemC Open Source License Version 2.3 (the "License");
00010   You may not use this file except in compliance with such restrictions and
00011   limitations. You may obtain instructions on how to receive a copy of the
00012   License at http://www.systemc.org/. Software distributed by Contributors
00013   under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF
00014   ANY KIND, either express or implied. See the License for the specific
00015   language governing rights and limitations under the License.
00016 
00017  *****************************************************************************/
00018 
00019 /*****************************************************************************
00020 
00021   _scv_associative_array.h -- 
00022   Associative array is an efficient implementation of a large array indexed
00023   by a range of integers, when most of the elements have the same
00024   default value (e.g. 0 or "undefined").
00025 
00026   Original Authors (Cadence Design Systems, Inc):
00027   Norris Ip, Dean Shea, John Rose, Jasvinder Singh, William Paulsen,
00028   John Pierce, Rachida Kebichi, Ted Elkind, David Bailey
00029   2002-09-23
00030 
00031  *****************************************************************************/
00032 
00033 /*****************************************************************************
00034 
00035   MODIFICATION LOG - modifiers, enter your name, affiliation, date and
00036   changes you are making here.
00037 
00038       Name, Affiliation, Date:
00039   Description of Modification:
00040 
00041  *****************************************************************************/
00042 
00043 #ifndef SCV_ASSOCIATIVE_ARRAY_H
00044 #define SCV_ASSOCIATIVE_ARRAY_H
00045 
00046 #include "scv/_scv_data_structure.h"
00047 #include "scv/scv_config.h"
00048 #include "scv/scv_util.h"
00049 #include "scv/scv_random.h"
00050 #include <map>
00051 
00052 #if defined(_USE_HASH_MAP)
00053 #include <hash_map.h>
00054 #endif
00055 
00056 template <class Data>   // needed for scv_random.cpp
00057 ostream& operator<<(ostream&os, const list<Data> & t) {
00058   typename list<Data>::const_iterator temp;
00059   for (temp=t.begin(); temp != t.end(); ++temp) {
00060     os << (*temp) << endl;
00061   }
00062   return os; 
00063 }
00064 
00065 template <class Key, class Data, typename container_type = map<Key,  Data, less<Key> > > 
00066 class _scv_associative_array : public _scv_data_structure {
00067 public:
00068   typedef typename container_type::iterator iteratorT;  
00069   typedef typename container_type::const_iterator constIteratorT;  
00070   typedef typename container_type::const_iterator const_iterator;  
00071 private:
00072   Data _defaultValue;
00073   container_type _map;  
00074 
00075 public:
00076 
00077   _scv_associative_array(const char *name,
00078              const Data& defaultValue)
00079    : _scv_data_structure(name), _defaultValue(defaultValue), _map() { }
00080     
00081   //
00082   // parameter access (corresponds to the argument of the constructor arguments
00083   // 
00084   const Data& defaultValue() const { return _defaultValue; } 
00085   const Data& default_value() const { return _defaultValue; } 
00086 
00087   //
00088   // copy constructor (create a new Associative array with the same values as the argument)
00089   // 
00090   _scv_associative_array(const _scv_associative_array& other, const char *nameP="<anonymous>") 
00091     : _scv_data_structure(nameP), _defaultValue(other._defaultValue), _map(other._map) { } 
00092 
00093   //
00094   // virtual destructor
00095   //
00096   virtual ~_scv_associative_array() { } 
00097 
00098   virtual const char *kind() const
00099     { static const char name[]="_scv_associative_array"; return name; }
00100 
00101  
00102   //
00103   // accessing an element
00104   // 
00105   virtual Data& operator[](const Key& i) { 
00106     if (_map.count(i)) return _map[i]; 
00107     else { 
00108       pair<const Key, Data> tmp(i, _defaultValue);  _map.insert(tmp);  // modified for hp
00109       return _map[i];
00110     }
00111   } 
00112   virtual const Data& operator[](const Key& i) const { 
00113     if (_map.count(i)) {
00114       container_type * myMap = (container_type *) &_map;
00115       return (*myMap)[i]; 
00116     }
00117     else { 
00118       return _defaultValue;
00119     } 
00120   } 
00121 
00122   void insert(const Key& k, const Data& data){
00123     _map[k] = data ;
00124   }
00125 
00126   // This procedure is written to provide the user a read only procedure.
00127   // the [] operator can be write or read for non-const objects. In such
00128   // a situation if [] is used on non-const object to query the value
00129   // [i] , if there is no value stored at index i, it creates a copy of
00130   // _defaultValue by allocating new memory and storing it in the associative array.
00131   // This is because when [] is used we do not know if it is for reading or writing.
00132   // If it is for reading we do not need to create a copy of _defaultValue.
00133   // The getValue procedure does this.
00134 
00135 
00136   const Data& getValue(const Key& i) const {
00137     return (*this)[i] ;
00138   }
00139 
00140   //
00141   // freeing the storage allocated for an element, and
00142   // resetting its value to the default value.
00143   //
00144   void erase(const Key& i) { _map.erase((const Key&)i); } 
00145 
00146   //
00147   // freeing all storages, and resetting all elements to the default value.
00148   //
00149   void clear() { _map.erase(_map.begin(), _map.end()); } 
00150 
00151   //
00152   // methods involving iteratorTs (to be specified once the iteratorT
00153   // interface is finalized)
00154   //
00155   iteratorT begin() { return _map.begin(); } 
00156   iteratorT end() { return _map.end(); } 
00157   constIteratorT begin() const { return _map.begin(); } 
00158   constIteratorT end() const { return _map.end(); } 
00159 
00160   bool empty() const { return _map.empty(); }
00161 
00162   iteratorT find(const Key& i) { return _map.find(i); }
00163   constIteratorT find(const Key& i) const { return _map.find(i); }
00164 
00165   //
00166   // assignment and equality comparison
00167   // 
00168   _scv_associative_array& operator=(const _scv_associative_array& other) { 
00169     if (this != &other) {
00170       _map = other._map;
00171     } 
00172     return *this;
00173   }
00174   bool is_equal(const _scv_associative_array<Key, Data, container_type> & other)  const ;
00175 
00176   //
00177   // return the number of explicit element allocated through []
00178   // but not yet erased through erase().
00179   //
00180   int numExplicitElt() const { return _map.size(); } 
00181   int num_explicit_elt() const { return _map.size(); } 
00182  
00183   //
00184   // return TRUE if there is at least one explicit element.
00185   // allocated through [] but not yet erased through erase().
00186   //
00187   bool hasExplicitElt() const { return numExplicitElt() > 0; } 
00188   bool has_explicit_elt() const { return numExplicitElt() > 0; } 
00189  
00190   //
00191   // return the number of elements with values differ from 
00192   // the default value. 
00193   // We just count them! (nkhalil)
00194   //
00195   int numNonDefaultElt() const ;
00196   int num_non_default_elt() const ;
00197 
00198   //
00199   // return TRUE if there is any element with values differ from 
00200   // the default value 
00201   //
00202   int hasNonDefaultElt() const;
00203   int has_non_default_elt() const;
00204 
00205   virtual void print(ostream& os=scv_out, int details=0, int indent=0) const {
00206   typename container_type::const_iterator temp;
00207   os << kind() << " Name: " << get_name() << endl;
00208   if ( ! details ) return;
00209   for (temp = _map.begin(); temp != _map.end(); ++temp) {
00210     os << "\t[ " << (*temp).first << " ] = ";
00211     os << (*temp).second << endl;
00212   }
00213   }
00214 
00215   virtual void show(int details=0, int indent=0) const {
00216     print(scv_out, details, indent);
00217   }
00218 
00219   //int get_debug() const {return -1;}
00220   //void set_debug(int) {}
00221 };
00222 
00223 template <class Key, class Data, typename container_type>
00224 ostream& operator<<(ostream&os, const _scv_associative_array<Key, Data, container_type>& t){
00225   t.print(os, 1);
00226   return(os);
00227 }
00228 
00229 template <class Key, class Data, typename container_type> 
00230 bool _scv_associative_array<Key, Data, container_type>::is_equal(const _scv_associative_array<Key, Data, container_type>& other) const {
00231   return this->_map == other._map;
00232 }
00233 
00234 template <class Key, class Data, typename container_type> 
00235 bool operator==(const _scv_associative_array<Key, Data, container_type>& a,
00236     const _scv_associative_array<Key, Data, container_type>& b){ 
00237   return a.is_equal(b);
00238 }
00239 
00240 template <class Key, class Data, typename container_type> 
00241 int _scv_associative_array<Key, Data, container_type>::numNonDefaultElt() const { 
00242   int nelements = 0;
00243   constIteratorT temp;
00244   for(temp = _map.begin(); temp != _map.end(); ++temp) { 
00245     if ((*temp).second != _defaultValue) nelements++;
00246   }
00247   return nelements;
00248 }
00249  
00250 template <class Key, class Data, typename container_type> 
00251 int _scv_associative_array<Key, Data, container_type>::num_non_default_elt() const { 
00252 return _scv_associative_array<Key, Data, container_type>::numNonDefaultElt();
00253 }
00254 
00255 template <class Key, class Data, typename container_type> 
00256 int _scv_associative_array<Key, Data, container_type>::hasNonDefaultElt() const { 
00257   constIteratorT temp;
00258   for(temp = _map.begin(); temp != _map.end(); ++temp) { 
00259     if ((*temp).second != _defaultValue) return 1;
00260   }
00261   return 0;
00262 } 
00263 
00264 template <class Key, class Data, typename container_type> 
00265 int _scv_associative_array<Key, Data, container_type>::has_non_default_elt() const { 
00266 return _scv_associative_array<Key, Data, container_type>::hasNonDefaultElt();
00267 }
00268 
00269 #if defined(_USE_HASH_MAP)
00270 // to support usage of hash_map instead of map 
00271 
00272 template<class T>
00273 class hash<T*> : public hash<T> {
00274 public:
00275   size_t operator()(T * i) const { return hash<T>::operator()((T) i)
00276 ; }
00277 };
00278 
00279 template<>
00280 class hash<string> : public hash<int> {
00281 public:
00282   size_t operator()(const string &i) const { return hash<int>::operator()((int) atoi(i.c_str())); }
00283 };
00284 
00285 #endif
00286 
00287 #endif // SCV_ASSOCIATIVE_ARRAY_H
00288 
00289 
00290 

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