ADMB Documentation  11.1.2364
 All Classes Files Functions Variables Typedefs Friends Defines
ivector.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: ivector.cpp 2311 2014-09-09 22:59:47Z 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   allocate(0,sz-1);
00215 
00216   for (unsigned int i=0; i<sz; i++)
00217   {
00218     cout << "Doing the assignment in constructor\n";
00219 #ifdef OPT_LIB
00220     v[i] = x[i];
00221 #else
00222     long int xi = x[i];
00223     assert(xi <= INT_MAX);
00224     v[i] = (int)xi;
00225 #endif
00226   }
00227 }
00228 
00232 ivector::ivector(const dvector& u)
00233 {
00234   allocate(u);
00235   for (int i=indexmin();i<=indexmax();i++)
00236   {
00237 #ifdef OPT_LIB
00238     elem(i) = int(u.elem(i));
00239 #else
00240     double ui = u.elem(i);
00241     assert(ui <= INT_MAX);
00242     v[i] = (int)ui;
00243 #endif
00244   }
00245 }
00246 
00251  ivector::ivector(int ncl,int nch)
00252  {
00253    allocate(ncl, nch);
00254  }
00255 
00260  void ivector::allocate(int ncl,int nch)
00261  {
00262    int itemp=nch-ncl;
00263    if (itemp<0)
00264    {
00265      //cerr << "Error in ivector constructor max index must be >= minindex\n"
00266      //  << "minindex = " << ncl << " maxindex = " << nch <<endl;
00267      //ad_exit(1);
00268      allocate();
00269    }
00270    else
00271    {
00272      if ( (v = new int [itemp+1]) == 0 )
00273      {
00274        cerr << " Error trying to allocate memory for ivector\n";
00275        ad_exit(21);
00276      }
00277 
00278      if ( (shape=new vector_shapex(ncl,nch,v)) == NULL)
00279      {
00280        cerr << "Error trying to allocate memory for ivector\n";
00281        ad_exit(1);
00282      }
00283 
00284      index_min=ncl;
00285      index_max=nch;
00286      v -= indexmin();
00287      #ifdef SAFE_INITIALIZE
00288        for ( int i=indexmin(); i<=indexmax(); i++)
00289        {
00290          v[i]=0.;
00291        }
00292      #endif
00293    }
00294  }
00295 
00300 void ivector::allocate(const dvector& dv)
00301 {
00302   allocate(dv.indexmin(),dv.indexmax());
00303 }
00304 
00309 void ivector::allocate(const ivector& dv)
00310 {
00311   allocate(dv.indexmin(),dv.indexmax());
00312 }
00313 
00318 void ivector::allocate(void)
00319 {
00320   shape=NULL;
00321   index_min=1;
00322   index_max=-1;
00323   v = NULL;
00324 }
00325 
00330 ivector::ivector(const preivector& pdv)
00331  {
00332    #ifdef DIAG
00333     // cout << "starting out in dvector contructor\n";
00334    #endif
00335    shape=pdv.p->shape;
00336    if (shape)
00337    {
00338      (shape->ncopies)++;
00339    }
00340    else
00341    {
00342      cerr << "Taking a subvector  of an unallocated ivector"<<endl;
00343    }
00344    v = pdv.p->v;
00345    index_min=pdv.lb;
00346    index_max=pdv.ub;
00347  }
00348 
00353   int norm2(const ivector& t1)
00354   {
00355     int tmp=0;;
00356    for (int i=t1.indexmin();i<=t1.indexmax();i++)
00357    {
00358      tmp+=t1(i)*t1(i);
00359    }
00360     return(tmp);
00361   }
00362   int sumsq(const ivector& t1) {return(norm2(t1));}
00363 
00368   void clean(ivector& v,int level)
00369   {
00370     int mmax=v.indexmax();
00371     for (int i=level+1;i<=mmax;i++)
00372     {
00373       v(i)=0;
00374     }
00375   }