ADMB Documentation  11.1.1916
 All Classes Files Functions Variables Typedefs Friends Defines
i4arr.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: i4arr.cpp 1714 2014-03-01 00:45:28Z johnoel $
00003  *
00004  * Author: David Fournier
00005  * Copyright (c) 2008-2012 Regents of the University of California
00006  */
00011 #include <fvar.hpp>
00012 #include "admb_messages.h"
00013 
00018 void i4_array::allocate(void)
00019 {
00020   t=0;
00021   shape=0;
00022 }
00023 
00028 void i4_array::initialize(void)
00029 {
00030   if (allocated(*this))
00031   {
00032     int mmin=indexmin();
00033     int mmax=indexmax();
00034     for (int i=mmin;i<=mmax;i++)
00035     {
00036       (*this)(i).initialize();
00037     }
00038   }
00039 }
00040 
00045 i4_array::i4_array(void)
00046 {
00047   allocate();
00048 }
00049 
00054 i4_array::i4_array(int hsl,int hsu)
00055 {
00056   allocate(hsl,hsu);
00057 }
00058 
00063 void i4_array::allocate(int hsl,int hsu)
00064 {
00065   int ss=hsu-hsl+1;
00066   if (ss>0)
00067   {
00068     if ( (t = new i3_array[ss]) == 0)
00069     {
00070       cerr << " Error allocating memory in i4_array contructor\n";
00071       ad_exit(21);
00072     }
00073     if ( (shape=new vector_shapex(hsl,hsu,t)) == 0)
00074     {
00075       cerr << " Error allocating memory in i4_array contructor\n";
00076       ad_exit(21);
00077     }
00078     t -= indexmin();
00079     for (int i=hsl; i<=hsu; i++)
00080     {
00081       (*this)(i).allocate();
00082     }
00083   }
00084   else
00085   {
00086     t=0;
00087     shape=0;
00088   }
00089 }
00090 
00095 i4_array::i4_array(int hsl, int hsu, int sl, const ivector& sh, int nrl,
00096   const imatrix& nrh, int ncl, const i3_array& nch)
00097 {
00098   allocate(hsl,hsu,sl,sh,nrl,nrh,ncl,nch);
00099 }
00100 
00105 void i4_array::allocate(int hsl,int hsu,int sl,int sh,int nrl,
00106    int nrh,int ncl,int nch)
00107  {
00108    int ss=hsu-hsl+1;
00109    if (ss>0)
00110    {
00111      if ( (t = new i3_array[ss]) == 0)
00112      {
00113        cerr << " Error allocating memory in i4_array contructor\n";
00114        ad_exit(21);
00115      }
00116      if ( (shape=new vector_shapex(hsl,hsu,t)) == 0)
00117      {
00118        cerr << " Error allocating memory in i4_array contructor\n";
00119        ad_exit(21);
00120      }
00121      t -= indexmin();
00122      for (int i=hsl; i<=hsu; i++)
00123      {
00124        (*this)(i).allocate(sl,sh,nrl,nrh,ncl,nch);
00125      }
00126    }
00127    else
00128    {
00129      t=0;
00130      shape=0;
00131    }
00132  }
00133 
00138  void i4_array::allocate(const ad_integer& hsl,const ad_integer& hsu,
00139    const index_type& sl,const index_type& sh,const index_type& nrl,
00140    const index_type& nrh,const index_type& ncl,const index_type& nch)
00141  {
00142    int ss=hsu-hsl+1;
00143    if (ss>0)
00144    {
00145      if ( (t = new i3_array[ss]) == 0)
00146      {
00147        cerr << " Error allocating memory in i4_array contructor\n";
00148        ad_exit(21);
00149      }
00150      if ( (shape=new vector_shapex(hsl,hsu,t)) == 0)
00151      {
00152        cerr << " Error allocating memory in i4_array contructor\n";
00153        ad_exit(21);
00154      }
00155      t -= indexmin();
00156      for (int i=hsl; i<=hsu; i++)
00157      {
00158        (*this)(i).allocate(ad_integer(sl(i)),ad_integer(sh(i)),nrl(i),nrh(i),
00159          ncl(i),nch(i));
00160      }
00161    }
00162    else
00163    {
00164      t=0;
00165      shape=0;
00166    }
00167  }
00168 
00173 i4_array::i4_array(const i4_array& m2)
00174  {
00175    if (m2.shape)
00176    {
00177      shape=m2.shape;
00178      (shape->ncopies)++;
00179      t = m2.t;
00180    }
00181    else
00182    {
00183      shape=NULL;
00184      t=NULL;
00185    }
00186  }
00187 
00192  i4_array::~i4_array()
00193  {
00194    deallocate();
00195  }
00196 
00201  void i4_array::deallocate()
00202  {
00203    if (shape)
00204    {
00205      if (shape->ncopies)
00206      {
00207        (shape->ncopies)--;
00208      }
00209      else
00210      {
00211        t= (i3_array*) (shape->get_truepointer());
00212        delete [] t;
00213        t=NULL;
00214        delete shape;
00215        shape=NULL;
00216      }
00217    }
00218  }
00219 
00220 #if !defined (OPT_LIB)
00221 
00226     i3_array& i4_array::operator ( ) (int i)
00227     {
00228 #     if defined(SAFE_ARRAYS)
00229       if (i < indexmin() || i > indexmax())
00230       {
00231         ADMB_ARRAY_BOUNDS_ERROR("Index out of bounds",
00232         "i3_array& i4_array::operator ( ) (int i)", indexmin(), indexmax(), i);
00233       }
00234 #     endif
00235       return t[i];
00236     }
00237 
00242     i3_array& i4_array::operator [] (int i)
00243     {
00244 #     if defined(SAFE_ARRAYS)
00245       if (i < indexmin() || i > indexmax())
00246       {
00247         ADMB_ARRAY_BOUNDS_ERROR("Index out of bounds",
00248         "i3_array& i4_array::operator [] (int i)", indexmin(), indexmax(), i);
00249       }
00250 #     endif
00251       return t[i];
00252     }
00253 
00258     imatrix& i4_array::operator ( ) (int i ,int j)
00259     {
00260       return ((*this)(i))(j);
00261     }
00262 
00267     ivector& i4_array::operator ( ) (int i,int j,int k)
00268     {
00269       return (((*this)(i,j))(k));
00270     }
00271 
00276     int& i4_array::operator ( ) (int i,int j,int k,int l)
00277     {
00278       return ( ((*this)(i,j,k))(l));
00279     }
00280 
00285 const i3_array& i4_array::operator()(int i) const
00286     {
00287 #     if defined(SAFE_ARRAYS)
00288       if (i<indexmin() || i>indexmax())
00289       {
00290         cerr << "Index out of bounds in i4_array::operator () (int)"
00291              << endl;
00292         ad_exit(1);
00293       }
00294 #     endif
00295       return t[i];
00296     }
00297 
00302 const i3_array& i4_array::operator[](int i) const
00303     {
00304 #     if defined(SAFE_ARRAYS)
00305       if (i<indexmin() || i>indexmax())
00306       {
00307         cerr << "Index out of bounds in i4_array::operator () (int)"
00308              << endl;
00309         ad_exit(1);
00310       }
00311 #     endif
00312       return t[i];
00313     }
00314 
00319 const imatrix& i4_array::operator()(int i, int j) const
00320     {
00321       return ((*this)(i))(j);
00322     }
00323 
00328 const ivector& i4_array::operator()(int i, int j, int k) const
00329     {
00330       return (((*this)(i,j))(k));
00331     }
00332 
00337 const int& i4_array::operator()(int i, int j, int k, int l) const
00338     {
00339       return ( ((*this)(i,j,k))(l));
00340     }
00341 #endif