ADMB Documentation  11.5.3197
 All Classes Files Functions Variables Typedefs Friends Defines
ivector.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 
00013 #ifdef __TURBOC__
00014   #pragma hdrstop
00015   #include <iostream.h>
00016 #endif
00017 
00018 #ifdef __ZTC__
00019   #include <iostream.hpp>
00020 #endif
00021 
00022 #include <stdlib.h>
00023 
00024 #ifdef DIAG
00025 long int _farptr_tolong(void* px);
00026 long int farptr_tolong(void*);
00027 #endif
00028 
00029 #ifndef OPT_LIB
00030   #include <cassert>
00031   #include <climits>
00032 #endif
00033 
00037 ivector::ivector()
00038 {
00039   allocate();
00040 }
00044 ivector::ivector(const ivector& t)
00045 {
00046 #ifdef DIAG
00047     cout << "Copy constructor called for ivector with address "
00048          << _farptr_tolong(t.v) <<"\n";
00049 #endif
00050 
00051   index_min = t.index_min;
00052   index_max = t.index_max;
00053 
00054   shape=t.shape;
00055   if (shape)
00056   {
00057     (shape->ncopies)++;
00058     v = t.v;
00059   }
00060   else
00061   {
00062     v = NULL;
00063   }
00064 }
00070 ivector::~ivector()
00071 {
00072   if (shape)
00073   {
00074     if (shape->ncopies)
00075     {
00076       (shape->ncopies)--;
00077     }
00078     else
00079     {
00080 #ifdef SAFE_ALL
00081   #ifdef DIAG
00082       myheapcheck(" Entering ~dvector");
00083   #endif
00084       if (v == NULL)
00085       {
00086         cerr << " Trying to delete NULL pointer in ~ivector\n";
00087         ad_exit(21);
00088       }
00089 #endif
00090       deallocate();
00091     }
00092   }
00093 }
00098  void ivector::safe_allocate(int ncl,int nch)
00099  {
00100    if (allocated())
00101    {
00102      cerr << "trying to allocate an already allocated dvector " << endl;
00103      ad_exit(1);
00104    }
00105    else
00106    {
00107      allocate(ncl,nch);
00108    }
00109  }
00114 void ivector::deallocate()
00115 {
00116   if (shape)
00117   {
00118     v = (int*)(shape->trueptr);
00119 
00120     if (v)
00121     {
00122       delete [] v;
00123       v = NULL;
00124     }
00125 
00126     delete shape;
00127     shape = NULL;
00128   }
00129 }
00133 void ivector::safe_deallocate()
00134 {
00135   if (shape)
00136   {
00137     if (shape->ncopies)
00138     {
00139       cerr << "trying to deallocate a ivector with copies" << endl;
00140       ad_exit(1);
00141     }
00142     deallocate();
00143   }
00144 }
00145 
00156 void ivector::shallow_copy(const ivector& t)
00157  {
00158    index_min=t.index_min;
00159    index_max=t.index_max;
00160    #ifdef DIAG
00161     cout << "Copy constructor called for ivector with address "
00162          << _farptr_tolong(t.v) <<"\n";
00163    #endif
00164    shape=t.shape;
00165    if (shape)
00166    {
00167      (shape->ncopies)++;
00168      v = t.v;
00169    }
00170  }
00171 
00176 ivector& ivector::operator=(const ivector& t)
00177  {
00178    // disconnect ivector  pointer  from old array
00179    if (::allocated(*this))
00180    {
00181      if (v != t.v)
00182      {
00183        if (indexmin() != t.indexmin() || indexmax() != t.indexmax())
00184        {
00185          cerr << " Array sizes do not match in ivector operator"
00186                  " =(const ivector&)" << endl;
00187          ad_exit(1);
00188        }
00189 
00190        for ( int i=indexmin(); i<=indexmax(); i++)
00191        {
00192          elem(i) = t.elem(i);
00193        }
00194      }
00195    }
00196    else
00197    {
00198      shallow_copy(t);
00199    }
00200    return (*this);
00201  }
00202 
00207 ivector& ivector::operator=(int u)
00208 {
00209   for (int i = indexmin(); i <= indexmax(); i++)
00210   {
00211     elem(i) = u;
00212   }
00213   return (*this);
00214 }
00215 
00219 ivector::ivector(unsigned int sz, long int* x)
00220 {
00221 #ifndef OPT_LIB
00222   assert(sz > 0 && sz <= INT_MAX);
00223   assert(x);
00224 #endif
00225   allocate(0, (int)(sz - 1));
00226 
00227   if (v)
00228   {
00229     for (unsigned int i = 0; i < sz; i++)
00230     {
00231 #ifdef OPT_LIB
00232       v[i] = (int)x[i];
00233 #else
00234       long int xi = x[i];
00235       assert(xi <= INT_MAX);
00236       v[i] = static_cast<int>(xi);
00237 #endif
00238     }
00239   }
00240 }
00241 
00245 ivector::ivector(const dvector& u)
00246 {
00247   allocate(u);
00248   for (int i=indexmin();i<=indexmax();i++)
00249   {
00250 #ifdef OPT_LIB
00251     elem(i) = int(u.elem(i));
00252 #else
00253     double ui = u.elem(i);
00254     assert(ui <= INT_MAX);
00255     v[i] = (int)ui;
00256 #endif
00257   }
00258 }
00259 
00264  ivector::ivector(int ncl,int nch)
00265  {
00266    allocate(ncl, nch);
00267  }
00268 
00273  void ivector::allocate(int ncl,int nch)
00274  {
00275    int itemp=nch-ncl;
00276    if (itemp<0)
00277    {
00278      //cerr << "Error in ivector constructor max index must be >= minindex\n"
00279      //  << "minindex = " << ncl << " maxindex = " << nch <<endl;
00280      //ad_exit(1);
00281      allocate();
00282    }
00283    else
00284    {
00285      if ( (v = new int [itemp+1]) == 0 )
00286      {
00287        cerr << " Error trying to allocate memory for ivector\n";
00288        ad_exit(21);
00289      }
00290 
00291      if ( (shape=new vector_shapex(ncl,nch,v)) == NULL)
00292      {
00293        cerr << "Error trying to allocate memory for ivector\n";
00294        ad_exit(1);
00295      }
00296 
00297      index_min=ncl;
00298      index_max=nch;
00299      v -= indexmin();
00300      #ifdef SAFE_INITIALIZE
00301        for ( int i=indexmin(); i<=indexmax(); i++)
00302        {
00303          v[i]=0.;
00304        }
00305      #endif
00306    }
00307  }
00308 
00313 void ivector::allocate(const dvector& dv)
00314 {
00315   allocate(dv.indexmin(),dv.indexmax());
00316 }
00317 
00322 void ivector::allocate(const ivector& dv)
00323 {
00324   allocate(dv.indexmin(),dv.indexmax());
00325 }
00329 void ivector::allocate()
00330 {
00331   shape = NULL;
00332   index_min = 1;
00333   index_max = -1;
00334   v = NULL;
00335 }
00336 
00341 ivector::ivector(const preivector& pdv)
00342  {
00343    #ifdef DIAG
00344     // cout << "starting out in dvector contructor\n";
00345    #endif
00346    shape=pdv.p->shape;
00347    if (shape)
00348    {
00349      (shape->ncopies)++;
00350    }
00351    else
00352    {
00353      cerr << "Taking a subvector  of an unallocated ivector"<<endl;
00354    }
00355    v = pdv.p->v;
00356    index_min=pdv.lb;
00357    index_max=pdv.ub;
00358  }
00359 
00364   int norm2(const ivector& t1)
00365   {
00366     int tmp=0;;
00367    for (int i=t1.indexmin();i<=t1.indexmax();i++)
00368    {
00369      tmp+=t1(i)*t1(i);
00370    }
00371     return(tmp);
00372   }
00373   int sumsq(const ivector& t1) {return(norm2(t1));}
00374 
00379   void clean(ivector& v,int level)
00380   {
00381     int mmax=v.indexmax();
00382     for (int i=level+1;i<=mmax;i++)
00383     {
00384       v(i)=0;
00385     }
00386   }