ADMB Documentation  11.2.2853
 All Classes Files Functions Variables Typedefs Friends Defines
ivector.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: ivector.cpp 2750 2014-12-04 20:55:27Z 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   assert(x);
00217 #endif
00218   allocate(0, (int)(sz - 1));
00219 
00220   if (v)
00221   {
00222     for (unsigned int i = 0; i < sz; i++)
00223     {
00224 #ifdef OPT_LIB
00225       v[i] = (int)x[i];
00226 #else
00227       long int xi = x[i];
00228       assert(xi <= INT_MAX);
00229       v[i] = static_cast<int>(xi);
00230 #endif
00231     }
00232   }
00233 }
00234 
00238 ivector::ivector(const dvector& u)
00239 {
00240   allocate(u);
00241   for (int i=indexmin();i<=indexmax();i++)
00242   {
00243 #ifdef OPT_LIB
00244     elem(i) = int(u.elem(i));
00245 #else
00246     double ui = u.elem(i);
00247     assert(ui <= INT_MAX);
00248     v[i] = (int)ui;
00249 #endif
00250   }
00251 }
00252 
00257  ivector::ivector(int ncl,int nch)
00258  {
00259    allocate(ncl, nch);
00260  }
00261 
00266  void ivector::allocate(int ncl,int nch)
00267  {
00268    int itemp=nch-ncl;
00269    if (itemp<0)
00270    {
00271      //cerr << "Error in ivector constructor max index must be >= minindex\n"
00272      //  << "minindex = " << ncl << " maxindex = " << nch <<endl;
00273      //ad_exit(1);
00274      allocate();
00275    }
00276    else
00277    {
00278      if ( (v = new int [itemp+1]) == 0 )
00279      {
00280        cerr << " Error trying to allocate memory for ivector\n";
00281        ad_exit(21);
00282      }
00283 
00284      if ( (shape=new vector_shapex(ncl,nch,v)) == NULL)
00285      {
00286        cerr << "Error trying to allocate memory for ivector\n";
00287        ad_exit(1);
00288      }
00289 
00290      index_min=ncl;
00291      index_max=nch;
00292      v -= indexmin();
00293      #ifdef SAFE_INITIALIZE
00294        for ( int i=indexmin(); i<=indexmax(); i++)
00295        {
00296          v[i]=0.;
00297        }
00298      #endif
00299    }
00300  }
00301 
00306 void ivector::allocate(const dvector& dv)
00307 {
00308   allocate(dv.indexmin(),dv.indexmax());
00309 }
00310 
00315 void ivector::allocate(const ivector& dv)
00316 {
00317   allocate(dv.indexmin(),dv.indexmax());
00318 }
00319 
00324 void ivector::allocate(void)
00325 {
00326   shape=NULL;
00327   index_min=1;
00328   index_max=-1;
00329   v = NULL;
00330 }
00331 
00336 ivector::ivector(const preivector& pdv)
00337  {
00338    #ifdef DIAG
00339     // cout << "starting out in dvector contructor\n";
00340    #endif
00341    shape=pdv.p->shape;
00342    if (shape)
00343    {
00344      (shape->ncopies)++;
00345    }
00346    else
00347    {
00348      cerr << "Taking a subvector  of an unallocated ivector"<<endl;
00349    }
00350    v = pdv.p->v;
00351    index_min=pdv.lb;
00352    index_max=pdv.ub;
00353  }
00354 
00359   int norm2(const ivector& t1)
00360   {
00361     int tmp=0;;
00362    for (int i=t1.indexmin();i<=t1.indexmax();i++)
00363    {
00364      tmp+=t1(i)*t1(i);
00365    }
00366     return(tmp);
00367   }
00368   int sumsq(const ivector& t1) {return(norm2(t1));}
00369 
00374   void clean(ivector& v,int level)
00375   {
00376     int mmax=v.indexmax();
00377     for (int i=level+1;i<=mmax;i++)
00378     {
00379       v(i)=0;
00380     }
00381   }