Subversion Repositories OpenSees

Rev

Rev 1276 | Rev 2152 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 fmk 1
/* ****************************************************************** **
2
**    OpenSees - Open System for Earthquake Engineering Simulation    **
3
**          Pacific Earthquake Engineering Research Center            **
4
**                                                                    **
5
**                                                                    **
6
** (C) Copyright 1999, The Regents of the University of California    **
7
** All Rights Reserved.                                               **
8
**                                                                    **
9
** Commercial use of this program without express permission of the   **
10
** University of California, Berkeley, is strictly prohibited.  See   **
11
** file 'COPYRIGHT'  in main directory for information on usage and   **
12
** redistribution,  and for a DISCLAIMER OF ALL WARRANTIES.           **
13
**                                                                    **
14
** Developed by:                                                      **
15
**   Frank McKenna (fmckenna@ce.berkeley.edu)                         **
16
**   Gregory L. Fenves (fenves@ce.berkeley.edu)                       **
17
**   Filip C. Filippou (filippou@ce.berkeley.edu)                     **
18
**                                                                    **
19
** ****************************************************************** */
20
 
1745 fmk 21
// $Revision: 1.7 $
22
// $Date: 2004-04-12 19:11:31 $
2 fmk 23
// $Source: /usr/local/cvs/OpenSees/SRC/matrix/ID.cpp,v $
24
 
25
 
26
// Written: fmk 
27
// Revision: A
28
//
29
// Description: This file contains the class implementation for ID.
30
//
31
// What: "@(#) ID.C, revA"
32
 
33
#include "ID.h"
34
#include <stdlib.h>
35
 
36
 
37
int ID::ID_NOT_VALID_ENTRY = 0;
38
 
39
// ID():
40
//      Standard constructor, sets size = 0;
41
 
42
ID::ID()
947 mhscott 43
  :sz(0), data(0), arraySize(0), fromFree(0)
2 fmk 44
{
45
 
46
}
47
 
48
 
49
// ID(int size):
50
//      Constructor used to allocate a ID of size size.
51
 
52
ID::ID(int size)
947 mhscott 53
  :sz(size), data(0), arraySize(size), fromFree(0)
2 fmk 54
{
55
 
56
#ifdef _G3DEBUG
57
  if (sz <= 0) {
1276 fmk 58
    opserr << "ID::ID(int) - size " << size << " specified <= 0\n";
2 fmk 59
    sz = 1;
60
    arraySize = 1;
61
  }
62
#endif    
63
 
64
  // create the space for the data & check space was available
1745 fmk 65
  //  data = (int *)malloc(size*sizeof(int));
66
  data = new int[size];
2 fmk 67
  if (data == 0) {
1271 fmk 68
    opserr << "ID::ID(int): ran out of memory with size " << size << endln;
2 fmk 69
    exit(-1);
70
  }
71
 
72
  // zero the data
73
  for (int i=0; i<size; i++)
74
    data[i] = 0;
75
}
76
 
77
 
78
// ID(int size):
79
//      Constructor used to allocate a ID of size size.
80
 
81
ID::ID(int size, int arraySz)
947 mhscott 82
  :sz(size), data(0), arraySize(arraySz), fromFree(0)
2 fmk 83
{
84
#ifdef _G3DEBUG
85
  if (sz < 0) {
1276 fmk 86
    opserr << "ID::ID(size, arraySize) - size " << size << " specified < 0\n";
2 fmk 87
    sz = 0;
88
  }
89
  if (arraySz <= 0) {
1276 fmk 90
    opserr << "ID::ID(size, arraySize) - arraySize " << arraySz << " specified < 0\n";
2 fmk 91
    if (sz != 0)
92
      arraySz = sz;
93
    else
94
      arraySz = 1;
95
  }
96
  if (arraySz < sz) {
1276 fmk 97
    opserr << "ID::ID(size, arraySize) - arraySize " << arraySz  << " specified < " << size << endln;
2 fmk 98
    arraySz = sz;
99
  }
100
#endif    
101
 
102
  // create the space
1745 fmk 103
  //  data = (int *)malloc(arraySize*sizeof(int));
104
  data = new int[arraySize];
2 fmk 105
  if (data == 0) {
1271 fmk 106
    opserr << "ID::ID(int, int): ran out of memory with arraySize: " << arraySize << endln;
2 fmk 107
    exit(-1);
108
  }
109
 
110
  // zero the data
111
  for (int i=0; i<arraySize; i++)
112
    data[i] = 0;
113
}
114
 
