ADMB Documentation  11.1.2503
 All Classes Files Functions Variables Typedefs Friends Defines
lvector.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: lvector.cpp 1919 2014-04-22 22:02:01Z johnoel $
00003  *
00004  * Author: David Fournier
00005  * Copyright (c) 2008-2012 Regents of the University of California
00006  */
00011 #ifdef __TURBOC__
00012   #pragma hdrstop
00013   #include <iostream.h>
00014 #endif
00015 
00016 #ifdef __ZTC__
00017   #include <iostream.hpp>
00018 #endif
00019 
00020 #include <stdlib.h>
00021 #include "fvar.hpp"
00022 
00026 lvector::lvector(void)
00027 {
00028   allocate();
00029 }
00033 lvector::lvector(const lvector& t)
00034 {
00035   shape=t.shape;
00036   (shape->ncopies)++;
00037   v = t.v;
00038 }
00042 lvector::~lvector()
00043 {
00044   if (shape)
00045   {
00046     if (shape->ncopies)
00047     {
00048       (shape->ncopies)--;
00049     }
00050     else
00051     {
00052       if (v)
00053       {
00054         v += indexmin();
00055         delete [] v;
00056         v = NULL;
00057       }
00058       delete shape;
00059       shape = NULL;
00060     }
00061   }
00062 }
00067 lvector::lvector(const dvector& u)
00068  {
00069    if ( (shape=new vector_shape(u.indexmin(),u.indexmax()))==0 )
00070    {
00071      cerr << " Error trying to allocate memory for ivector\n";
00072    }
00073    v = new AD_LONG_INT [(size_t) u.size() ];
00074    if (v ==0)
00075    {
00076      cerr << " Error trying to allocate memory for ivector\n";
00077      ad_exit(21);
00078    }
00079 
00080    v -= indexmin();
00081    for (int i=indexmin(); i<=indexmax(); i++)
00082    {
00083    #if !defined(USE_DDOUBLE)
00084      v[i]= (AD_LONG_INT) u.elem(i);
00085    #else
00086      v[i]= int(u.elem(i));
00087    #endif
00088    }
00089  }
00090 
00095 lvector& lvector::operator=(const lvector& t)
00096  {
00097    if (v != t.v)
00098    {
00099      if (indexmin() != t.indexmin() || indexmax() != t.indexmax())
00100      {
00101        cerr <<
00102          " Array sizes do not match in lvector operator =(const lvector&)\n";
00103      }
00104 
00105      for ( int i=indexmin(); i<=indexmax(); i++)
00106      {
00107        elem(i) = t.elem(i);
00108      }
00109    }
00110    return (*this);
00111  }
00112 
00117  lvector::lvector( unsigned int sz, AD_LONG_INT * x )
00118  {
00119    if ( (shape=new vector_shape(0,sz-1))==0 )
00120    {
00121      cerr << " Error trying to allocate memory for lvector\n";
00122    }
00123    if ( (v = new AD_LONG_INT [sz]) == NULL)
00124    {
00125      cerr << "Error trying to allocate memory for lvector\n";
00126      ad_exit(1);
00127    }
00128 
00129    for (unsigned int i=0; i<sz; i++)
00130    {
00131      cout << "Doing the assignment in constructor\n";
00132      v[i] = x[i];
00133    }
00134  }
00139 void lvector::allocate(const lvector& lv)
00140 {
00141   allocate(lv.indexmin(),lv.indexmax());
00142 }
00149 void lvector::allocate(int ncl, int nch)
00150 {
00151   if ((shape = new vector_shape(ncl,nch)) == 0)
00152   {
00153      cerr << " Error trying to allocate memory for lvector\n";
00154   }
00155   v = new AD_LONG_INT[(size_t)(nch-ncl+1)];
00156   if (v == 0)
00157   {
00158     cerr << " Error trying to allocate memory for lvector\n";
00159     ad_exit(21);
00160   }
00161   v -= indexmin();
00162 #ifndef OPT_LIB
00163   initialize();
00164 #endif
00165 }
00169 void lvector::allocate(void)
00170 {
00171   shape = NULL;
00172   v = NULL;
00173 }
00180 lvector::lvector(int ncl, int nch)
00181 {
00182   allocate(ncl, nch);
00183 }
00188 lvector::lvector(const ivector& u)
00189  {
00190    if ((shape=new vector_shape(u.indexmin(),u.indexmax()))==0 )
00191    {
00192      cerr << " Error trying to allocate memory for lvector\n";
00193    }
00194    v = new AD_LONG_INT [(size_t) (size()) ];
00195    if (v ==0)
00196    {
00197      cerr << " Error trying to allocate memory for lvector\n";
00198      ad_exit(21);
00199    }
00200    v -= indexmin();
00201    for ( int i=indexmin(); i<=indexmax(); i++)
00202    {
00203      elem(i)=u.elem(i);
00204    }
00205  }
00209 void lvector::initialize(void)
00210 {
00211   //for (int i = indexmin(); i <= indexmax(); i++) { elem(i) = 0; }
00212   memset((void*)(v + indexmin()), 0, sizeof(AD_LONG_INT) * size());
00213 }