ADMB Documentation  11.5.3197
 All Classes Files Functions Variables Typedefs Friends Defines
indextyp.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 <admodel.h>
00012 
00014 index_guts::index_guts()
00015 {
00016   ncopies = new int;
00017   *ncopies=0;
00018 }
00020 index_guts::index_guts(const index_guts& ig)
00021 {
00022   ncopies = ig.ncopies;
00023   (*ncopies)++;
00024 }
00026 index_guts::~index_guts()
00027 {
00028   if (!(*ncopies))
00029   {
00030     delete ncopies;
00031     ncopies = NULL;
00032   }
00033 }
00035 smart_counter::smart_counter()
00036 {
00037   ncopies = new int;
00038   *ncopies = 0;
00039 }
00041 smart_counter::smart_counter(const smart_counter& sc)
00042 {
00043     ncopies = sc.ncopies;
00044     (*ncopies)++;
00045 }
00047 smart_counter::~smart_counter()
00048 {
00049   if (*ncopies == 0)
00050   {
00051     delete ncopies;
00052     ncopies = 0;
00053   }
00054   else
00055   {
00056     (*ncopies)--;
00057   }
00058 }
00059 int* smart_counter::get_ncopies()
00060 {
00061   return ncopies;
00062 }
00064 index_type::index_type(const index_type& it): smart_counter(it)
00065 {
00066   p = it.p;
00067 }
00068 /*
00069 index_type::index_type(const data_int& x)
00070 {
00071   p = new number_index(int(data_int(x)));
00072 }
00073 */
00078 index_type::index_type(int x)
00079 {
00080   p = new number_index(x);
00081 }
00086 index_type::index_type(const ivector& x)
00087 {
00088   p = new vector_index((const ivector&)(x));
00089 }
00094 index_type::index_type(const imatrix& x)
00095 {
00096   p = new matrix_index((const imatrix&)(x));
00097 }
00102 index_type::index_type(const i3_array& x)
00103 {
00104   p = new i3_index((i3_array&)(x));
00105 }
00110 index_type::index_type(const i4_array& x)
00111 {
00112   p = new i4_index((i4_array&)(x));
00113 }
00118 index_type::index_type(const pre_index_type& pit)
00119 {
00120   p = (*(*(pit.a)).p)[pit.i];
00121   // Dave uncommented this august 1998 because program crashed
00122   // (*p->ncopies)++;
00123 }
00125 index_type::~index_type()
00126 {
00127   if (*get_ncopies()==0)
00128   {
00129     if (!p)
00130     {
00131       cerr << "trying to delete a NULL optr in ~index_type()"  << endl;
00132     }
00133     else
00134     {
00135       if (!(*(p->ncopies)))
00136       {
00137         delete p;
00138         p = NULL;
00139       }
00140       else
00141       {
00142         (*(p->ncopies))--;
00143       }
00144     }
00145   }
00146 }
00151 index_type index_type::operator[](int i) const
00152 {
00153   return pre_index_type(this,i);
00154 }
00155 
00160 index_type index_type::operator()(int i) const
00161 {
00162   return pre_index_type(this,i);
00163 }
00164 
00169 index_type index_type::operator[](int i)
00170 {
00171   return pre_index_type(this,i);
00172 }
00177 index_type index_type::operator()(int i)
00178 {
00179   return pre_index_type(this,i);
00180 }
00181 int index_type::integer(void) const
00182 {
00183   return int(*p);
00184 }
00185 
00187 vector_index::vector_index(const ivector& v): ivector(v)
00188 {
00189 }
00191 vector_index::~vector_index() {}
00192 
00197  dmatrix::dmatrix(const ad_integer& nrl,const ad_integer& nrh,
00198    const index_type& ncl,const index_type& nch)
00199  {
00200    allocate(nrl,nrh,ncl,nch);
00201  }
00202 
00207  void dmatrix::allocate(const ad_integer& nrl,const ad_integer& nrh,
00208    const index_type& ncl,const index_type& nch)
00209  {
00210    if (nrh<nrl)
00211    {
00212      allocate();
00213      return;
00214    }
00215 
00216    if ( (ncl.isinteger() && (nrl !=ncl.indexmin() || nrh !=ncl.indexmax())) ||
00217      (nch.isinteger() && (nrl !=nch.indexmin() || nrh !=nch.indexmax())))
00218    {
00219      cerr << "Incompatible array bounds in dmatrix(int nrl,int nrh,"
00220       "const ivector& ncl, const ivector& nch)" << endl;
00221      ad_exit(1);
00222    }
00223    index_min=int(nrl);
00224    index_max=int(nrh);
00225 
00226    int rs=rowsize();
00227    if ( (m = new dvector [rs]) == 0)
00228    {
00229      cerr << " Error allocating memory in dmatrix contructor" << endl;
00230      ad_exit(21);
00231    }
00232    if ( (shape = new mat_shapex(m))== 0)
00233    {
00234      cerr << " Error allocating memory in dmatrix contructor" << endl;
00235      ad_exit(21);
00236    }
00237    m -= int(nrl);
00238    for (int i=nrl; i<=nrh; i++)
00239    {
00240      m[i].allocate(ncl[i],nch[i]);
00241    }
00242  }
00243 
00248  d3_array::d3_array(const ad_integer& sl,const ad_integer& sh,
00249    const index_type& nrl,const index_type& nrh,
00250    const index_type& ncl,const index_type& nch)
00251  {
00252    allocate(sl,sh,nrl,nrh,ncl,nch);
00253  }
00254 
00259  i3_array::i3_array(int sl,int sh,const index_type& nrl,const index_type& nrh,
00260    const index_type& ncl,const index_type& nch)
00261  {
00262    allocate(sl,sh,nrl,nrh,ncl,nch);
00263  }
00264 
00269  void i3_array::allocate(int sl,int sh,const index_type& nrl,
00270   const index_type& nrh,const index_type& ncl,const index_type& nch)
00271  {
00272    if (sl>sh)
00273    {
00274      allocate();
00275      return;
00276    }
00277 
00278    //int imin=nrh.indexmin();
00279    //int rmin=nch.rowmin();
00280    //int cmin=nch(rmin).indexmin();
00281    if ( (nrl.isinteger() && (sl !=nrl.indexmin() || sh !=nrl.indexmax())) ||
00282        (nch.isinteger() && (sl !=nch.indexmin() || sh !=nch.indexmax())) ||
00283        (ncl.isinteger() && (sl !=ncl.indexmin() || sh !=ncl.indexmax())) ||
00284        (nrh.isinteger() && (sl !=nrh.indexmin() || sh !=nrh.indexmax())) )
00285    {
00286      cout << nrl.isinteger() << endl;
00287      cout << nrl.indexmin() << endl;
00288      cout << nrl.indexmax() << endl;
00289      cout << nrh.isinteger() << endl;
00290      cout << nrh.indexmin() << endl;
00291      cout << nrh.indexmax() << endl;
00292      cout << ncl.isinteger() << endl;
00293      cout << ncl.indexmin() << endl;
00294      cout << ncl.indexmax() << endl;
00295      cout << nch.isinteger() << endl;
00296      cout << nch.indexmin() << endl;
00297      cout << nch.indexmax() << endl;
00298      cerr << "Incompatible array bounds in i3_array(int nrl,int nrh,"
00299       "const index_type& nrl, const index_type& nrh,"
00300       "const index_type& ncl, const index_type& nch)" << endl;
00301      cout << sl << " " << nrl.indexmin() << endl
00302           << sh << " " << nrl.indexmax() << endl
00303           << sl << " " << nrh.indexmin() << endl
00304           << sh << " " << nrh.indexmax() << endl
00305           << sl << " " << ncl.indexmin() << endl
00306           << sh << " " << ncl.indexmax() << endl
00307           << sl << " " << nch.indexmin() << endl
00308           << sh << " " << nch.indexmax() << endl;
00309 
00310      ad_exit(1);
00311    }
00312 
00313    if ( (shape=new three_array_shape(sl,sh)) == 0)
00314    {
00315      cerr << " Error allocating memory in i3_array contructor" << endl;
00316    }
00317    int ss=slicesize();
00318    if ( (t = new imatrix[ss]) == 0)
00319    {
00320      cerr << " Error allocating memory in i3_array contructor" << endl;
00321      ad_exit(21);
00322    }
00323    t -= slicemin();
00324    for (int i=sl; i<=sh; i++)
00325    {
00326      t[i].allocate(nrl(i),nrh(i),ncl(i),nch(i));
00327    }
00328  }
00329 
00334 void d3_array::allocate(const ad_integer& sl,const ad_integer& sh,
00335   const index_type& nrl, const index_type& nrh, const index_type& ncl,
00336   const index_type& nch)
00337  {
00338    if (int(sl)>int(sh))
00339    {
00340      allocate();
00341      return;
00342    }
00343 
00344    if ( (nrl.isinteger() && (sl !=nrl.indexmin() || sh !=nrl.indexmax())) ||
00345        (nrh.isinteger() && (sl !=nrh.indexmin() || sh !=nrh.indexmax())) )
00346    {
00347      cerr << "Incompatible array bounds in i3_array(int nrl,int nrh,"
00348       "const index_type& nrl, const index_type& nrh,"
00349       "const index_type& ncl, const index_type& nch)" << endl;
00350      ad_exit(1);
00351    }
00352 
00353    if ( (shape=new three_array_shape(sl,sh))== 0)
00354    {
00355      cerr << " Error allocating memory in d3_array contructor" << endl;
00356    }
00357 
00358    int ss=slicesize();
00359    if ( (t = new dmatrix[ss]) == 0)
00360    {
00361      cerr << " Error allocating memory in d3_array contructor" << endl;
00362      ad_exit(21);
00363    }
00364    t -= indexmin();
00365    for (int i=sl; i<=sh; i++)
00366    {
00367      t[i].allocate(nrl[i],nrh[i],ncl[i],nch[i]);
00368    }
00369  }
00370 
00375  void imatrix::allocate(const ad_integer& nrl,const ad_integer& nrh,
00376     const index_type& ncl,const index_type& nch)
00377  {
00378    if (nrl>nrh)
00379    {
00380      allocate();
00381      return;
00382    }
00383    index_min=nrl;
00384    index_max=nrh;
00385    if ( (ncl.isinteger() && (nrl !=ncl.indexmin() || nrh !=ncl.indexmax())) ||
00386      (nch.isinteger() && (nrl !=nch.indexmin() || nrh !=nch.indexmax())))
00387    {
00388      cerr << "Incompatible array bounds in "
00389      "dmatrix(int nrl,int nrh, const ivector& ncl, const ivector& nch)\n";
00390      ad_exit(1);
00391    }
00392    int ss=nrh-nrl+1;
00393    if ( (m = new ivector [ss]) == 0)
00394    {
00395      cerr << " Error allocating memory in imatrix contructor\n";
00396      ad_exit(21);
00397    }
00398    if ( (shape = new mat_shapex(m))== 0)
00399    {
00400      cerr << " Error allocating memory in imatrix contructor\n";
00401      ad_exit(21);
00402    }
00403    m -= int(nrl);
00404    for (int i=nrl; i<=nrh; i++)
00405    {
00406      m[i].allocate(ncl(i),nch(i));
00407    }
00408  }
00409 
00414  dvector::dvector(const ad_integer& ncl,const index_type& nch)
00415  {
00416    allocate(ncl,nch);
00417  }
00418 
00423  void dvector::allocate(const ad_integer& _ncl,const index_type& _nch)
00424  {
00425    int ncl=_ncl;
00426    int nch=ad_integer(_nch);
00427    int itemp=nch-ncl;
00428    if (itemp<0)
00429    {
00430      allocate();
00431      return;
00432    }
00433    if ( (v = new double [itemp+1]) ==0)
00434    {
00435      cerr << " Error trying to allocate memory for dvector\n";
00436      ad_exit(21);
00437    }
00438 #if defined(THREAD_SAFE)
00439    if ( (shape=new ts_vector_shapex(ncl,nch,v)) == NULL)
00440 #else
00441    if ( (shape=new vector_shapex(ncl,nch,v)) == NULL)
00442 #endif
00443    {
00444      cerr << "Error trying to allocate memory for dvector\n";
00445      ad_exit(1);
00446    }
00447 
00448    index_min=ncl;
00449    index_max=nch;
00450    v -= indexmin();
00451    #ifdef SAFE_INITIALIZE
00452      for ( int i=indexmin(); i<=indexmax(); i++)
00453      {
00454        v[i]=0.;
00455      }
00456    #endif
00457  }
00458 
00463  ivector::ivector(const ad_integer& ncl,const index_type& nch)
00464  {
00465    allocate(ncl,nch);
00466  }
00467 
00472  void ivector::allocate(const ad_integer& _ncl,const index_type& _nch)
00473  {
00474    index_min=_ncl;
00475    index_max=ad_integer(_nch);
00476    int itemp=index_max-index_min;
00477    if (itemp<0)
00478    {
00479      allocate();
00480      return;
00481    }
00482    if ( (v = new int [itemp+1]) ==0)
00483    {
00484      cerr << " Error trying to allocate memory for dvector\n";
00485      ad_exit(21);
00486    }
00487    if ( (shape=new vector_shapex(index_min,index_max,v)) == NULL)
00488    {
00489      cerr << "Error trying to allocate memory for dvector\n";
00490      ad_exit(1);
00491    }
00492 
00493    v -= indexmin();
00494    #ifdef SAFE_INITIALIZE
00495      for ( int i=indexmin(); i<=indexmax(); i++)
00496      {
00497        v[i]=0.;
00498      }
00499    #endif
00500  }
00501 
00506 index_guts * number_index::operator [] (int i)
00507 {
00508   return new number_index(int(*this));
00509 }
00510 ad_integer::ad_integer(const index_type& it) : d(it.integer()) {}
00511 index_guts* matrix_index::operator [] (int i)
00512 {
00513   return new vector_index(imatrix::operator [](i));
00514 }
00515 
00517 matrix_index::~matrix_index()
00518 {
00519   //cout << "in ~matrix_index()" << endl;
00520 }
00521