ADMB Documentation  11.5.3197
 All Classes Files Functions Variables Typedefs Friends Defines
lmat.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 #ifdef __TURBOC__
00013   #pragma hdrstop
00014 #endif
00015 
00020  lmatrix::lmatrix(int nrl, int nrh, int ncl, int nch)
00021  {
00022    allocate(nrl,nrh,ncl,nch);
00023  }
00024 
00029  void lmatrix::allocate(int nrl,int nrh,int ncl,int nch)
00030  {
00031    if ( (shape = new mat_shape(nrl,nrh,ncl,nch))== 0)
00032    {
00033      cerr << " Error allocating memory in lmatrix contructor\n";
00034      ad_exit(21);
00035    }
00036 
00037    size_t rs = rowsize();
00038    if ( (m = new lvector [rs]) == 0)
00039    {
00040      cerr << " Error allocating memory in lmatrix contructor\n";
00041      ad_exit(21);
00042    }
00043 
00044    m -= rowmin();
00045    for (int i=rowmin(); i<=rowmax(); i++)
00046    {
00047      m[i].allocate(ncl,nch);
00048    }
00049  }
00050 
00055  void lmatrix::allocate(int nrl,int nrh,const ivector& ncl,const ivector& nch)
00056  {
00057    if ((shape = new mat_shape(nrl,nrh,ncl(ncl.indexmin()),
00058                       nch(nch.indexmin()) ))== 0)
00059    {
00060      cerr << " Error allocating memory in lmatrix contructor\n";
00061      ad_exit(21);
00062    }
00063    if (nrl !=ncl.indexmin() || nrh !=ncl.indexmax() ||
00064      nrl !=nch.indexmin() || nrh !=nch.indexmax())
00065    {
00066      cerr << "Incompatible array bounds in "
00067      "dmatrix(int nrl,int nrh, const ivector& ncl, const ivector& nch)\n";
00068      ad_exit(1);
00069    }
00070    size_t rs=rowsize();
00071    if ( (m = new lvector [rs]) == 0)
00072    {
00073      cerr << " Error allocating memory in lmatrix contructor\n";
00074      ad_exit(21);
00075    }
00076    m -= rowmin();
00077    for (int i=rowmin(); i<=rowmax(); i++)
00078    {
00079      m[i].allocate(ncl(i),nch(i));
00080    }
00081  }
00082 
00087  void lmatrix::allocate(int nrl, int nrh, int ncl, const ivector& nch)
00088  {
00089    if (nrl !=nch.indexmin() || nrh !=nch.indexmax())
00090    {
00091      cerr << "Incompatible array bounds in "
00092      "lmatrix::allocate(int nrl,int nrh,int ncl, const ivector& nch)\n";
00093      ad_exit(1);
00094    }
00095    if ( (shape = new mat_shape(nrl,nrh,ncl,nch(nch.indexmin())))== 0)
00096    {
00097      cerr << " Error allocating memory in lmatrix contructor\n";
00098      ad_exit(21);
00099    }
00100    size_t rs=rowsize();
00101    if ( (m = new lvector [rs]) == 0)
00102    {
00103      cerr << " Error allocating memory in lmatrix contructor\n";
00104      ad_exit(21);
00105    }
00106    m -= rowmin();
00107    for (int i=rowmin(); i<=rowmax(); i++)
00108    {
00109      m[i].allocate(ncl,nch(i));
00110    }
00111  }
00112 
00117 lmatrix::lmatrix(const lmatrix& m2)
00118  {
00119    if (m2.shape)
00120    {
00121      shape=m2.shape;
00122      (shape->ncopies)++;
00123      m = m2.m;
00124    }
00125    else
00126    {
00127      shape=NULL;
00128      m=NULL;
00129    }
00130  }
00131 
00136 lmatrix::lmatrix(int nrl, int nrh, const ivector& ncl, const ivector& nch)
00137  {
00138    allocate(nrl,nrh,ncl,nch);
00139  }
00140 
00145 lmatrix::lmatrix(int nrl, int nrh, int ncl, const ivector& nch)
00146  {
00147    allocate(nrl,nrh,ncl,nch);
00148  }
00152 lmatrix::lmatrix(void)
00153 {
00154    shape = NULL;
00155    m=NULL;
00156 }
00160 lmatrix::~lmatrix()
00161 {
00162   deallocate();
00163 }
00168  void lmatrix::deallocate()
00169  {
00170    if (shape)
00171    {
00172      if (shape->ncopies)
00173      {
00174        (shape->ncopies)--;
00175      }
00176      else
00177      {
00178        //int offset = rowmin();
00179        m += rowmin();
00180        delete [] m;
00181        m=NULL;
00182        delete shape;
00183        shape=NULL;
00184      }
00185    }
00186    else
00187    {
00188      //cerr << "Warning -- trying to deallocate an unallocated lmatrix"<<endl;
00189    }
00190  }