ADMB Documentation  11.1.1920
 All Classes Files Functions Variables Typedefs Friends Defines
ivector.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: ivector.cpp 1788 2014-03-13 04:14: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 long int _farptr_tolong(void * px);
00025 long int farptr_tolong(void *);
00026 
00030 ivector::~ivector()
00031 {
00032   if (shape)
00033   {
00034     if (shape->ncopies)
00035     {
00036       (shape->ncopies)--;
00037     }
00038     else
00039     {
00040       if ( v == NULL)
00041       {
00042          cerr << " Trying to delete NULL pointer in ~ivector\n";
00043          ad_exit(21);
00044       }
00045       deallocate();
00046     }
00047   }
00048 }
00049 
00054  void ivector::safe_allocate(int ncl,int nch)
00055  {
00056    if (allocated())
00057    {
00058      cerr << "trying to allocate an already allocated dvector " << endl;
00059      ad_exit(1);
00060    }
00061    else
00062    {
00063      allocate(ncl,nch);
00064    }
00065  }
00066 
00071   void ivector::safe_deallocate()
00072   {
00073     if (shape)
00074     {
00075       if (shape->ncopies)
00076       {
00077         cerr << "trying to deallocate a dvector with copies" << endl;
00078         ad_exit(1);
00079       }
00080     }
00081     else
00082     {
00083       deallocate();
00084     }
00085   }
00086 
00091  void ivector::deallocate()
00092  {
00093    if (shape)
00094    {
00095      v = (int*) (shape->trueptr);
00096      delete [] v;
00097      v=NULL;
00098      delete  shape;
00099    }
00100    else
00101    {
00102      //cerr << "Warning -- trying to deallocate an unitialized ivector" << endl;
00103    }
00104    shape=NULL;
00105  }
00106 
00111 ivector::ivector(const ivector& t)
00112  {
00113    index_min=t.index_min;
00114    index_max=t.index_max;
00115    #ifdef DIAG
00116     cout << "Copy constructor called for ivector with address "
00117          << _farptr_tolong(t.v) <<"\n";
00118    #endif
00119    shape=t.shape;
00120    if (shape)
00121    {
00122      (shape->ncopies)++;
00123      v = t.v;
00124    }
00125  }
00126 
00131 void ivector::shallow_copy(const ivector& t)
00132  {
00133    index_min=t.index_min;
00134    index_max=t.index_max;
00135    #ifdef DIAG
00136     cout << "Copy constructor called for ivector with address "
00137          << _farptr_tolong(t.v) <<"\n";
00138    #endif
00139    shape=t.shape;
00140    if (shape)
00141    {
00142      (shape->ncopies)++;
00143      v = t.v;
00144    }
00145  }
00146 
00151 ivector& ivector::operator=(const ivector& t)
00152  {
00153    // disconnect ivector  pointer  from old array
00154    if (::allocated(*this))
00155    {
00156      if (v != t.v)
00157      {
00158        if (indexmin() != t.indexmin() || indexmax() != t.indexmax())
00159        {
00160          cerr << " Array sizes do not match in ivector operator"
00161                  " =(const ivector&)" << endl;
00162          ad_exit(1);
00163        }
00164 
00165        for ( int i=indexmin(); i<=indexmax(); i++)
00166        {
00167          elem(i) = t.elem(i);
00168        }
00169      }
00170    }
00171    else
00172    {
00173      shallow_copy(t);
00174    }
00175    return (*this);
00176  }
00177 
00182  ivector& ivector::operator = (int u)
00183  {
00184    for ( int i=indexmin(); i<=indexmax(); i++)
00185    {
00186      elem(i) = u;
00187    }
00188    return (*this);
00189  }
00190 
00195  ivector::ivector( unsigned int sz, long int * x )
00196  {
00197    allocate(0,sz-1);
00198 
00199    for (unsigned int i=0; i<sz; i++)
00200    {
00201      cout << "Doing the assignment in constructor\n";
00202      v[i] = x[i];
00203    }
00204  }
00205 
00210 ivector::ivector(const dvector& u)
00211  {
00212    allocate(u);
00213    for (int i=indexmin();i<=indexmax();i++)
00214    {
00215      elem(i)=int(u.elem(i));
00216    }
00217  }
00218 
00223  ivector::ivector(int ncl,int nch)
00224  {
00225    allocate(ncl, nch);
00226  }
00227 
00232  ivector::ivector(void)
00233  {
00234    allocate();
00235  }
00236 
00241  void ivector::allocate(int ncl,int nch)
00242  {
00243    int itemp=nch-ncl;
00244    if (itemp<0)
00245    {
00246      //cerr << "Error in ivector constructor max index must be >= minindex\n"
00247      //  << "minindex = " << ncl << " maxindex = " << nch <<endl;
00248      //ad_exit(1);
00249      allocate();
00250    }
00251    else
00252    {
00253      if ( (v = new int [itemp+1]) == 0 )
00254      {
00255        cerr << " Error trying to allocate memory for ivector\n";
00256        ad_exit(21);
00257      }
00258 
00259      if ( (shape=new vector_shapex(ncl,nch,v)) == NULL)
00260      {
00261        cerr << "Error trying to allocate memory for ivector\n";
00262        ad_exit(1);
00263      }
00264 
00265      index_min=ncl;
00266      index_max=nch;
00267      v -= indexmin();
00268      #ifdef SAFE_INITIALIZE
00269        for ( int i=indexmin(); i<=indexmax(); i++)
00270        {
00271          v[i]=0.;
00272        }
00273      #endif
00274    }
00275  }
00276 
00281 void ivector::allocate(const dvector& dv)
00282 {
00283   allocate(dv.indexmin(),dv.indexmax());
00284 }
00285 
00290 void ivector::allocate(const ivector& dv)
00291 {
00292   allocate(dv.indexmin(),dv.indexmax());
00293 }
00294 
00299 void ivector::allocate(void)
00300 {
00301   shape=NULL;
00302   index_min=1;
00303   index_max=-1;
00304   v = NULL;
00305 }
00306 
00311 ivector::ivector(const preivector& pdv)
00312  {
00313    #ifdef DIAG
00314     // cout << "starting out in dvector contructor\n";
00315    #endif
00316    shape=pdv.p->shape;
00317    if (shape)
00318    {
00319      (shape->ncopies)++;
00320    }
00321    else
00322    {
00323      cerr << "Taking a subvector  of an unallocated ivector"<<endl;
00324    }
00325    v = pdv.p->v;
00326    index_min=pdv.lb;
00327    index_max=pdv.ub;
00328  }
00329 
00334   int norm2(const ivector& t1)
00335   {
00336     int tmp=0;;
00337    for (int i=t1.indexmin();i<=t1.indexmax();i++)
00338    {
00339      tmp+=t1(i)*t1(i);
00340    }
00341     return(tmp);
00342   }
00343   int sumsq(const ivector& t1) {return(norm2(t1));}
00344 
00349   void clean(ivector& v,int level)
00350   {
00351     int mmax=v.indexmax();
00352     for (int i=level+1;i<=mmax;i++)
00353     {
00354       v(i)=0;
00355     }
00356   }