ADMB Documentation  11.5.3197
 All Classes Files Functions Variables Typedefs Friends Defines
lvector.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 #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 #ifndef OPT_LIB
00023   #include <cassert>
00024   #include <climits>
00025 #endif
00026 
00030 lvector::lvector(void)
00031 {
00032   allocate();
00033 }
00037 lvector::lvector(const lvector& t)
00038 {
00039   shape=t.shape;
00040   (shape->ncopies)++;
00041   v = t.v;
00042 }
00046 lvector::~lvector()
00047 {
00048   if (shape)
00049   {
00050     if (shape->ncopies)
00051     {
00052       (shape->ncopies)--;
00053     }
00054     else
00055     {
00056       if (v)
00057       {
00058         v += indexmin();
00059         delete [] v;
00060         v = NULL;
00061       }
00062       delete shape;
00063       shape = NULL;
00064     }
00065   }
00066 }
00071 lvector::lvector(const dvector& u)
00072  {
00073    if ( (shape=new vector_shape(u.indexmin(),u.indexmax()))==0 )
00074    {
00075      cerr << " Error trying to allocate memory for ivector\n";
00076    }
00077    v = new AD_LONG_INT [(size_t) u.size() ];
00078    if (v ==0)
00079    {
00080      cerr << " Error trying to allocate memory for ivector\n";
00081      ad_exit(21);
00082    }
00083 
00084    v -= indexmin();
00085    for (int i=indexmin(); i<=indexmax(); i++)
00086    {
00087    #if !defined(USE_DDOUBLE)
00088      v[i]= (AD_LONG_INT) u.elem(i);
00089    #else
00090      v[i]= int(u.elem(i));
00091    #endif
00092    }
00093  }
00094 
00099 lvector& lvector::operator=(const lvector& t)
00100  {
00101    if (v != t.v)
00102    {
00103      if (indexmin() != t.indexmin() || indexmax() != t.indexmax())
00104      {
00105        cerr <<
00106          " Array sizes do not match in lvector operator =(const lvector&)\n";
00107      }
00108 
00109      for ( int i=indexmin(); i<=indexmax(); i++)
00110      {
00111        elem(i) = t.elem(i);
00112      }
00113    }
00114    return (*this);
00115  }
00116 
00121 lvector::lvector( unsigned int sz, AD_LONG_INT * x )
00122 {
00123 #ifndef OPT_LIB
00124   assert(sz > 0 && sz - 1 <= INT_MAX);
00125 #endif
00126   if ((shape=new vector_shape(0, (int)(sz - 1))) == 0)
00127   {
00128     cerr << " Error trying to allocate memory for lvector\n";
00129   }
00130   if ( (v = new AD_LONG_INT [sz]) == NULL)
00131   {
00132     cerr << "Error trying to allocate memory for lvector\n";
00133     ad_exit(1);
00134   }
00135 
00136   for (unsigned int i = 0; i < sz; i++)
00137   {
00138     //cout << "Doing the assignment in constructor\n";
00139     v[i] = x[i];
00140   }
00141 }
00146 void lvector::allocate(const lvector& lv)
00147 {
00148   allocate(lv.indexmin(),lv.indexmax());
00149 }
00156 void lvector::allocate(int ncl, int nch)
00157 {
00158   if ((shape = new vector_shape(ncl,nch)) == 0)
00159   {
00160      cerr << " Error trying to allocate memory for lvector\n";
00161   }
00162   v = new AD_LONG_INT[(size_t)(nch-ncl+1)];
00163   if (v == 0)
00164   {
00165     cerr << " Error trying to allocate memory for lvector\n";
00166     ad_exit(21);
00167   }
00168   v -= indexmin();
00169 #ifndef OPT_LIB
00170   initialize();
00171 #endif
00172 }
00176 void lvector::allocate(void)
00177 {
00178   shape = NULL;
00179   v = NULL;
00180 }
00187 lvector::lvector(int ncl, int nch)
00188 {
00189   allocate(ncl, nch);
00190 }
00195 lvector::lvector(const ivector& u)
00196  {
00197    if ((shape=new vector_shape(u.indexmin(),u.indexmax()))==0 )
00198    {
00199      cerr << " Error trying to allocate memory for lvector\n";
00200    }
00201    v = new AD_LONG_INT [(size_t) (size()) ];
00202    if (v ==0)
00203    {
00204      cerr << " Error trying to allocate memory for lvector\n";
00205      ad_exit(21);
00206    }
00207    v -= indexmin();
00208    for ( int i=indexmin(); i<=indexmax(); i++)
00209    {
00210      elem(i)=u.elem(i);
00211    }
00212  }
00216 void lvector::initialize(void)
00217 {
00218   //for (int i = indexmin(); i <= indexmax(); i++) { elem(i) = 0; }
00219 #ifndef OPT_LIB
00220   assert(size() > 0);
00221 #endif
00222   memset((void*)(v + indexmin()), 0,
00223     sizeof(AD_LONG_INT) * (unsigned int)size());
00224 }