ADMB Documentation  11.5.3197
 All Classes Files Functions Variables Typedefs Friends Defines
i3arr.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 //#include "i3_array.h"
00013 
00018  i3_array::i3_array(void)
00019  {
00020    allocate();
00021  }
00022 
00027  i3_array::~i3_array()
00028  {
00029    if (shape)
00030    {
00031      if (shape->ncopies)
00032      {
00033        (shape->ncopies)--;
00034      }
00035      else
00036      {
00037        deallocate();
00038      }
00039    }
00040    else
00041    {
00042     // cerr << "Warning -- trying to deallocate an unallocated i3_array"<<endl;
00043    }
00044  }
00045 
00050  void i3_array::allocate(int sl,int sh,int nrl,int nrh,int ncl,int nch)
00051  {
00052    if ( (shape=new three_array_shape(sl,sh)) == 0)
00053    {
00054      cerr << " Error allocating memory in i3_array contructor" << endl;
00055    }
00056    int ss=slicesize();
00057    if ( (t = new imatrix[ss]) == 0)
00058    {
00059      cerr << " Error allocating memory in i3_array contructor" << endl;
00060      ad_exit(21);
00061    }
00062    t -= slicemin();
00063    for (int i=sl; i<=sh; i++)
00064    {
00065      t[i].allocate(nrl,nrh,ncl,nch);
00066    }
00067  }
00068 
00073  void i3_array::allocate(int sl,int sh)
00074  {
00075    if ( (shape=new three_array_shape(sl,sh)) == 0)
00076    {
00077      cerr << " Error allocating memory in i3_array contructor" << endl;
00078    }
00079    int ss=slicesize();
00080    if ( (t = new imatrix[ss]) == 0)
00081    {
00082      cerr << " Error allocating memory in i3_array contructor" << endl;
00083      ad_exit(21);
00084    }
00085    t -= slicemin();
00086    for (int i=sl; i<=sh; i++)
00087    {
00088      t[i].allocate();
00089    }
00090  }
00091 
00096  i3_array::i3_array(int sl,int sh,int nrl,int nrh,const ivector& ncl,
00097   const ivector& nch)
00098  {
00099    allocate(sl,sh,nrl,nrh,ncl,nch);
00100  }
00101 
00106  i3_array::i3_array(int sl,int sh)
00107  {
00108    allocate(sl,sh);
00109  }
00110 
00115  i3_array::i3_array(int sl,int sh,int nrl,const ivector& nrh,int ncl,
00116    int nch)
00117  {
00118    allocate(sl,sh,nrl,nrh,ncl,nch);
00119  }
00120 
00125  void i3_array::allocate(int sl,int sh,int nrl,int nrh,const ivector& ncl,
00126   const ivector& nch)
00127  {
00128    if ( (shape=new three_array_shape(sl,sh)) == 0)
00129    {
00130      cerr << " Error allocating memory in i3_array contructor" << endl;
00131    }
00132    int ss=slicesize();
00133    if ( (t = new imatrix[ss]) == 0)
00134    {
00135      cerr << " Error allocating memory in i3_array contructor" << endl;
00136      ad_exit(21);
00137    }
00138    t -= slicemin();
00139    for (int i=sl; i<=sh; i++)
00140    {
00141      t[i].allocate(nrl,nrh,ncl(i),nch(i));
00142    }
00143  }
00144 
00149  void i3_array::allocate(int sl,int sh,int nrl,const ivector& nrh,int ncl,
00150   int nch)
00151  {
00152    if ( (shape=new three_array_shape(sl,sh)) == 0)
00153    {
00154      cerr << " Error allocating memory in i3_array contructor" << endl;
00155    }
00156    int ss=slicesize();
00157    if ( (t = new imatrix[ss]) == 0)
00158    {
00159      cerr << " Error allocating memory in i3_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,nrh(i),ncl,nch);
00166    }
00167  }
00168 
00173 i3_array::i3_array(int sl, int sh, int nrl, const ivector& nrh,
00174   int ncl, const imatrix& nch)
00175  {
00176    allocate(sl,sh,nrl,nrh,ncl,nch);
00177  }
00178 
00183  i3_array::i3_array(int sl,int sh,int nrl,int nrh, int ncl,int nch)
00184  {
00185    allocate(sl,sh,nrl,nrh,ncl,nch);
00186  }
00187 
00192 void i3_array::allocate(int sl, int sh, int nrl, const ivector& nrh,
00193   int ncl, const imatrix& nch)
00194  {
00195    //int imin=nrh.indexmin();
00196    //int rmin=nch.rowmin();
00197    //int cmin=nch(rmin).indexmin();
00198 
00199    if ( (shape=new three_array_shape(sl,sh)) == 0)
00200    {
00201      cerr << " Error allocating memory in i3_array contructor" << endl;
00202    }
00203    int ss=slicesize();
00204    if ( (t = new imatrix[ss]) == 0)
00205    {
00206      cerr << " Error allocating memory in i3_array contructor" << endl;
00207      ad_exit(21);
00208    }
00209    t -= slicemin();
00210    for (int i=sl; i<=sh; i++)
00211    {
00212      t[i].allocate(nrl,nrh(i),ncl,nch(i));
00213    }
00214  }
00215 
00220 i3_array::i3_array(int _sl, int _sh, const imatrix& m1)
00221  {
00222    int sl=_sl;
00223    int sh=_sh;
00224    //int nrl=m1.rowmin();
00225    //int nrh=m1.rowmax();
00226    //int ncl=m1.colmin();
00227    //int nch=m1.colmax();
00228    if ( (shape=new three_array_shape(sl,sh)) == 0)
00229    {
00230      cerr << " Error allocating memory in i3_array contructor" << endl;
00231    }
00232    int ss=slicesize();
00233    if ( (t = new imatrix[ss]) == 0)
00234    {
00235      cerr << " Error allocating memory in i3_array contructor" << endl;
00236      ad_exit(21);
00237    }
00238    t -= slicemin();
00239    for (int i=sl; i<=sh; i++)
00240    {
00241      if (m1.shape)
00242      {
00243        t[i].shape=m1.shape;
00244        (t[i].shape->ncopies)++;
00245        t[i].m = m1.m;
00246      }
00247      else
00248      {
00249        t[i].shape=NULL;
00250        t[i].m=NULL;
00251      }
00252    }
00253  }
00254 
00259 void i3_array::allocate(const i3_array& i3v)
00260  {
00261    int sl=i3v.slicemin();
00262    int sh=i3v.slicemax();
00263    //int nrl=i3v.rowmin();
00264    //int nrh=i3v.rowmax();
00265    //int ncl=i3v.colmin();
00266    //int nch=i3v.colmax();
00267    if ( (shape=new three_array_shape(sl,sh)) == 0)
00268    {
00269      cerr << " Error allocating memory in i3_array contructor" << endl;
00270    }
00271    int ss=slicesize();
00272    if ( (t = new imatrix[ss]) == 0)
00273    {
00274      cerr << " Error allocating memory in i3_array contructor" << endl;
00275      ad_exit(21);
00276    }
00277    t -= slicemin();
00278    for (int i=sl; i<=sh; i++)
00279    {
00280      t[i].allocate(i3v[i]);
00281      //elem(i).initialize();
00282    }
00283  }
00284 
00289  void i3_array::allocate(void)
00290  {
00291    shape=NULL;
00292    t = NULL;
00293  }
00294 
00299  void i3_array::initialize()
00300  {
00301    if (!(!(*this)))  // only initialize allocated objects
00302    {
00303      for (int i=slicemin();i<=slicemax();i++)
00304      {
00305        elem(i).initialize();
00306      }
00307    }
00308  }
00309 
00314  i3_array::i3_array(const i3_array& m2)
00315  {
00316    shape=m2.shape;
00317    if (shape)
00318    {
00319      (shape->ncopies)++;
00320    }
00321    else
00322    {
00323      cerr << "Making a copy of an unallocated d3_array"<<endl;
00324    }
00325    t = m2.t;
00326  }
00327 
00332  void i3_array::deallocate()
00333  {
00334    if (shape)
00335    {
00336      t += slicemin();
00337      //int ss=slicesize();
00338      delete [] t;
00339      delete shape;
00340      t=NULL;
00341      shape=NULL;
00342    }
00343    else
00344    {
00345      //cerr << "Warning -- trying to deallocate an unallocated imatrix"<<endl;
00346    }
00347  }