1745 fmk 115
ID::ID(int *d, int size, bool cleanIt)
947 mhscott 116
  :sz(size), data(d), arraySize(size), fromFree(1)
117
{
1271 fmk 118
  if (d == 0) { // OOPS must have been other constructor we wanted
119
    sz = 0;
120
    data = 0;
121
    arraySize = size;
122
    fromFree = 0;
2 fmk 123
 
1271 fmk 124
    // create the space
125
    if (arraySize != 0) {
126
      data = (int *)malloc(arraySize*sizeof(int));
127
      if (data == 0) {
128
        opserr << "ID::ID(int, int): ran out of memory with arraySize " << arraySize << endln;
129
        exit(-1);
130
      }
131
    }
132
 
133
    // zero the data
134
    for (int i=0; i<arraySize; i++)
135
      data[i] = 0;
136
  }
1745 fmk 137
 
138
  if (cleanIt == true)
139
    fromFree = 0;
947 mhscott 140
}
2 fmk 141
 
142
// ID(const ID&):
143
//      Constructor to init a ID from another.
144
 
145
ID::ID(const ID &other)
947 mhscott 146
  :sz(other.sz), data(0), arraySize(other.arraySize), fromFree(0)
2 fmk 147
{
148
  // create the space
1745 fmk 149
  //  data = (int *)malloc(arraySize*sizeof(int));
150
  data = new int[arraySize];
2 fmk 151
  if (data == 0) {
1271 fmk 152
    opserr << "ID::ID(ID): ran out of memory with arraySize " << arraySize << endln,
2 fmk 153
    exit(-1);
154
  }
155
 
156
  // copy the data 
157
  for (int i=0; i<sz; i++)
158
    data[i] = other.data[i];
159
}      
160
 
161
 
162
 
163
// ~ID():
164
//      destructor, deletes the [] data
165
 
166
ID::~ID()
167
{
947 mhscott 168
  if (data != 0 && fromFree == 0)
1745 fmk 169
    //    free((void *)data);
170
    delete [] data;
2 fmk 171
}
172
 
1137 fmk 173
int
1745 fmk 174
ID::setData(int *newData, int size, bool cleanIt){
1137 fmk 175
  if (data != 0 && fromFree == 0)
1745 fmk 176
    //    free((void *)data);
177
    delete [] data;
2 fmk 178
 
1137 fmk 179
  sz = size;
180
  data = newData;
1745 fmk 181
 
182
  if (cleanIt == false)
183
    fromFree = 1;
184
  else
185
    fromFree = 0;
1137 fmk 186
 
187
  if (sz <= 0) {
1271 fmk 188
    opserr << "ID::ID(int *, size) - size " << size << " specified <= 0\n";
1137 fmk 189
    sz = 0;
190
  }
191
 
192
  return 0;
193
}
194
 
195
 
2 fmk 196
void
197
ID::Zero(void)
198
{
199
  for (int i=0; i<sz; i++)
200
    data[i] =0;
201
}
202
 
203
int
204
ID::getLocation(int value) const
205
{
206
  // search through ID for the value
207
  for (int i=0; i<sz; i++)
208
    if (data[i] == value)
209
      return i;
210
 
211
  // if we get here the value is not in the array
212
  return -1;
213
}
214
 
215
int
216
ID::removeValue(int value)
217
{
218
  int place = -1;
219
  for (int i=0; i<sz; i++)
220
    if (data[i] == value) {
221
      place = i;
222
      // copy the rest of the components down one in ID
223
      for (int j=i; j<sz-1; j++)
224
        data[j] = data[j+1];           
225
      sz--;
226
    }
227
  return place;
228
}    
229
 
230
 
