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