ADMB Documentation  11.1.2445
 All Classes Files Functions Variables Typedefs Friends Defines
indextyp.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: indextyp.cpp 1963 2014-04-30 21:47:58Z johnoel $
00003  *
00004  * Author: David Fournier
00005  * Copyright (c) 2008-2012 Regents of the University of California
00006  */
00011 #include <admodel.h>
00012 
00017   int * smart_counter::get_ncopies(void)
00018   {
00019     return ncopies;
00020   }
00021 
00025 smart_counter::smart_counter(void)
00026 {
00027   ncopies = new int;
00028   *ncopies = 0;
00029 }
00033 smart_counter::smart_counter(const smart_counter& sc)
00034 {
00035     ncopies = sc.ncopies;
00036     (*ncopies)++;
00037 }
00041 smart_counter::~smart_counter(void)
00042 {
00043   if (*ncopies == 0)
00044   {
00045     delete ncopies;
00046     ncopies = 0;
00047   }
00048   else
00049   {
00050     (*ncopies)--;
00051   }
00052 }
00053 
00054   ad_integer::ad_integer(const index_type& it) : d(it.integer()) {}
00055 
00060   index_guts * matrix_index::operator [] (int i)
00061   {
00062     return new vector_index(imatrix::operator [](i));
00063   }
00064 
00069   int index_type::integer(void) const
00070   {
00071     return int(*p);
00072   }
00073   /*
00074   index_type::index_type(const data_int& x)
00075   {
00076     p = new number_index(int(data_int(x)));
00077   }
00078    */
00079 
00084   index_type::index_type(const index_type & it) : smart_counter(it)
00085   {
00086     p = it.p;
00087   }
00088 
00093   index_type::index_type(int x)
00094   {
00095     p = new number_index(x);
00096   }
00097 
00102 index_type::index_type(const ivector& x)
00103   {
00104     p = new vector_index((const ivector&)(x));
00105   }
00106 
00111 index_type::index_type(const imatrix& x)
00112   {
00113     p = new matrix_index((const imatrix&)(x));
00114   }
00115 
00120   matrix_index::~matrix_index()
00121   {
00122     //cout << "in ~matrix_index()" << endl;
00123   }
00124 
00129   index_type::index_type(const i3_array& x)
00130   {
00131     p = new i3_index((i3_array&)(x));
00132   }
00133 
00138   index_type::index_type(const i4_array& x)
00139   {
00140     p = new i4_index((i4_array&)(x));
00141   }
00142 
00147   index_type::index_type(const pre_index_type& pit)
00148   {
00149     p = (*(*(pit.a)).p)[pit.i];
00150   // Dave uncommented this august 1998 because program crashed
00151    // (*p->ncopies)++;
00152   }
00153 
00158   index_type index_type::operator [] (int i) const
00159   {
00160     return pre_index_type(this,i);
00161   }
00162 
00167   index_type index_type::operator () (int i) const
00168   {
00169     return pre_index_type(this,i);
00170   }
00171 
00176   index_type index_type::operator [] (int i)
00177   {
00178     return pre_index_type(this,i);
00179   }
00180 
00185   index_type index_type::operator () (int i)
00186   {
00187     return pre_index_type(this,i);
00188   }
00189 
00190   vector_index::~vector_index() {}
00191 
00196   index_type::~index_type ()
00197   {
00198     if (*get_ncopies()==0)
00199     {
00200       if (!p)
00201       {
00202         cerr << "trying to delete a NULL optr in ~index_type()"  << endl;
00203       }
00204       else
00205       {
00206         if (!(*(p->ncopies)))
00207         {
00208           delete p;
00209           p = NULL;
00210         }
00211         else
00212         {
00213           (*(p->ncopies))--;
00214         }
00215       }
00216     }
00217   }
00218 
00223  dmatrix::dmatrix(const ad_integer& nrl,const ad_integer& nrh,
00224    const index_type& ncl,const index_type& nch)
00225  {
00226    allocate(nrl,nrh,ncl,nch);
00227  }
00228 
00233  void dmatrix::allocate(const ad_integer& nrl,const ad_integer& nrh,
00234    const index_type& ncl,const index_type& nch)
00235  {
00236    if (nrh<nrl)
00237    {
00238      allocate();
00239      return;
00240    }
00241 
00242    if ( (ncl.isinteger() && (nrl !=ncl.indexmin() || nrh !=ncl.indexmax())) ||
00243      (nch.isinteger() && (nrl !=nch.indexmin() || nrh !=nch.indexmax())))
00244    {
00245      cerr << "Incompatible array bounds in dmatrix(int nrl,int nrh,"
00246       "const ivector& ncl, const ivector& nch)" << endl;
00247      ad_exit(1);
00248    }
00249    index_min=int(nrl);
00250    index_max=int(nrh);
00251 
00252    int rs=rowsize();
00253    if ( (m = new dvector [rs]) == 0)
00254    {
00255      cerr << " Error allocating memory in dmatrix contructor" << endl;
00256      ad_exit(21);
00257    }
00258    if ( (shape = new mat_shapex(m))== 0)
00259    {
00260      cerr << " Error allocating memory in dmatrix contructor" << endl;
00261      ad_exit(21);
00262    }
00263    m -= int(nrl);
00264    for (int i=nrl; i<=nrh; i++)
00265    {
00266      m[i].allocate(ncl[i],nch[i]);
00267    }
00268  }
00269 
00274  d3_array::d3_array(const ad_integer& sl,const ad_integer& sh,
00275    const index_type& nrl,const index_type& nrh,
00276    const index_type& ncl,const index_type& nch)
00277  {
00278    allocate(sl,sh,nrl,nrh,ncl,nch);
00279  }
00280 
00285  i3_array::i3_array(int sl,int sh,const index_type& nrl,const index_type& nrh,
00286    const index_type& ncl,const index_type& nch)
00287  {
00288    allocate(sl,sh,nrl,nrh,ncl,nch);
00289  }
00290 
00295  void i3_array::allocate(int sl,int sh,const index_type& nrl,
00296   const index_type& nrh,const index_type& ncl,const index_type& nch)
00297  {
00298    if (sl>sh)
00299    {
00300      allocate();
00301      return;
00302    }
00303 
00304    //int imin=nrh.indexmin();
00305    //int rmin=nch.rowmin();
00306    //int cmin=nch(rmin).indexmin();
00307    if ( (nrl.isinteger() && (sl !=nrl.indexmin() || sh !=nrl.indexmax())) ||
00308        (nch.isinteger() && (sl !=nch.indexmin() || sh !=nch.indexmax())) ||
00309        (ncl.isinteger() && (sl !=ncl.indexmin() || sh !=ncl.indexmax())) ||
00310        (nrh.isinteger() && (sl !=nrh.indexmin() || sh !=nrh.indexmax())) )
00311    {
00312      cout << nrl.isinteger() << endl;
00313      cout << nrl.indexmin() << endl;
00314      cout << nrl.indexmax() << endl;
00315      cout << nrh.isinteger() << endl;
00316      cout << nrh.indexmin() << endl;
00317      cout << nrh.indexmax() << endl;
00318      cout << ncl.isinteger() << endl;
00319      cout << ncl.indexmin() << endl;
00320      cout << ncl.indexmax() << endl;
00321      cout << nch.isinteger() << endl;
00322      cout << nch.indexmin() << endl;
00323      cout << nch.indexmax() << endl;
00324      cerr << "Incompatible array bounds in i3_array(int nrl,int nrh,"
00325       "const index_type& nrl, const index_type& nrh,"
00326       "const index_type& ncl, const index_type& nch)" << endl;
00327      cout << sl << " " << nrl.indexmin() << endl
00328           << sh << " " << nrl.indexmax() << endl
00329           << sl << " " << nrh.indexmin() << endl
00330           << sh << " " << nrh.indexmax() << endl
00331           << sl << " " << ncl.indexmin() << endl
00332           << sh << " " << ncl.indexmax() << endl
00333           << sl << " " << nch.indexmin() << endl
00334           << sh << " " << nch.indexmax() << endl;
00335 
00336      ad_exit(1);
00337    }
00338 
00339    if ( (shape=new three_array_shape(sl,sh)) == 0)
00340    {
00341      cerr << " Error allocating memory in i3_array contructor" << endl;
00342    }
00343    int ss=slicesize();
00344    if ( (t = new imatrix[ss]) == 0)
00345    {
00346      cerr << " Error allocating memory in i3_array contructor" << endl;
00347      ad_exit(21);
00348    }
00349    t -= slicemin();
00350    for (int i=sl; i<=sh; i++)
00351    {
00352      t[i].allocate(nrl(i),nrh(i),ncl(i),nch(i));
00353    }
00354  }
00355 
00360 void d3_array::allocate(const ad_integer& sl,const ad_integer& sh,
00361   const index_type& nrl, const index_type& nrh, const index_type& ncl,
00362   const index_type& nch)
00363  {
00364    if (int(sl)>int(sh))
00365    {
00366      allocate();
00367      return;
00368    }
00369 
00370    if ( (nrl.isinteger() && (sl !=nrl.indexmin() || sh !=nrl.indexmax())) ||
00371        (nrh.isinteger() && (sl !=nrh.indexmin() || sh !=nrh.indexmax())) )
00372    {
00373      cerr << "Incompatible array bounds in i3_array(int nrl,int nrh,"
00374       "const index_type& nrl, const index_type& nrh,"
00375       "const index_type& ncl, const index_type& nch)" << endl;
00376      ad_exit(1);
00377    }
00378 
00379    if ( (shape=new three_array_shape(sl,sh))== 0)
00380    {
00381      cerr << " Error allocating memory in d3_array contructor" << endl;
00382    }
00383 
00384    int ss=slicesize();
00385    if ( (t = new dmatrix[ss]) == 0)
00386    {
00387      cerr << " Error allocating memory in d3_array contructor" << endl;
00388      ad_exit(21);
00389    }
00390    t -= indexmin();
00391    for (int i=sl; i<=sh; i++)
00392    {
00393      t[i].allocate(nrl[i],nrh[i],ncl[i],nch[i]);
00394    }
00395  }
00396 
00401  void imatrix::allocate(const ad_integer& nrl,const ad_integer& nrh,
00402     const index_type& ncl,const index_type& nch)
00403  {
00404    if (nrl>nrh)
00405    {
00406      allocate();
00407      return;
00408    }
00409    index_min=nrl;
00410    index_max=nrh;
00411    if ( (ncl.isinteger() && (nrl !=ncl.indexmin() || nrh !=ncl.indexmax())) ||
00412      (nch.isinteger() && (nrl !=nch.indexmin() || nrh !=nch.indexmax())))
00413    {
00414      cerr << "Incompatible array bounds in "
00415      "dmatrix(int nrl,int nrh, const ivector& ncl, const ivector& nch)\n";
00416      ad_exit(1);
00417    }
00418    int ss=nrh-nrl+1;
00419    if ( (m = new ivector [ss]) == 0)
00420    {
00421      cerr << " Error allocating memory in imatrix contructor\n";
00422      ad_exit(21);
00423    }
00424    if ( (shape = new mat_shapex(m))== 0)
00425    {
00426      cerr << " Error allocating memory in imatrix contructor\n";
00427      ad_exit(21);
00428    }
00429    m -= int(nrl);
00430    for (int i=nrl; i<=nrh; i++)
00431    {
00432      m[i].allocate(ncl(i),nch(i));
00433    }
00434  }
00435 
00440 index_guts::~index_guts()
00441 {
00442   if (!(*ncopies))
00443   {
00444     delete ncopies;
00445     ncopies=NULL;
00446   }
00447 }
00448 
00453  dvector::dvector(const ad_integer& ncl,const index_type& nch)
00454  {
00455    allocate(ncl,nch);
00456  }
00457 
00462  void dvector::allocate(const ad_integer& _ncl,const index_type& _nch)
00463  {
00464    int ncl=_ncl;
00465    int nch=ad_integer(_nch);
00466    int itemp=nch-ncl;
00467    if (itemp<0)
00468    {
00469      allocate();
00470      return;
00471    }
00472    if ( (v = new double [itemp+1]) ==0)
00473    {
00474      cerr << " Error trying to allocate memory for dvector\n";
00475      ad_exit(21);
00476    }
00477 #if defined(THREAD_SAFE)
00478    if ( (shape=new ts_vector_shapex(ncl,nch,v)) == NULL)
00479 #else
00480    if ( (shape=new vector_shapex(ncl,nch,v)) == NULL)
00481 #endif
00482    {
00483      cerr << "Error trying to allocate memory for dvector\n";
00484      ad_exit(1);
00485    }
00486 
00487    index_min=ncl;
00488    index_max=nch;
00489    v -= indexmin();
00490    #ifdef SAFE_INITIALIZE
00491      for ( int i=indexmin(); i<=indexmax(); i++)
00492      {
00493        v[i]=0.;
00494      }
00495    #endif
00496  }
00497 
00502  ivector::ivector(const ad_integer& ncl,const index_type& nch)
00503  {
00504    allocate(ncl,nch);
00505  }
00506 
00511  void ivector::allocate(const ad_integer& _ncl,const index_type& _nch)
00512  {
00513    index_min=_ncl;
00514    index_max=ad_integer(_nch);
00515    int itemp=index_max-index_min;
00516    if (itemp<0)
00517    {
00518      allocate();
00519      return;
00520    }
00521    if ( (v = new int [itemp+1]) ==0)
00522    {
00523      cerr << " Error trying to allocate memory for dvector\n";
00524      ad_exit(21);
00525    }
00526    if ( (shape=new vector_shapex(index_min,index_max,v)) == NULL)
00527    {
00528      cerr << "Error trying to allocate memory for dvector\n";
00529      ad_exit(1);
00530    }
00531 
00532    v -= indexmin();
00533    #ifdef SAFE_INITIALIZE
00534      for ( int i=indexmin(); i<=indexmax(); i++)
00535      {
00536        v[i]=0.;
00537      }
00538    #endif
00539  }
00540 
00545 vector_index::vector_index(const ivector& v) : ivector(v)
00546 {
00547 }
00548 
00553 index_guts::index_guts()
00554 {
00555   ncopies = new int;
00556   *ncopies=0;
00557 }
00558 
00563 index_guts::index_guts(const index_guts& ig)
00564 {
00565   ncopies = ig.ncopies;
00566   (*ncopies)++;
00567 }
00568 
00573 index_guts * number_index::operator [] (int i)
00574 {
00575   return new number_index(int(*this));
00576 }