ADMB Documentation  11.1x.2735
 All Classes Files Functions Variables Typedefs Friends Defines
ivector.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: ivector.cpp 2625 2014-11-12 19:30:52Z johnoel $
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(void)
00038 {
00039   allocate();
00040 }
00044 ivector::ivector(const ivector& t)
00045  {
00046    index_min=t.index_min;
00047    index_max=t.index_max;
00048    #ifdef DIAG
00049     cout << "Copy constructor called for ivector with address "
00050          << _farptr_tolong(t.v) <<"\n";
00051    #endif
00052    shape=t.shape;
00053    if (shape)
00054    {
00055      (shape->ncopies)++;
00056      v = t.v;
00057    }
00058  }
00062 ivector::~ivector()
00063 {
00064   if (shape)
00065   {
00066     if (shape->ncopies)
00067     {
00068       (shape->ncopies)--;
00069     }
00070     else
00071     {
00072       if (v != NULL)
00073       {
00074         deallocate();
00075       }
00076 #ifdef SAFE_ALL
00077       else
00078       {
00079          cerr << " Trying to delete NULL pointer in ~ivector\n";
00080          ad_exit(21);
00081       }
00082 #endif
00083     }
00084   }
00085 }
00086 
00091  void ivector::safe_allocate(int ncl,int nch)
00092  {
00093    if (allocated())
00094    {
00095      cerr << "trying to allocate an already allocated dvector " << endl;
00096      ad_exit(1);
00097    }
00098    else
00099    {
00100      allocate(ncl,nch);
00101    }
00102  }
00103 
00108   void ivector::safe_deallocate()
00109   {
00110     if (shape)
00111     {
00112       if (shape->ncopies)
00113       {
00114         cerr << "trying to deallocate a dvector with copies" << endl;
00115         ad_exit(1);
00116       }
00117     }
00118     else
00119     {
00120       deallocate();
00121     }
00122   }
00123 
00128  void ivector::deallocate()
00129  {
00130    if (shape)
00131    {
00132      v = (int*) (shape->trueptr);
00133      delete [] v;
00134      v=NULL;
00135      delete  shape;
00136    }
00137    else
00138    {
00139      //cerr << "Warning -- trying to deallocate an unitialized ivector" << endl;
00140    }
00141    shape=NULL;
00142  }
00143 
00144 
00149 void ivector::shallow_copy(const ivector& t)
00150  {
00151    index_min=t.index_min;
00152    index_max=t.index_max;
00153    #ifdef DIAG
00154     cout << "Copy constructor called for ivector with address "
00155          << _farptr_tolong(t.v) <<"\n";
00156    #endif
00157    shape=t.shape;
00158    if (shape)
00159    {
00160      (shape->ncopies)++;
00161      v = t.v;
00162    }
00163  }
00164 
00169 ivector& ivector::operator=(const ivector& t)
00170  {
00171    // disconnect ivector  pointer  from old array
00172    if (::allocated(*this))
00173    {
00174      if (v != t.v)
00175      {
00176        if (indexmin() != t.indexmin() || indexmax() != t.indexmax())
00177        {
00178          cerr << " Array sizes do not match in ivector operator"
00179                  " =(const ivector&)" << endl;
00180          ad_exit(1);
00181        }
00182 
00183        for ( int i=indexmin(); i<=indexmax(); i++)
00184        {
00185          elem(i) = t.elem(i);
00186        }
00187      }
00188    }
00189    else
00190    {
00191      shallow_copy(t);
00192    }
00193    return (*this);
00194  }
00195 
00200 ivector& ivector::operator=(int u)
00201 {
00202   for (int i = indexmin(); i <= indexmax(); i++)
00203   {
00204     elem(i) = u;
00205   }
00206   return (*this);
00207 }
00208 
00212 ivector::ivector(unsigned int sz, long int* x )
00213 {
00214 #ifndef OPT_LIB
00215   assert(sz > 0 && sz <= INT_MAX);
00216 #endif
00217   allocate(0, (int)(sz - 1));
00218 
00219   for (unsigned int i = 0; i < sz; i++)
00220   {
00221     //cout << "Doing the assignment in constructor\n";
00222 #ifdef OPT_LIB
00223     v[i] = (int)x[i];
00224 #else
00225     long int xi = x[i];
00226     assert(xi <= INT_MAX);
00227     v[i] = (int)xi;
00228 #endif
00229   }
00230 }
00231 
00235 ivector::ivector(const dvector& u)
00236 {
00237   allocate(u);
00238   for (int i=indexmin();i<=indexmax();i++)
00239   {
00240 #ifdef OPT_LIB
00241     elem(i) = int(u.elem(i));
00242 #else
00243     double ui = u.elem(i);
00244     assert(ui <= INT_MAX);
00245     v[i] = (int)ui;
00246 #endif
00247   }
00248 }
00249 
00254  ivector::ivector(int ncl,int nch)
00255  {
00256    allocate(ncl, nch);
00257  }
00258 
00263  void ivector::allocate(int ncl,int nch)
00264  {
00265    int itemp=nch-ncl;
00266    if (itemp<0)
00267    {
00268      //cerr << "Error in ivector constructor max index must be >= minindex\n"
00269      //  << "minindex = " << ncl << " maxindex = " << nch <<endl;
00270      //ad_exit(1);
00271      allocate();
00272    }
00273    else
00274    {
00275      if ( (v = new int [itemp+1]) == 0 )
00276      {
00277        cerr << " Error trying to allocate memory for ivector\n";
00278        ad_exit(21);
00279      }
00280 
00281      if ( (shape=new vector_shapex(ncl,nch,v)) == NULL)
00282      {
00283        cerr << "Error trying to allocate memory for ivector\n";
00284        ad_exit(1);
00285      }
00286 
00287      index_min=ncl;
00288      index_max=nch;
00289      v -= indexmin();
00290      #ifdef SAFE_INITIALIZE
00291        for ( int i=indexmin(); i<=indexmax(); i++)
00292        {
00293          v[i]=0.;
00294        }
00295      #endif
00296    }
00297  }
00298 
00303 void ivector::allocate(const dvector& dv)
00304 {
00305   allocate(dv.indexmin(),dv.indexmax());
00306 }
00307 
00312 void ivector::allocate(const ivector& dv)
00313 {
00314   allocate(dv.indexmin(),dv.indexmax());
00315 }
00316 
00321 void ivector::allocate(void)
00322 {
00323   shape=NULL;
00324   index_min=1;
00325   index_max=-1;
00326   v = NULL;
00327 }
00328 
00333 ivector::ivector(const preivector& pdv)
00334  {
00335    #ifdef DIAG
00336     // cout << "starting out in dvector contructor\n";
00337    #endif
00338    shape=pdv.p->shape;
00339    if (shape)
00340    {
00341      (shape->ncopies)++;
00342    }
00343    else
00344    {
00345      cerr << "Taking a subvector  of an unallocated ivector"<<endl;
00346    }
00347    v = pdv.p->v;
00348    index_min=pdv.lb;
00349    index_max=pdv.ub;
00350  }
00351 
00356   int norm2(const ivector& t1)
00357   {
00358     int tmp=0;;
00359    for (int i=t1.indexmin();i<=t1.indexmax();i++)
00360    {
00361      tmp+=t1(i)*t1(i);
00362    }
00363     return(tmp);
00364   }
00365   int sumsq(const ivector& t1) {return(norm2(t1));}
00366 
00371   void clean(ivector& v,int level)
00372   {
00373     int mmax=v.indexmax();
00374     for (int i=level+1;i<=mmax;i++)
00375     {
00376       v(i)=0;
00377     }
00378   }