231
int &
232
ID::operator[](int x)
233
{
234
#ifdef _G3DEBUG
235
  // check if it is inside range [0,sz-1]
236
  if (x < 0) {
1276 fmk 237
    opserr << "ID::[] - location " << x << " < 0\n";
2 fmk 238
    return ID_NOT_VALID_ENTRY;
239
  }
240
#endif
241
 
242
  // see if quick return
243
  if (x < sz)
244
    return data[x];
1271 fmk 245
 
2 fmk 246
  /*
247
   * otherwise we have to enlarge the order of the ID
248
   */
249
 
250
  // see if we can just enlarge the array
251
  // without having to go get more space
252
 
253
  if (x < arraySize) {
254
    for (int i=sz; i<x; i++)
255
      data[i] = 0;
256
    sz = x+1;
257
    return data[x];
258
  }
259
 
260
  // otherwise we go get more space
261
  if (x >= arraySize) {
262
    int newArraySize = arraySize * 2;
263
    if (newArraySize < x)
264
      newArraySize = x;
1745 fmk 265
    //    int *newData = (int *)malloc(newArraySize*sizeof(int));    
266
    int *newData = new int[newArraySize];
2 fmk 267
    if (newData != 0) {
268
      // copy the old
269
      for (int i=0; i<sz; i++)
270
        newData[i] = data[i];
271
      // zero the new
272
      for (int j=sz; j<arraySize; j++)
273
        newData[j] = 0;
274
 
275
      sz = x+1;
276
      // release the memory held by the old
1745 fmk 277
      //      free((void *)data);           
278
      delete [] data;
2 fmk 279
      data = newData;
280
      arraySize = newArraySize;
281
 
282
      return newData[x];
283
    }
284
    else {
285
      // we could not allocate more mem .. leave the current size
1271 fmk 286
      opserr << "ID::[]): ran out of memory with arraySize " << arraySize << endln;
2 fmk 287
      return ID_NOT_VALID_ENTRY;
288
    }
289
  }
290
 
291
  // we should never get here, but some compilers need this line
292
  return ID_NOT_VALID_ENTRY;   
293
}
294
 
295
 
296
 
297
// ID &operator=(const ID  &V):
298
//      the assignment operator, This is assigned to be a copy of V. if sizes
299
//      are not compatable this.data [] is deleted. The data pointers will not
300
//      point to the same area in mem after the assignment.
301
//
302
 
303
ID &
304
ID::operator=(const ID &V)
305
{
306
    // first check we are not trying v = v
307
    if (this != &V) {
308
 
309
        // check size compatability, if different delete
310
        // old and make room for new.
311
        if (sz != V.sz) {
312
            if (arraySize < V.sz) {
313
                arraySize = V.sz;
314
                if (data != 0)
1745 fmk 315
                  //free((void *)data);
316
                  delete [] data;
317
                //              data = (int *)malloc(arraySize*sizeof(int));            
318
                data = new int[arraySize];
2 fmk 319
                // check we got the memory requested
320
                if (data == 0) {
1271 fmk 321
                    opserr << "WARNING ID::=(ID) - ran out of memory ";
322
                    opserr << "for new array of size" << arraySize << endln;
2 fmk 323
                    sz = 0;
324
                    arraySize = 0;
325
                }
326
            }
968 fmk 327
            sz = V.sz;
2 fmk 328
        }
329
 
330
        // copy the data
331
        for (int i=0; i<sz; i++)
332
            data[i] = V(i);
333
    }
334
 
335
    return *this;
336
}
337
 
338
 
339
 
340
 
341
 
1271 fmk 342
// friend OPS_Stream &operator<<(OPS_Stream &s, const ID &V)
343
//      A function is defined to allow user to print the IDs using OPS_Streams.
2 fmk 344
 
1271 fmk 345
OPS_Stream &operator<<(OPS_Stream &s, const ID &V)
2 fmk 346
{
347
    for (int i=0; i<V.Size(); i++)
348
    {
349
        s << V(i) << " ";
350
    }
1271 fmk 351
    return s << endln;
2 fmk 352
}
353
 
354
// friend istream &operator>>(istream &s, ID &V)
355
//      A function is defined to allow user to input the data into a ID which has already
356
//      been constructed with data, i.e. ID(int) or ID(const ID &) constructors.
357
 
1271 fmk 358
/*
2 fmk 359
istream &operator>>(istream &s, ID &V)
360
{
361
    for (int i=0; i<V.Size(); i++)
362
        s >> V(i);
363
 
364
    return s;
365
}
1271 fmk 366
*/
2 fmk 367
 
368
 
369
 
370