ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
indextyp.cpp
Go to the documentation of this file.
1 /*
2  * $Id$
3  *
4  * Author: David Fournier
5  * Copyright (c) 2008-2012 Regents of the University of California
6  */
11 #include <admodel.h>
12 
15 {
16  ncopies = new int;
17  *ncopies=0;
18 }
21 {
22  ncopies = ig.ncopies;
23  (*ncopies)++;
24 }
27 {
28  if (!(*ncopies))
29  {
30  delete ncopies;
31  ncopies = NULL;
32  }
33 }
36 {
37  ncopies = new int;
38  *ncopies = 0;
39 }
42 {
43  ncopies = sc.ncopies;
44  (*ncopies)++;
45 }
48 {
49  if (*ncopies == 0)
50  {
51  delete ncopies;
52  ncopies = 0;
53  }
54  else
55  {
56  (*ncopies)--;
57  }
58 }
60 {
61  return ncopies;
62 }
65 {
66  p = it.p;
67 }
68 /*
69 index_type::index_type(const data_int& x)
70 {
71  p = new number_index(int(data_int(x)));
72 }
73 */
79 {
80  p = new number_index(x);
81 }
87 {
88  p = new vector_index((const ivector&)(x));
89 }
95 {
96  p = new matrix_index((const imatrix&)(x));
97 }
103 {
104  p = new i3_index((i3_array&)(x));
105 }
111 {
112  p = new i4_index((i4_array&)(x));
113 }
119 {
120  p = (*(*(pit.a)).p)[pit.i];
121  // Dave uncommented this august 1998 because program crashed
122  // (*p->ncopies)++;
123 }
126 {
127  if (*get_ncopies()==0)
128  {
129  if (!p)
130  {
131  cerr << "trying to delete a NULL optr in ~index_type()" << endl;
132  }
133  else
134  {
135  if (!(*(p->ncopies)))
136  {
137  delete p;
138  p = NULL;
139  }
140  else
141  {
142  (*(p->ncopies))--;
143  }
144  }
145  }
146 }
152 {
153  return pre_index_type(this,i);
154 }
155 
161 {
162  return pre_index_type(this,i);
163 }
164 
170 {
171  return pre_index_type(this,i);
172 }
178 {
179  return pre_index_type(this,i);
180 }
181 int index_type::integer(void) const
182 {
183  return int(*p);
184 }
185 
188 {
189 }
192 
197  dmatrix::dmatrix(const ad_integer& nrl,const ad_integer& nrh,
198  const index_type& ncl,const index_type& nch)
199  {
200  allocate(nrl,nrh,ncl,nch);
201  }
202 
211  const ad_integer& nrl, const ad_integer& nrh,
212  const index_type& ncl, const index_type& nch)
213 {
214  if (nrh < nrl)
215  {
216  allocate();
217  }
218  else
219  {
220  if ((ncl.isinteger() && (nrl != ncl.indexmin() || nrh != ncl.indexmax()))
221  || (nch.isinteger() && (nrl != nch.indexmin() || nrh != nch.indexmax())))
222  {
223  cerr << "Incompatible dmatrix bounds in " << __FILE__ << ':' << __LINE__ << ".\n";
224  ad_exit(1);
225  }
226  index_min = int(nrl);
227  index_max = int(nrh);
228  if ((m = new dvector[rowsize()]) == 0)
229  {
230  cerr << " Error: dmatrix unable to allocate memory in "
231  << __FILE__ << ':' << __LINE__ << '\n';
232  ad_exit(1);
233  }
234  if ((shape = new mat_shapex(m)) == 0)
235  {
236  cerr << " Error: dmatrix unable to allocate memory in "
237  << __FILE__ << ':' << __LINE__ << '\n';
238  ad_exit(1);
239  }
240  m -= int(nrl);
241  for (int i = nrl; i <= nrh; ++i)
242  {
243  m[i].allocate(ncl[i], nch[i]);
244  }
245  }
246 }
247 
253  const index_type& nrl,const index_type& nrh,
254  const index_type& ncl,const index_type& nch)
255  {
256  allocate(sl,sh,nrl,nrh,ncl,nch);
257  }
258 
263  i3_array::i3_array(int sl,int sh,const index_type& nrl,const index_type& nrh,
264  const index_type& ncl,const index_type& nch)
265  {
266  allocate(sl,sh,nrl,nrh,ncl,nch);
267  }
268 
280  int sl, int sh,
281  const index_type& nrl, const index_type& nrh,
282  const index_type& ncl, const index_type& nch)
283 {
284  if (sl > sh)
285  {
286  allocate();
287  }
288  else
289  {
290  if ((nrl.isinteger() && (sl !=nrl.indexmin() || sh !=nrl.indexmax()))
291  || (nch.isinteger() && (sl !=nch.indexmin() || sh !=nch.indexmax()))
292  || (ncl.isinteger() && (sl !=ncl.indexmin() || sh !=ncl.indexmax()))
293  || (nrh.isinteger() && (sl !=nrh.indexmin() || sh !=nrh.indexmax())))
294  {
295  cerr << "Incompatible i3_array bounds in " << __FILE__ << ':' << __LINE__ << ".\n";
296  ad_exit(1);
297  }
298  if ((shape = new three_array_shape(sl,sh)) == 0)
299  {
300  cerr << " Error: d3_array unable to allocate memory in "
301  << __FILE__ << ':' << __LINE__ << '\n';
302  ad_exit(1);
303  }
304  if ((t = new imatrix[slicesize()]) == 0)
305  {
306  cerr << " Error: d3_array unable to allocate memory in "
307  << __FILE__ << ':' << __LINE__ << '\n';
308  ad_exit(1);
309  }
310  t -= slicemin();
311  for (int i = sl; i <= sh; ++i)
312  {
313  t[i].allocate(nrl(i), nrh(i), ncl(i), nch(i));
314  }
315  }
316 }
328  const ad_integer& sl, const ad_integer& sh,
329  const index_type& nrl, const index_type& nrh,
330  const index_type& ncl, const index_type& nch)
331 {
332  if (int(sl) > int(sh))
333  {
334  allocate();
335  }
336  else
337  {
338  if ((nrl.isinteger() && (sl !=nrl.indexmin() || sh !=nrl.indexmax()))
339  || (nrh.isinteger() && (sl !=nrh.indexmin() || sh !=nrh.indexmax())))
340  {
341  cerr << "Incompatible d3_array bounds in " << __FILE__ << ':' << __LINE__ << ".\n";
342  ad_exit(1);
343  }
344  if ((shape = new three_array_shape(sl, sh)) == 0)
345  {
346  cerr << " Error: d3_array unable to allocate memory in "
347  << __FILE__ << ':' << __LINE__ << '\n';
348  ad_exit(1);
349  }
350  if ((t = new dmatrix[slicesize()]) == 0)
351  {
352  cerr << " Error: d3_array unable to allocate memory in "
353  << __FILE__ << ':' << __LINE__ << '\n';
354  ad_exit(1);
355  }
356  t -= indexmin();
357  for (int i = sl; i <= sh; ++i)
358  {
359  t[i].allocate(nrl[i], nrh[i], ncl[i], nch[i]);
360  }
361  }
362 }
371  const ad_integer& nrl, const ad_integer& nrh,
372  const index_type& ncl, const index_type& nch)
373 {
374  if (nrl > nrh)
375  {
376  allocate();
377  }
378  else
379  {
380  index_min = nrl;
381  index_max = nrh;
382  if ((ncl.isinteger() && (nrl != ncl.indexmin() || nrh != ncl.indexmax()))
383  || (nch.isinteger() && (nrl != nch.indexmin() || nrh != nch.indexmax())))
384  {
385  cerr << "Incompatible imatrix bounds in " << __FILE__ << ':' << __LINE__ << ".\n";
386  if(nrh==0)
387  {
388  // Some models use 0 for columns to "turn off" a parameter, so we
389  // don't want to exit in this case, just throw error.
390  cerr << "0 columns - was this intentional?\n" ;
391  }
392  else
393  {
394  ad_exit(1);
395  }
396  }
397  unsigned int ss = static_cast<unsigned int>(nrh - nrl + 1);
398  if ((m = new ivector[ss]) == 0)
399  {
400  cerr << " Error: imatrix unable to allocate memory in "
401  << __FILE__ << ':' << __LINE__ << '\n';
402  ad_exit(1);
403  }
404  if ((shape = new mat_shapex(m)) == 0)
405  {
406  cerr << " Error: imatrix unable to allocate memory in "
407  << __FILE__ << ':' << __LINE__ << '\n';
408  ad_exit(1);
409  }
410  m -= int(nrl);
411  for (int i = nrl; i <= nrh; ++i)
412  {
413  m[i].allocate(ncl(i), nch(i));
414  }
415  }
416 }
417 
422  dvector::dvector(const ad_integer& ncl,const index_type& nch)
423  {
424  allocate(ncl,nch);
425  }
426 
433 void dvector::allocate(const ad_integer& _ncl,const index_type& _nch)
434 {
435  int ncl = _ncl;
436  int nch = ad_integer(_nch);
437  unsigned int ss =
438  static_cast<unsigned int>(nch < ncl ? 0 : nch - ncl + 1);
439  if (ss > 0)
440  {
441  if ((v = new double[ss]) == 0)
442  {
443  cerr << " Error: dvector unable to allocate memory in "
444  << __FILE__ << ':' << __LINE__ << '\n';
445  ad_exit(1);
446  }
447 #if defined(THREAD_SAFE)
448  if ((shape = new ts_vector_shapex(ncl, nch, v)) == NULL)
449 #else
450  if ((shape = new vector_shapex(ncl, nch, v)) == NULL)
451 #endif
452  {
453  cerr << " Error: dvector unable to allocate memory in "
454  << __FILE__ << ':' << __LINE__ << '\n';
455  ad_exit(1);
456  }
457  index_min = ncl;
458  index_max = nch;
459  v -= indexmin();
460 #ifdef SAFE_INITIALIZE
461  for (int i = indexmin(); i <= indexmax(); ++i)
462  {
463  v[i] = 0.0;
464  }
465 #endif
466  }
467  else
468  {
469  allocate();
470  }
471 }
472 
477  ivector::ivector(const ad_integer& ncl,const index_type& nch)
478  {
479  allocate(ncl,nch);
480  }
481 
488 void ivector::allocate(const ad_integer& _ncl, const index_type& _nch)
489 {
490  index_min = _ncl;
491  index_max = ad_integer(_nch);
492  unsigned int ss = static_cast<unsigned int>(
493  index_max < index_min ? 0 : index_max - index_min + 1);
494  if (ss > 0)
495  {
496  if ((v = new int[ss]) == 0)
497  {
498  cerr << " Error: ivector unable to allocate memory in "
499  << __FILE__ << ':' << __LINE__ << '\n';
500  ad_exit(1);
501  }
502  if ((shape = new vector_shapex(index_min, index_max, v)) == NULL)
503  {
504  cerr << " Error: ivector unable to allocate memory in "
505  << __FILE__ << ':' << __LINE__ << '\n';
506  ad_exit(1);
507  }
508  v -= indexmin();
509 #ifdef SAFE_INITIALIZE
510  for (int i = indexmin(); i <= indexmax(); ++i)
511  {
512  v[i] = 0;
513  }
514 #endif
515  }
516  else
517  {
518  allocate();
519  }
520 }
521 
527 {
528  return new number_index(int(*this));
529 }
532 {
533  return new vector_index(imatrix::operator [](i));
534 }
537 {
538  //cout << "in ~matrix_index()" << endl;
539 }
index_guts()
Default constructor.
Definition: indextyp.cpp:14
int * ncopies
Definition: fvar.hpp:7685
int index_min
Definition: fvar.hpp:2822
ivector * m
Definition: imatrix.h:74
void allocate(void)
Does NOT allocate, but initializes empty d3_array.
Definition: d3arr10.cpp:11
double * v
pointer to the data
Definition: dvector.h:53
virtual index_guts * operator[](int i)
Definition: indextyp.cpp:531
vector_index(const ivector &v)
Constructor for ivector.
Definition: indextyp.cpp:187
Uses polymorphism to get index information from various data types to be used in constructing and all...
Definition: fvar.hpp:7731
index_guts * p
Definition: fvar.hpp:7733
ivector()
Default constructor.
Definition: ivector.cpp:37
void allocate(void)
Does not allocate, but initializes imatrix members.
Definition: imat.cpp:138
Description not yet available.
Definition: fvar.hpp:2030
mat_shapex * shape
Definition: imatrix.h:75
dvector()
Construct a dvector without allocating memory.
Definition: dvector.cpp:378
Description not yet available.
Definition: imatrix.h:69
void allocate(void)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: dmat0.cpp:8
Derived class of index types for i3_array.
Definition: fvar.hpp:7866
int isinteger() const
Definition: fvar.hpp:7752
imatrix * t
Definition: fvar.hpp:3946
dmatrix(void)
Default constructor.
Definition: dmat0.cpp:16
#define x
Vector of double precision numbers.
Definition: dvector.h:50
three_array_shape * shape
Definition: fvar.hpp:3730
int indexmin() const
Get minimum valid index.
Definition: dvector.h:199
int index_max
Definition: imatrix.h:73
~smart_counter()
Destructor.
Definition: indextyp.cpp:47
ad_integer(const int &_d, const adkludge &)
Definition: fvar.hpp:7664
index_type operator()(int i)
Description not yet available.
Definition: indextyp.cpp:177
Derived class of index types for ivector.
Definition: fvar.hpp:7809
void allocate(int ncl, int ncu)
Allocate memory for a dvector.
Definition: dvector.cpp:409
number_index(int i)
Definition: fvar.hpp:7793
int * get_ncopies()
Definition: indextyp.cpp:59
Description not yet available.
Definition: fvar.hpp:3698
exitptr ad_exit
Definition: gradstrc.cpp:53
int index_max
Definition: fvar.hpp:2823
void allocate(void)
Allocate dvector without allocating memory.
Definition: dvector.cpp:495
Keeps track of total number of copies.
Definition: fvar.hpp:7716
Derived class of index types for i4_array.
Definition: fvar.hpp:7893
dmatrix * t
Definition: fvar.hpp:3729
Description not yet available.
Definition: fvar.hpp:7638
Derived class of index types for ad_integer.
Definition: fvar.hpp:7772
int slicemin() const
Definition: fvar.hpp:4029
mat_shapex * shape
Definition: fvar.hpp:2825
Abstract base class for different index types.
Definition: fvar.hpp:7682
i3_array(void)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: i3arr.cpp:8
d3_array(void)
Default constructor.
Definition: d3arr10.cpp:20
Description not yet available.
Definition: fvar.hpp:5769
int * ncopies
Definition: fvar.hpp:7718
smart_counter()
Default constructor.
Definition: indextyp.cpp:35
prnstream & endl(prnstream &)
void allocate(void)
Does NOT allocate, but set empty i3_array.
Definition: i3arr.cpp:237
Array of integers(int) with indexes from index_min to indexmax.
Definition: ivector.h:50
virtual ~index_guts()
Destructor.
Definition: indextyp.cpp:26
virtual ~vector_index()
Destructor.
Definition: indextyp.cpp:191
vector_shapex * shape
Definition: ivector.h:56
int index_max
Definition: ivector.h:54
Derived class of index types for imatrix.
Definition: fvar.hpp:7837
int indexmax() const
Get maximum valid index.
Definition: dvector.h:204
Description not yet available.
int indexmin() const
Definition: ivector.h:99
dvector * m
Definition: fvar.hpp:2824
int indexmax() const
Definition: fvar.hpp:7764
int indexmax() const
Definition: ivector.h:104
Description not yet available.
Definition: fvar.hpp:2819
virtual ~matrix_index()
Destructor.
Definition: indextyp.cpp:536
index_type operator[](int i)
Description not yet available.
Definition: indextyp.cpp:169
void allocate()
Does not allocate, but initializes class members.
Definition: ivector.cpp:316
int index_min
minimum valid subscript
Definition: dvector.h:54
Holds &quot;shape&quot; information for vector objects.
Definition: vector_shapex.h:46
long int integer
Definition: cbivnorm.cpp:31
const index_type * a
Definition: fvar.hpp:7640
virtual index_guts * operator[](int i)
Description not yet available.
Definition: indextyp.cpp:526
vector_shapex * shape
pointer to vector &quot;shape&quot;
Definition: dvector.h:60
index_type(const int x)
Description not yet available.
Definition: indextyp.cpp:78
three_array_shape * shape
Definition: fvar.hpp:3947
~index_type()
Destructor.
Definition: indextyp.cpp:125
int integer() const
Definition: indextyp.cpp:181
unsigned int rowsize() const
Definition: fvar.hpp:2934
int indexmin() const
Definition: fvar.hpp:3818
int * v
Definition: ivector.h:55
unsigned int slicesize() const
Definition: fvar.hpp:3853
unsigned int slicesize() const
Definition: fvar.hpp:4054
Stores integer.
Definition: fvar.hpp:7654
Description not yet available.
Definition: fvar.hpp:3944
int index_max
maximum valid subscript
Definition: dvector.h:55
int index_min
Definition: imatrix.h:72
void allocate(const ad_integer &ncl, const index_type &ncu)
Allocate vector of integers with dimension [_ncl to _nch].
Definition: indextyp.cpp:488
int index_min
Definition: ivector.h:53
int indexmin() const
Definition: fvar.hpp:7760