ADMB Documentation  11.5.3197
 All Classes Files Functions Variables Typedefs Friends Defines
ptr_vec.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 
00033 struct void_ptr
00034 {
00035   void * t;
00036 };
00037 
00039 ptr_vector::~ptr_vector()
00040 {
00041 #ifdef DIAG
00042      cout << "deleting an ivector with address " << _farptr_tolong(v)
00043           <<" and ncopies = " << *ncopies <<"\n";
00044 #endif
00045   if (shape->ncopies)
00046   {
00047     (shape->ncopies)--;
00048   }
00049   else
00050   {
00051     if (v == NULL)
00052     {
00053       cerr << " Trying to delete NULL pointer in ~ivector\n";
00054       ad_exit(21);
00055     }
00056     v += indexmin();
00057     delete [] v;
00058     v=NULL;
00059 
00060 #ifdef DIAG
00061        cout << "Really deleting an ivector with address "
00062             << _farptr_tolong(v) <<"\n";
00063 #endif
00064 
00065     delete  shape;
00066     shape = NULL;
00067   }
00068 }
00072 ptr_vector::ptr_vector(const ptr_vector& t)
00073 {
00074 #ifdef DIAG
00075     cout << "Copy constructor called for ivector with address "
00076          << _farptr_tolong(t.v) <<"\n";
00077 #endif
00078   shape=t.shape;
00079   (shape->ncopies)++;
00080   v = t.v;
00081 }
00087 ptr_vector& ptr_vector::operator=(const ptr_vector& _t)
00088 {
00089   ADUNCONST(ptr_vector,t)
00090   // disconnect ivector  pointer  from old array
00091   if (v != t.v)
00092   {
00093     if (indexmin() != t.indexmin() || indexmax() != t.indexmax())
00094     {
00095       cerr << " Array sizes do not match in "
00096       "ptr_vector operator =(const ivector&)\n";
00097     }
00098     for ( int i=indexmin(); i<=indexmax(); i++)
00099     {
00100       elem(i) = t.elem(i);
00101     }
00102   }
00103   return *this;
00104 }
00111 ptr_vector::ptr_vector(int ncl,int nch)
00112 {
00113   allocate(ncl,nch);
00114 }
00118 ptr_vector::ptr_vector()
00119 {
00120   shape=NULL;
00121   v=NULL;
00122 }
00131 void ptr_vector::allocate(int ncl,int nch)
00132 {
00133   if ((shape = new vector_shape(ncl,nch)) == 0)
00134   {
00135     cerr << " Error trying to allocate memory for ivector\n";
00136   }
00137   v = (void**) new void_ptr [(size_t) (nch-ncl+1)];
00138 #ifdef DIAG
00139   cout << "Created a ivector with address " << _farptr_tolong(v) <<"\n";
00140 #endif
00141   if (v == 0)
00142   {
00143     cerr << " Error trying to allocate memory for ptr_vector\n";
00144     ad_exit(21);
00145   }
00146   v -= indexmin();
00147 #ifndef OPT_LIB
00148   this->initialize();
00149 #endif
00150 }
00154 void ptr_vector::initialize(void)
00155 {
00156   for (int i = indexmin(); i <= indexmax(); i++)
00157   {
00158     v[i] = NULL;
00159   }
00160 }
00165 void*& ptr_vector::operator[] (int i)
00166 {
00167   if (i > indexmax())
00168   {
00169     cerr << "array bound exceeded -- index too high in ivector::operator[]";
00170     ad_exit(1);
00171   }
00172   if (i < indexmin())
00173   {
00174     cerr << "array bound exceeded -- index too low in ivector::operator[]";
00175     ad_exit(1);
00176   }
00177   return (*(v+i));
00178 }
00179 
00184 void*& ptr_vector::operator()(int i)
00185 {
00186   if (i>indexmax())
00187   {
00188     cerr << "array bound exceeded -- index too high in ivector::operator[]";
00189     ad_exit(1);
00190   }
00191   if (i<indexmin())
00192   {
00193     cerr << "array bound exceeded -- index too low in ivector::operator[]";
00194     ad_exit(1);
00195   }
00196   return(*(v+i));
00197 }
00202 ostream& operator<<(const ostream& _s, const ptr_vector& _v)
00203 {
00204   ADUNCONST(ptr_vector,v)
00205   ostream & s = (ostream&) _s;
00206   for (int i=v.indexmin();i<=v.indexmax();i++)
00207   {
00208     s << v.elem(i) << " ";
00209   }
00210   s << endl;
00211   return s;
00212 }