ADMB Documentation  11.5.3197
 All Classes Files Functions Variables Typedefs Friends Defines
f3arr.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id$
00003  *
00004  * Author: David Fournier
00005  * Copyright (c) 2008-2012 Regents of the University of California
00006  */
00011 #include "fvar.hpp"
00012 
00017  void dvar3_array::initialize()
00018  {
00019    if (!(!(*this)))  // only initialize allocated objects
00020    {
00021      for (int i=slicemin();i<=slicemax();i++)
00022      {
00023        elem(i).initialize();
00024      }
00025    }
00026  }
00027 
00032  dvar3_array dvar3_array::sub(int nrl,int nrh)
00033  {
00034    if (allocated(*this))
00035    {
00036      dvar3_array tmp(nrl,nrh);
00037      for (int i=nrl; i<=nrh; i++)
00038      {
00039        tmp[i].shallow_copy((*this)(i));
00040      }
00041      return tmp;
00042    }
00043    else
00044    {
00045      return *this;
00046    }
00047  }
00048 
00053  dvar3_array::dvar3_array (int nrl,int nrh)
00054  {
00055    allocate(nrl,nrh);
00056  }
00057 
00062  dvar3_array::dvar3_array(int sl,int sh,int nrl,int nrh,int ncl,int nch)
00063  {
00064    if (sh<sl)
00065    {
00066      allocate();
00067      return;
00068    }
00069    allocate(sl,sh,nrl,nrh,ncl,nch);
00070 #ifndef OPT_LIB
00071    initialize();
00072 #endif
00073  }
00074 
00079  void dvar3_array::allocate(int sl,int sh,int nrl,int nrh,int ncl,int nch)
00080  {
00081    if (sh<sl)
00082    {
00083      allocate();
00084      return;
00085    }
00086    #ifdef DIAG
00087      myheapcheck("Entering dvar3_array matrix(sl,sh,nrl,nrh,ncl,nch)" );
00088    #endif
00089    if ( (shape=new three_array_shape(sl,sh)) == 0)
00090    {
00091      cerr << " Error allocating memory in dvar3_array contructor" << endl;
00092    }
00093    int ss=slicesize();
00094    if ( (t = new dvar_matrix[ss]) == 0)
00095    {
00096      cerr << " Error allocating memory in dvar3_array contructor" << endl;
00097      ad_exit(21);
00098    }
00099    t -= slicemin();
00100    for (int i=sl; i<=sh; i++)
00101    {
00102      t[i].allocate(nrl,nrh,ncl,nch);
00103    }
00104  }
00105 
00110  void dvar3_array::allocate(int sl,int sh,int nrl,int nrh)
00111  {
00112    if (sh<sl)
00113    {
00114      allocate();
00115      return;
00116    }
00117    #ifdef DIAG
00118      myheapcheck("Entering dvar3_array matrix(sl,sh,nrl,nrh,ncl,nch)" );
00119    #endif
00120    if ( (shape=new three_array_shape(sl,sh)) == 0)
00121    {
00122      cerr << " Error allocating memory in dvar3_array contructor" << endl;
00123    }
00124    int ss=slicesize();
00125    if ( (t = new dvar_matrix[ss]) == 0)
00126    {
00127      cerr << " Error allocating memory in dvar3_array contructor" << endl;
00128      ad_exit(21);
00129    }
00130    t -= slicemin();
00131    for (int i=sl; i<=sh; i++)
00132    {
00133      t[i].allocate(nrl,nrh);
00134    }
00135  }
00136 
00141  void dvar3_array::allocate(int sl,int sh,
00142     const index_type& nrl,const index_type& nrh)
00143  {
00144    if (sh<sl)
00145    {
00146      allocate();
00147      return;
00148    }
00149    #ifdef DIAG
00150      myheapcheck("Entering dvar3_array matrix(sl,sh,nrl,nrh,ncl,nch)" );
00151    #endif
00152    if ( (shape=new three_array_shape(sl,sh)) == 0)
00153    {
00154      cerr << " Error allocating memory in dvar3_array contructor" << endl;
00155    }
00156    int ss=slicesize();
00157    if ( (t = new dvar_matrix[ss]) == 0)
00158    {
00159      cerr << " Error allocating memory in dvar3_array contructor" << endl;
00160      ad_exit(21);
00161    }
00162    t -= slicemin();
00163    for (int i=sl; i<=sh; i++)
00164    {
00165      t[i].allocate(nrl(i),nrh(i));
00166    }
00167  }
00168 
00173  void dvar3_array::allocate(int sl,int sh)
00174  {
00175    if (sh<sl)
00176    {
00177      allocate();
00178      return;
00179    }
00180    #ifdef DIAG
00181      myheapcheck("Entering dvar3_array matrix(sl,sh,nrl,nrh,ncl,nch)" );
00182    #endif
00183    if ( (shape=new three_array_shape(sl,sh)) == 0)
00184    {
00185      cerr << " Error allocating memory in dvar3_array contructor" << endl;
00186    }
00187    int ss=slicesize();
00188    if ( (t = new dvar_matrix[ss]) == 0)
00189    {
00190      cerr << " Error allocating memory in dvar3_array contructor" << endl;
00191      ad_exit(21);
00192    }
00193    t -= slicemin();
00194    for (int i=sl; i<=sh; i++)
00195    {
00196      t[i].allocate();
00197    }
00198  }
00199 
00204 dvar3_array::dvar3_array(int sl, int sh, int nrl, int nrh,
00205   const ivector& ncl, int nch)
00206  {
00207    if (sh<sl)
00208    {
00209      allocate();
00210      return;
00211    }
00212    allocate(sl,sh,nrl,nrh,ncl,nch);
00213 #ifndef OPT_LIB
00214    initialize();
00215 #endif
00216  }
00217 
00222 void dvar3_array::allocate(int sl, int sh, int nrl, int nrh,
00223   const ivector& ncl, int nch)
00224  {
00225    if (sh<sl)
00226    {
00227      allocate();
00228      return;
00229    }
00230    #ifdef DIAG
00231      myheapcheck("Entering dvar3_array matrix(sl,sh,nrl,nrh,ncl,nch)" );
00232    #endif
00233    if ( (shape=new three_array_shape(sl,sh)) == 0)
00234    {
00235      cerr << " Error allocating memory in dvar3_array contructor" << endl;
00236    }
00237    int ss=slicesize();
00238    if ( (t = new dvar_matrix[ss]) == 0)
00239    {
00240      cerr << " Error allocating memory in dvar3_array contructor" << endl;
00241      ad_exit(21);
00242    }
00243    t -= slicemin();
00244    for (int i=sl; i<=sh; i++)
00245    {
00246      t[i].allocate(nrl,nrh,ncl(i),nch);
00247    }
00248  }
00249 
00254 void dvar3_array::allocate(int sl, int sh, int nrl, int nrh,
00255   const ivector& ncl, const ivector& nch)
00256  {
00257    if (sh<sl)
00258    {
00259      allocate();
00260      return;
00261    }
00262    #ifdef DIAG
00263      myheapcheck("Entering dvar3_array matrix(sl,sh,nrl,nrh,ncl,nch)" );
00264    #endif
00265    if (sl !=ncl.indexmin() || sh !=ncl.indexmax() ||
00266        sl !=nch.indexmin() || sh !=nch.indexmax())
00267    {
00268      cerr << "Incompatible array bounds in "
00269      "dmatrix(int nrl,int nrh, const ivector& ncl, const ivector& nch)" << endl;
00270      ad_exit(1);
00271    }
00272    if ( (shape=new three_array_shape(sl,sh)) == 0)
00273    {
00274      cerr << " Error allocating memory in dvar3_array contructor" << endl;
00275    }
00276    int ss=slicesize();
00277    if ( (t = new dvar_matrix[ss]) == 0)
00278    {
00279      cerr << " Error allocating memory in dvar3_array contructor" << endl;
00280      ad_exit(21);
00281    }
00282    t -= slicemin();
00283    for (int i=sl; i<=sh; i++)
00284    {
00285      t[i].allocate(nrl,nrh,ncl(i),nch(i));
00286    }
00287  }
00288 
00293 void dvar3_array::allocate(int sl, int sh, int nrl, int nrh,
00294   int ncl, const ivector& nch)
00295  {
00296    if (sh<sl)
00297    {
00298      allocate();
00299      return;
00300    }
00301    #ifdef DIAG
00302      myheapcheck("Entering dvar3_array matrix(sl,sh,nrl,nrh,ncl,nch)" );
00303    #endif
00304    if (sl !=nch.indexmin() || sh !=nch.indexmax())
00305    {
00306      cerr << "Incompatible array bounds in "
00307      "dmatrix(int nrl,int nrh,int ncl, const ivector& nch)" << endl;
00308      ad_exit(1);
00309    }
00310    if ( (shape=new three_array_shape(sl,sh)) == 0)
00311    {
00312      cerr << " Error allocating memory in dvar3_array contructor" << endl;
00313    }
00314    int ss=slicesize();
00315    if ( (t = new dvar_matrix[ss]) == 0)
00316    {
00317      cerr << " Error allocating memory in dvar3_array contructor" << endl;
00318      ad_exit(21);
00319    }
00320    t -= slicemin();
00321    for (int i=sl; i<=sh; i++)
00322    {
00323      t[i].allocate(nrl,nrh,ncl,nch(i));
00324    }
00325  }
00326 
00331 dvar3_array::dvar3_array(const d3_array& m1)
00332  {
00333    allocate(m1);
00334    for (int i=slicemin(); i<=slicemax(); i++)
00335    {
00336      t[i]=m1[i];
00337    }
00338  }
00339 
00344 void dvar3_array::allocate(const d3_array& m1)
00345  {
00346    #ifdef DIAG
00347      myheapcheck("Entering dvar3_array matrix(const d3_array& m1)" );
00348    #endif
00349    if ( (shape=new three_array_shape(m1.indexmin(),m1.indexmax())) == 0)
00350    {
00351      cerr << " Error allocating memory in dvar3_array contructor" << endl;
00352    }
00353    int ss=slicesize();
00354    if ( (t = new dvar_matrix[ss]) == 0)
00355    {
00356      cerr << " Error allocating memory in dvar3_array contructor" << endl;
00357      ad_exit(21);
00358    }
00359    t -= slicemin();
00360    for (int i=slicemin(); i<=slicemax(); i++)
00361    {
00362      t[i].allocate(m1[i]);
00363    }
00364  }
00365 
00370 void dvar3_array::allocate(const dvar3_array& m1)
00371  {
00372    #ifdef DIAG
00373      myheapcheck("Entering dvar3_array matrix(const d3_array& m1)" );
00374    #endif
00375    if ( (shape=new three_array_shape(m1.slicemin(),m1.slicemax())) == 0)
00376    {
00377      cerr << " Error allocating memory in dvar3_array contructor" << endl;
00378    }
00379    int ss=slicesize();
00380    if ( (t = new dvar_matrix[ss]) == 0)
00381    {
00382      cerr << " Error allocating memory in dvar3_array contructor" << endl;
00383      ad_exit(21);
00384    }
00385    t -= slicemin();
00386    for (int i=slicemin(); i<=slicemax(); i++)
00387    {
00388      t[i].allocate(m1[i]);
00389    }
00390  }
00391 
00396 dvar3_array::dvar3_array(int sl, int sh, const ivector& nrl, const ivector& nrh,
00397   ivector& ncl, const ivector& nch)
00398  {
00399    if (sh<sl)
00400    {
00401      allocate();
00402      return;
00403    }
00404    allocate(sl,sh,nrl,nrh,ncl,nch);
00405 #ifndef OPT_LIB
00406    initialize();
00407 #endif
00408  }
00409 
00414 void dvar3_array::allocate(int sl, int sh, const ivector& nrl,
00415   const ivector& nrh, const ivector& ncl, const ivector& nch)
00416  {
00417    if (sh<sl)
00418    {
00419      allocate();
00420      return;
00421    }
00422    #ifdef DIAG
00423      myheapcheck("Entering d3_array matrix(sl,sh,nrl,nrh,ncl,nch)" );
00424    #endif
00425    if ( (shape=new three_array_shape(sl,sh)) == 0)
00426    {
00427      cerr << " Error allocating memory in dvar3_array contructor" << endl;
00428    }
00429    int ss=slicesize();
00430    if ( (t = new dvar_matrix[ss]) == 0)
00431    {
00432      cerr << " Error allocating memory in dvar3_array contructor" << endl;
00433      ad_exit(21);
00434    }
00435    t -= slicemin();
00436    for (int i=sl; i<=sh; i++)
00437    {
00438      t[i].allocate(nrl[i],nrh[i],ncl[i],nch[i]);
00439    }
00440  }
00441 
00446 dvar3_array::dvar3_array(int sl, int sh, int nrl, const ivector& nrh,
00447   int ncl, const ivector& nch)
00448  {
00449    if (sh<sl)
00450    {
00451      allocate();
00452      return;
00453    }
00454    allocate(sl,sh,nrl,nrh,ncl,nch);
00455 #ifndef OPT_LIB
00456    initialize();
00457 #endif
00458  }
00459 
00464 void dvar3_array::allocate(int sl, int sh, int nrl, const ivector& nrh,
00465   int ncl, const ivector& nch)
00466  {
00467    if (sh<sl)
00468    {
00469      allocate();
00470      return;
00471    }
00472    #ifdef DIAG
00473      myheapcheck("Entering d3_array matrix(sl,sh,nrl,nrh,ncl,nch)" );
00474    #endif
00475    if ( (shape=new three_array_shape(sl,sh)) == 0)
00476    {
00477      cerr << " Error allocating memory in dvar3_array contructor" << endl;
00478    }
00479    int ss=slicesize();
00480    if ( (t = new dvar_matrix[ss]) == 0)
00481    {
00482      cerr << " Error allocating memory in dvar3_array contructor" << endl;
00483      ad_exit(21);
00484    }
00485    t -= slicemin();
00486    for (int i=sl; i<=sh; i++)
00487    {
00488      t[i].allocate(nrl,nrh[i],ncl,nch[i]);
00489    }
00490  }
00491 
00496 dvar3_array::dvar3_array(int sl, int sh, int nrl, const ivector& nrh,
00497   int ncl,int nch)
00498  {
00499    if (sh<sl)
00500    {
00501      allocate();
00502      return;
00503    }
00504    allocate(sl,sh,nrl,nrh,ncl,nch);
00505 #ifndef OPT_LIB
00506    initialize();
00507 #endif
00508  }
00509 
00514 void dvar3_array::allocate(int sl, int sh, int nrl, const ivector& nrh,
00515     int ncl,int nch)
00516  {
00517    #ifdef DIAG
00518      myheapcheck("Entering d3_array matrix(sl,sh,nrl,nrh,ncl,nch)" );
00519    #endif
00520    if ( (shape=new three_array_shape(sl,sh)) == 0)
00521    {
00522      cerr << " Error allocating memory in dvar3_array contructor" << endl;
00523    }
00524    int ss=slicesize();
00525    if ( (t = new dvar_matrix [ss]) == 0)
00526    {
00527      cerr << " Error allocating memory in dvar3_array contructor" << endl;
00528      ad_exit(21);
00529    }
00530    t -= slicemin();
00531    for (int i=sl; i<=sh; i++)
00532    {
00533      t[i].allocate(nrl,nrh[i],ncl,nch);
00534    }
00535  }
00536 
00541 dvar3_array::dvar3_array(const dvar3_array& m2)
00542  {
00543    shape=m2.shape;
00544    if (shape) (shape->ncopies)++;
00545    t = m2.t;
00546  }
00547 
00552 void dvar3_array::shallow_copy(const dvar3_array& m2)
00553  {
00554    shape=m2.shape;
00555    if (shape) (shape->ncopies)++;
00556    t = m2.t;
00557  }
00558 
00563  dvar3_array::~dvar3_array()
00564  {
00565    if (shape)
00566    {
00567      if (shape->ncopies)
00568      {
00569        (shape->ncopies)--;
00570      }
00571      else
00572      {
00573        deallocate();
00574      }
00575    }
00576 #ifdef SAFE_ALL
00577    else
00578    {
00579      cerr << "Warning -- trying to deallocate an unallocated dmatrix"<<endl;
00580    }
00581 #endif
00582  }
00583 
00588  void dvar3_array::deallocate()
00589  {
00590    if (shape)
00591    {
00592      t += slicemin();
00593      //int ss=slicesize();
00594      delete [] t;
00595      delete shape;
00596      t=NULL;
00597      shape=NULL;
00598    }
00599 #ifdef SAFE_ALL
00600    else
00601    {
00602      cerr << "Warning -- trying to deallocate an unallocated dmatrix"<<endl;
00603    }
00604 #endif
00605  }
00606 
00611 void dvar3_array::allocate(int sl, int sh, const ivector& nrl,
00612   const ivector& nrh, int ncl, int nch)
00613  {
00614    if (sl !=nrl.indexmin() || sh !=nrl.indexmax() ||
00615        sl !=nrh.indexmin() || sh !=nrh.indexmax())
00616    {
00617      cerr << "Incompatible array bounds in "
00618      "dmatrix(int nrl,int nrh, const ivector& ncl, const ivector& nch)" << endl;
00619      ad_exit(1);
00620    }
00621    if ( (shape=new three_array_shape(sl,sh)) == 0)
00622    {
00623      cerr << " Error allocating memory in dvar3_array contructor" << endl;
00624    }
00625    int ss=slicesize();
00626    if ( (t = new dvar_matrix[ss]) == 0)
00627    {
00628      cerr << " Error allocating memory in dvar3_array contructor" << endl;
00629      ad_exit(21);
00630    }
00631    t -= slicemin();
00632    for (int i=sl; i<=sh; i++)
00633    {
00634      t[i].allocate(nrl(i),nrh(i),ncl,nch);
00635    }
00636  }
00637 
00642 void dvar3_array::allocate(int sl, int sh, const ivector& nrl, int nrh,
00643   int ncl, int nch)
00644  {
00645    if (sh<sl)
00646    {
00647      allocate();
00648      return;
00649    }
00650    if (sl !=nrl.indexmin() || sh !=nrl.indexmax())
00651    {
00652      cerr << "Incompatible array bounds in "
00653      "dmatrix(int nrl,int nrh, const ivector& ncl, const ivector& nch)" << endl;
00654      ad_exit(1);
00655    }
00656    if ( (shape=new three_array_shape(sl,sh)) == 0)
00657    {
00658      cerr << " Error allocating memory in dvar3_array contructor" << endl;
00659    }
00660    int ss=slicesize();
00661    if ( (t = new dvar_matrix[ss]) == 0)
00662    {
00663      cerr << " Error allocating memory in dvar3_array contructor" << endl;
00664      ad_exit(21);
00665    }
00666    t -= slicemin();
00667    for (int i=sl; i<=sh; i++)
00668    {
00669      t[i].allocate(nrl(i),nrh,ncl,nch);
00670    }
00671  }