source: git/factory/var_intglobal.cc @ 362fc67

jengelh-datetimespielwiese
Last change on this file since 362fc67 was 362fc67, checked in by Martin Lee <martinlee84@…>, 11 years ago
chg: remove $Id$
  • 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#include "config.h"
17
18#include "cf_assert.h"
19
20#include <string.h>
21
22#include "var_intglobal.h"
23
24class Prim;
25class 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//}}}
36template <class T>
37T &
38PrimitiveArray<T>::operator [] ( int i )
39{
40    ASSERT( i >= 0, "array index less than zero" );
41    if ( i < _size ) {
42        _initializedFlags[ i ] = true;
43        return _array[ i ];
44    } else {
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 ];
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//}}}
82template <class T>
83bool
84PrimitiveArray<T>::isInitialized ( int i ) const
85{
86    ASSERT( i >= 0, "array index less than zero" );
87    if ( i >= _size )
88        return false;
89    else
90        return _initializedFlags[ i ];
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//}}}
101template <class T>
102int
103PrimitiveArray<T>::getFirstUninitialized () const
104{
105    int i = 0;
106    while ( i < _size && _initializedFlags[ i ] )
107        i++;
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//}}}
125void
126globalVarData::initializeLongName( char * longName )
127{
128    delete [] _longName;
129    if ( ! longName )
130        _longName = 0;
131    else {
132        _longName = new char[ strlen( longName ) + 1 ];
133        strcpy( _longName, longName );
134    }
135}
136//}}}
137
138//{{{ globalVarData::globalVarData ( char name, char * longName )
139//{{{ docu
140//
141// globalVarData() - (default) constructor.
142//
143//}}}
144globalVarData::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//}}}
157globalVarData::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//}}}
170globalVarData &
171globalVarData::operator = ( const globalVarData & d )
172{
173    if ( this != &d ) {
174        initializeLongName( d._longName );
175        _name = d._name;
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//}}}
189bool
190operator == ( const globalVarData & lhs, const globalVarData & rhs )
191{
192    if ( ! lhs._longName && ! rhs._longName )
193        return true;
194    if ( lhs._longName && rhs._longName )
195        return ! strcmp ( lhs._longName, rhs._longName );
196    else
197        return false;
198}
199
200bool
201operator != ( 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//}}}
213globalAlgData &
214globalAlgData::operator = ( const globalAlgData & d )
215{
216    if ( this != &d ) {
217        globalVarData::operator= ( d );
218        _mipo = d._mipo;
219        _reduce = d._reduce;
220    }
221    return *this;
222}
223//}}}
224
225//{{{ globalAlgNumData & globalAlgNumData::operator = ( const globalAlgNumData & d )
226//{{{ docu
227//
228// operator =() - assignment operator.
229//
230//}}}
231globalAlgNumData &
232globalAlgNumData::operator = ( const globalAlgNumData & d )
233{
234    if ( this != &d ) {
235        (globalVarData)*this = d;
236        _prim = d._prim;
237    }
238    return *this;
239}
240//}}}
241
242// for convenience, the templates are instantiated in this place
243// instead of in ftmpl_inst.cc
244template class PrimitiveArray<globalVarData>;
245template class PrimitiveArray<globalAlgNumData>;
246template class PrimitiveArray<globalAlgData>;
Note: See TracBrowser for help on using the repository browser.