ADMB Documentation  11.1.1015
 All Classes Files Functions Variables Typedefs Friends Defines
string5.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: string5.cpp 889 2013-03-29 00:25:06Z 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 
00011 adstring_array::adstring_array(const adstring_array& sa):clist(sa)
00012   {
00013     shape=sa.shape;
00014     ptr=sa.ptr;
00015   }
00016 
00017   adstring_array::~adstring_array()
00018   {
00019     if (ptr)
00020     {
00021       if (next==this)
00022       {
00023         int min=indexmin();
00024         int max=indexmax();
00025         for(int i=min;i<=max;i++)
00026         {
00027           if (ptr[i])
00028           {
00029             delete ptr[i];
00030           }
00031         }
00032         ptr+=indexmin();
00033         delete [] ptr;
00034         delete shape;
00035         shape=NULL;
00036         ptr=NULL;
00037       }
00038     }
00039   }
00040 
00041   adstring_array::adstring_array(void)
00042   {
00043     shape=NULL;
00044     ptr=NULL;
00045   }
00046 
00047 int adstring_array::size() const { return shape->indexmax()-shape->indexmin() + 1; }
00048 int adstring_array::indexmin(void) const { return shape->indexmin();}
00049 int adstring_array::indexmax(void) const { return shape->indexmax();}
00050 
00051   adstring_array::adstring_array(int min,int max) 
00052   {
00053     allocate(min,max); 
00054   }
00055 
00056   void adstring_array::allocate(int min,int max) 
00057   {
00058     if (min > max)
00059     {
00060       cerr << " Error in adstring_array(int min,int max) --"
00061               " max must be >= min" << endl;
00062       exit(1);
00063     }
00064     if (!(shape=new vector_shape(min,max)))
00065     {
00066       cerr << "Error allocating memory in adstring_array" << endl;
00067     }
00068     if (!(ptr=new adstring* [max-min+1]))
00069     {
00070       cerr << "Error allocating memory in adstring_array" << endl;
00071     }
00072     ptr-=indexmin();
00073     for (int i=min;i<=max;i++)
00074     {
00075       ptr[i]=new adstring;
00076     }
00077   }
00078 
00079   adstring& adstring_array::operator [] (int i)
00080   {
00081     if (!shape)
00082     {
00083       cerr << "Error -- trying to acess unallocated adstring array"
00084         << endl;
00085       exit(1);
00086     }
00087 
00088     if (i<indexmin())
00089     {
00090       ADMB_ARRAY_BOUNDS_ERROR("Error index too low", "adstring& adstring_array::operator [] (int i)", indexmin(), indexmax(), i);
00091     }
00092     if (i>indexmax())
00093     {
00094       ADMB_ARRAY_BOUNDS_ERROR("Error index too high", "adstring& adstring_array::operator [] (int i)", indexmin(), indexmax(), i);
00095     }
00096     return *(ptr[i]);
00097   }
00098 
00099   adstring& adstring_array::operator () (int i)
00100   {
00101     if (!shape)
00102     {
00103       cerr << "Error -- trying to acess unallocated adstring array"
00104         << endl;
00105       exit(1);
00106     }
00107     if (i<indexmin())
00108     {
00109       ADMB_ARRAY_BOUNDS_ERROR("Error index too low", "adstring& adstring_array::operator () (int i)", indexmin(), indexmax(), i);
00110     }
00111     if (i>indexmax())
00112     {
00113       ADMB_ARRAY_BOUNDS_ERROR("Error index too high", "adstring& adstring_array::operator () (int i)", indexmin(), indexmax(), i);
00114     }
00115     return *(ptr[i]);
00116   }
00117 
00118 #ifdef USE_CONST
00119 
00120 const adstring& adstring_array::operator[](int i) const
00121   {
00122     if (!shape)
00123     {
00124       cerr << "Error -- trying to acess unallocated adstring array"
00125         << endl;
00126       exit(1);
00127     }
00128 
00129     if (i<indexmin())
00130     {
00131       ADMB_ARRAY_BOUNDS_ERROR("Error index too low", "adstring& adstring_array::operator [] (int i) const ", indexmin(), indexmax(), i);
00132     }
00133     if (i>indexmax())
00134     {
00135       ADMB_ARRAY_BOUNDS_ERROR("Error index too high", "adstring& adstring_array::operator [] (int i) const ", indexmin(), indexmax(), i);
00136     }
00137     return *(ptr[i]);
00138   }
00139 
00140 const adstring& adstring_array::operator()(int i) const
00141   {
00142     if (!shape)
00143     {
00144       cerr << "Error -- trying to acess unallocated adstring array"
00145         << endl;
00146       exit(1);
00147     }
00148     if (i<indexmin())
00149     {
00150       ADMB_ARRAY_BOUNDS_ERROR("Error index too low", "adstring& adstring_array::operator () (int i) const", indexmin(), indexmax(), i);
00151     }
00152     if (i>indexmax())
00153     {
00154       ADMB_ARRAY_BOUNDS_ERROR("Error index too high", "adstring& adstring_array::operator () (int i) const", indexmin(), indexmax(), i);
00155     }
00156     return *(ptr[i]);
00157   }
00158 #endif