ADMB Documentation  11.1.2397
 All Classes Files Functions Variables Typedefs Friends Defines
string5.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: string5.cpp 2371 2014-09-17 23:15: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 
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 ? shape->indexmax() - shape->indexmin() + 1 : 0; }
00063 
00064 int adstring_array::indexmin() const
00065   { return shape ? shape->indexmin() : 0;}
00066 
00067 int adstring_array::indexmax() const
00068   { return shape ? shape->indexmax() : 0;}
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   shape = new vector_shape(min, max);
00079   if (!shape)
00080   {
00081     cerr << "Error allocating memory in adstring_array" << endl;
00082   }
00083   ptr = new adstring*[max - min + 1];
00084   if (!ptr)
00085   {
00086     cerr << "Error allocating memory in adstring_array" << endl;
00087   }
00088   ptr-=indexmin();
00089   for (int i=min;i<=max;i++)
00090   {
00091     ptr[i]=new adstring;
00092   }
00093 }
00099 adstring& adstring_array::operator[](int i)
00100 {
00101   if (!shape)
00102   {
00103     cerr << "Error -- trying to acess unallocated adstring array" << endl;
00104     ad_exit(1);
00105   }
00106   if (i < indexmin())
00107   {
00108     ADMB_ARRAY_BOUNDS_ERROR("Error index too low",
00109     "adstring& adstring_array::operator [] (int i)",
00110     indexmin(), indexmax(), i);
00111   }
00112   if (i > indexmax())
00113   {
00114     ADMB_ARRAY_BOUNDS_ERROR("Error index too high",
00115     "adstring& adstring_array::operator [] (int i)",
00116     indexmin(), indexmax(), i);
00117   }
00118   return *(ptr[i]);
00119 }
00125 adstring& adstring_array::operator()(int i)
00126 {
00127   if (!shape)
00128   {
00129     cerr << "Error -- trying to acess unallocated adstring array" << endl;
00130     ad_exit(1);
00131   }
00132   if (i < indexmin())
00133   {
00134     ADMB_ARRAY_BOUNDS_ERROR("Error index too low",
00135     "adstring& adstring_array::operator () (int i)",
00136     indexmin(), indexmax(), i);
00137   }
00138   if (i > indexmax())
00139   {
00140     ADMB_ARRAY_BOUNDS_ERROR("Error index too high",
00141     "adstring& adstring_array::operator () (int i)",
00142     indexmin(), indexmax(), i);
00143   }
00144   return *(ptr[i]);
00145 }
00151 const adstring& adstring_array::operator[](int i) const
00152 {
00153   if (!shape)
00154   {
00155     cerr << "Error -- trying to acess unallocated adstring array" << endl;
00156     exit(1);
00157   }
00158   if (i < indexmin())
00159   {
00160     ADMB_ARRAY_BOUNDS_ERROR("Error index too low",
00161     "adstring& adstring_array::operator [] (int i) const ",
00162     indexmin(), indexmax(), i);
00163   }
00164   if (i > indexmax())
00165   {
00166     ADMB_ARRAY_BOUNDS_ERROR("Error index too high",
00167     "adstring& adstring_array::operator [] (int i) const ",
00168     indexmin(), indexmax(), i);
00169   }
00170   return *(ptr[i]);
00171 }
00177 const adstring& adstring_array::operator()(int i) const
00178 {
00179   if (!shape)
00180   {
00181     cerr << "Error -- trying to acess unallocated adstring array" << endl;
00182     ad_exit(1);
00183   }
00184   if (i < indexmin())
00185   {
00186     ADMB_ARRAY_BOUNDS_ERROR("Error index too low",
00187     "adstring& adstring_array::operator () (int i) const",
00188     indexmin(), indexmax(), i);
00189   }
00190   if (i > indexmax())
00191   {
00192     ADMB_ARRAY_BOUNDS_ERROR("Error index too high",
00193     "adstring& adstring_array::operator () (int i) const",
00194     indexmin(), indexmax(), i);
00195   }
00196   return *(ptr[i]);
00197 }