ADMB Documentation  11.1.2251
 All Classes Files Functions Variables Typedefs Friends Defines
string5.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: string5.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  */
00007 #include <fvar.hpp>
00008 #include <stdlib.h>
00009 #include "admb_messages.h"
00010 
00014 adstring_array::adstring_array()
00015 {
00016   shape=NULL;
00017   ptr=NULL;
00018 }
00022 adstring_array::adstring_array(const adstring_array& sa): clist(sa)
00023 {
00024   shape=sa.shape;
00025   ptr=sa.ptr;
00026 }
00027 adstring_array::adstring_array(int min,int max)
00028 {
00029   allocate(min,max);
00030 }
00034 adstring_array::~adstring_array()
00035 {
00036   if (ptr)
00037   {
00038     if (next==this)
00039     {
00040       int min=indexmin();
00041       int max=indexmax();
00042       for(int i=min;i<=max;i++)
00043       {
00044         if (ptr[i])
00045         {
00046           delete ptr[i];
00047           ptr[i] = 0;
00048         }
00049       }
00050       ptr+=indexmin();
00051 
00052       delete [] ptr;
00053       ptr = 0;
00054 
00055       delete shape;
00056       shape = 0;
00057     }
00058   }
00059 }
00060 
00061 int adstring_array::size() const
00062   { return shape->indexmax()-shape->indexmin() + 1; }
00063 
00064 int adstring_array::indexmin() const
00065   { return shape->indexmin();}
00066 
00067 int adstring_array::indexmax() const
00068   { return shape->indexmax();}
00069 
00070 void adstring_array::allocate(int min, int max)
00071 {
00072   if (min > max)
00073   {
00074     cerr << " Error in adstring_array(int min,int max) --"
00075             " max must be >= min" << endl;
00076     exit(1);
00077   }
00078   if (!(shape=new vector_shape(min,max)))
00079   {
00080     cerr << "Error allocating memory in adstring_array" << endl;
00081   }
00082   if (!(ptr=new adstring* [max-min+1]))
00083   {
00084     cerr << "Error allocating memory in adstring_array" << endl;
00085   }
00086   ptr-=indexmin();
00087   for (int i=min;i<=max;i++)
00088   {
00089     ptr[i]=new adstring;
00090   }
00091 }
00097 adstring& adstring_array::operator[](int i)
00098 {
00099   if (!shape)
00100   {
00101     cerr << "Error -- trying to acess unallocated adstring array" << endl;
00102     ad_exit(1);
00103   }
00104   if (i < indexmin())
00105   {
00106     ADMB_ARRAY_BOUNDS_ERROR("Error index too low",
00107     "adstring& adstring_array::operator [] (int i)",
00108     indexmin(), indexmax(), i);
00109   }
00110   if (i > indexmax())
00111   {
00112     ADMB_ARRAY_BOUNDS_ERROR("Error index too high",
00113     "adstring& adstring_array::operator [] (int i)",
00114     indexmin(), indexmax(), i);
00115   }
00116   return *(ptr[i]);
00117 }
00123 adstring& adstring_array::operator()(int i)
00124 {
00125   if (!shape)
00126   {
00127     cerr << "Error -- trying to acess unallocated adstring array" << endl;
00128     ad_exit(1);
00129   }
00130   if (i < indexmin())
00131   {
00132     ADMB_ARRAY_BOUNDS_ERROR("Error index too low",
00133     "adstring& adstring_array::operator () (int i)",
00134     indexmin(), indexmax(), i);
00135   }
00136   if (i > indexmax())
00137   {
00138     ADMB_ARRAY_BOUNDS_ERROR("Error index too high",
00139     "adstring& adstring_array::operator () (int i)",
00140     indexmin(), indexmax(), i);
00141   }
00142   return *(ptr[i]);
00143 }
00149 const adstring& adstring_array::operator[](int i) const
00150 {
00151   if (!shape)
00152   {
00153     cerr << "Error -- trying to acess unallocated adstring array" << endl;
00154     exit(1);
00155   }
00156   if (i < indexmin())
00157   {
00158     ADMB_ARRAY_BOUNDS_ERROR("Error index too low",
00159     "adstring& adstring_array::operator [] (int i) const ",
00160     indexmin(), indexmax(), i);
00161   }
00162   if (i > indexmax())
00163   {
00164     ADMB_ARRAY_BOUNDS_ERROR("Error index too high",
00165     "adstring& adstring_array::operator [] (int i) const ",
00166     indexmin(), indexmax(), i);
00167   }
00168   return *(ptr[i]);
00169 }
00175 const adstring& adstring_array::operator()(int i) const
00176 {
00177   if (!shape)
00178   {
00179     cerr << "Error -- trying to acess unallocated adstring array" << endl;
00180     ad_exit(1);
00181   }
00182   if (i < indexmin())
00183   {
00184     ADMB_ARRAY_BOUNDS_ERROR("Error index too low",
00185     "adstring& adstring_array::operator () (int i) const",
00186     indexmin(), indexmax(), i);
00187   }
00188   if (i > indexmax())
00189   {
00190     ADMB_ARRAY_BOUNDS_ERROR("Error index too high",
00191     "adstring& adstring_array::operator () (int i) const",
00192     indexmin(), indexmax(), i);
00193   }
00194   return *(ptr[i]);
00195 }