ADMB Documentation  11.4.2891
 All Classes Files Functions Variables Typedefs Friends Defines
ivector.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id$
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  }
00064 ivector::~ivector()
00065 {
00066   if (shape)
00067   {
00068     if (shape->ncopies)
00069     {
00070       (shape->ncopies)--;
00071     }
00072     else
00073     {
00074 #ifdef SAFE_ALL
00075   #ifdef DIAG
00076       myheapcheck(" Entering ~dvector");
00077   #endif
00078       if (v == NULL)
00079       {
00080         cerr << " Trying to delete NULL pointer in ~ivector\n";
00081         ad_exit(21);
00082       }
00083 #endif
00084       deallocate();
00085     }
00086   }
00087 }
00092  void ivector::safe_allocate(int ncl,int nch)
00093  {
00094    if (allocated())
00095    {
00096      cerr << "trying to allocate an already allocated dvector " << endl;
00097      ad_exit(1);
00098    }
00099    else
00100    {
00101      allocate(ncl,nch);
00102    }
00103  }
00108 void ivector::deallocate()
00109 {
00110   if (shape)
00111   {
00112     v = (int*)(shape->trueptr);
00113 
00114     if (v)
00115     {
00116       delete [] v;
00117       v = NULL;
00118     }
00119 
00120     delete shape;
00121     shape = NULL;
00122   }
00123 }
00127 void ivector::safe_deallocate()
00128 {
00129   if (shape)
00130   {
00131     if (shape->ncopies)
00132     {
00133       cerr << "trying to deallocate a ivector with copies" << endl;
00134       ad_exit(1);
00135     }
00136     deallocate();
00137   }
00138 }
00139 
00150 void ivector::shallow_copy(const ivector& t)
00151  {
00152    index_min=t.index_min;
00153    index_max=t.index_max;
00154    #ifdef DIAG
00155     cout << "Copy constructor called for ivector with address "
00156          << _farptr_tolong(t.v) <<"\n";
00157    #endif
00158    shape=t.shape;
00159    if (shape)
00160    {
00161      (shape->ncopies)++;
00162      v = t.v;
00163    }
00164  }
00165 
00170 ivector& ivector::operator=(const ivector& t)
00171  {
00172    // disconnect ivector  pointer  from old array
00173    if (::allocated(*this))
00174    {
00175      if (v != t.v)
00176      {
00177        if (indexmin() != t.indexmin() || indexmax() != t.indexmax())
00178        {
00179          cerr << " Array sizes do not match in ivector operator"
00180                  " =(const ivector&)" << endl;
00181          ad_exit(1);
00182        }
00183 
00184        for ( int i=indexmin(); i<=indexmax(); i++)
00185        {
00186          elem(i) = t.elem(i);
00187        }
00188      }
00189    }
00190    else
00191    {
00192      shallow_copy(t);
00193    }
00194    return (*this);
00195  }
00196 
00201 ivector& ivector::operator=(int u)
00202 {
00203   for (int i = indexmin(); i <= indexmax(); i++)
00204   {
00205     elem(i) = u;
00206   }
00207   return (*this);
00208 }
00209 
00213 ivector::ivector(unsigned int sz, long int* x)
00214 {
00215 #ifndef OPT_LIB
00216   assert(sz > 0 && sz <= INT_MAX);
00217   assert(x);
00218 #endif
00219   allocate(0, (int)(sz - 1));
00220 
00221   if (v)
00222   {
00223     for (unsigned int i = 0; i < sz; i++)
00224     {
00225 #ifdef OPT_LIB
00226       v[i] = (int)x[i];
00227 #else
00228       long int xi = x[i];
00229       assert(xi <= INT_MAX);
00230       v[i] = static_cast<int>(xi);
00231 #endif
00232     }
00233   }
00234 }
00235 
00239 ivector::ivector(const dvector& u)
00240 {
00241   allocate(u);
00242   for (int i=indexmin();i<=indexmax();i++)
00243   {
00244 #ifdef OPT_LIB
00245     elem(i) = int(u.elem(i));
00246 #else
00247     double ui = u.elem(i);
00248     assert(ui <= INT_MAX);
00249     v[i] = (int)ui;
00250 #endif
00251   }
00252 }
00253 
00258  ivector::ivector(int ncl,int nch)
00259  {
00260    allocate(ncl, nch);
00261  }
00262 
00267  void ivector::allocate(int ncl,int nch)
00268  {
00269    int itemp=nch-ncl;
00270    if (itemp<0)
00271    {
00272      //cerr << "Error in ivector constructor max index must be >= minindex\n"
00273      //  << "minindex = " << ncl << " maxindex = " << nch <<endl;
00274      //ad_exit(1);
00275      allocate();
00276    }
00277    else
00278    {
00279      if ( (v = new int [itemp+1]) == 0 )
00280      {
00281        cerr << " Error trying to allocate memory for ivector\n";
00282        ad_exit(21);
00283      }
00284 
00285      if ( (shape=new vector_shapex(ncl,nch,v)) == NULL)
00286      {
00287        cerr << "Error trying to allocate memory for ivector\n";
00288        ad_exit(1);
00289      }
00290 
00291      index_min=ncl;
00292      index_max=nch;
00293      v -= indexmin();
00294      #ifdef SAFE_INITIALIZE
00295        for ( int i=indexmin(); i<=indexmax(); i++)
00296        {
00297          v[i]=0.;
00298        }
00299      #endif
00300    }
00301  }
00302 
00307 void ivector::allocate(const dvector& dv)
00308 {
00309   allocate(dv.indexmin(),dv.indexmax());
00310 }
00311 
00316 void ivector::allocate(const ivector& dv)
00317 {
00318   allocate(dv.indexmin(),dv.indexmax());
00319 }
00320 
00325 void ivector::allocate(void)
00326 {
00327   shape=NULL;
00328   index_min=1;
00329   index_max=-1;
00330   v = NULL;
00331 }
00332 
00337 ivector::ivector(const preivector& pdv)
00338  {
00339    #ifdef DIAG
00340     // cout << "starting out in dvector contructor\n";
00341    #endif
00342    shape=pdv.p->shape;
00343    if (shape)
00344    {
00345      (shape->ncopies)++;
00346    }
00347    else
00348    {
00349      cerr << "Taking a subvector  of an unallocated ivector"<<endl;
00350    }
00351    v = pdv.p->v;
00352    index_min=pdv.lb;
00353    index_max=pdv.ub;
00354  }
00355 
00360   int norm2(const ivector& t1)
00361   {
00362     int tmp=0;;
00363    for (int i=t1.indexmin();i<=t1.indexmax();i++)
00364    {
00365      tmp+=t1(i)*t1(i);
00366    }
00367     return(tmp);
00368   }
00369   int sumsq(const ivector& t1) {return(norm2(t1));}
00370 
00375   void clean(ivector& v,int level)
00376   {
00377     int mmax=v.indexmax();
00378     for (int i=level+1;i<=mmax;i++)
00379     {
00380       v(i)=0;
00381     }
00382   }