ADMB Documentation  11.5.3197
 All Classes Files Functions Variables Typedefs Friends Defines
string5.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  */
00007 #include <fvar.hpp>
00008 #include <stdlib.h>
00009 
00010 #ifndef OPT_LIB
00011   #include <cassert>
00012 #endif
00013 
00017 adstring_array::adstring_array()
00018 {
00019   shape=NULL;
00020   ptr=NULL;
00021 }
00025 adstring_array::adstring_array(const adstring_array& sa): clist(sa)
00026 {
00027   shape=sa.shape;
00028   ptr=sa.ptr;
00029 }
00030 adstring_array::adstring_array(int min,int max)
00031 {
00032   allocate(min,max);
00033 }
00037 adstring_array::~adstring_array()
00038 {
00039   if (ptr)
00040   {
00041     if (next==this)
00042     {
00043       int min=indexmin();
00044       int max=indexmax();
00045       for(int i=min;i<=max;i++)
00046       {
00047         if (ptr[i])
00048         {
00049           delete ptr[i];
00050           ptr[i] = 0;
00051         }
00052       }
00053       ptr+=indexmin();
00054 
00055       delete [] ptr;
00056       ptr = 0;
00057 
00058       delete shape;
00059       shape = 0;
00060     }
00061   }
00062 }
00063 
00064 int adstring_array::size() const
00065   { return shape ? shape->indexmax() - shape->indexmin() + 1 : 0; }
00066 
00067 int adstring_array::indexmin() const
00068   { return shape ? shape->indexmin() : 0;}
00069 
00070 int adstring_array::indexmax() const
00071   { return shape ? shape->indexmax() : 0;}
00072 
00073 void adstring_array::allocate(int min, int max)
00074 {
00075   if (min > max)
00076   {
00077     cerr << " Error in adstring_array(int min,int max) --"
00078             " max must be >= min" << endl;
00079     exit(1);
00080   }
00081   shape = new vector_shape(min, max);
00082   if (!shape)
00083   {
00084     cerr << "Error allocating memory in adstring_array" << endl;
00085   }
00086   ptr = new adstring*[max - min + 1];
00087   if (!ptr)
00088   {
00089     cerr << "Error allocating memory in adstring_array" << endl;
00090   }
00091   ptr-=indexmin();
00092   for (int i=min;i<=max;i++)
00093   {
00094     ptr[i]=new adstring;
00095   }
00096 }
00102 adstring& adstring_array::operator[](int i)
00103 {
00104   if (!shape)
00105   {
00106     cerr << "Error -- trying to acess unallocated adstring array" << endl;
00107     ad_exit(1);
00108   }
00109 
00110 #ifndef OPT_LIB
00111   assert(indexmin() <= i && i <= indexmax());
00112 #endif
00113 
00114   return *(ptr[i]);
00115 }
00121 adstring& adstring_array::operator()(int i)
00122 {
00123   if (!shape)
00124   {
00125     cerr << "Error -- trying to acess unallocated adstring array" << endl;
00126     ad_exit(1);
00127   }
00128 
00129 #ifndef OPT_LIB
00130   assert(indexmin() <= i && i <= indexmax());
00131 #endif
00132 
00133   return *(ptr[i]);
00134 }
00140 const adstring& adstring_array::operator[](int i) const
00141 {
00142   if (!shape)
00143   {
00144     cerr << "Error -- trying to acess unallocated adstring array" << endl;
00145     exit(1);
00146   }
00147 
00148 #ifndef OPT_LIB
00149   assert(indexmin() <= i && i <= indexmax());
00150 #endif
00151 
00152   return *(ptr[i]);
00153 }
00159 const adstring& adstring_array::operator()(int i) const
00160 {
00161   if (!shape)
00162   {
00163     cerr << "Error -- trying to acess unallocated adstring array" << endl;
00164     ad_exit(1);
00165   }
00166 
00167 #ifndef OPT_LIB
00168   assert(indexmin() <= i && i <= indexmax());
00169 #endif
00170 
00171   return *(ptr[i]);
00172 }