[ee4e53] | 1 | /* emacs edit mode for this file is -*- C++ -*- */ |
---|
[341696] | 2 | /* $Id$ */ |
---|
[ee4e53] | 3 | |
---|
| 4 | //{{{ docu |
---|
| 5 | // |
---|
| 6 | // var_intglobal.cc - internal data layout for global variable |
---|
| 7 | // data. |
---|
| 8 | // |
---|
| 9 | // In this file, some auxiliary classes and template classes are |
---|
| 10 | // defined which help managing global data associated with |
---|
| 11 | // variables. |
---|
| 12 | // |
---|
| 13 | // Used by: var_global.cc |
---|
| 14 | // |
---|
| 15 | //}}} |
---|
| 16 | |
---|
[e4fe2b] | 17 | #include "config.h" |
---|
[ee4e53] | 18 | |
---|
[650f2d8] | 19 | #include "cf_assert.h" |
---|
[ee4e53] | 20 | |
---|
| 21 | #include <string.h> |
---|
| 22 | |
---|
| 23 | #include "var_intglobal.h" |
---|
| 24 | |
---|
| 25 | class Prim; |
---|
| 26 | class InternalPoly; |
---|
| 27 | |
---|
| 28 | //{{{ template <class T> T & PrimitiveArray<T>::operator [] ( int i ) |
---|
| 29 | //{{{ docu |
---|
| 30 | // |
---|
| 31 | // operator []() - get i'th element. |
---|
| 32 | // |
---|
| 33 | // Resize array if necessary. Mark the i'th element as |
---|
| 34 | // accessed. |
---|
| 35 | // |
---|
| 36 | //}}} |
---|
| 37 | template <class T> |
---|
| 38 | T & |
---|
| 39 | PrimitiveArray<T>::operator [] ( int i ) |
---|
| 40 | { |
---|
| 41 | ASSERT( i >= 0, "array index less than zero" ); |
---|
| 42 | if ( i < _size ) { |
---|
[806c18] | 43 | _initializedFlags[ i ] = true; |
---|
| 44 | return _array[ i ]; |
---|
[ee4e53] | 45 | } else { |
---|
[806c18] | 46 | // resize arrays |
---|
| 47 | T * newArray = new T[ i+1 ]; |
---|
| 48 | bool * newFlags = new bool[ i+1 ]; |
---|
| 49 | int j = 0; |
---|
| 50 | // copy old contents |
---|
| 51 | while ( j < _size ) { |
---|
| 52 | newArray[ j ] = _array[ j ]; |
---|
| 53 | newFlags[ j ] = _initializedFlags[ j ]; |
---|
| 54 | j++; |
---|
| 55 | } |
---|
| 56 | // mark rest as uninitialized |
---|
| 57 | while ( j < i ) { |
---|
| 58 | newFlags[ j ] = false; |
---|
| 59 | j++; |
---|
| 60 | } |
---|
| 61 | // actual element mark as initialized |
---|
| 62 | newFlags[ i ] = true; |
---|
| 63 | |
---|
| 64 | // make new arrays actual ones |
---|
| 65 | delete [] _array; |
---|
| 66 | delete [] _initializedFlags; |
---|
| 67 | _array = newArray; |
---|
| 68 | _initializedFlags = newFlags; |
---|
| 69 | _size = i + 1; |
---|
| 70 | |
---|
| 71 | return _array[ i ]; |
---|
[ee4e53] | 72 | } |
---|
| 73 | } |
---|
| 74 | //}}} |
---|
| 75 | |
---|
| 76 | //{{{ template <class T> bool PrimitiveArray<T>::isInitialized ( int i ) const |
---|
| 77 | //{{{ docu |
---|
| 78 | // |
---|
| 79 | // isInitialized() - check whether element i is already |
---|
| 80 | // initialized ( = has been accessed previously). |
---|
| 81 | // |
---|
| 82 | //}}} |
---|
| 83 | template <class T> |
---|
| 84 | bool |
---|
| 85 | PrimitiveArray<T>::isInitialized ( int i ) const |
---|
| 86 | { |
---|
| 87 | ASSERT( i >= 0, "array index less than zero" ); |
---|
| 88 | if ( i >= _size ) |
---|
[806c18] | 89 | return false; |
---|
[ee4e53] | 90 | else |
---|
[806c18] | 91 | return _initializedFlags[ i ]; |
---|
[ee4e53] | 92 | } |
---|
| 93 | //}}} |
---|
| 94 | |
---|
| 95 | //{{{ template <class T> int PrimitiveArray<T>::getFirstUninitialized () const |
---|
| 96 | //{{{ docu |
---|
| 97 | // |
---|
| 98 | // getFirstUninitialized() - get index to first uninitialized |
---|
| 99 | // element. |
---|
| 100 | // |
---|
| 101 | //}}} |
---|
| 102 | template <class T> |
---|
| 103 | int |
---|
| 104 | PrimitiveArray<T>::getFirstUninitialized () const |
---|
| 105 | { |
---|
| 106 | int i = 0; |
---|
| 107 | while ( i < _size && _initializedFlags[ i ] ) |
---|
[806c18] | 108 | i++; |
---|
[ee4e53] | 109 | return i; |
---|
| 110 | } |
---|
| 111 | //}}} |
---|
| 112 | |
---|
| 113 | //{{{ void globalVarData::initializeLongName( char * longName ) |
---|
| 114 | //{{{ docu |
---|
| 115 | // |
---|
| 116 | // initializeLongName() - correctly initialize _longName from |
---|
| 117 | // longName. |
---|
| 118 | // |
---|
| 119 | // _longName is first destroyed. If longName does not equal zero |
---|
| 120 | // we copy it to _longName after allocating memory for it. |
---|
| 121 | // |
---|
| 122 | // Note: be sure that _longName may be destroyed (e.g. when |
---|
| 123 | // calling this function from constructors). |
---|
| 124 | // |
---|
| 125 | //}}} |
---|
| 126 | void |
---|
| 127 | globalVarData::initializeLongName( char * longName ) |
---|
| 128 | { |
---|
| 129 | delete [] _longName; |
---|
| 130 | if ( ! longName ) |
---|
[806c18] | 131 | _longName = 0; |
---|
[ee4e53] | 132 | else { |
---|
[806c18] | 133 | _longName = new char[ strlen( longName ) + 1 ]; |
---|
| 134 | strcpy( _longName, longName ); |
---|
[ee4e53] | 135 | } |
---|
| 136 | } |
---|
| 137 | //}}} |
---|
| 138 | |
---|
| 139 | //{{{ globalVarData::globalVarData ( char name, char * longName ) |
---|
| 140 | //{{{ docu |
---|
| 141 | // |
---|
| 142 | // globalVarData() - (default) constructor. |
---|
| 143 | // |
---|
| 144 | //}}} |
---|
| 145 | globalVarData::globalVarData ( char name, char * longName ) : _name( name ) |
---|
| 146 | { |
---|
| 147 | _longName = 0; |
---|
| 148 | initializeLongName( longName ); |
---|
| 149 | } |
---|
| 150 | //}}} |
---|
| 151 | |
---|
| 152 | //{{{ globalVarData::globalVarData ( const globalVarData & d ) |
---|
| 153 | //{{{ docu |
---|
| 154 | // |
---|
| 155 | // globalVarData() - copy constructor. |
---|
| 156 | // |
---|
| 157 | //}}} |
---|
| 158 | globalVarData::globalVarData ( const globalVarData & d ) : _name( d._name ) |
---|
| 159 | { |
---|
| 160 | _longName = 0; |
---|
| 161 | initializeLongName( d._longName ); |
---|
| 162 | } |
---|
| 163 | //}}} |
---|
| 164 | |
---|
| 165 | //{{{ globalVarData & globalVarData::operator = ( const globalVarData & d ) |
---|
| 166 | //{{{ docu |
---|
| 167 | // |
---|
| 168 | // operator =() - assignment operator. |
---|
| 169 | // |
---|
| 170 | //}}} |
---|
| 171 | globalVarData & |
---|
| 172 | globalVarData::operator = ( const globalVarData & d ) |
---|
| 173 | { |
---|
| 174 | if ( this != &d ) { |
---|
[806c18] | 175 | initializeLongName( d._longName ); |
---|
| 176 | _name = d._name; |
---|
[ee4e53] | 177 | } |
---|
| 178 | return *this; |
---|
| 179 | } |
---|
| 180 | //}}} |
---|
| 181 | |
---|
| 182 | //{{{ bool operator ==, operator != ( const globalVarData & lhs, const globalVarData & rhs ) |
---|
| 183 | //{{{ docu |
---|
| 184 | // |
---|
| 185 | // operator ==(), operator !=() - compare two global data fields. |
---|
| 186 | // |
---|
| 187 | // Two data fields are equal iff their long names are equal. |
---|
| 188 | // |
---|
| 189 | //}}} |
---|
| 190 | bool |
---|
| 191 | operator == ( const globalVarData & lhs, const globalVarData & rhs ) |
---|
| 192 | { |
---|
| 193 | if ( ! lhs._longName && ! rhs._longName ) |
---|
[806c18] | 194 | return true; |
---|
[ee4e53] | 195 | if ( lhs._longName && rhs._longName ) |
---|
[806c18] | 196 | return ! strcmp ( lhs._longName, rhs._longName ); |
---|
[ee4e53] | 197 | else |
---|
[806c18] | 198 | return false; |
---|
[ee4e53] | 199 | } |
---|
| 200 | |
---|
| 201 | bool |
---|
| 202 | operator != ( const globalVarData & lhs, const globalVarData & rhs ) |
---|
| 203 | { |
---|
| 204 | return ! (lhs == rhs); |
---|
| 205 | } |
---|
| 206 | //}}} |
---|
| 207 | |
---|
| 208 | //{{{ globalAlgData & globalAlgData::operator = ( const globalAlgData & d ) |
---|
| 209 | //{{{ docu |
---|
| 210 | // |
---|
| 211 | // operator =() - assignment operator. |
---|
| 212 | // |
---|
| 213 | //}}} |
---|
| 214 | globalAlgData & |
---|
| 215 | globalAlgData::operator = ( const globalAlgData & d ) |
---|
| 216 | { |
---|
| 217 | if ( this != &d ) { |
---|
[806c18] | 218 | globalVarData::operator= ( d ); |
---|
| 219 | _mipo = d._mipo; |
---|
| 220 | _reduce = d._reduce; |
---|
[ee4e53] | 221 | } |
---|
| 222 | return *this; |
---|
| 223 | } |
---|
| 224 | //}}} |
---|
| 225 | |
---|
| 226 | //{{{ globalAlgNumData & globalAlgNumData::operator = ( const globalAlgNumData & d ) |
---|
| 227 | //{{{ docu |
---|
| 228 | // |
---|
| 229 | // operator =() - assignment operator. |
---|
| 230 | // |
---|
| 231 | //}}} |
---|
| 232 | globalAlgNumData & |
---|
| 233 | globalAlgNumData::operator = ( const globalAlgNumData & d ) |
---|
| 234 | { |
---|
| 235 | if ( this != &d ) { |
---|
[806c18] | 236 | (globalVarData)*this = d; |
---|
| 237 | _prim = d._prim; |
---|
[ee4e53] | 238 | } |
---|
| 239 | return *this; |
---|
| 240 | } |
---|
| 241 | //}}} |
---|
| 242 | |
---|
| 243 | // for convenience, the templates are instantiated in this place |
---|
| 244 | // instead of in ftmpl_inst.cc |
---|
| 245 | template class PrimitiveArray<globalVarData>; |
---|
| 246 | template class PrimitiveArray<globalAlgNumData>; |
---|
| 247 | template class PrimitiveArray<globalAlgData>; |
---|