ADMB Documentation  11.1.2192
 All Classes Files Functions Variables Typedefs Friends Defines
ivector.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: ivector.cpp 1972 2014-05-02 20:15:36Z 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 
00032 ivector::ivector(void)
00033 {
00034   allocate();
00035 }
00039 ivector::ivector(const ivector& t)
00040  {
00041    index_min=t.index_min;
00042    index_max=t.index_max;
00043    #ifdef DIAG
00044     cout << "Copy constructor called for ivector with address "
00045          << _farptr_tolong(t.v) <<"\n";
00046    #endif
00047    shape=t.shape;
00048    if (shape)
00049    {
00050      (shape->ncopies)++;
00051      v = t.v;
00052    }
00053  }
00057 ivector::~ivector()
00058 {
00059   if (shape)
00060   {
00061     if (shape->ncopies)
00062     {
00063       (shape->ncopies)--;
00064     }
00065     else
00066     {
00067       if (v != NULL)
00068       {
00069         deallocate();
00070       }
00071 #ifdef SAFE_ALL
00072       else
00073       {
00074          cerr << " Trying to delete NULL pointer in ~ivector\n";
00075          ad_exit(21);
00076       }
00077 #endif
00078     }
00079   }
00080 }
00081 
00086  void ivector::safe_allocate(int ncl,int nch)
00087  {
00088    if (allocated())
00089    {
00090      cerr << "trying to allocate an already allocated dvector " << endl;
00091      ad_exit(1);
00092    }
00093    else
00094    {
00095      allocate(ncl,nch);
00096    }
00097  }
00098 
00103   void ivector::safe_deallocate()
00104   {
00105     if (shape)
00106     {
00107       if (shape->ncopies)
00108       {
00109         cerr << "trying to deallocate a dvector with copies" << endl;
00110         ad_exit(1);
00111       }
00112     }
00113     else
00114     {
00115       deallocate();
00116     }
00117   }
00118 
00123  void ivector::deallocate()
00124  {
00125    if (shape)
00126    {
00127      v = (int*) (shape->trueptr);
00128      delete [] v;
00129      v=NULL;
00130      delete  shape;
00131    }
00132    else
00133    {
00134      //cerr << "Warning -- trying to deallocate an unitialized ivector" << endl;
00135    }
00136    shape=NULL;
00137  }
00138 
00139 
00144 void ivector::shallow_copy(const ivector& t)
00145  {
00146    index_min=t.index_min;
00147    index_max=t.index_max;
00148    #ifdef DIAG
00149     cout << "Copy constructor called for ivector with address "
00150          << _farptr_tolong(t.v) <<"\n";
00151    #endif
00152    shape=t.shape;
00153    if (shape)
00154    {
00155      (shape->ncopies)++;
00156      v = t.v;
00157    }
00158  }
00159 
00164 ivector& ivector::operator=(const ivector& t)
00165  {
00166    // disconnect ivector  pointer  from old array
00167    if (::allocated(*this))
00168    {
00169      if (v != t.v)
00170      {
00171        if (indexmin() != t.indexmin() || indexmax() != t.indexmax())
00172        {
00173          cerr << " Array sizes do not match in ivector operator"
00174                  " =(const ivector&)" << endl;
00175          ad_exit(1);
00176        }
00177 
00178        for ( int i=indexmin(); i<=indexmax(); i++)
00179        {
00180          elem(i) = t.elem(i);
00181        }
00182      }
00183    }
00184    else
00185    {
00186      shallow_copy(t);
00187    }
00188    return (*this);
00189  }
00190 
00195  ivector& ivector::operator = (int u)
00196  {
00197    for ( int i=indexmin(); i<=indexmax(); i++)
00198    {
00199      elem(i) = u;
00200    }
00201    return (*this);
00202  }
00203 
00208  ivector::ivector( unsigned int sz, long int * x )
00209  {
00210    allocate(0,sz-1);
00211 
00212    for (unsigned int i=0; i<sz; i++)
00213    {
00214      cout << "Doing the assignment in constructor\n";
00215      v[i] = x[i];
00216    }
00217  }
00218 
00223 ivector::ivector(const dvector& u)
00224  {
00225    allocate(u);
00226    for (int i=indexmin();i<=indexmax();i++)
00227    {
00228      elem(i)=int(u.elem(i));
00229    }
00230  }
00231 
00236  ivector::ivector(int ncl,int nch)
00237  {
00238    allocate(ncl, nch);
00239  }
00240 
00245  void ivector::allocate(int ncl,int nch)
00246  {
00247    int itemp=nch-ncl;
00248    if (itemp<0)
00249    {
00250      //cerr << "Error in ivector constructor max index must be >= minindex\n"
00251      //  << "minindex = " << ncl << " maxindex = " << nch <<endl;
00252      //ad_exit(1);
00253      allocate();
00254    }
00255    else
00256    {
00257      if ( (v = new int [itemp+1]) == 0 )
00258      {
00259        cerr << " Error trying to allocate memory for ivector\n";
00260        ad_exit(21);
00261      }
00262 
00263      if ( (shape=new vector_shapex(ncl,nch,v)) == NULL)
00264      {
00265        cerr << "Error trying to allocate memory for ivector\n";
00266        ad_exit(1);
00267      }
00268 
00269      index_min=ncl;
00270      index_max=nch;
00271      v -= indexmin();
00272      #ifdef SAFE_INITIALIZE
00273        for ( int i=indexmin(); i<=indexmax(); i++)
00274        {
00275          v[i]=0.;
00276        }
00277      #endif
00278    }
00279  }
00280 
00285 void ivector::allocate(const dvector& dv)
00286 {
00287   allocate(dv.indexmin(),dv.indexmax());
00288 }
00289 
00294 void ivector::allocate(const ivector& dv)
00295 {
00296   allocate(dv.indexmin(),dv.indexmax());
00297 }
00298 
00303 void ivector::allocate(void)
00304 {
00305   shape=NULL;
00306   index_min=1;
00307   index_max=-1;
00308   v = NULL;
00309 }
00310 
00315 ivector::ivector(const preivector& pdv)
00316  {
00317    #ifdef DIAG
00318     // cout << "starting out in dvector contructor\n";
00319    #endif
00320    shape=pdv.p->shape;
00321    if (shape)
00322    {
00323      (shape->ncopies)++;
00324    }
00325    else
00326    {
00327      cerr << "Taking a subvector  of an unallocated ivector"<<endl;
00328    }
00329    v = pdv.p->v;
00330    index_min=pdv.lb;
00331    index_max=pdv.ub;
00332  }
00333 
00338   int norm2(const ivector& t1)
00339   {
00340     int tmp=0;;
00341    for (int i=t1.indexmin();i<=t1.indexmax();i++)
00342    {
00343      tmp+=t1(i)*t1(i);
00344    }
00345     return(tmp);
00346   }
00347   int sumsq(const ivector& t1) {return(norm2(t1));}
00348 
00353   void clean(ivector& v,int level)
00354   {
00355     int mmax=v.indexmax();
00356     for (int i=level+1;i<=mmax;i++)
00357     {
00358       v(i)=0;
00359     }
00360   }