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

scv_sparse_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_sparse_array.h -- 
00022   A sparse 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_SPARSE_ARRAY_H
00044 #define SCV_SPARSE_ARRAY_H
00045 
00046 #include <systemc.h>
00047 #include <limits.h>
00048 #include "scv/scv_config.h"
00049 #include "scv/_scv_associative_array.h"
00050 #include "scv/scv_report.h"
00051 
00052 #include <math.h>
00053 #include <map>
00054 
00055 // scv_sparse_array (with integer indexing) (using map)
00056 
00057 template <class Key, class T>
00058 class scv_sparse_array : public _scv_associative_array<Key, T, map<Key, T, less<Key> > > {
00059   T _dummy;     // only for the purpose of error recovery
00060   Key _indexLB;
00061   Key _indexUB;
00062 public:
00063 
00064 typedef map<Key, T, less<Key> > container_type;
00065 
00066   // constructor
00067   scv_sparse_array(const char *nameP, const T& defaultValue,
00068       const Key& indexLB=0 , const Key& indexUB=INT_MAX ):
00069 
00070     _scv_associative_array<Key, T, container_type>(nameP, defaultValue),
00071     _dummy(defaultValue),
00072     _indexLB(indexLB), 
00073     _indexUB(indexUB)
00074   { };
00075 
00076   //
00077   // copy constructor (create new sparse array with same values as argument)
00078   // 
00079   scv_sparse_array(const scv_sparse_array& other, const char *nameP="<anonymous>") 
00080     : _scv_associative_array<Key, T, container_type>(other, nameP),
00081     _dummy(other._dummy),
00082     _indexLB(other._indexLB), _indexUB(other._indexUB){}
00083 
00084   //
00085   // virtual destructor
00086   //
00087   virtual ~scv_sparse_array() {}
00088 
00089   virtual const char* kind() const
00090     { static const char name[]="scv_sparse_array"; return name; }
00091 
00092   //
00093   // parameter access (corresponds to the argument of the constructor arguments
00094   // 
00095   const Key& indexLB() const { return _indexLB; };
00096   const Key& indexUB() const { return _indexUB; };
00097 
00098   const Key& lower_bound() const { return _indexLB; };
00099   const Key& upper_bound() const { return _indexUB; };
00100 
00101   //
00102   // accessing an element
00103   //
00104   // If "i" is out-of-bound, issues warning and return default value;
00105   // modifying T& returned from operator[] will have no effect on the array.
00106   // 
00107   // 
00108   T& operator[](const Key& i) { 
00109     if ((i >= _indexLB) &&  (i <= _indexUB)) 
00110       return _scv_associative_array<Key, T, container_type>::operator[](i);
00111     else {
00112       // _scv_message do not handle yet systemc objects.
00113       //_scv_message::message(_scv_message::SPARSE_ARRAY_ILLEGAL_INDEX,
00114       //    i, this->nameP(), _indexLB, _indexUB);
00115       scv_out << "*** SCV_ERROR: SPARSE_ARRAY_ILLEGAL_INDEX: " << i << endl ;
00116       scv_out << "In scv_sparse_array '" << this->nameP() << "'." << endl;
00117       scv_out << "The valid range is lower = " << _indexLB ;
00118       scv_out << ", upper = " << _indexUB << endl; 
00119       // return dummy so that if the user changes its value,
00120       // it won't affect the other operations
00121       return _dummy; 
00122     }
00123   }
00124   const T& operator[](const Key& i) const { 
00125     if (!((i >= _indexLB) &&  (i <= _indexUB))) { 
00126       //_scv_message::message(_scv_message::SPARSE_ARRAY_ILLEGAL_INDEX,
00127       //    i, this->nameP(), _indexLB, _indexUB);
00128       scv_out << "*** SCV_ERROR: SPARSE_ARRAY_ILLEGAL_INDEX: " << i << endl ;
00129       scv_out << "In scv_sparse_array '" << this->nameP() << "'." << endl;
00130       scv_out << "The valid range is lower = " << _indexLB ;
00131       scv_out << ", upper = " << _indexUB << endl; 
00132       return _dummy;
00133     } else 
00134       return _scv_associative_array<Key, T, container_type>::operator[](i);
00135   } 
00136 
00137   //
00138   // assignment and equality comparison
00139   // 
00140   scv_sparse_array& operator=(const scv_sparse_array<Key, T>& other) { 
00141     if (this != &other) {
00142       *((_scv_associative_array<Key, T, container_type> *) this) =
00143   (_scv_associative_array<Key, T, container_type> ) other;
00144       _indexUB = other._indexUB;
00145       _indexLB = other._indexLB;
00146     } 
00147     return *this;
00148   }
00149 
00150   virtual void print(ostream& os=scv_out, int details=0, int indent=0) const {
00151   //typename container_type::const_iterator temp;
00152   //os << kind() << " Name: " << get_name() << endl;
00153   _scv_associative_array<Key, T,container_type>::print(os,details,indent);
00154   }
00155 
00156   virtual void show(int details=0, int indent=0) const {
00157     print(scv_out, details, indent);
00158   }
00159 
00160 };
00161 
00162 #endif // SCV_SPARSE_ARRAY_H

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