source: git/factory/var_intglobal.cc @ 59d00e

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